Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions translate/src/modules/translationform/components/EditField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import React, {
useContext,
useEffect,
useImperativeHandle,
useRef,
useState,
} from 'react';

Expand All @@ -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<EditFieldHandle, EditFieldProps>(
Expand All @@ -46,6 +47,11 @@ export const EditField = memo(
const keyHandlers = useKeyHandlers();
const [view, setView] = useState<EditorView | null>(null);

const callbacks = useRef({ onFocus, setResultFromInput });
useEffect(() => {
callbacks.current = { onFocus, setResultFromInput };
}, [onFocus, setResultFromInput]);

const initView = useCallback(
(parent: HTMLDivElement | null) => {
if (parent) {
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -47,28 +47,29 @@ 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 (
<Locale.Provider value={locale}>
<MockLocalizationProvider>
<EntityView.Provider value={{ entity }}>
<EntityView.Provider value={{ entity: currentEntity }}>
<EditorProvider>
<Spy />
<TranslationForm />
</EditorProvider>
</EntityView.Provider>
</MockLocalizationProvider>
</Locale.Provider>
),
store,
);
);
}, store);
vi.runAllTimers();

// TODO:Replace the querySelector with testing-library-ish approaches
Expand All @@ -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('<TranslationForm> with multiple fields', () => {
Expand Down Expand Up @@ -434,6 +435,39 @@ describe('<TranslationForm> 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 =
Expand Down