@@ -259,48 +259,6 @@ export const WithControlledSearchBar = getStory(
259259
260260
261261\`\`\`tsx
262-
263- type MySearchInputProps = {
264- className?: string;
265- id: string;
266- placeholder: string;
267- type: "search";
268- search: string;
269- onSearchChange: (search: string) => void;
270- };
271-
272- function MySearchInput(props: MySearchInputProps) {
273-
274- const { className, id, placeholder, type, search, onSearchChange } = props;
275-
276- const [
277- inputElement,
278- setInputElement
279- ] = useState<HTMLInputElement | null>(null);
280-
281- return (
282- <input
283- ref={setInputElement}
284- className={className}
285- id={id}
286- placeholder={placeholder}
287- type={type}
288- value={search}
289- // Note: The default behavior for an input of type 'text' is to clear the input value when the escape key is pressed.
290- // However, due to a bug in @gouvfr/dsfr the escape key event is not propagated to the input element.
291- // As a result this onChange is not called when the escape key is pressed.
292- onChange={event => onSearchChange(event.currentTarget.value)}
293- // Same goes for the keydown event so this is useless but we hope the bug will be fixed soon.
294- onKeyDown={event => {
295- if (event.key === "Escape") {
296- inputElement?.blur();
297- }
298- }}
299- />
300- );
301-
302- }
303-
304262function Root(){
305263
306264 const [search, onSearchChange] = useState("");
@@ -309,21 +267,32 @@ function Root(){
309267 <>
310268 <Header
311269 ...
312- renderSearchInput={({
313- className,
314- id,
315- placeholder,
316- type
317- })=>
318- <MySearchInput
319- className={className}
320- id={id}
321- placeholder={placeholder}
322- type={type}
323- search={search}
324- onSearchChange={onSearchChange}
325- />
326- }
270+ renderSearchInput={({ className, id, placeholder, type }) => {
271+ const [inputElement, setInputElement] =
272+ useState<HTMLInputElement | null>(null);
273+
274+ return (
275+ <input
276+ ref={setInputElement}
277+ className={className}
278+ id={id}
279+ placeholder={placeholder}
280+ type={type}
281+ value={search}
282+ // Note: The default behavior for an input of type 'text' is to clear the input value when the escape key is pressed.
283+ // However, due to a bug in @gouvfr/dsfr the escape key event is not propagated to the input element.
284+ // As a result this onChange is not called when the escape key is pressed.
285+ onChange={event => onSearchChange(event.currentTarget.value)}
286+ // Same goes for the keydown event so this is useless but we hope the bug will be fixed soon.
287+ onKeyDown={event => {
288+ if (event.key === "Escape") {
289+ assert(inputElement !== null);
290+ inputElement.blur();
291+ }
292+ }}
293+ />
294+ );
295+ }}
327296 ...
328297 />
329298 <p>Search results for: {search}</p>
@@ -332,7 +301,6 @@ function Root(){
332301 );
333302
334303}
335-
336304\`\`\`
337305
338306If you want to feature a modern search experience with realtime hinting you can omit providing a \`onSearchButtonClick\` callback and instead
0 commit comments