Skip to content

Commit 132c06e

Browse files
committed
Better search bar documentation
1 parent 63a739b commit 132c06e

File tree

1 file changed

+24
-27
lines changed

1 file changed

+24
-27
lines changed

stories/SearchBar.stories.tsx

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -97,37 +97,34 @@ import { SearchBar } from "@codegouvfr/react-dsfr/SearchBar";
9797
function Root(){
9898
9999
const [search, onSearchChange] = useState("");
100+
101+
const [inputElement, setInputElement] = useState<HTMLInputElement | null>(null);
100102
101103
return (
102104
<>
103105
<SearchBar
104106
...
105-
renderInput={({ className, id, placeholder, type }) => {
106-
const [inputElement, setInputElement] =
107-
useState<HTMLInputElement | null>(null);
108-
109-
return (
110-
<input
111-
ref={setInputElement}
112-
className={className}
113-
id={id}
114-
placeholder={placeholder}
115-
type={type}
116-
value={search}
117-
// Note: The default behavior for an input of type 'text' is to clear the input value when the escape key is pressed.
118-
// However, due to a bug in @gouvfr/dsfr the escape key event is not propagated to the input element.
119-
// As a result this onChange is not called when the escape key is pressed.
120-
onChange={event => onSearchChange(event.currentTarget.value)}
121-
// Same goes for the keydown event so this is useless but we hope the bug will be fixed soon.
122-
onKeyDown={event => {
123-
if (event.key === "Escape") {
124-
assert(inputElement !== null);
125-
inputElement.blur();
126-
}
127-
}}
128-
/>
129-
);
130-
}}
107+
renderInput={({ className, id, placeholder, type }) =>
108+
<input
109+
ref={setInputElement}
110+
className={className}
111+
id={id}
112+
placeholder={placeholder}
113+
type={type}
114+
value={search}
115+
// Note: The default behavior for an input of type 'text' is to clear the input value when the escape key is pressed.
116+
// However, due to a bug in @gouvfr/dsfr the escape key event is not propagated to the input element.
117+
// As a result this onChange is not called when the escape key is pressed.
118+
onChange={event => onSearchChange(event.currentTarget.value)}
119+
// Same goes for the keydown event so this is useless but we hope the bug will be fixed soon.
120+
onKeyDown={event => {
121+
if (event.key === "Escape") {
122+
assert(inputElement !== null);
123+
inputElement.blur();
124+
}
125+
}}
126+
/>
127+
}
131128
...
132129
/>
133130
<p>Search results for: {search}</p>
@@ -155,7 +152,7 @@ make sure you provide an overlay with the search results in the the \`renderSear
155152
156153
As, to this day, the DSFR do not provide any component to help you with that, you are on your own for implementing the overlay.
157154
You can achieve great result by using [MUI's autocomplete](https://mui.com/material-ui/react-autocomplete/) component.
158-
[Video demo](https://youtu.be/AT3CvmY_Y7M?t=64).
155+
[Implementation example](https://github.com/mui/material-ui/issues/37838).
159156
If you go with MUI make sure to use the [\`<MuiDsfrProvider />\`](https://react-dsfr.etalab.studio/mui).
160157
161158
\`\`\`tsx

0 commit comments

Comments
 (0)