Skip to content

Commit 8ae1e53

Browse files
committed
Moving on with implementing an MUI search input
1 parent 10d4375 commit 8ae1e53

File tree

3 files changed

+113
-47
lines changed

3 files changed

+113
-47
lines changed

.storybook/DocsContainer.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import "../dist/dsfr/dsfr.css";
88
import { useIsDark } from "../dist/useIsDark";
99
import { startReactDsfr } from "../dist/spa";
1010
import { useColors } from "../dist/useColors";
11+
import { MuiDsfrThemeProvider } from "../dist/mui";
1112

1213
startReactDsfr({
1314
"defaultColorScheme": "system",
@@ -69,7 +70,9 @@ export const DocsContainer = ({ children, context }) => {
6970
}
7071
}}
7172
>
72-
{children}
73+
<MuiDsfrThemeProvider>
74+
{children}
75+
</MuiDsfrThemeProvider>
7376
</BaseContainer>
7477
</>
7578
);

stories/MuiSearchInput.tsx

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import React, { type ReactNode } from "react";
2+
import Autocomplete from "@mui/material/Autocomplete";
3+
import { cx } from "../dist/tools/cx";
4+
5+
export type SearchResult = {
6+
children: ReactNode;
7+
href: string;
8+
};
9+
10+
export type MuiSearchInputProps = {
11+
className: string;
12+
id: string;
13+
placeholder: string;
14+
type: "search";
15+
16+
value: string;
17+
onChange: (newValue: string) => void;
18+
results: SearchResult[];
19+
};
20+
21+
const options = ["Option 1", "Option 2"];
22+
23+
export function MuiSearchInput(props: MuiSearchInputProps) {
24+
const {
25+
className,
26+
id,
27+
placeholder,
28+
type
29+
/*
30+
value,
31+
onChange,
32+
results,
33+
*/
34+
} = props;
35+
36+
return (
37+
<Autocomplete
38+
style={{ "width": "100%" }}
39+
id={id}
40+
options={options}
41+
renderInput={params => (
42+
<div ref={params.InputProps.ref}>
43+
<input
44+
{...params.inputProps}
45+
className={cx(params.inputProps.className, className)}
46+
placeholder={placeholder}
47+
type={type}
48+
/>
49+
</div>
50+
)}
51+
/>
52+
);
53+
}

stories/SearchBar.stories.tsx

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { useState } from "react";
22
import { SearchBar } from "../dist/SearchBar";
33
import { sectionName } from "./sectionName";
44
import { getStoryFactory } from "./getStory";
5+
import { MuiSearchInput } from "./MuiSearchInput";
56

67
const { meta, getStory } = getStoryFactory({
78
sectionName,
@@ -51,53 +52,40 @@ import { SearchBar } from "@codegouvfr/react-dsfr/SearchBar";
5152
}
5253
);
5354

54-
type MySearchInputProps = {
55-
className?: string;
56-
id: string;
57-
placeholder: string;
58-
type: "search";
59-
};
60-
61-
function MySearchInput(props: MySearchInputProps) {
62-
const { className, id, placeholder, type } = props;
63-
64-
const [search, onSearchChange] = useState("");
65-
66-
const [inputElement, setInputElement] = useState<HTMLInputElement | null>(null);
67-
68-
return (
69-
<>
70-
<input
71-
ref={setInputElement}
72-
className={className}
73-
id={id}
74-
placeholder={placeholder}
75-
type={type}
76-
value={search}
77-
onChange={event => onSearchChange(event.currentTarget.value)}
78-
onKeyDown={event => {
79-
if (event.key === "Escape") {
80-
inputElement?.blur();
81-
}
82-
}}
83-
/>
84-
<p
85-
style={{
86-
"position": "absolute",
87-
"top": 84
88-
}}
89-
>
90-
Search results for: {search}
91-
</p>
92-
</>
93-
);
94-
}
95-
9655
export const WithControlledInput = getStory(
9756
{
98-
"renderInput": ({ className, id, placeholder, type }) => (
99-
<MySearchInput className={className} id={id} placeholder={placeholder} type={type} />
100-
)
57+
"renderInput": ({ className, id, placeholder, type }) => {
58+
const [search, onSearchChange] = useState("");
59+
60+
const [inputElement, setInputElement] = useState<HTMLInputElement | null>(null);
61+
62+
return (
63+
<>
64+
<input
65+
ref={setInputElement}
66+
className={className}
67+
id={id}
68+
placeholder={placeholder}
69+
type={type}
70+
value={search}
71+
onChange={event => onSearchChange(event.currentTarget.value)}
72+
onKeyDown={event => {
73+
if (event.key === "Escape") {
74+
inputElement?.blur();
75+
}
76+
}}
77+
/>
78+
<p
79+
style={{
80+
"position": "absolute",
81+
"top": 84
82+
}}
83+
>
84+
Search results for: {search}
85+
</p>
86+
</>
87+
);
88+
}
10189
},
10290
{
10391
"description": `
@@ -148,6 +136,28 @@ function Root(){
148136
149137
}
150138
\`\`\`
139+
`
140+
}
141+
);
142+
143+
export const WithMuiAutocomplete = getStory(
144+
{
145+
"renderInput": ({ className, id, placeholder, type }) => (
146+
<MuiSearchInput
147+
className={className}
148+
id={id}
149+
placeholder={placeholder}
150+
type={type}
151+
value=""
152+
onChange={() => {
153+
/**/
154+
}}
155+
results={[]}
156+
/>
157+
)
158+
},
159+
{
160+
"description": `
151161
152162
If you want to feature a modern search experience with realtime hinting you can omit providing a \`onSearchButtonClick\` callback and instead
153163
make sure you provide an overlay with the search results in the the \`renderSearchInput\` function.
@@ -176,12 +186,12 @@ function MySearchInput(props: MySearchInputProps) {
176186
return (
177187
<Autocomplete
178188
...
189+
id={id}
179190
renderInput={params =>
180191
<div ref={params.InputProps.ref}>
181192
<input
182193
{...params.inputProps}
183194
className={cx(params.inputProps.className, className)}
184-
id={id}
185195
placeholder={placeholder}
186196
type={type}
187197
/>

0 commit comments

Comments
 (0)