Skip to content

Commit d1f6819

Browse files
committed
add arrow submit button to search page
1 parent 15fe476 commit d1f6819

3 files changed

Lines changed: 52 additions & 14 deletions

File tree

src/components/Icon/index.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export type IconKind =
22
| "arrow"
3+
| "arrow-lg"
34
| "code-brackets"
45
| "refresh"
56
| "play"
@@ -46,6 +47,27 @@ export const Icon = (props: IconProps) => {
4647
/>
4748
</svg>
4849
);
50+
case "arrow-lg":
51+
return (
52+
<svg
53+
width="61"
54+
height="32"
55+
viewBox="0 0 61 32"
56+
fill="none"
57+
xmlns="http://www.w3.org/2000/svg"
58+
className={props.className}
59+
>
60+
<path
61+
d="M45 31L60 16L45 0.999999"
62+
stroke="currentColor"
63+
stroke-linecap="round"
64+
/>
65+
<path
66+
d="M15 15.5C14.7239 15.5 14.5 15.7239 14.5 16C14.5 16.2761 14.7239 16.5 15 16.5L15 15.5ZM60 15.5L15 15.5L15 16.5L60 16.5L60 15.5Z"
67+
fill="currentColor"
68+
/>
69+
</svg>
70+
);
4971
case "chevron-down":
5072
return (
5173
<svg

src/components/SearchProvider/index.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,9 @@ const SearchProvider = ({
108108
);
109109
}, [searchTerm, currentLocale]);
110110

111-
const handleSearchTermChange = (event: Event) => {
112-
const target = event.target as HTMLInputElement;
113-
const { value } = target;
114-
if (value) {
115-
setSearchTerm(target.value);
111+
const handleSearchTermChange = (term?: string) => {
112+
if (term !== undefined) {
113+
setSearchTerm(term);
116114
}
117115
};
118116

@@ -123,6 +121,7 @@ const SearchProvider = ({
123121
currentLocale={currentLocale as string}
124122
onSearchChange={handleSearchTermChange}
125123
uiTranslations={uiTranslations}
124+
key={searchTerm}
126125
/>
127126
);
128127
};

src/components/SearchResults/index.tsx

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type SearchResultProps = {
1313
results: SearchResult[];
1414
searchTerm: string;
1515
currentLocale: string;
16-
onSearchChange: JSX.GenericEventHandler<HTMLInputElement>;
16+
onSearchChange: (term?: string) => void;
1717
uiTranslations: Record<string, string>;
1818
};
1919

@@ -25,6 +25,7 @@ const SearchResults = ({
2525
}: SearchResultProps) => {
2626
const inputRef = useRef<HTMLInputElement>(null);
2727
const [currentFilter, setCurrentFilter] = useState("");
28+
const [isInputEdited, setInputEdited] = useState(false);
2829

2930
const allUniqueCategoriesForResults = useMemo(() => {
3031
const categories = results.map((result) => result.category);
@@ -97,6 +98,11 @@ const SearchResults = ({
9798
inputRef.current.value = "";
9899
}
99100
};
101+
const submitInput = () => {
102+
if (inputRef.current) {
103+
onSearchChange(inputRef.current.value);
104+
}
105+
};
100106

101107
const renderBigSearchForm = () => {
102108
return (
@@ -111,22 +117,33 @@ const SearchResults = ({
111117
value={searchTerm}
112118
placeholder={uiTranslations["Search"]}
113119
onKeyDown={(e) => {
120+
setInputEdited(true);
114121
if (e.key === "Enter") {
115122
e.preventDefault();
116-
onSearchChange(e);
123+
submitInput();
117124
}
118125
}}
119126
class="h-fit w-full appearance-none bg-transparent px-md text-4xl placeholder-sidebar-type-color focus:outline-0"
120127
aria-label="Search through site content"
121128
required
122129
/>
123-
<button
124-
type="reset"
125-
class="absolute right-0 top-0 px-[22px] py-[13px]"
126-
onClick={clearInput}
127-
>
128-
<Icon kind="close-lg" />
129-
</button>
130+
{isInputEdited ? (
131+
<button
132+
type="submit"
133+
class="absolute right-0 top-[2px] px-[22px] py-[13px]"
134+
onClick={submitInput}
135+
>
136+
<Icon kind="arrow-lg" />
137+
</button>
138+
) : (
139+
<button
140+
type="reset"
141+
class="absolute right-0 top-0 px-[22px] py-[13px]"
142+
onClick={clearInput}
143+
>
144+
<Icon kind="close-lg" />
145+
</button>
146+
)}
130147
</search>
131148
);
132149
};

0 commit comments

Comments
 (0)