Skip to content

Commit 06bfe4f

Browse files
committed
Add allowEmptySearch option to SearchBar #240
1 parent f2f648c commit 06bfe4f

File tree

5 files changed

+40
-7
lines changed

5 files changed

+40
-7
lines changed

src/Header/Header.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ export type HeaderProps = {
6565
onSearchButtonClick?: (text: string) => void;
6666
/** Default: false */
6767
clearSearchInputOnSearch?: boolean;
68+
/** Default: false */
69+
allowEmptySearch?: boolean;
6870
classes?: Partial<
6971
Record<
7072
| "root"
@@ -127,6 +129,7 @@ export const Header = memo(
127129
operatorLogo,
128130
renderSearchInput,
129131
clearSearchInputOnSearch = false,
132+
allowEmptySearch = false,
130133
onSearchButtonClick,
131134
classes = {},
132135
style,
@@ -404,6 +407,7 @@ export const Header = memo(
404407
clearInputOnSearch={
405408
clearSearchInputOnSearch
406409
}
410+
allowEmptySearch={allowEmptySearch}
407411
/>
408412
</div>
409413
</div>

src/SearchBar/SearchBar.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export type SearchBarProps = {
3232
) => JSX.Element;
3333
/** Default: false */
3434
clearInputOnSearch?: boolean;
35+
/** Default: false */
36+
allowEmptySearch?: boolean;
3537
onButtonClick?: (text: string) => void;
3638
};
3739

@@ -51,6 +53,7 @@ export const SearchBar = memo(
5153
<input className={className} id={id} placeholder={placeholder} type={type} />
5254
),
5355
clearInputOnSearch = false,
56+
allowEmptySearch = false,
5457
onButtonClick,
5558
...rest
5659
} = props;
@@ -96,6 +99,7 @@ export const SearchBar = memo(
9699
clearInputOnSearch={clearInputOnSearch}
97100
id={`${id}-button`}
98101
searchInputId={inputId}
102+
allowEmptySearch={allowEmptySearch}
99103
onClick={onButtonClick}
100104
/>
101105
</div>

src/SearchBar/SearchButton.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,18 @@ export type SearchButtonProps = {
1313
id: string;
1414
searchInputId: string;
1515
clearInputOnSearch: boolean;
16+
allowEmptySearch: boolean;
1617
onClick: ((text: string) => void) | undefined;
1718
};
1819

1920
export function SearchButton(props: SearchButtonProps) {
20-
const { searchInputId, clearInputOnSearch, onClick: onClick_props, id: id_props } = props;
21+
const {
22+
searchInputId,
23+
clearInputOnSearch,
24+
allowEmptySearch,
25+
onClick: onClick_props,
26+
id: id_props
27+
} = props;
2128

2229
const { t } = useTranslation();
2330

@@ -38,7 +45,7 @@ export function SearchButton(props: SearchButtonProps) {
3845
const onClick = useConstCallback(() => {
3946
const inputValue = getInputValue();
4047

41-
if (inputValue === "") {
48+
if (!allowEmptySearch && inputValue === "") {
4249
focusInputElement();
4350
return;
4451
}

stories/Header.stories.tsx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ const isOpen = useIsHeaderMenuModalOpen();
5656
"description":
5757
"Default: false, if true the search input value will be cleared when the user click on the search button or press enter",
5858
"control": { "type": "boolean" }
59+
},
60+
"allowEmptySearch": {
61+
"description":
62+
"Default: false, if true the user will be able to search with an empty input, otherwise clicking ont the search button or pressing enter will focus the input",
63+
"control": { "type": "boolean" }
5964
}
6065
},
6166
"disabledProps": ["lang"]
@@ -106,7 +111,8 @@ export const SimpleHeader = getStory({
106111
"target": "_self"
107112
}
108113
}
109-
]
114+
],
115+
"onSearchButtonClick": undefined
110116
});
111117

112118
export const SimpleHeaderWithServiceTitleAndTagline = getStory({
@@ -123,7 +129,8 @@ export const SimpleHeaderWithServiceTitleAndTagline = getStory({
123129
"title": "Accueil - Nom de l’entité (ministère, secrétariat d‘état, gouvernement)"
124130
},
125131
"serviceTitle": "Nom du site / service",
126-
"serviceTagline": "baseline - précisions sur l'organisation"
132+
"serviceTagline": "baseline - précisions sur l'organisation",
133+
"onSearchButtonClick": undefined
127134
});
128135

129136
export const SimpleHeaderWithServiceTitleAndBetaBadge = getStory({
@@ -146,7 +153,8 @@ export const SimpleHeaderWithServiceTitleAndBetaBadge = getStory({
146153
Beta
147154
</Badge>
148155
</>
149-
)
156+
),
157+
"onSearchButtonClick": undefined
150158
});
151159

152160
export const HeaderWithQuickAccessItems = getStory(
@@ -189,7 +197,8 @@ export const HeaderWithQuickAccessItems = getStory(
189197
}
190198
}
191199
}
192-
]
200+
],
201+
"onSearchButtonClick": undefined
193202
},
194203
{
195204
"description": `See [\\<Display \\/\\>](https://components.react-dsfr.codegouv.studio/?path=/docs/components-display) for instructions on how to integrate the Dark mode switch.
@@ -218,14 +227,18 @@ export const WithUncontrolledSearchBar = getStory(
218227
},
219228
"serviceTitle": "Nom du site / service",
220229
"serviceTagline": "baseline - précisions sur l'organisation",
221-
"onSearchButtonClick": text => alert(`TODO: implement search with text: ${text}`)
230+
"onSearchButtonClick": text => alert(`TODO: implement search with text: ${text}`),
231+
"clearSearchInputOnSearch": true,
232+
"allowEmptySearch": true
222233
},
223234
{
224235
"description": `
225236
226237
If you you do not plan to provide any realtime hinting to the user as he types the search query you can provide a \`onSearchButtonClick\`
227238
callback that will be called when the user click on the search button or press enter.
228239
240+
You can also have a use the \`clearSearchInputOnSearch\` and \`allowEmptySearch\` props to control the behavior of the search input.
241+
229242
> NOTE: There is a bug in the DSFR that prevent te input to be cleared when the user press the escape key.
230243
We hope it will be fixed soon.
231244

stories/SearchBar.stories.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ const { meta, getStory } = getStoryFactory({
2525
"Default: false, if true the input value will be cleared when the user click on the search button or press enter",
2626
"control": { "type": "boolean" }
2727
},
28+
"allowEmptySearch": {
29+
"description":
30+
"Default: false, if true the user will be able to search with an empty input, otherwise clicking ont the search button or pressing enter will focus the input",
31+
"control": { "type": "boolean" }
32+
},
2833
"renderInput": {
2934
"description": `Optional: To control the input yourself`,
3035
"control": { "type": null }

0 commit comments

Comments
 (0)