Skip to content

Commit

Permalink
🕵️‍♀️ improve logic on register absent inputs (#5556)
Browse files Browse the repository at this point in the history
* improve logic on register absent inputs

* rename variable

* remove any and update with test
  • Loading branch information
bluebill1049 committed Jun 10, 2021
1 parent 6290d87 commit ae8db23
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
6 changes: 5 additions & 1 deletion src/__tests__/useForm/getValues.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ describe('getValues', () => {
test: {
firstName: 'test',
lastName: 'test',
time: new Date('1999-09-09'),
file: new File([''], 'filename'),
},
},
});
Expand Down Expand Up @@ -228,6 +230,8 @@ describe('getValues', () => {
test: {
firstName: 'test',
lastName: 'test',
time: new Date('1999-09-09'),
file: new File([''], 'filename'),
},
});

Expand All @@ -241,7 +245,7 @@ describe('getValues', () => {
fireEvent.click(screen.getByRole('button'));
});

expect(data).toEqual({
expect(data).toMatchObject({
test: {
firstName: '1234',
lastName: 'test',
Expand Down
25 changes: 25 additions & 0 deletions src/__tests__/useForm/setValue.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
fireEvent,
render,
screen,
waitFor,
} from '@testing-library/react';
import { act, renderHook } from '@testing-library/react-hooks';

Expand Down Expand Up @@ -1052,4 +1053,28 @@ describe('setValue', () => {
});
});
});

it('should only be able to update value of array of inputs which is not registered', async () => {
const App = () => {
const { setValue, watch } = useForm({
defaultValues: {
test: ['1', '2', '3'],
},
});

React.useEffect(() => {
setValue('test', ['2', '2']);
}, [setValue]);

const result = watch('test');

return <p>{JSON.stringify(result)}</p>;
};

render(<App />);

await waitFor(async () => {
screen.getByText('["2","2","3"]');
});
});
});
30 changes: 11 additions & 19 deletions src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1029,27 +1029,19 @@ export function useForm<
);

const registerAbsentFields = <T extends DefaultValues<TFieldValues>>(
value: T,
defaultValues: T,
name = '',
): void => {
const field = get(fieldsRef.current, name);

if (!field || (field && !field._f)) {
if (
!field &&
(isPrimitive(value) ||
(isWeb && (value instanceof FileList || value instanceof Date)))
) {
register(name as Path<TFieldValues>, { value } as RegisterOptions);
}

if (Array.isArray(value) || isObject(value)) {
if (name && !get(fieldsRef.current, name)) {
set(fieldsRef.current, name, Array.isArray(value) ? [] : {});
}

for (const key in value) {
registerAbsentFields(value[key], name + (name ? '.' : '') + key);
for (const key in defaultValues) {
const value = defaultValues[key];
const fieldName = name + (name ? '.' : '') + key;
const field = get(fieldsRef.current, fieldName);

if (!field || !field._f) {
if (isObject(value) || Array.isArray(value)) {
registerAbsentFields(value, fieldName);
} else if (!field) {
register(fieldName as Path<TFieldValues>, { value });
}
}
}
Expand Down

0 comments on commit ae8db23

Please sign in to comment.