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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form.useWatch support selector #6461

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"classnames": "^2.3.2",
"dayjs": "^1.11.7",
"lodash": "^4.17.21",
"rc-field-form": "~1.27.4",
"rc-field-form": "~1.41.0",
"rc-util": "^5.30.0",
"react-is": "^18.2.0",
"runes2": "^1.1.2",
Expand Down
16 changes: 8 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 4 additions & 29 deletions src/components/form/form-subscribe.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React, { memo, useContext } from 'react'
import React, { useContext } from 'react'
import type { FC, ReactNode } from 'react'
import { FieldContext, useWatch } from 'rc-field-form'
import { useUpdate } from 'ahooks'
import type { FormInstance } from 'rc-field-form'
import type { NamePath } from 'rc-field-form/es/interface'
import { useIsomorphicUpdateLayoutEffect } from '../../utils/use-isomorphic-update-layout-effect'
import { cloneByNamePathList } from './utils'

type RenderChildren<Values = any> = (
changedValues: Record<string, any>,
Expand All @@ -18,9 +17,9 @@ export interface FormSubscribeProps {
}

export const FormSubscribe: FC<FormSubscribeProps> = props => {
const update = useUpdate()
const form = useContext(FieldContext)

useWatch(values => cloneByNamePathList(values, props.to), form)
const value = form.getFieldsValue(props.to)

// Memo to avoid useless render
Expand All @@ -29,29 +28,5 @@ export const FormSubscribe: FC<FormSubscribeProps> = props => {
[JSON.stringify(value), props.children]
)

return (
<>
{childNode}
{props.to.map(namePath => (
<Watcher
key={namePath.toString()}
form={form}
namePath={namePath}
onChange={update}
/>
))}
</>
)
return childNode as React.ReactElement
}

export const Watcher = memo<{
form: FormInstance
namePath: NamePath
onChange: () => void
}>(props => {
const value = useWatch(props.namePath, props.form)
useIsomorphicUpdateLayoutEffect(() => {
props.onChange()
}, [value])
return null
})
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 const cloneByNamePathList = (
store: Store,
namePathList: NamePath[]
): Store => {
return namePathList.reduce((accumulator, currentValue) => {
return setValue(
accumulator,
toArray(currentValue),
getValue(store, toArray(currentValue))
)
}, {})
}
Loading