chore: clean up types for HiddenField and WatchCondition (#9208)

### What?
Aligns types for HiddenField and the WatchCondition component with the
rest of the fields. Since path is required when rendering a Field
component, there is no need to keep it optional in the WatchCondition
component.

### Why?
Hidden fields were requiring the `field` property to be passed, but the
only reason it needed it was to allow the path to fallback to name if
path was not passed. But path is required so there is no need for this
anymore.

This makes using the HiddenField simpler now.

### How?
Adjusts type on the HiddenField and the WatchCondition component.
This commit is contained in:
Jarrod Flesch
2024-11-14 12:56:17 -05:00
committed by GitHub
parent 5482e7ea15
commit e75527b0a1
7 changed files with 15 additions and 48 deletions

View File

@@ -79,18 +79,7 @@ export const ResetPasswordForm: React.FC<Args> = ({ token }) => {
schemaPath={`${userSlug}.password`} schemaPath={`${userSlug}.password`}
/> />
<ConfirmPasswordField /> <ConfirmPasswordField />
<HiddenField <HiddenField path="token" schemaPath={`${userSlug}.token`} value={token} />
field={{
name: 'token',
type: 'text',
admin: {
hidden: true,
},
}}
path="token"
schemaPath={`${userSlug}.token`}
value={token}
/>
</div> </div>
<FormSubmit size="large">{i18n.t('authentication:resetPassword')}</FormSubmit> <FormSubmit size="large">{i18n.t('authentication:resetPassword')}</FormSubmit>
</Form> </Form>

View File

@@ -1,13 +1,11 @@
import type { ClientField } from '../../fields/config/types.js'
import type { ClientFieldBase } from '../types.js' import type { ClientFieldBase } from '../types.js'
type HiddenFieldBaseClientProps = { type HiddenFieldBaseClientProps = {
readonly disableModifyingForm?: false readonly disableModifyingForm?: false
readonly field?: { readonly field?: never
readonly name?: string
} & ClientField
readonly path: string readonly path: string
readonly value?: unknown readonly value?: unknown
} }
export type HiddenFieldProps = ClientFieldBase & HiddenFieldBaseClientProps export type HiddenFieldProps = HiddenFieldBaseClientProps &
Pick<ClientFieldBase, 'forceRender' | 'schemaPath'>

View File

@@ -12,14 +12,7 @@ import { withCondition } from '../../forms/withCondition/index.js'
* For example, this sets the `ìd` property of a block in the Blocks field. * For example, this sets the `ìd` property of a block in the Blocks field.
*/ */
const HiddenFieldComponent: React.FC<HiddenFieldProps> = (props) => { const HiddenFieldComponent: React.FC<HiddenFieldProps> = (props) => {
const { const { disableModifyingForm = true, path, value: valueFromProps } = props
disableModifyingForm = true,
field: { name } = {},
path: pathFromProps,
value: valueFromProps,
} = props
const path = pathFromProps ?? (name || '')
const { setValue, value } = useField({ const { setValue, value } = useField({
path, path,

View File

@@ -5,6 +5,7 @@ import type {
GenericDescriptionProps, GenericDescriptionProps,
GenericErrorProps, GenericErrorProps,
GenericLabelProps, GenericLabelProps,
HiddenFieldProps,
} from 'payload' } from 'payload'
import type React from 'react' import type React from 'react'
@@ -43,9 +44,10 @@ import { UploadField } from './Upload/index.js'
export * from './shared/index.js' export * from './shared/index.js'
export type FieldTypesComponents = { export type FieldTypesComponents = {
[K in 'hidden' | 'password' | FieldTypes]: React.FC<ClientFieldBase> [K in 'password' | FieldTypes]: React.FC<ClientFieldBase>
} & { } & {
confirmPassword: React.FC<ConfirmPasswordFieldProps> confirmPassword: React.FC<ConfirmPasswordFieldProps>
hidden: React.FC<HiddenFieldProps>
} }
export const fieldComponents: FieldTypesComponents = { export const fieldComponents: FieldTypesComponents = {

View File

@@ -67,7 +67,7 @@ export function RenderField({
} }
if (clientFieldConfig.admin?.hidden) { if (clientFieldConfig.admin?.hidden) {
return <HiddenField {...baseFieldProps} field={clientFieldConfig} path={path} /> return <HiddenField {...baseFieldProps} path={path} />
} }
switch (clientFieldConfig.type) { switch (clientFieldConfig.type) {

View File

@@ -1,5 +1,4 @@
'use client' 'use client'
import type { FieldTypes } from 'payload'
import React, { Fragment } from 'react' import React, { Fragment } from 'react'
@@ -8,13 +7,9 @@ import { useFormFields } from '../Form/context.js'
export const WatchCondition: React.FC<{ export const WatchCondition: React.FC<{
children: React.ReactNode children: React.ReactNode
indexPath: string indexPath: string
name?: string path: string
path?: string
type: FieldTypes
}> = (props) => { }> = (props) => {
const { name, children, path: pathFromProps } = props const { children, path } = props
const path = typeof pathFromProps === 'string' ? pathFromProps : name
const field = useFormFields(([fields]) => (fields && fields?.[path]) || null) const field = useFormFields(([fields]) => (fields && fields?.[path]) || null)

View File

@@ -1,29 +1,19 @@
'use client' 'use client'
import type { ClientFieldProps, FieldPaths } from 'payload' import type { FieldPaths } from 'payload'
import type { MarkOptional } from 'ts-essentials' import type { MarkOptional } from 'ts-essentials'
import React from 'react' import React from 'react'
import { WatchCondition } from './WatchCondition.js' import { WatchCondition } from './WatchCondition.js'
export const withCondition = < export const withCondition = <P extends MarkOptional<FieldPaths, 'indexPath' | 'path'>>(
P extends MarkOptional<FieldPaths & Pick<ClientFieldProps, 'field'>, 'indexPath' | 'path'>,
>(
Field: React.ComponentType<P>, Field: React.ComponentType<P>,
): React.FC<P> => { ): React.FC<P> => {
const CheckForCondition: React.FC<P> = (props) => { const CheckForCondition: React.FC<P> = (props) => {
const { const { indexPath, path } = props
field: { type },
indexPath,
path: pathFromProps,
} = props
const name = 'name' in props.field ? props.field.name : undefined
const path = pathFromProps ?? name
return ( return (
<WatchCondition indexPath={indexPath} name={name} path={path} type={type}> <WatchCondition indexPath={indexPath} path={path}>
<Field {...props} /> <Field {...props} />
</WatchCondition> </WatchCondition>
) )