Skip to content

Commit

Permalink
Fixing async Autocomplete example in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
anishk committed Mar 28, 2021
1 parent 917a3dd commit b13840e
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 24 deletions.
64 changes: 55 additions & 9 deletions docs/src/pages/components/autocomplete/Asynchronous.js
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-use-before-define */
import * as React from 'react';
import TextField from '@material-ui/core/TextField';
import Autocomplete from '@material-ui/core/Autocomplete';
Expand All @@ -22,16 +23,10 @@ export default function Asynchronous() {
}

(async () => {
// https://www.registers.service.gov.uk/registers/country/use-the-api
const response = await fetch(
'https://country.register.gov.uk/records.json?page-size=5000',
);

await sleep(1e3); // For demo purposes.
const countries = await response.json();

if (active) {
setOptions(Object.keys(countries).map((key) => countries[key].item[0]));
setOptions([...topFilms]);
}
})();

Expand All @@ -57,8 +52,8 @@ export default function Asynchronous() {
onClose={() => {
setOpen(false);
}}
getOptionSelected={(option, value) => option.name === value.name}
getOptionLabel={(option) => option.name}
getOptionSelected={(option, value) => option.title === value.title}
getOptionLabel={(option) => option.title}
options={options}
loading={loading}
renderInput={(params) => (
Expand All @@ -79,3 +74,54 @@ export default function Asynchronous() {
/>
);
}

// Top films as rated by IMDb users. http://www.imdb.com/chart/top
const topFilms = [
{ title: 'The Shawshank Redemption', year: 1994 },
{ title: 'The Godfather', year: 1972 },
{ title: 'The Godfather: Part II', year: 1974 },
{ title: 'The Dark Knight', year: 2008 },
{ title: '12 Angry Men', year: 1957 },
{ title: "Schindler's List", year: 1993 },
{ title: 'Pulp Fiction', year: 1994 },
{
title: 'The Lord of the Rings: The Return of the King',
year: 2003,
},
{ title: 'The Good, the Bad and the Ugly', year: 1966 },
{ title: 'Fight Club', year: 1999 },
{
title: 'The Lord of the Rings: The Fellowship of the Ring',
year: 2001,
},
{
title: 'Star Wars: Episode V - The Empire Strikes Back',
year: 1980,
},
{ title: 'Forrest Gump', year: 1994 },
{ title: 'Inception', year: 2010 },
{
title: 'The Lord of the Rings: The Two Towers',
year: 2002,
},
{ title: "One Flew Over the Cuckoo's Nest", year: 1975 },
{ title: 'Goodfellas', year: 1990 },
{ title: 'The Matrix', year: 1999 },
{ title: 'Seven Samurai', year: 1954 },
{
title: 'Star Wars: Episode IV - A New Hope',
year: 1977,
},
{ title: 'City of God', year: 2002 },
{ title: 'Se7en', year: 1995 },
{ title: 'The Silence of the Lambs', year: 1991 },
{ title: "It's a Wonderful Life", year: 1946 },
{ title: 'Life Is Beautiful', year: 1997 },
{ title: 'The Usual Suspects', year: 1995 },
{ title: 'Léon: The Professional', year: 1994 },
{ title: 'Spirited Away', year: 2001 },
{ title: 'Saving Private Ryan', year: 1998 },
{ title: 'Once Upon a Time in the West', year: 1968 },
{ title: 'American History X', year: 1998 },
{ title: 'Interstellar', year: 2014 },
];
74 changes: 59 additions & 15 deletions docs/src/pages/components/autocomplete/Asynchronous.tsx
@@ -1,10 +1,12 @@
/* eslint-disable @typescript-eslint/no-use-before-define */
import * as React from 'react';
import TextField from '@material-ui/core/TextField';
import Autocomplete from '@material-ui/core/Autocomplete';
import CircularProgress from '@material-ui/core/CircularProgress';

interface CountryType {
name: string;
interface Film {
title: string;
year: number;
}

function sleep(delay = 0) {
Expand All @@ -15,7 +17,7 @@ function sleep(delay = 0) {

export default function Asynchronous() {
const [open, setOpen] = React.useState(false);
const [options, setOptions] = React.useState<CountryType[]>([]);
const [options, setOptions] = React.useState<Film[]>([]);
const loading = open && options.length === 0;

React.useEffect(() => {
Expand All @@ -26,19 +28,10 @@ export default function Asynchronous() {
}

(async () => {
// https://www.registers.service.gov.uk/registers/country/use-the-api
const response = await fetch(
'https://country.register.gov.uk/records.json?page-size=5000',
);
await sleep(1e3); // For demo purposes.
const countries = await response.json();

if (active) {
setOptions(
Object.keys(countries).map(
(key) => countries[key].item[0],
) as CountryType[],
);
setOptions([...topFilms]);
}
})();

Expand All @@ -64,8 +57,8 @@ export default function Asynchronous() {
onClose={() => {
setOpen(false);
}}
getOptionSelected={(option, value) => option.name === value.name}
getOptionLabel={(option) => option.name}
getOptionSelected={(option, value) => option.title === value.title}
getOptionLabel={(option) => option.title}
options={options}
loading={loading}
renderInput={(params) => (
Expand All @@ -86,3 +79,54 @@ export default function Asynchronous() {
/>
);
}

// Top films as rated by IMDb users. http://www.imdb.com/chart/top
const topFilms = [
{ title: 'The Shawshank Redemption', year: 1994 },
{ title: 'The Godfather', year: 1972 },
{ title: 'The Godfather: Part II', year: 1974 },
{ title: 'The Dark Knight', year: 2008 },
{ title: '12 Angry Men', year: 1957 },
{ title: "Schindler's List", year: 1993 },
{ title: 'Pulp Fiction', year: 1994 },
{
title: 'The Lord of the Rings: The Return of the King',
year: 2003,
},
{ title: 'The Good, the Bad and the Ugly', year: 1966 },
{ title: 'Fight Club', year: 1999 },
{
title: 'The Lord of the Rings: The Fellowship of the Ring',
year: 2001,
},
{
title: 'Star Wars: Episode V - The Empire Strikes Back',
year: 1980,
},
{ title: 'Forrest Gump', year: 1994 },
{ title: 'Inception', year: 2010 },
{
title: 'The Lord of the Rings: The Two Towers',
year: 2002,
},
{ title: "One Flew Over the Cuckoo's Nest", year: 1975 },
{ title: 'Goodfellas', year: 1990 },
{ title: 'The Matrix', year: 1999 },
{ title: 'Seven Samurai', year: 1954 },
{
title: 'Star Wars: Episode IV - A New Hope',
year: 1977,
},
{ title: 'City of God', year: 2002 },
{ title: 'Se7en', year: 1995 },
{ title: 'The Silence of the Lambs', year: 1991 },
{ title: "It's a Wonderful Life", year: 1946 },
{ title: 'Life Is Beautiful', year: 1997 },
{ title: 'The Usual Suspects', year: 1995 },
{ title: 'Léon: The Professional', year: 1994 },
{ title: 'Spirited Away', year: 2001 },
{ title: 'Saving Private Ryan', year: 1998 },
{ title: 'Once Upon a Time in the West', year: 1968 },
{ title: 'American History X', year: 1998 },
{ title: 'Interstellar', year: 2014 },
];

0 comments on commit b13840e

Please sign in to comment.