Skip to content

Commit ba5ff81

Browse files
fix issues reported
1 parent 04dca9d commit ba5ff81

File tree

10 files changed

+495
-60
lines changed

10 files changed

+495
-60
lines changed

docs/pages/api/autocomplete.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ You can learn more about the difference by [reading this guide](/guides/minimizi
7373
| <span class="prop-name required">renderInput&nbsp;*</span> | <span class="prop-type">func</span> | | Render the input.<br><br>**Signature:**<br>`function(params: object) => ReactNode`<br>*params:* null |
7474
| <span class="prop-name">renderOption</span> | <span class="prop-type">func</span> | | Render the option, use `getOptionLabel` by default.<br><br>**Signature:**<br>`function(option: T, state: object) => ReactNode`<br>*option:* The option to render.<br>*state:* The state of the component. |
7575
| <span class="prop-name">renderTags</span> | <span class="prop-type">func</span> | | Render the selected value.<br><br>**Signature:**<br>`function(value: undefined, getTagProps: function) => ReactNode`<br>*value:* The `value` provided to the component.<br>*getTagProps:* A tag props getter. |
76-
| <span class="prop-name">selectOnFocus</span> | <span class="prop-type">bool</span> | <span class="prop-default">!props.freeSolo</span> | If `true`, the input's text will be selected on focus. |
76+
| <span class="prop-name">selectOnFocus</span> | <span class="prop-type">bool</span> | <span class="prop-default">!props.freeSolo</span> | If `true`, the input's text will be selected on focus. It helps the user clearning the selected value. |
7777
| <span class="prop-name">size</span> | <span class="prop-type">'medium'<br>&#124;&nbsp;'small'</span> | <span class="prop-default">'medium'</span> | The size of the autocomplete. |
7878
| <span class="prop-name">value</span> | <span class="prop-type">any<br>&#124;&nbsp;array</span> | | The value of the autocomplete.<br>The value must have reference equality with the option in order to be selected. You can customize the equality behavior with the `getOptionSelected` prop. |
7979

docs/src/pages/components/autocomplete/CustomizedHook.js

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable no-use-before-define */
22
import React from 'react';
33
import useAutocomplete from '@material-ui/lab/useAutocomplete';
4+
import NoSsr from '@material-ui/core/NoSsr';
45
import CheckIcon from '@material-ui/icons/Check';
56
import CloseIcon from '@material-ui/icons/Close';
67
import styled from 'styled-components';
@@ -146,28 +147,30 @@ export default function CustomizedHook() {
146147
});
147148

148149
return (
149-
<div>
150-
<div {...getRootProps()}>
151-
<Label {...getInputLabelProps()}>Customized hook</Label>
152-
<InputWrapper ref={setAnchorEl} className={focused ? 'focused' : ''}>
153-
{value.map((option, index) => (
154-
<Tag label={option.title} {...getTagProps({ index })} />
155-
))}
150+
<NoSsr>
151+
<div>
152+
<div {...getRootProps()}>
153+
<Label {...getInputLabelProps()}>Customized hook</Label>
154+
<InputWrapper ref={setAnchorEl} className={focused ? 'focused' : ''}>
155+
{value.map((option, index) => (
156+
<Tag label={option.title} {...getTagProps({ index })} />
157+
))}
156158

157-
<input {...getInputProps()} />
158-
</InputWrapper>
159+
<input {...getInputProps()} />
160+
</InputWrapper>
161+
</div>
162+
{groupedOptions.length > 0 ? (
163+
<Listbox {...getListboxProps()}>
164+
{groupedOptions.map((option, index) => (
165+
<li {...getOptionProps({ option, index })}>
166+
<span>{option.title}</span>
167+
<CheckIcon fontSize="small" />
168+
</li>
169+
))}
170+
</Listbox>
171+
) : null}
159172
</div>
160-
{groupedOptions.length > 0 ? (
161-
<Listbox {...getListboxProps()}>
162-
{groupedOptions.map((option, index) => (
163-
<li {...getOptionProps({ option, index })}>
164-
<span>{option.title}</span>
165-
<CheckIcon fontSize="small" />
166-
</li>
167-
))}
168-
</Listbox>
169-
) : null}
170-
</div>
173+
</NoSsr>
171174
);
172175
}
173176

docs/src/pages/components/autocomplete/CustomizedHook.tsx

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable no-use-before-define */
22
import React from 'react';
33
import useAutocomplete from '@material-ui/lab/useAutocomplete';
4+
import NoSsr from '@material-ui/core/NoSsr';
45
import CheckIcon from '@material-ui/icons/Check';
56
import CloseIcon from '@material-ui/icons/Close';
67
import styled from 'styled-components';
@@ -146,27 +147,29 @@ export default function CustomizedHook() {
146147
});
147148

148149
return (
149-
<div>
150-
<div {...getRootProps()}>
151-
<Label {...getInputLabelProps()}>Customized hook</Label>
152-
<InputWrapper ref={setAnchorEl} className={focused ? 'focused' : ''}>
153-
{value.map((option: FilmOptionType, index: number) => (
154-
<Tag label={option.title} {...getTagProps({ index })} />
155-
))}
156-
<input {...getInputProps()} />
157-
</InputWrapper>
150+
<NoSsr>
151+
<div>
152+
<div {...getRootProps()}>
153+
<Label {...getInputLabelProps()}>Customized hook</Label>
154+
<InputWrapper ref={setAnchorEl} className={focused ? 'focused' : ''}>
155+
{value.map((option: FilmOptionType, index: number) => (
156+
<Tag label={option.title} {...getTagProps({ index })} />
157+
))}
158+
<input {...getInputProps()} />
159+
</InputWrapper>
160+
</div>
161+
{groupedOptions.length > 0 ? (
162+
<Listbox {...getListboxProps()}>
163+
{groupedOptions.map((option, index) => (
164+
<li {...getOptionProps({ option, index })}>
165+
<span>{option.title}</span>
166+
<CheckIcon fontSize="small" />
167+
</li>
168+
))}
169+
</Listbox>
170+
) : null}
158171
</div>
159-
{groupedOptions.length > 0 ? (
160-
<Listbox {...getListboxProps()}>
161-
{groupedOptions.map((option, index) => (
162-
<li {...getOptionProps({ option, index })}>
163-
<span>{option.title}</span>
164-
<CheckIcon fontSize="small" />
165-
</li>
166-
))}
167-
</Listbox>
168-
) : null}
169-
</div>
172+
</NoSsr>
170173
);
171174
}
172175

docs/src/pages/components/autocomplete/FreeSoloCreateOption.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ import Autocomplete, { createFilterOptions } from '@material-ui/lab/Autocomplete
55

66
const filter = createFilterOptions();
77

8-
export default function ComboBox() {
9-
const [value, setValue] = React.useState('');
8+
export default function FreeSoloCreateOption() {
9+
const [value, setValue] = React.useState(null);
1010

1111
return (
1212
<Autocomplete
1313
value={value}
1414
onChange={(event, newValue) => {
15-
if (newValue && newValue.freeSolo) {
15+
if (newValue && newValue.inputValue) {
1616
setValue({
1717
title: newValue.inputValue,
1818
});
19+
1920
return;
2021
}
2122

@@ -26,7 +27,6 @@ export default function ComboBox() {
2627

2728
if (params.inputValue !== '') {
2829
filtered.push({
29-
freeSolo: true,
3030
inputValue: params.inputValue,
3131
title: `Add "${params.inputValue}"`,
3232
});
@@ -36,7 +36,17 @@ export default function ComboBox() {
3636
}}
3737
id="free-solo-with-text-demo"
3838
options={top100Films}
39-
getOptionLabel={option => option.title}
39+
getOptionLabel={option => {
40+
// e.g value selected with enter, right from the input
41+
if (typeof option === 'string') {
42+
return option;
43+
}
44+
if (option.inputValue) {
45+
return option.inputValue;
46+
}
47+
return option.title;
48+
}}
49+
renderOption={option => option.title}
4050
style={{ width: 300 }}
4151
freeSolo
4252
renderInput={params => (
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/* eslint-disable no-use-before-define */
2+
import React from 'react';
3+
import TextField from '@material-ui/core/TextField';
4+
import Autocomplete, { createFilterOptions } from '@material-ui/lab/Autocomplete';
5+
6+
const filter = createFilterOptions();
7+
8+
export default function FreeSoloCreateOption() {
9+
const [value, setValue] = React.useState<FilmOptionType | null>(null);
10+
11+
return (
12+
<Autocomplete
13+
value={value}
14+
onChange={(event: any, newValue: FilmOptionType | null) => {
15+
if (newValue && newValue.inputValue) {
16+
setValue({
17+
title: newValue.inputValue,
18+
});
19+
return;
20+
}
21+
22+
setValue(newValue);
23+
}}
24+
filterOptions={(options, params) => {
25+
const filtered = filter(options, params) as FilmOptionType[];
26+
27+
if (params.inputValue !== '') {
28+
filtered.push({
29+
inputValue: params.inputValue,
30+
title: `Add "${params.inputValue}"`,
31+
});
32+
}
33+
34+
return filtered;
35+
}}
36+
id="free-solo-with-text-demo"
37+
options={top100Films as FilmOptionType[]}
38+
getOptionLabel={option => {
39+
// e.g value selected with enter, right from the input
40+
if (typeof option === 'string') {
41+
return option;
42+
}
43+
if (option.inputValue) {
44+
return option.inputValue;
45+
}
46+
return option.title;
47+
}}
48+
renderOption={option => option.title}
49+
style={{ width: 300 }}
50+
freeSolo
51+
renderInput={params => (
52+
<TextField {...params} label="Free solo with text demo" variant="outlined" fullWidth />
53+
)}
54+
/>
55+
);
56+
}
57+
58+
interface FilmOptionType {
59+
inputValue?: string;
60+
title: string;
61+
year?: number;
62+
}
63+
64+
// Top 100 films as rated by IMDb users. http://www.imdb.com/chart/top
65+
const top100Films = [
66+
{ title: 'The Shawshank Redemption', year: 1994 },
67+
{ title: 'The Godfather', year: 1972 },
68+
{ title: 'The Godfather: Part II', year: 1974 },
69+
{ title: 'The Dark Knight', year: 2008 },
70+
{ title: '12 Angry Men', year: 1957 },
71+
{ title: "Schindler's List", year: 1993 },
72+
{ title: 'Pulp Fiction', year: 1994 },
73+
{ title: 'The Lord of the Rings: The Return of the King', year: 2003 },
74+
{ title: 'The Good, the Bad and the Ugly', year: 1966 },
75+
{ title: 'Fight Club', year: 1999 },
76+
{ title: 'The Lord of the Rings: The Fellowship of the Ring', year: 2001 },
77+
{ title: 'Star Wars: Episode V - The Empire Strikes Back', year: 1980 },
78+
{ title: 'Forrest Gump', year: 1994 },
79+
{ title: 'Inception', year: 2010 },
80+
{ title: 'The Lord of the Rings: The Two Towers', year: 2002 },
81+
{ title: "One Flew Over the Cuckoo's Nest", year: 1975 },
82+
{ title: 'Goodfellas', year: 1990 },
83+
{ title: 'The Matrix', year: 1999 },
84+
{ title: 'Seven Samurai', year: 1954 },
85+
{ title: 'Star Wars: Episode IV - A New Hope', year: 1977 },
86+
{ title: 'City of God', year: 2002 },
87+
{ title: 'Se7en', year: 1995 },
88+
{ title: 'The Silence of the Lambs', year: 1991 },
89+
{ title: "It's a Wonderful Life", year: 1946 },
90+
{ title: 'Life Is Beautiful', year: 1997 },
91+
{ title: 'The Usual Suspects', year: 1995 },
92+
{ title: 'Léon: The Professional', year: 1994 },
93+
{ title: 'Spirited Away', year: 2001 },
94+
{ title: 'Saving Private Ryan', year: 1998 },
95+
{ title: 'Once Upon a Time in the West', year: 1968 },
96+
{ title: 'American History X', year: 1998 },
97+
{ title: 'Interstellar', year: 2014 },
98+
{ title: 'Casablanca', year: 1942 },
99+
{ title: 'City Lights', year: 1931 },
100+
{ title: 'Psycho', year: 1960 },
101+
{ title: 'The Green Mile', year: 1999 },
102+
{ title: 'The Intouchables', year: 2011 },
103+
{ title: 'Modern Times', year: 1936 },
104+
{ title: 'Raiders of the Lost Ark', year: 1981 },
105+
{ title: 'Rear Window', year: 1954 },
106+
{ title: 'The Pianist', year: 2002 },
107+
{ title: 'The Departed', year: 2006 },
108+
{ title: 'Terminator 2: Judgment Day', year: 1991 },
109+
{ title: 'Back to the Future', year: 1985 },
110+
{ title: 'Whiplash', year: 2014 },
111+
{ title: 'Gladiator', year: 2000 },
112+
{ title: 'Memento', year: 2000 },
113+
{ title: 'The Prestige', year: 2006 },
114+
{ title: 'The Lion King', year: 1994 },
115+
{ title: 'Apocalypse Now', year: 1979 },
116+
{ title: 'Alien', year: 1979 },
117+
{ title: 'Sunset Boulevard', year: 1950 },
118+
{
119+
title: 'Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb',
120+
year: 1964,
121+
},
122+
{ title: 'The Great Dictator', year: 1940 },
123+
{ title: 'Cinema Paradiso', year: 1988 },
124+
{ title: 'The Lives of Others', year: 2006 },
125+
{ title: 'Grave of the Fireflies', year: 1988 },
126+
{ title: 'Paths of Glory', year: 1957 },
127+
{ title: 'Django Unchained', year: 2012 },
128+
{ title: 'The Shining', year: 1980 },
129+
{ title: 'WALL·E', year: 2008 },
130+
{ title: 'American Beauty', year: 1999 },
131+
{ title: 'The Dark Knight Rises', year: 2012 },
132+
{ title: 'Princess Mononoke', year: 1997 },
133+
{ title: 'Aliens', year: 1986 },
134+
{ title: 'Oldboy', year: 2003 },
135+
{ title: 'Once Upon a Time in America', year: 1984 },
136+
{ title: 'Witness for the Prosecution', year: 1957 },
137+
{ title: 'Das Boot', year: 1981 },
138+
{ title: 'Citizen Kane', year: 1941 },
139+
{ title: 'North by Northwest', year: 1959 },
140+
{ title: 'Vertigo', year: 1958 },
141+
{ title: 'Star Wars: Episode VI - Return of the Jedi', year: 1983 },
142+
{ title: 'Reservoir Dogs', year: 1992 },
143+
{ title: 'Braveheart', year: 1995 },
144+
{ title: 'M', year: 1931 },
145+
{ title: 'Requiem for a Dream', year: 2000 },
146+
{ title: 'Amélie', year: 2001 },
147+
{ title: 'A Clockwork Orange', year: 1971 },
148+
{ title: 'Like Stars on Earth', year: 2007 },
149+
{ title: 'Taxi Driver', year: 1976 },
150+
{ title: 'Lawrence of Arabia', year: 1962 },
151+
{ title: 'Double Indemnity', year: 1944 },
152+
{ title: 'Eternal Sunshine of the Spotless Mind', year: 2004 },
153+
{ title: 'Amadeus', year: 1984 },
154+
{ title: 'To Kill a Mockingbird', year: 1962 },
155+
{ title: 'Toy Story 3', year: 2010 },
156+
{ title: 'Logan', year: 2017 },
157+
{ title: 'Full Metal Jacket', year: 1987 },
158+
{ title: 'Dangal', year: 2016 },
159+
{ title: 'The Sting', year: 1973 },
160+
{ title: '2001: A Space Odyssey', year: 1968 },
161+
{ title: "Singin' in the Rain", year: 1952 },
162+
{ title: 'Toy Story', year: 1995 },
163+
{ title: 'Bicycle Thieves', year: 1948 },
164+
{ title: 'The Kid', year: 1921 },
165+
{ title: 'Inglourious Basterds', year: 2009 },
166+
{ title: 'Snatch', year: 2000 },
167+
{ title: '3 Idiots', year: 2009 },
168+
{ title: 'Monty Python and the Holy Grail', year: 1975 },
169+
];

0 commit comments

Comments
 (0)