Files
react-test/docs/data/joy/components/input/DebouncedInput.js
how2ice 005cf56baf
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
init project
2025-12-12 14:26:25 +09:00

43 lines
1.2 KiB
JavaScript

import * as React from 'react';
import PropTypes from 'prop-types';
import Input from '@mui/joy/Input';
import Typography from '@mui/joy/Typography';
import Box from '@mui/joy/Box';
function DebounceInput(props) {
const { handleDebounce, debounceTimeout, ...other } = props;
const timerRef = React.useRef(undefined);
const handleChange = (event) => {
clearTimeout(timerRef.current);
timerRef.current = setTimeout(() => {
handleDebounce(event.target.value);
}, debounceTimeout);
};
return <Input {...other} onChange={handleChange} />;
}
DebounceInput.propTypes = {
debounceTimeout: PropTypes.number.isRequired,
handleDebounce: PropTypes.func.isRequired,
};
export default function DebouncedInput() {
const [debouncedValue, setDebouncedValue] = React.useState('');
const handleDebounce = (value) => {
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>
);
}