Skip to content

Commit

Permalink
Let useQueryParameterState accept custom delay
Browse files Browse the repository at this point in the history
  • Loading branch information
cipherlogs committed Mar 15, 2024
1 parent 9b5b900 commit 4246617
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
13 changes: 5 additions & 8 deletions docs/data/material/components/material-icons/SearchIcons.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import copy from 'clipboard-copy';
import InputBase from '@mui/material/InputBase';
import Typography from '@mui/material/Typography';
import PropTypes from 'prop-types';
import { debounce } from '@mui/material/utils';
import Grid from '@mui/material/Grid';
import Dialog from '@mui/material/Dialog';
import DialogActions from '@mui/material/DialogActions';
Expand Down Expand Up @@ -486,7 +485,7 @@ export default function SearchIcons() {
const lazyKeys = React.useDeferredValue(keys);
const [theme, setTheme] = useQueryParameterState('theme', 'All');
const [selectedIcon, setSelectedIcon] = useQueryParameterState('selected', '');
const [query, setQuery] = useQueryParameterState('query', '');
const [query, setQuery] = useQueryParameterState('query', '', 0);
const lazyQuery = React.useDeferredValue(query);
const [minIcons, setMinIcons] = React.useState(49);

Expand Down Expand Up @@ -529,8 +528,6 @@ export default function SearchIcons() {
[theme, lazyKeys],
);

const totalNumIcons = icons.length;

const dialogSelectedIcon = useLatest(
selectedIcon ? allIconsMap[selectedIcon] : null,
);
Expand Down Expand Up @@ -573,20 +570,20 @@ export default function SearchIcons() {
autoFocus
value={query}
onChange={(event) => setQuery(event.target.value)}
placeholder={`Search over ${formatNumber(totalNumIcons)} available icons...`}
placeholder={`Search icons...`}
inputProps={{ 'aria-label': 'search icons' }}
/>
</Paper>
<Typography variant="subtitle1" sx={{ mb: 1 }}>
{`${formatNumber(totalNumIcons)} matching results`}
{`${formatNumber(icons.length)} matching results`}
</Typography>
<Stack spacing={4} sx={{ height: '100%' }}>
<Icons
icons={icons.slice(0, minIcons)}
handleOpenClick={handleOpenClick}
data={{ total: totalNumIcons, length: minIcons }}
data={{ total: icons.length, length: minIcons }}
/>
{totalNumIcons > minIcons && (
{icons.length > minIcons && (
<Button
size="small"
color="primary"
Expand Down
5 changes: 2 additions & 3 deletions docs/src/modules/utils/useQueryParameterState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import * as React from 'react';
import { useRouter } from 'next/router';
import { debounce } from '@mui/material/utils';

const QUERY_UPDATE_WAIT_MS = 220;

/**
* Similar to `React.useState`, but it syncs back the current state to a query
* parameter in the url, therefore it only supports strings. Wrap the result with
Expand All @@ -14,6 +12,7 @@ const QUERY_UPDATE_WAIT_MS = 220;
export default function useQueryParameterState(
name: string,
initialValue = '',
waitMs = 220,
): [string, (newValue: string) => void] {
const initialValueRef = React.useRef(initialValue);

Expand Down Expand Up @@ -50,7 +49,7 @@ export default function useQueryParameterState(
},
);
}
}, QUERY_UPDATE_WAIT_MS),
}, waitMs),
[name, router],
);

Expand Down

0 comments on commit 4246617

Please sign in to comment.