From 2e5b3aa12d549798744cf5e60b31a8a12a82dc31 Mon Sep 17 00:00:00 2001 From: Boyuan Gao Date: Tue, 5 Apr 2022 11:40:30 +0800 Subject: [PATCH] =?UTF-8?q?feature:=20to=20#401=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E4=B8=8B=E7=9B=B4=E6=8E=A5=E8=AE=BE=E7=BD=AEnamePath=E5=92=8Cv?= =?UTF-8?q?alue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/useForm.ts | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/useForm.ts b/src/useForm.ts index 025a62b8e..6b76bf682 100644 --- a/src/useForm.ts +++ b/src/useForm.ts @@ -1,5 +1,6 @@ import * as React from 'react'; import warning from 'rc-util/lib/warning'; +import set from 'rc-util/lib/utils/set'; import type { Callbacks, FieldData, @@ -694,14 +695,32 @@ export class FormStore { this.triggerOnFieldsChange([namePath, ...childrenFields]); }; + private isNotTrivalStore(store: Store | (() => [NamePath, Store[keyof Store]][])): + store is (() => [NamePath, Store[keyof Store]][]) { + return typeof store === 'function'; + } + // Let all child Field get update. - private setFieldsValue = (store: Store) => { + private setFieldsValue = (store: Store | (() => [NamePath, Store[keyof Store]][])) => { this.warningUnhooked(); const prevStore = this.store; - if (store) { - this.updateStore(setValues(this.store, store)); + let newStore: Store; + if(this.isNotTrivalStore(store)) { + const valuePairs = store(); + newStore = valuePairs.reduce((prev: Store, namePathValuePair: [NamePath, Store[keyof Store]]) => { + if(!namePathValuePair) { + return prev; + } + set(prev, namePathValuePair[0], namePathValuePair[1]); + return prev; + }, {} as Store); + } else { + newStore = store; + } + if (newStore) { + this.updateStore(setValues(this.store, newStore)); } this.notifyObservers(prevStore, null, {