fix: properly update duplicated and none i18n fields when default locale updates (#934)
This commit is contained in:
parent
a508158f59
commit
7b9d653411
@ -105,13 +105,15 @@ const EditorControl = ({
|
|||||||
[field, i18n?.defaultLocale, parentDuplicate, locale],
|
[field, i18n?.defaultLocale, parentDuplicate, locale],
|
||||||
);
|
);
|
||||||
const i18nDisabled = useMemo(
|
const i18nDisabled = useMemo(
|
||||||
() => isFieldHidden(field, locale, i18n?.defaultLocale),
|
() =>
|
||||||
|
isFieldHidden(field, locale, i18n?.defaultLocale) ||
|
||||||
|
isFieldDuplicate(field, locale, i18n?.defaultLocale),
|
||||||
[field, i18n?.defaultLocale, locale],
|
[field, i18n?.defaultLocale, locale],
|
||||||
);
|
);
|
||||||
const hidden = useHidden(field, entry, listItemPath);
|
const hidden = useHidden(field, entry, listItemPath);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!['list', 'object'].includes(field.widget)) {
|
if (!['list', 'object'].includes(field.widget) && !i18nDisabled) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -220,7 +222,7 @@ const EditorControl = ({
|
|||||||
field: field as UnknownField,
|
field: field as UnknownField,
|
||||||
fieldsErrors,
|
fieldsErrors,
|
||||||
submitted,
|
submitted,
|
||||||
disabled: disabled || duplicate || hidden || i18nDisabled,
|
disabled: disabled || duplicate || controlled || i18nDisabled,
|
||||||
duplicate,
|
duplicate,
|
||||||
label: getFieldLabel(field, t),
|
label: getFieldLabel(field, t),
|
||||||
locale,
|
locale,
|
||||||
@ -237,7 +239,7 @@ const EditorControl = ({
|
|||||||
hasErrors,
|
hasErrors,
|
||||||
errors,
|
errors,
|
||||||
theme,
|
theme,
|
||||||
controlled,
|
controlled: controlled || i18nDisabled,
|
||||||
})}
|
})}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
import React, { useCallback, useMemo, useRef, useState } from 'react';
|
||||||
|
|
||||||
import Field from '@staticcms/core/components/common/field/Field';
|
import Field from '@staticcms/core/components/common/field/Field';
|
||||||
import TextField from '@staticcms/core/components/common/text-field/TextField';
|
import TextField from '@staticcms/core/components/common/text-field/TextField';
|
||||||
import useDebounce from '@staticcms/core/lib/hooks/useDebounce';
|
|
||||||
import classNames from '@staticcms/core/lib/util/classNames.util';
|
import classNames from '@staticcms/core/lib/util/classNames.util';
|
||||||
import { generateClassNames } from '@staticcms/core/lib/util/theming.util';
|
import { generateClassNames } from '@staticcms/core/lib/util/theming.util';
|
||||||
|
|
||||||
@ -36,21 +35,16 @@ const StringControl: FC<WidgetControlProps<string, StringOrTextField>> = ({
|
|||||||
() => (controlled || duplicate ? rawValue : internalRawValue),
|
() => (controlled || duplicate ? rawValue : internalRawValue),
|
||||||
[controlled, duplicate, rawValue, internalRawValue],
|
[controlled, duplicate, rawValue, internalRawValue],
|
||||||
);
|
);
|
||||||
const debouncedInternalValue = useDebounce(internalValue, 250);
|
|
||||||
|
|
||||||
const ref = useRef<HTMLInputElement | null>(null);
|
const ref = useRef<HTMLInputElement | null>(null);
|
||||||
|
|
||||||
const handleChange = useCallback((event: ChangeEvent<HTMLInputElement>) => {
|
const handleChange = useCallback(
|
||||||
setInternalValue(event.target.value);
|
(event: ChangeEvent<HTMLInputElement>) => {
|
||||||
}, []);
|
onChange(event.target.value);
|
||||||
|
setInternalValue(event.target.value);
|
||||||
useEffect(() => {
|
},
|
||||||
if (rawValue === debouncedInternalValue) {
|
[onChange],
|
||||||
return;
|
);
|
||||||
}
|
|
||||||
|
|
||||||
onChange(debouncedInternalValue);
|
|
||||||
}, [debouncedInternalValue, onChange, rawValue]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Field
|
<Field
|
||||||
|
@ -66,10 +66,8 @@ describe(StringControl.name, () => {
|
|||||||
await userEvent.type(input, 'I am some text');
|
await userEvent.type(input, 'I am some text');
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(onChange).toHaveBeenCalledTimes(0);
|
|
||||||
|
|
||||||
await waitFor(() => {
|
await waitFor(() => {
|
||||||
expect(onChange).toHaveBeenCalledTimes(1);
|
expect(onChange).toHaveBeenCalledTimes(14);
|
||||||
expect(onChange).toHaveBeenLastCalledWith('I am some text');
|
expect(onChange).toHaveBeenLastCalledWith('I am some text');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
import React, { useCallback, useMemo, useRef, useState } from 'react';
|
||||||
|
|
||||||
import Field from '@staticcms/core/components/common/field/Field';
|
import Field from '@staticcms/core/components/common/field/Field';
|
||||||
import TextArea from '@staticcms/core/components/common/text-field/TextArea';
|
import TextArea from '@staticcms/core/components/common/text-field/TextArea';
|
||||||
import useDebounce from '@staticcms/core/lib/hooks/useDebounce';
|
|
||||||
import classNames from '@staticcms/core/lib/util/classNames.util';
|
import classNames from '@staticcms/core/lib/util/classNames.util';
|
||||||
import { generateClassNames } from '@staticcms/core/lib/util/theming.util';
|
import { generateClassNames } from '@staticcms/core/lib/util/theming.util';
|
||||||
|
|
||||||
@ -27,29 +26,25 @@ const TextControl: FC<WidgetControlProps<string, StringOrTextField>> = ({
|
|||||||
disabled,
|
disabled,
|
||||||
field,
|
field,
|
||||||
forSingleList,
|
forSingleList,
|
||||||
|
controlled,
|
||||||
onChange,
|
onChange,
|
||||||
}) => {
|
}) => {
|
||||||
const rawValue = useMemo(() => value ?? '', [value]);
|
const rawValue = useMemo(() => value ?? '', [value]);
|
||||||
const [internalRawValue, setInternalValue] = useState(rawValue);
|
const [internalRawValue, setInternalValue] = useState(rawValue);
|
||||||
const internalValue = useMemo(
|
const internalValue = useMemo(
|
||||||
() => (duplicate ? rawValue : internalRawValue),
|
() => (controlled || duplicate ? rawValue : internalRawValue),
|
||||||
[internalRawValue, duplicate, rawValue],
|
[controlled, duplicate, rawValue, internalRawValue],
|
||||||
);
|
);
|
||||||
const debouncedInternalValue = useDebounce(internalValue, 250);
|
|
||||||
|
|
||||||
const ref = useRef<HTMLInputElement | null>(null);
|
const ref = useRef<HTMLInputElement | null>(null);
|
||||||
|
|
||||||
const handleChange = useCallback((event: ChangeEvent<HTMLInputElement>) => {
|
const handleChange = useCallback(
|
||||||
setInternalValue(event.target.value);
|
(event: ChangeEvent<HTMLInputElement>) => {
|
||||||
}, []);
|
onChange(event.target.value);
|
||||||
|
setInternalValue(event.target.value);
|
||||||
useEffect(() => {
|
},
|
||||||
if (rawValue === debouncedInternalValue) {
|
[onChange],
|
||||||
return;
|
);
|
||||||
}
|
|
||||||
|
|
||||||
onChange(debouncedInternalValue);
|
|
||||||
}, [debouncedInternalValue, onChange, rawValue]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Field
|
<Field
|
||||||
|
@ -2,8 +2,8 @@
|
|||||||
* @jest-environment jsdom
|
* @jest-environment jsdom
|
||||||
*/
|
*/
|
||||||
import '@testing-library/jest-dom';
|
import '@testing-library/jest-dom';
|
||||||
|
import { act } from '@testing-library/react';
|
||||||
import userEvent from '@testing-library/user-event';
|
import userEvent from '@testing-library/user-event';
|
||||||
import { act, waitFor } from '@testing-library/react';
|
|
||||||
|
|
||||||
import { mockTextField } from '@staticcms/test/data/fields.mock';
|
import { mockTextField } from '@staticcms/test/data/fields.mock';
|
||||||
import { createWidgetControlHarness } from '@staticcms/test/harnesses/widget.harness';
|
import { createWidgetControlHarness } from '@staticcms/test/harnesses/widget.harness';
|
||||||
@ -66,12 +66,8 @@ describe(TextControl.name, () => {
|
|||||||
await userEvent.type(input, 'I am some text');
|
await userEvent.type(input, 'I am some text');
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(onChange).toHaveBeenCalledTimes(0);
|
expect(onChange).toHaveBeenCalledTimes(14);
|
||||||
|
expect(onChange).toHaveBeenLastCalledWith('I am some text');
|
||||||
await waitFor(() => {
|
|
||||||
expect(onChange).toHaveBeenCalledTimes(1);
|
|
||||||
expect(onChange).toHaveBeenLastCalledWith('I am some text');
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should show error', async () => {
|
it('should show error', async () => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user