Skip to content

Commit

Permalink
[docs][material-ui][Autocomplete] Refactor asynchronous loading demo (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sai6855 authored Sep 7, 2024
1 parent a25b030 commit fc65397
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 54 deletions.
40 changes: 13 additions & 27 deletions docs/data/material/components/autocomplete/Asynchronous.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,30 @@ function sleep(duration) {
export default function Asynchronous() {
const [open, setOpen] = React.useState(false);
const [options, setOptions] = React.useState([]);
const loading = open && options.length === 0;

React.useEffect(() => {
let active = true;

if (!loading) {
return undefined;
}
const [loading, setLoading] = React.useState(false);

const handleOpen = () => {
setOpen(true);
(async () => {
setLoading(true);
await sleep(1e3); // For demo purposes.
setLoading(false);

if (active) {
setOptions([...topFilms]);
}
setOptions([...topFilms]);
})();
};

return () => {
active = false;
};
}, [loading]);

React.useEffect(() => {
if (!open) {
setOptions([]);
}
}, [open]);
const handleClose = () => {
setOpen(false);
setOptions([]);
};

return (
<Autocomplete
sx={{ width: 300 }}
open={open}
onOpen={() => {
setOpen(true);
}}
onClose={() => {
setOpen(false);
}}
onOpen={handleOpen}
onClose={handleClose}
isOptionEqualToValue={(option, value) => option.title === value.title}
getOptionLabel={(option) => option.title}
options={options}
Expand Down
40 changes: 13 additions & 27 deletions docs/data/material/components/autocomplete/Asynchronous.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,30 @@ function sleep(duration: number): Promise<void> {
export default function Asynchronous() {
const [open, setOpen] = React.useState(false);
const [options, setOptions] = React.useState<readonly Film[]>([]);
const loading = open && options.length === 0;

React.useEffect(() => {
let active = true;

if (!loading) {
return undefined;
}
const [loading, setLoading] = React.useState(false);

const handleOpen = () => {
setOpen(true);
(async () => {
setLoading(true);
await sleep(1e3); // For demo purposes.
setLoading(false);

if (active) {
setOptions([...topFilms]);
}
setOptions([...topFilms]);
})();
};

return () => {
active = false;
};
}, [loading]);

React.useEffect(() => {
if (!open) {
setOptions([]);
}
}, [open]);
const handleClose = () => {
setOpen(false);
setOptions([]);
};

return (
<Autocomplete
sx={{ width: 300 }}
open={open}
onOpen={() => {
setOpen(true);
}}
onClose={() => {
setOpen(false);
}}
onOpen={handleOpen}
onClose={handleClose}
isOptionEqualToValue={(option, value) => option.title === value.title}
getOptionLabel={(option) => option.title}
options={options}
Expand Down

0 comments on commit fc65397

Please sign in to comment.