Skip to content

Commit 97c708f

Browse files
committed
[material-ui][material-icons] Make the search more responsive and intuitive
1 parent c4894dd commit 97c708f

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

docs/data/material/components/material-icons/SearchIcons.js

+37-9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import DialogTitle from '@mui/material/DialogTitle';
1414
import IconButton from '@mui/material/IconButton';
1515
import Tooltip from '@mui/material/Tooltip';
1616
import Button from '@mui/material/Button';
17+
import Stack from '@mui/material/Stack';
1718
import * as flexsearch from 'flexsearch';
1819
import SearchIcon from '@mui/icons-material/Search';
1920
import FormControlLabel from '@mui/material/FormControlLabel';
@@ -478,11 +479,14 @@ function useLatest(value) {
478479
return value ?? latest.current;
479480
}
480481

482+
481483
export default function SearchIcons() {
482484
const [keys, setKeys] = React.useState(null);
483485
const [theme, setTheme] = useQueryParameterState('theme', 'All');
484486
const [selectedIcon, setSelectedIcon] = useQueryParameterState('selected', '');
485487
const [query, setQuery] = useQueryParameterState('query', '');
488+
const [isPending, startUpdateSearchTransition] = React.useTransition();
489+
const [minIcons, setMinIcons] = React.useState(49);
486490

487491
const handleOpenClick = React.useCallback(
488492
(event) => {
@@ -518,10 +522,10 @@ export default function SearchIcons() {
518522
);
519523

520524
React.useEffect(() => {
521-
updateSearchResults(query);
522-
return () => {
523-
updateSearchResults.clear();
524-
};
525+
startUpdateSearchTransition(() => updateSearchResults(query));
526+
setMinIcons(49);
527+
528+
return () => updateSearchResults.clear();
525529
}, [query, updateSearchResults]);
526530

527531
const icons = React.useMemo(
@@ -532,6 +536,8 @@ export default function SearchIcons() {
532536
[theme, keys],
533537
);
534538

539+
const totalNumIcons = icons.length;
540+
535541
const dialogSelectedIcon = useLatest(
536542
selectedIcon ? allIconsMap[selectedIcon] : null,
537543
);
@@ -574,14 +580,36 @@ export default function SearchIcons() {
574580
autoFocus
575581
value={query}
576582
onChange={(event) => setQuery(event.target.value)}
577-
placeholder="Search icons…"
583+
placeholder={`Search over ${formatNumber(totalNumIcons)} available icons...`}
578584
inputProps={{ 'aria-label': 'search icons' }}
579585
/>
580586
</Paper>
581-
<Typography sx={{ mb: 1 }}>{`${formatNumber(
582-
icons.length,
583-
)} matching results`}</Typography>
584-
<Icons icons={icons} handleOpenClick={handleOpenClick} />
587+
{query.length > 0 && (
588+
<Typography variant="subtitle1" sx={{ mb: 1 }}>
589+
{isPending
590+
? `loading...`
591+
: `${formatNumber(totalNumIcons)} matching results`}
592+
</Typography>
593+
)}
594+
{!isPending && (
595+
<Stack spacing={4} sx={{ height: '100%' }}>
596+
<Icons
597+
icons={icons.slice(0, minIcons)}
598+
handleOpenClick={handleOpenClick}
599+
/>
600+
{totalNumIcons > minIcons && (
601+
<Button
602+
size="small"
603+
color="primary"
604+
variant="outlined"
605+
sx={{ mt: 'auto' }}
606+
onClick={() => setMinIcons((m) => (m += 49))}
607+
>
608+
View more
609+
</Button>
610+
)}
611+
</Stack>
612+
)}
585613
</Grid>
586614
<DialogDetails
587615
open={!!selectedIcon}

0 commit comments

Comments
 (0)