diff --git a/translate/src/modules/translationform/components/EditField.tsx b/translate/src/modules/translationform/components/EditField.tsx index c86c46cefe..ed774ab932 100644 --- a/translate/src/modules/translationform/components/EditField.tsx +++ b/translate/src/modules/translationform/components/EditField.tsx @@ -9,6 +9,7 @@ import React, { useContext, useEffect, useImperativeHandle, + useRef, useState, } from 'react'; @@ -30,9 +31,9 @@ export type EditFieldProps = { /** * The CodeMirror initialization is only run once, * so changes in props or context are not reflected in the editor unless handled separately, - * as is done e.g. for `defaultValue`. + * as with `defaultValue` and the callbacks below. * - * Make sure apply an appropriate `key` prop to keep old instances from being reused. + * Use an appropriate `key` prop when a change requires a new instance. */ export const EditField = memo( forwardRef( @@ -46,6 +47,11 @@ export const EditField = memo( const keyHandlers = useKeyHandlers(); const [view, setView] = useState(null); + const callbacks = useRef({ onFocus, setResultFromInput }); + useEffect(() => { + callbacks.current = { onFocus, setResultFromInput }; + }, [onFocus, setResultFromInput]); + const initView = useCallback( (parent: HTMLDivElement | null) => { if (parent) { @@ -66,6 +72,7 @@ export const EditField = memo( } extensions.push( EditorView.updateListener.of((update) => { + const { onFocus, setResultFromInput } = callbacks.current; if (onFocus && update.focusChanged && update.view.hasFocus) { onFocus(); } diff --git a/translate/src/modules/translationform/components/TranslationForm-multiple.test.jsx b/translate/src/modules/translationform/components/TranslationForm-multiple.test.jsx index e32e6e4348..b5e94e445b 100644 --- a/translate/src/modules/translationform/components/TranslationForm-multiple.test.jsx +++ b/translate/src/modules/translationform/components/TranslationForm-multiple.test.jsx @@ -2,7 +2,7 @@ import { EditorView } from '@codemirror/view'; import ftl from '@fluent/dedent'; import { fluentParseEntry } from '@mozilla/l10n'; import { fireEvent } from '@testing-library/react'; -import { useContext } from 'react'; +import { useContext, useState } from 'react'; import { act } from 'react-dom/test-utils'; import { beforeAll, describe, expect, it, vi } from 'vitest'; @@ -47,18 +47,20 @@ function mountForm(source, target = null, locale = DEFAULT_LOCALE) { }, }; - let actions, result; + let actions, result, setCurrentEntity; const Spy = () => { actions = useContext(EditorActions); result = useContext(EditorResult); return null; }; - const wrapper = mountComponentWithStore( - () => ( + const wrapper = mountComponentWithStore(() => { + const [currentEntity, updateCurrentEntity] = useState(entity); + setCurrentEntity = updateCurrentEntity; + return ( - + @@ -66,9 +68,8 @@ function mountForm(source, target = null, locale = DEFAULT_LOCALE) { - ), - store, - ); + ); + }, store); vi.runAllTimers(); // TODO:Replace the querySelector with testing-library-ish approaches @@ -77,7 +78,7 @@ function mountForm(source, target = null, locale = DEFAULT_LOCALE) { EditorView.findFromDOM(el), ); - return { actions, getResult: () => result, views, wrapper }; + return { actions, getResult: () => result, setCurrentEntity, views, wrapper }; } describe(' with multiple fields', () => { @@ -434,6 +435,39 @@ describe(' with multiple fields', () => { expect(docs()).toEqual(['COMPOSED', 'COMPOSED_LABEL']); }); + it('inserts a placeable into the focused field after a rejection', () => { + const source = ftl` + key = + { $num -> + [one] ONE + *[other] OTHER + } + `; + const { actions, getResult, setCurrentEntity, views } = mountForm(source); + + act(() => + setCurrentEntity((entity) => ({ + ...entity, + translation: { ...entity.translation, status: 'rejected' }, + })), + ); + vi.runAllTimers(); + + act(() => views[1].contentDOM.focus()); + vi.runAllTimers(); + + act(() => actions.setEditorSelection('{ $num }')); + + expect(views.map((view) => view.state.doc.toString())).toEqual([ + '', + '{ $num }', + ]); + expect(getResult().value.alt).toMatchObject([ + { keys: ['one'], pat: [] }, + { keys: [{ '*': 'other' }], pat: [{ $: 'num' }] }, + ]); + }); + it('copies a translation naming its catchall after the source, on one click', () => { const source = ftl` key =