From 5038f4549857ca9500d67998c6dbf7b12124e7cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B6=E6=9E=AB?= <645381995@qq.com> Date: Thu, 30 Nov 2023 17:09:36 +0800 Subject: [PATCH] feat: test --- src/components/form/form-subscribe.tsx | 5 +++-- src/components/form/utils.ts | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/components/form/form-subscribe.tsx b/src/components/form/form-subscribe.tsx index b627f31e34..0ebf98ac1d 100644 --- a/src/components/form/form-subscribe.tsx +++ b/src/components/form/form-subscribe.tsx @@ -3,7 +3,7 @@ import type { FC, ReactNode } from 'react' import { FieldContext, useWatch } from 'rc-field-form' import type { FormInstance } from 'rc-field-form' import type { NamePath } from 'rc-field-form/es/interface' -import { cloneByNamePathList } from 'rc-field-form/es/utils/valueUtil' +import { cloneByNamePathList } from './utils' type RenderChildren = ( changedValues: Record, @@ -19,7 +19,8 @@ export interface FormSubscribeProps { export const FormSubscribe: FC = props => { const form = useContext(FieldContext) - const value = useWatch(values => cloneByNamePathList(values, props.to), form) + useWatch(values => cloneByNamePathList(values, props.to), form) + const value = form.getFieldsValue(props.to) // Memo to avoid useless render const childNode = React.useMemo( diff --git a/src/components/form/utils.ts b/src/components/form/utils.ts index bcfa86d1b7..c5fa16d2b3 100644 --- a/src/components/form/utils.ts +++ b/src/components/form/utils.ts @@ -1,4 +1,8 @@ import { isMemo, isFragment } from 'react-is' +import { NamePath, Store } from 'rc-field-form/es/interface' +import getValue from 'rc-util/lib/utils/get' +import setValue from 'rc-util/lib/utils/set' + export function toArray(candidate?: T | T[] | false): T[] { if (candidate === undefined || candidate === false) return [] @@ -25,3 +29,16 @@ export function isSafeSetRefComponent(component: any): boolean { return !isSimpleFunctionComponent(component.type) } + +export function cloneByNamePathList( + store: Store, + namePathList: NamePath[] +): Store { + let newStore = {} + namePathList.forEach(namePath => { + const value = getValue(store, toArray(namePath)) + newStore = setValue(newStore, toArray(namePath), value) + }) + + return newStore +}