mirror of
https://github.com/wassname/optuna-dashboard.git
synced 2026-09-10 12:23:22 +08:00
Add Debounce.tsx
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
import React, { FC, useEffect } from 'react';
|
||||
import { TextField, TextFieldProps } from '@mui/material';
|
||||
|
||||
export const DebouncedInputTextField: FC<{
|
||||
onChange: (s: string, valid: boolean) => void;
|
||||
delay: number;
|
||||
textFieldProps: TextFieldProps;
|
||||
}> = ({ onChange, delay, textFieldProps }) => {
|
||||
const [text, setText] = React.useState<string>('');
|
||||
const [valid, setValidity] = React.useState<boolean>(true);
|
||||
useEffect(() => {
|
||||
const timer = setTimeout(() => {
|
||||
onChange(text, valid);
|
||||
}, delay);
|
||||
return () => {
|
||||
clearTimeout(timer);
|
||||
};
|
||||
}, [text, delay]);
|
||||
return (
|
||||
<TextField
|
||||
onChange={e => {
|
||||
setText(e.target.value);
|
||||
setValidity(e.target.validity.valid);
|
||||
}}
|
||||
{...textFieldProps}
|
||||
/>
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user