Skip to content

Commit

Permalink
feat: test
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyair committed Nov 30, 2023
1 parent 48e32e8 commit 5038f45
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/components/form/form-subscribe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Values = any> = (
changedValues: Record<string, any>,
Expand All @@ -19,7 +19,8 @@ export interface FormSubscribeProps {
export const FormSubscribe: FC<FormSubscribeProps> = 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(
Expand Down
17 changes: 17 additions & 0 deletions src/components/form/utils.ts
Original file line number Diff line number Diff line change
@@ -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<T>(candidate?: T | T[] | false): T[] {
if (candidate === undefined || candidate === false) return []

Expand All @@ -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
}

0 comments on commit 5038f45

Please sign in to comment.