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 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
5e4e11d
fix: check field value is not empty
sungh0lim Dec 24, 2023
e2f1065
feat: add test isEmptyArray
sungh0lim Dec 24, 2023
7f2fcf6
add lock file
sungh0lim Dec 24, 2023
2a75acb
Merge branch 'master' into trigger-validation-netsted-field
bluebill1049 Jan 13, 2024
f759d33
Merge branch 'master' into trigger-validation-netsted-field
bluebill1049 Jan 13, 2024
6aae0a3
Update setValue.test.tsx
bluebill1049 Jan 13, 2024
ce2dec8
fix lint
bluebill1049 Jan 13, 2024
ac2c2e2
Update empty array test
sungh0lim Feb 5, 2024
3f27155
Merge branch 'master' into trigger-validation-netsted-field
sungh0lim Feb 5, 2024
d4f5fba
Merge branch 'master' into trigger-validation-netsted-field
bluebill1049 Feb 16, 2024
44e2380
Merge branch 'master' into trigger-validation-netsted-field
sungh0lim Mar 2, 2024
f984cdb
Merge branch 'master' into trigger-validation-netsted-field
sungh0lim Mar 16, 2024
bfa430f
Merge branch 'master' into trigger-validation-netsted-field
sungh0lim Mar 22, 2024
0fd683b
Merge branch 'master' into trigger-validation-netsted-field
sungh0lim Apr 12, 2024
31d8feb
Merge branch 'master' into trigger-validation-netsted-field
sungh0lim Apr 16, 2024
24a7299
Merge branch 'master' into trigger-validation-netsted-field
sungh0lim May 13, 2024
45f287c
Merge branch 'master' into trigger-validation-netsted-field
sungh0lim May 29, 2024
2afdac6
Merge branch 'master' into trigger-validation-netsted-field
sungh0lim Jun 3, 2024
e60f895
Merge branch 'master' into trigger-validation-netsted-field
sungh0lim Jun 10, 2024
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 @@ -632,8 +633,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;