Skip to content

Commit 10d4375

Browse files
committed
Update storybook
1 parent ece142a commit 10d4375

File tree

2 files changed

+52
-112
lines changed

2 files changed

+52
-112
lines changed

stories/Header.stories.tsx

Lines changed: 26 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
304262
function 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
338306
If you want to feature a modern search experience with realtime hinting you can omit providing a \`onSearchButtonClick\` callback and instead

stories/SearchBar.stories.tsx

Lines changed: 26 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -103,45 +103,7 @@ export const WithControlledInput = getStory(
103103
"description": `
104104
105105
\`\`\`tsx
106-
107106
import { SearchBar } from "@codegouvfr/react-dsfr/SearchBar";
108-
109-
type MySearchInputProps = {
110-
className?: string;
111-
id: string;
112-
placeholder: string;
113-
type: "search";
114-
search: string;
115-
onSearchChange: (search: string) => void;
116-
};
117-
118-
function MySearchInput(props: MySearchInputProps) {
119-
120-
const { className, id, placeholder, type, search, onSearchChange } = props;
121-
122-
const [
123-
inputElement,
124-
setInputElement
125-
] = useState<HTMLInputElement | null>(null);
126-
127-
return (
128-
<input
129-
className={className}
130-
id={id}
131-
placeholder={placeholder}
132-
type={type}
133-
value={search}
134-
onChange={event => onSearchChange(event.currentTarget.value)}
135-
onKeyDown={event => {
136-
if (event.key === "Escape") {
137-
setInputElement?.blur();
138-
}
139-
}}
140-
/>
141-
);
142-
143-
}
144-
145107
146108
function Root(){
147109
@@ -151,21 +113,32 @@ function Root(){
151113
<>
152114
<SearchBar
153115
...
154-
renderInput={({
155-
className,
156-
id,
157-
placeholder,
158-
type
159-
})=>
160-
<MySearchInput
161-
className={className}
162-
id={id}
163-
placeholder={placeholder}
164-
type={type}
165-
search={search}
166-
onSearchChange={onSearchChange}
167-
/>
168-
}
116+
renderInput={({ className, id, placeholder, type }) => {
117+
const [inputElement, setInputElement] =
118+
useState<HTMLInputElement | null>(null);
119+
120+
return (
121+
<input
122+
ref={setInputElement}
123+
className={className}
124+
id={id}
125+
placeholder={placeholder}
126+
type={type}
127+
value={search}
128+
// Note: The default behavior for an input of type 'text' is to clear the input value when the escape key is pressed.
129+
// However, due to a bug in @gouvfr/dsfr the escape key event is not propagated to the input element.
130+
// As a result this onChange is not called when the escape key is pressed.
131+
onChange={event => onSearchChange(event.currentTarget.value)}
132+
// Same goes for the keydown event so this is useless but we hope the bug will be fixed soon.
133+
onKeyDown={event => {
134+
if (event.key === "Escape") {
135+
assert(inputElement !== null);
136+
inputElement.blur();
137+
}
138+
}}
139+
/>
140+
);
141+
}}
169142
...
170143
/>
171144
<p>Search results for: {search}</p>
@@ -174,7 +147,6 @@ function Root(){
174147
);
175148
176149
}
177-
178150
\`\`\`
179151
180152
If you want to feature a modern search experience with realtime hinting you can omit providing a \`onSearchButtonClick\` callback and instead

0 commit comments

Comments
 (0)