File tree Expand file tree Collapse file tree 2 files changed +26
-54
lines changed
docs/data/material/components/autocomplete Expand file tree Collapse file tree 2 files changed +26
-54
lines changed Original file line number Diff line number Diff line change @@ -14,44 +14,30 @@ function sleep(duration) {
14
14
export default function Asynchronous ( ) {
15
15
const [ open , setOpen ] = React . useState ( false ) ;
16
16
const [ options , setOptions ] = React . useState ( [ ] ) ;
17
- const loading = open && options . length === 0 ;
18
-
19
- React . useEffect ( ( ) => {
20
- let active = true ;
21
-
22
- if ( ! loading ) {
23
- return undefined ;
24
- }
17
+ const [ loading , setLoading ] = React . useState ( false ) ;
25
18
19
+ const handleOpen = ( ) => {
20
+ setOpen ( true ) ;
26
21
( async ( ) => {
22
+ setLoading ( true ) ;
27
23
await sleep ( 1e3 ) ; // For demo purposes.
24
+ setLoading ( false ) ;
28
25
29
- if ( active ) {
30
- setOptions ( [ ...topFilms ] ) ;
31
- }
26
+ setOptions ( [ ...topFilms ] ) ;
32
27
} ) ( ) ;
28
+ } ;
33
29
34
- return ( ) => {
35
- active = false ;
36
- } ;
37
- } , [ loading ] ) ;
38
-
39
- React . useEffect ( ( ) => {
40
- if ( ! open ) {
41
- setOptions ( [ ] ) ;
42
- }
43
- } , [ open ] ) ;
30
+ const handleClose = ( ) => {
31
+ setOpen ( false ) ;
32
+ setOptions ( [ ] ) ;
33
+ } ;
44
34
45
35
return (
46
36
< Autocomplete
47
37
sx = { { width : 300 } }
48
38
open = { open }
49
- onOpen = { ( ) => {
50
- setOpen ( true ) ;
51
- } }
52
- onClose = { ( ) => {
53
- setOpen ( false ) ;
54
- } }
39
+ onOpen = { handleOpen }
40
+ onClose = { handleClose }
55
41
isOptionEqualToValue = { ( option , value ) => option . title === value . title }
56
42
getOptionLabel = { ( option ) => option . title }
57
43
options = { options }
Original file line number Diff line number Diff line change @@ -19,44 +19,30 @@ function sleep(duration: number): Promise<void> {
19
19
export default function Asynchronous ( ) {
20
20
const [ open , setOpen ] = React . useState ( false ) ;
21
21
const [ options , setOptions ] = React . useState < readonly Film [ ] > ( [ ] ) ;
22
- const loading = open && options . length === 0 ;
23
-
24
- React . useEffect ( ( ) => {
25
- let active = true ;
26
-
27
- if ( ! loading ) {
28
- return undefined ;
29
- }
22
+ const [ loading , setLoading ] = React . useState ( false ) ;
30
23
24
+ const handleOpen = ( ) => {
25
+ setOpen ( true ) ;
31
26
( async ( ) => {
27
+ setLoading ( true ) ;
32
28
await sleep ( 1e3 ) ; // For demo purposes.
29
+ setLoading ( false ) ;
33
30
34
- if ( active ) {
35
- setOptions ( [ ...topFilms ] ) ;
36
- }
31
+ setOptions ( [ ...topFilms ] ) ;
37
32
} ) ( ) ;
33
+ } ;
38
34
39
- return ( ) => {
40
- active = false ;
41
- } ;
42
- } , [ loading ] ) ;
43
-
44
- React . useEffect ( ( ) => {
45
- if ( ! open ) {
46
- setOptions ( [ ] ) ;
47
- }
48
- } , [ open ] ) ;
35
+ const handleClose = ( ) => {
36
+ setOpen ( false ) ;
37
+ setOptions ( [ ] ) ;
38
+ } ;
49
39
50
40
return (
51
41
< Autocomplete
52
42
sx = { { width : 300 } }
53
43
open = { open }
54
- onOpen = { ( ) => {
55
- setOpen ( true ) ;
56
- } }
57
- onClose = { ( ) => {
58
- setOpen ( false ) ;
59
- } }
44
+ onOpen = { handleOpen }
45
+ onClose = { handleClose }
60
46
isOptionEqualToValue = { ( option , value ) => option . title === value . title }
61
47
getOptionLabel = { ( option ) => option . title }
62
48
options = { options }
You can’t perform that action at this time.
0 commit comments