fix
This commit is contained in:
@@ -25,6 +25,12 @@ export const CommitInput: React.FC<CommitInputProps> = ({
|
||||
}) => {
|
||||
const [localVal, setLocalVal] = useState<string>(String(value));
|
||||
const focusedRef = useRef(false);
|
||||
const localValRef = useRef(localVal);
|
||||
const valueRef = useRef(value);
|
||||
const onCommitRef = useRef(onCommit);
|
||||
localValRef.current = localVal;
|
||||
valueRef.current = value;
|
||||
onCommitRef.current = onCommit;
|
||||
|
||||
useEffect(() => {
|
||||
if (!focusedRef.current) {
|
||||
@@ -32,12 +38,27 @@ export const CommitInput: React.FC<CommitInputProps> = ({
|
||||
}
|
||||
}, [value]);
|
||||
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (
|
||||
focusedRef.current &&
|
||||
localValRef.current !== String(valueRef.current)
|
||||
) {
|
||||
onCommitRef.current(localValRef.current);
|
||||
}
|
||||
};
|
||||
}, []);
|
||||
|
||||
const commitIfChanged = () => {
|
||||
if (localValRef.current !== String(valueRef.current)) {
|
||||
onCommitRef.current(localValRef.current);
|
||||
}
|
||||
};
|
||||
|
||||
const handleBlur = (e: React.FocusEvent<HTMLInputElement>) => {
|
||||
focusedRef.current = false;
|
||||
onBlur?.(e);
|
||||
if (localVal !== String(value)) {
|
||||
onCommit(localVal);
|
||||
}
|
||||
commitIfChanged();
|
||||
};
|
||||
|
||||
const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
|
||||
|
||||
Reference in New Issue
Block a user