Some checks failed
No response / noResponse (push) Has been cancelled
CI / Continuous releases (push) Has been cancelled
CI / test-dev (macos-latest) (push) Has been cancelled
CI / test-dev (ubuntu-latest) (push) Has been cancelled
CI / test-dev (windows-latest) (push) Has been cancelled
Maintenance / main (push) Has been cancelled
Scorecards supply-chain security / Scorecards analysis (push) Has been cancelled
CodeQL / Analyze (push) Has been cancelled
42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
import * as React from 'react';
|
|
import Input, { InputProps } from '@mui/joy/Input';
|
|
import Typography from '@mui/joy/Typography';
|
|
import Box from '@mui/joy/Box';
|
|
|
|
type DebounceProps = {
|
|
handleDebounce: (value: string) => void;
|
|
debounceTimeout: number;
|
|
};
|
|
|
|
function DebounceInput(props: InputProps & DebounceProps) {
|
|
const { handleDebounce, debounceTimeout, ...other } = props;
|
|
|
|
const timerRef = React.useRef<ReturnType<typeof setTimeout>>(undefined);
|
|
|
|
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
clearTimeout(timerRef.current);
|
|
timerRef.current = setTimeout(() => {
|
|
handleDebounce(event.target.value);
|
|
}, debounceTimeout);
|
|
};
|
|
|
|
return <Input {...other} onChange={handleChange} />;
|
|
}
|
|
|
|
export default function DebouncedInput() {
|
|
const [debouncedValue, setDebouncedValue] = React.useState('');
|
|
const handleDebounce = (value: string) => {
|
|
setDebouncedValue(value);
|
|
};
|
|
return (
|
|
<Box sx={{ display: 'flex', flexDirection: 'column', gap: '0.5rem' }}>
|
|
<DebounceInput
|
|
placeholder="Type in here…"
|
|
debounceTimeout={1000}
|
|
handleDebounce={handleDebounce}
|
|
/>
|
|
<Typography>Debounced input: {debouncedValue}</Typography>
|
|
</Box>
|
|
);
|
|
}
|