Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: trigger validation netsted field #11336

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/__tests__/useForm/setValue.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1386,4 +1386,54 @@ describe('setValue', () => {
expect(screen.getByText('dirty')).toBeVisible();
expect(screen.getByText('touched')).toBeVisible();
});

it('should be able to set to empty array value', () => {
const App = () => {
const { register, setValue } = useForm({
values: {
item1: {
positions: [{ activities: ['Value1', 'Value2'] }],
},
item2: {
positions: [{ activities: ['Value1', 'Value2'] }],
},
},
});

return (
<form>
<input
{...register('item1.positions.0.activities')}
data-testid={'a'}
/>
<input
{...register('item2.positions.0.activities')}
data-testid={'b'}
/>

<button
type="button"
onClick={() => {
setValue('item1.positions', [{ activities: [] }], {
shouldValidate: true,
});

setValue('item2.positions.0.activities', [], {
shouldValidate: true,
});
}}
>
Reset
</button>
</form>
);
};

render(<App />);

fireEvent.click(screen.getByRole('button'));

expect((screen.getByTestId('a') as HTMLInputElement).value).toEqual('');
expect((screen.getByTestId('b') as HTMLInputElement).value).toEqual('');
});
});
20 changes: 20 additions & 0 deletions src/__tests__/utils/isEmptyArray.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import isEmptyArray from '../../utils/isEmptyArray';

describe('isEmptyArray', () => {
it('should return true when value is an empty array', () => {
expect(isEmptyArray([])).toBeTruthy();
});

it('should return false when value is not an empty array', () => {
expect(isEmptyArray(null)).toBeFalsy();
expect(isEmptyArray(undefined)).toBeFalsy();
expect(isEmptyArray(-1)).toBeFalsy();
expect(isEmptyArray(0)).toBeFalsy();
expect(isEmptyArray(1)).toBeFalsy();
expect(isEmptyArray('')).toBeFalsy();
expect(isEmptyArray(() => null)).toBeFalsy();
expect(isEmptyArray({ foo: 'bar' })).toBeFalsy();
expect(isEmptyArray({})).toBeFalsy();
expect(isEmptyArray(['foo', 'bar'])).toBeFalsy();
});
});
6 changes: 5 additions & 1 deletion src/logic/createFormControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import get from '../utils/get';
import isBoolean from '../utils/isBoolean';
import isCheckBoxInput from '../utils/isCheckBoxInput';
import isDateObject from '../utils/isDateObject';
import isEmptyArray from '../utils/isEmptyArray';
import isEmptyObject from '../utils/isEmptyObject';
import isFileInput from '../utils/isFileInput';
import isFunction from '../utils/isFunction';
Expand Down Expand Up @@ -630,8 +631,11 @@ export function createFormControl<
const fieldName = `${name}.${fieldKey}`;
const field = get(_fields, fieldName);

const isEmptyNonPrimitive =
isEmptyObject(fieldValue) || isEmptyArray(fieldValue);

(_names.array.has(name) ||
!isPrimitive(fieldValue) ||
(!isPrimitive(fieldValue) && !isEmptyNonPrimitive) ||
(field && !field._f)) &&
!isDateObject(fieldValue)
? setValues(fieldName, fieldValue, options)
Expand Down
1 change: 1 addition & 0 deletions src/utils/isEmptyArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default (value: unknown) => Array.isArray(value) && value.length === 0;