Skip to content

Commit 92b6fdc

Browse files
committed
Unset parent field if the child form contains no fields
1 parent 5451e5d commit 92b6fdc

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

Todo.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
- Require index for array fields
77
- Add React.forwardRef to input elements
88
- Pass FormEvent through form.handleSubmit's handler
9+
- Rename `setDefaultValues` to `setAllValues`
10+
- Let `comparePrimitiveObject` compare deep objects too

src/form.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ export class FormState<T, State = DefaultState, Error extends string = DefaultEr
155155
notifyParent: boolean = true,
156156
fireAny: boolean = true
157157
) {
158+
if (key === "author") console.trace("setValueInternal", key, value, dirty);
159+
158160
let valueMap = isDefault ? this.defaultValues : this.values;
159161
if (value === undefined) {
160162
if (Array.isArray(valueMap)) valueMap.splice(key as number, 1);
@@ -240,7 +242,7 @@ export class FormState<T, State = DefaultState, Error extends string = DefaultEr
240242
* @param notifyChild Should this form notify the child form about this change?
241243
* @param notifyParent Should this form notify the parent form about this change?
242244
*/
243-
public setValues(values: T, validate?: boolean, isDefault: boolean = false, notifyChild: boolean = true, notifyParent: boolean = true) {
245+
public setValues(values: Partial<T>, validate?: boolean, isDefault: boolean = false, notifyChild: boolean = true, notifyParent: boolean = true) {
244246
// No set is used because this could cause problems with array keys, which must always be in the right order
245247
let keys = Object.keys(isDefault ? this.defaultValues : this.values);
246248
let newKeys = Object.keys(values);
@@ -274,7 +276,7 @@ export class FormState<T, State = DefaultState, Error extends string = DefaultEr
274276
* @param notifyChild Should this form notify the child form about this change?
275277
* @param notifyParent Should this form notify the parent form about this change?
276278
*/
277-
public setDefaultValues(values: T, validate: boolean = true, notifyChild: boolean = true, notifyParent: boolean = true) {
279+
public setDefaultValues(values: Partial<T>, validate: boolean = true, notifyChild: boolean = true, notifyParent: boolean = true) {
278280
this.setValues(values, false, true, notifyChild, notifyParent);
279281
this.setValues(values, validate, false, notifyChild, notifyParent);
280282
}
@@ -541,9 +543,10 @@ export class ChildFormState<Parent, Key extends keyof Parent, ParentState, Paren
541543
}
542544

543545
protected updateParentValues(isDefault: boolean, validate: boolean | undefined) {
546+
let values = isDefault ? memberCopy(this.defaultValues) : memberCopy(this.values);
544547
this.parent.setValueInternal(
545548
this.name,
546-
isDefault ? memberCopy(this.defaultValues) : memberCopy(this.values),
549+
Object.keys(values).length > 0 ? values : undefined,
547550
this.dirty,
548551
validate,
549552
isDefault,

src/hooks.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ export function useChildForm<T, Key extends keyof T, State = DefaultState, Error
5959
c.current!.name = name;
6060

6161
// First, set new default values, without validating
62-
c.current!.setValues(parentForm.defaultValues[name] ?? ({} as any), false, true, true, false);
62+
c.current!.setValues(parentForm.defaultValues[name] ?? {}, false, true, true, false);
6363
// Then, set new values and validate if needed
64-
c.current!.setValues(parentForm.values[name] ?? ({} as any), c.current!.validateOnMount, false, true, true);
64+
c.current!.setValues(parentForm.values[name] ?? {}, c.current!.validateOnMount, false, true, true);
6565

6666
return () => {
67-
// Only clear if is not already overwritten
67+
// Only delete if is not already overwritten by new form
6868
if (parentForm.childMap[name] === c.current!) {
6969
delete parentForm.childMap[name];
7070
}

0 commit comments

Comments
 (0)