Skip to content

Commit 5348b58

Browse files
Adjust TokenChip so onGlossChange is only triggered when draft and committed gloss differ
1 parent c97dcdf commit 5348b58

2 files changed

Lines changed: 7 additions & 5 deletions

File tree

src/__tests__/components/TokenChip.test.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,7 @@ describe('TokenChip', () => {
202202
);
203203
await userEvent.click(screen.getByRole('textbox', { name: 'Gloss for hello' }));
204204
await userEvent.tab();
205-
expect(spy).toHaveBeenCalledTimes(1);
206-
expect(spy).toHaveBeenCalledWith('GEN 1:1:0', '');
205+
expect(spy).not.toHaveBeenCalled();
207206
});
208207

209208
it('calls onFocus when the input is focused', async () => {

src/components/TokenChip.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import { useGloss, useGlossDispatch } from './AnalysisStore';
55
/**
66
* Renders a single word token as an inline chip with an editable gloss input below the surface
77
* text. Gloss value and dispatch are read from {@link AnalysisStoreProvider} context via
8-
* {@link useGloss} and {@link useGlossDispatch}. The gloss is written to the store only on blur to
9-
* avoid creating a new analysis entry on every keystroke.
8+
* {@link useGloss} and {@link useGlossDispatch}. The gloss is written to the store only on blur, and
9+
* only when the draft differs from the committed value, to avoid creating empty analysis entries on
10+
* focus/blur cycles with no edits.
1011
*
1112
* @param props - Component props
1213
* @param props.token - The word token to render.
@@ -37,7 +38,9 @@ export function TokenChip({
3738
style={{ fieldSizing: 'content', minWidth: '5ch' }}
3839
value={draft}
3940
onChange={(e) => setDraft(e.target.value)}
40-
onBlur={() => onGlossChange(token.ref, token.surfaceText, draft)}
41+
onBlur={() => {
42+
if (draft !== committedGloss) onGlossChange(token.ref, token.surfaceText, draft);
43+
}}
4144
onFocus={onFocus}
4245
type="text"
4346
/>

0 commit comments

Comments
 (0)