From 601123618a07d4f8aae3ab30f566aa1a4da04cbd Mon Sep 17 00:00:00 2001 From: mayurrogheliya Date: Thu, 13 Nov 2025 11:36:45 +0530 Subject: [PATCH] fix: prevent page reload when pressing no-character keys in search input --- data.json | 8 ++++++++ scripts/app.js | 34 ++++++++++++++++++++++++++++------ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/data.json b/data.json index 51551af..4b11fba 100644 --- a/data.json +++ b/data.json @@ -1132,5 +1132,13 @@ "twitter": "https://x.com/X4Git", "linkedin": "", "skills": ["Node.js", "TypeScript", "MySQL", "Next.js", "Expo", "Express.js","JavaScript"] + }, + { + "name": "Mayur Rogheliya", + "image": "https://avatars.githubusercontent.com/u/136033979?v=4", + "github": "https://github.com/mayurrogheliya", + "twitter": "https://x.com/MayurRogheliya", + "linkedin": "https://www.linkedin.com/in/mayur-rogheliya/", + "skills": ["MERN", "NextJS", "MySQL"] } ] diff --git a/scripts/app.js b/scripts/app.js index ef5e0e0..c5d0850 100644 --- a/scripts/app.js +++ b/scripts/app.js @@ -96,12 +96,30 @@ searchInput.addEventListener('keyup', () => { clearTimeout(debounceTimer); const searchTerm = searchInput.value.trim().toLowerCase(); - // Redirect if the search input is empty - if (searchTerm === '') { - window.location.href = '/'; // Redirect to the root URL if search input is empty - return; // Exit the function early + const ignoredKeys = [ + "ArrowUp", + "ArrowDown", + "ArrowLeft", + "ArrowRight", + "Shift", + "Control", + "Alt", + "Meta", + "CapsLock", + "Tab", + "Escape", + ]; + + if (ignoredKeys.includes(event.key)) { + return; } + if (searchTerm === "" && (event.key === "Backspace" || event.key === "Delete")) { + updateURL(''); + filterProfiles(''); + return; +} + debounceTimer = setTimeout(() => { updateURL(searchTerm); // Update the URL with the search term filterProfiles(searchTerm); @@ -111,8 +129,12 @@ searchInput.addEventListener('keyup', () => { // Function to update the URL const updateURL = (searchTerm) => { const url = new URL(window.location); - url.searchParams.set('search', searchTerm); - window.history.pushState({}, '', url); + if (searchTerm) { + url.searchParams.set("search", searchTerm); + } else { + url.searchParams.delete("search"); + } + window.history.pushState({}, "", url); }; // Filter profiles based on search term