2022-01-14 07:49:24 +01:00
|
|
|
import { Textarea, TextareaProps } from '@chakra-ui/react'
|
|
|
|
import { ChangeEvent, useEffect, useState } from 'react'
|
|
|
|
|
|
|
|
type Props = Omit<TextareaProps, 'onChange' | 'value'> & {
|
|
|
|
initialValue: string
|
|
|
|
onChange: (debouncedValue: string) => void
|
|
|
|
}
|
|
|
|
|
|
|
|
export const DebouncedTextarea = ({
|
|
|
|
onChange,
|
|
|
|
initialValue,
|
|
|
|
...props
|
|
|
|
}: Props) => {
|
|
|
|
const [currentValue, setCurrentValue] = useState(initialValue)
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-01-28 09:42:31 +01:00
|
|
|
if (currentValue === initialValue) return
|
|
|
|
onChange(currentValue)
|
2022-01-14 07:49:24 +01:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2022-01-28 09:42:31 +01:00
|
|
|
}, [currentValue])
|
2022-01-14 07:49:24 +01:00
|
|
|
|
|
|
|
const handleChange = (e: ChangeEvent<HTMLTextAreaElement>) => {
|
|
|
|
setCurrentValue(e.target.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Textarea
|
|
|
|
{...props}
|
|
|
|
value={currentValue}
|
|
|
|
onChange={handleChange}
|
|
|
|
resize={'vertical'}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|