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

[joy-ui][Input] Add debounce input demo #39300

Merged
merged 3 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions docs/data/joy/components/input/DebouncedInput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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, ...rest } = props;

const timerRef = React.useRef();

const handleChange = (event) => {
if (timerRef.current) {
clearTimeout(timerRef.current);
}

timerRef.current = window.setTimeout(() => {
handleDebounce(event.target.value);
}, debounceTimeout);
};

return <Input {...rest} 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>
);
}
44 changes: 44 additions & 0 deletions docs/data/joy/components/input/DebouncedInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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, ...rest } = props;

const timerRef = React.useRef<number>();

const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
if (timerRef.current) {
clearTimeout(timerRef.current);
}

timerRef.current = window.setTimeout(() => {
handleDebounce(event.target.value);
}, debounceTimeout);
};

return <Input {...rest} 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>
);
}
6 changes: 6 additions & 0 deletions docs/data/joy/components/input/DebouncedInput.tsx.preview
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<DebounceInput
placeholder="Type in here…"
debounceTimeout={1000}
handleDebounce={handleDebounce}
/>
<Typography>Debounced input: {debouncedValue}</Typography>
4 changes: 4 additions & 0 deletions docs/data/joy/components/input/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ To create a floating label input, a custom component (combination of `<input>` a

{{"demo": "PasswordMeterInput.js"}}

### Debounced Input

{{"demo": "DebouncedInput.js"}}

### Third-party formatting

The Input component can be integrated with third-party formatting libraries for more complex use cases.
Expand Down
Loading