blob: ade19b26254d36dcf4b1f6e2951c11e585f8a03e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import React from 'react';
import s0 from './Input.module.scss';
const { useState, useRef, useEffect, useCallback } = React;
export default function Input(props: React.InputHTMLAttributes<HTMLInputElement>) {
return <input className={s0.input} {...props} />;
}
export function SelfControlledInput({ value, ...restProps }) {
const [internalValue, setInternalValue] = useState(value);
const refValue = useRef(value);
useEffect(() => {
if (refValue.current !== value) {
// ideally we should only do this when this input is not focused
setInternalValue(value);
}
refValue.current = value;
}, [value]);
const onChange = useCallback((e) => setInternalValue(e.target.value), [setInternalValue]);
return <input className={s0.input} value={internalValue} onChange={onChange} {...restProps} />;
}
|