Skip to content

Commit 1eafca2

Browse files
only21milcodex
andcommitted
fix(web): save settings fields only after an edit
Co-Authored-By: GPT-6.1 Sol <[email protected]>
1 parent 9c361cd commit 1eafca2

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

‎apps/web/src/hooks/useCommitOnBlur.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
import { type ChangeEvent, type KeyboardEvent, useState } from "react";
1+
import { type ChangeEvent, type KeyboardEvent, useRef, useState } from "react";
22

33
export function useCommitOnBlur(value: string, onCommit: (next: string) => void) {
44
const [draft, setDraft] = useState<string | null>(null);
5+
const valueOnFocus = useRef(value);
56

67
return {
78
value: draft ?? value,
89
onChange: (event: ChangeEvent<HTMLInputElement>) => {
910
setDraft(event.target.value);
1011
},
1112
onFocus: () => {
13+
valueOnFocus.current = value;
1214
setDraft(value);
1315
},
1416
onBlur: () => {
1517
const next = draft ?? value;
1618
setDraft(null);
17-
if (next !== value) {
19+
if (next !== valueOnFocus.current && next !== value) {
1820
onCommit(next);
1921
}
2022
},

0 commit comments

Comments
 (0)