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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃挭 improve watch() with useFieldArray #1339

Merged
merged 4 commits into from Apr 4, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/src/app.tsx
Expand Up @@ -80,7 +80,7 @@ const App: React.FC = () => {
exact
component={WatchDefaultValues}
/>
<Route path="/watch-field-array" component={WatchFieldArray} />
<Route path="/watch-field-array/:mode" component={WatchFieldArray} />
<Route
path="/customSchemaValidation/:mode"
exact
Expand Down
12 changes: 11 additions & 1 deletion cypress/integration/watchUseFieldArray.ts
@@ -1,6 +1,6 @@
context('watchUseFieldArray', () => {
it('should behaviour correctly when watching the field array', () => {
cy.visit('http://localhost:3000/watch-field-array');
cy.visit('http://localhost:3000/watch-field-array/normal');

cy.get('#append').click();
cy.get('#result').contains('[{"name":"2"}]');
Expand Down Expand Up @@ -41,4 +41,14 @@ context('watchUseFieldArray', () => {
cy.get('#removeAll').click();
cy.get('#result').should('be.empty');
});

it('should return empty when items been removed and defaultValues are supplied', () => {
cy.visit('http://localhost:3000/watch-field-array/default');

cy.get('#delete0').click();
cy.get('#delete0').click();
cy.get('#delete0').click();

cy.get('#result').should('be.empty');
});
});
4 changes: 2 additions & 2 deletions src/useFieldArray.ts
Expand Up @@ -90,7 +90,7 @@ export const useFieldArray = <
if (readFormStateRef.current.dirty) {
isDirtyRef.current = isUndefined(flagOrFields)
? true
: getIsFieldsDifferent(flagOrFields, memoizedDefaultValues.current);
: getIsFieldsDifferent(flagOrFields, defaultValuesRef.current[name]);
}

for (const key in fieldsRef.current) {
Expand Down Expand Up @@ -183,7 +183,7 @@ export const useFieldArray = <
}

if (readFormStateRef.current.dirty) {
dirtyFieldsRef.current.forEach((dirtyField) => {
dirtyFieldsRef.current.forEach(dirtyField => {
if (isUndefined(name) || dirtyField.startsWith(`${name}[${index}]`)) {
dirtyFieldsRef.current.delete(dirtyField);
}
Expand Down
28 changes: 16 additions & 12 deletions src/useForm.ts
Expand Up @@ -213,7 +213,7 @@ export function useForm<
}
} else if (isMultipleSelect(ref)) {
[...ref.options].forEach(
(selectRef) =>
selectRef =>
(selectRef.selected = (value as string).includes(selectRef.value)),
);
} else if (isCheckBoxInput(ref) && options) {
Expand Down Expand Up @@ -375,7 +375,7 @@ export function useForm<
isValidRef.current = isEmptyObject(errors);

if (isArray(payload)) {
payload.forEach((name) => {
payload.forEach(name => {
const error = get(errors, name);

if (error) {
Expand Down Expand Up @@ -418,7 +418,7 @@ export function useForm<

if (isArray(fields)) {
const result = await Promise.all(
fields.map(async (data) => await executeValidation(data, true)),
fields.map(async data => await executeValidation(data, true)),
);
reRender();
return result.every(Boolean);
Expand Down Expand Up @@ -620,7 +620,7 @@ export function useForm<
fieldsWithValidationRef,
validFieldsRef,
watchFieldsRef,
].forEach((data) => data.current.delete(name));
].forEach(data => data.current.delete(name));

if (
readFormStateRef.current.isValid ||
Expand Down Expand Up @@ -716,7 +716,7 @@ export function useForm<
}),
});
} else if (isArray(name)) {
name.forEach((error) =>
name.forEach(error =>
setInternalError({ ...error, preventRender: true }),
);
reRender();
Expand All @@ -740,7 +740,9 @@ export function useForm<
| { nest: boolean },
defaultValue?: string | DeepPartial<FormValues>,
): FieldValue<FormValues> | DeepPartial<FormValues> | string | undefined {
const combinedDefaultValues = isUndefined(defaultValue)
const combinedDefaultValues = isDirtyRef.current
? {}
: isUndefined(defaultValue)
? isUndefined(defaultValuesRef.current)
? {}
: defaultValuesRef.current
Expand All @@ -751,6 +753,10 @@ export function useForm<
);
const watchFields = watchFieldsRef.current;

if (!isEmptyObject(combinedDefaultValues)) {
readFormStateRef.current.dirty = true;
}

if (isString(fieldNames)) {
return assignWatchFields<FormValues>(
fieldValues,
Expand Down Expand Up @@ -778,9 +784,7 @@ export function useForm<
isWatchAllRef.current = true;

const result =
(!isEmptyObject(fieldValues) && fieldValues) ||
defaultValue ||
defaultValuesRef.current;
(!isEmptyObject(fieldValues) && fieldValues) || combinedDefaultValues;

return fieldNames && fieldNames.nest
? transformToNestObject(result as FieldValues)
Expand All @@ -793,7 +797,7 @@ export function useForm<
names: FieldName<FormValues> | FieldName<FormValues>[],
): void {
if (!isEmptyObject(fieldsRef.current)) {
(isArray(names) ? names : [names]).forEach((fieldName) =>
(isArray(names) ? names : [names]).forEach(fieldName =>
removeFieldEventListenerAndRef(fieldsRef.current[fieldName], true),
);
}
Expand Down Expand Up @@ -887,7 +891,7 @@ export function useForm<

if (!isOnSubmit && readFormStateRef.current.isValid) {
validateField(fieldsRef, validateAllFieldCriteria, currentField).then(
(error) => {
error => {
const previousFormIsValid = isValidRef.current;
if (isEmptyObject(error)) {
validFieldsRef.current.add(name);
Expand Down Expand Up @@ -1122,7 +1126,7 @@ export function useForm<
}

Object.values(resetFieldArrayFunctionRef.current).forEach(
(resetFieldArray) => isFunction(resetFieldArray) && resetFieldArray(),
resetFieldArray => isFunction(resetFieldArray) && resetFieldArray(),
);

resetRefs(omitResetState);
Expand Down