Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs][material-icons] Add option to search for all icon styles #41415

Closed
wants to merge 8 commits into from
52 changes: 40 additions & 12 deletions docs/data/material/components/material-icons/SearchIcons.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import DialogTitle from '@mui/material/DialogTitle';
import IconButton from '@mui/material/IconButton';
import Tooltip from '@mui/material/Tooltip';
import Button from '@mui/material/Button';
import Stack from '@mui/material/Stack';
import * as flexsearch from 'flexsearch';
import SearchIcon from '@mui/icons-material/Search';
import FormControlLabel from '@mui/material/FormControlLabel';
Expand Down Expand Up @@ -478,11 +479,14 @@ function useLatest(value) {
return value ?? latest.current;
}


export default function SearchIcons() {
const [keys, setKeys] = React.useState(null);
const [theme, setTheme] = useQueryParameterState('theme', 'Filled');
const [theme, setTheme] = useQueryParameterState('theme', 'All');
const [selectedIcon, setSelectedIcon] = useQueryParameterState('selected', '');
const [query, setQuery] = useQueryParameterState('query', '');
const [isPending, startUpdateSearchTransition] = React.useTransition();
const [minIcons, setMinIcons] = React.useState(99);

const handleOpenClick = React.useCallback(
(event) => {
Expand Down Expand Up @@ -518,20 +522,22 @@ export default function SearchIcons() {
);

React.useEffect(() => {
updateSearchResults(query);
return () => {
updateSearchResults.clear();
};
startUpdateSearchTransition(() => updateSearchResults(query));
setMinIcons(99);

return () => updateSearchResults.clear();
}, [query, updateSearchResults]);

const icons = React.useMemo(
() =>
(keys === null ? allIcons : keys.map((key) => allIconsMap[key])).filter(
(icon) => theme === icon.theme,
(icon) => theme === 'All' || theme === icon.theme,
),
[theme, keys],
);

const totalNumIcons = icons.length;

const dialogSelectedIcon = useLatest(
selectedIcon ? allIconsMap[selectedIcon] : null,
);
Expand All @@ -544,7 +550,7 @@ export default function SearchIcons() {
Filter the style
</Typography>
<RadioGroup>
{['Filled', 'Outlined', 'Rounded', 'Two tone', 'Sharp'].map(
{['All', 'Filled', 'Outlined', 'Rounded', 'Two tone', 'Sharp'].map(
(currentTheme) => {
return (
<FormControlLabel
Expand Down Expand Up @@ -574,14 +580,36 @@ export default function SearchIcons() {
autoFocus
value={query}
onChange={(event) => setQuery(event.target.value)}
placeholder="Search icons…"
placeholder={`Search over ${formatNumber(totalNumIcons)} available icons...`}
inputProps={{ 'aria-label': 'search icons' }}
/>
</Paper>
<Typography sx={{ mb: 1 }}>{`${formatNumber(
icons.length,
)} matching results`}</Typography>
<Icons icons={icons} handleOpenClick={handleOpenClick} />
{query.length > 0 && (
<Typography variant="subtitle1" sx={{ mb: 1 }}>
{isPending
? `loading...`
: `${formatNumber(totalNumIcons)} matching results`}
</Typography>
)}
{!isPending && (
<Stack spacing={4} sx={{ height: '100%' }}>
<Icons
icons={icons.slice(0, minIcons)}
handleOpenClick={handleOpenClick}
/>
{totalNumIcons > minIcons && (
<Button
size="small"
color="primary"
variant="outlined"
sx={{ mt: 'auto' }}
onClick={() => setMinIcons((m) => (m += 99))}
>
View more
</Button>
)}
</Stack>
)}
</Grid>
<DialogDetails
open={!!selectedIcon}
Expand Down
Loading