Skip to content

Commit c509da8

Browse files
authored
Fix placeable insertion after a translation is rejected (#4465)
1 parent efc44ca commit c509da8

2 files changed

Lines changed: 52 additions & 11 deletions

File tree

‎translate/src/modules/translationform/components/EditField.tsx‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import React, {
99
useContext,
1010
useEffect,
1111
useImperativeHandle,
12+
useRef,
1213
useState,
1314
} from 'react';
1415

@@ -30,9 +31,9 @@ export type EditFieldProps = {
3031
/**
3132
* The CodeMirror initialization is only run once,
3233
* so changes in props or context are not reflected in the editor unless handled separately,
33-
* as is done e.g. for `defaultValue`.
34+
* as with `defaultValue` and the callbacks below.
3435
*
35-
* Make sure apply an appropriate `key` prop to keep old instances from being reused.
36+
* Use an appropriate `key` prop when a change requires a new instance.
3637
*/
3738
export const EditField = memo(
3839
forwardRef<EditFieldHandle, EditFieldProps>(
@@ -46,6 +47,11 @@ export const EditField = memo(
4647
const keyHandlers = useKeyHandlers();
4748
const [view, setView] = useState<EditorView | null>(null);
4849

50+
const callbacks = useRef({ onFocus, setResultFromInput });
51+
useEffect(() => {
52+
callbacks.current = { onFocus, setResultFromInput };
53+
}, [onFocus, setResultFromInput]);
54+
4955
const initView = useCallback(
5056
(parent: HTMLDivElement | null) => {
5157
if (parent) {
@@ -66,6 +72,7 @@ export const EditField = memo(
6672
}
6773
extensions.push(
6874
EditorView.updateListener.of((update) => {
75+
const { onFocus, setResultFromInput } = callbacks.current;
6976
if (onFocus && update.focusChanged && update.view.hasFocus) {
7077
onFocus();
7178
}

‎translate/src/modules/translationform/components/TranslationForm-multiple.test.jsx‎

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { EditorView } from '@codemirror/view';
22
import ftl from '@fluent/dedent';
33
import { fluentParseEntry } from '@mozilla/l10n';
44
import { fireEvent } from '@testing-library/react';
5-
import { useContext } from 'react';
5+
import { useContext, useState } from 'react';
66
import { act } from 'react-dom/test-utils';
77
import { beforeAll, describe, expect, it, vi } from 'vitest';
88

@@ -47,28 +47,29 @@ function mountForm(source, target = null, locale = DEFAULT_LOCALE) {
4747
},
4848
};
4949

50-
let actions, result;
50+
let actions, result, setCurrentEntity;
5151
const Spy = () => {
5252
actions = useContext(EditorActions);
5353
result = useContext(EditorResult);
5454
return null;
5555
};
5656

57-
const wrapper = mountComponentWithStore(
58-
() => (
57+
const wrapper = mountComponentWithStore(() => {
58+
const [currentEntity, updateCurrentEntity] = useState(entity);
59+
setCurrentEntity = updateCurrentEntity;
60+
return (
5961
<Locale.Provider value={locale}>
6062
<MockLocalizationProvider>
61-
<EntityView.Provider value={{ entity }}>
63+
<EntityView.Provider value={{ entity: currentEntity }}>
6264
<EditorProvider>
6365
<Spy />
6466
<TranslationForm />
6567
</EditorProvider>
6668
</EntityView.Provider>
6769
</MockLocalizationProvider>
6870
</Locale.Provider>
69-
),
70-
store,
71-
);
71+
);
72+
}, store);
7273
vi.runAllTimers();
7374

7475
// TODO:Replace the querySelector with testing-library-ish approaches
@@ -77,7 +78,7 @@ function mountForm(source, target = null, locale = DEFAULT_LOCALE) {
7778
EditorView.findFromDOM(el),
7879
);
7980

80-
return { actions, getResult: () => result, views, wrapper };
81+
return { actions, getResult: () => result, setCurrentEntity, views, wrapper };
8182
}
8283

8384
describe('<TranslationForm> with multiple fields', () => {
@@ -434,6 +435,39 @@ describe('<TranslationForm> with multiple fields', () => {
434435
expect(docs()).toEqual(['COMPOSED', 'COMPOSED_LABEL']);
435436
});
436437

438+
it('inserts a placeable into the focused field after a rejection', () => {
439+
const source = ftl`
440+
key =
441+
{ $num ->
442+
[one] ONE
443+
*[other] OTHER
444+
}
445+
`;
446+
const { actions, getResult, setCurrentEntity, views } = mountForm(source);
447+
448+
act(() =>
449+
setCurrentEntity((entity) => ({
450+
...entity,
451+
translation: { ...entity.translation, status: 'rejected' },
452+
})),
453+
);
454+
vi.runAllTimers();
455+
456+
act(() => views[1].contentDOM.focus());
457+
vi.runAllTimers();
458+
459+
act(() => actions.setEditorSelection('{ $num }'));
460+
461+
expect(views.map((view) => view.state.doc.toString())).toEqual([
462+
'',
463+
'{ $num }',
464+
]);
465+
expect(getResult().value.alt).toMatchObject([
466+
{ keys: ['one'], pat: [] },
467+
{ keys: [{ '*': 'other' }], pat: [{ $: 'num' }] },
468+
]);
469+
});
470+
437471
it('copies a translation naming its catchall after the source, on one click', () => {
438472
const source = ftl`
439473
key =

0 commit comments

Comments
 (0)