This commit is contained in:
iqudoo
2026-06-13 01:35:20 +08:00
parent 4167fb2683
commit f2689520bd
3 changed files with 68 additions and 10 deletions

View File

@@ -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>) => {