chore(ui): client-side renders all default field labels, descriptions, and errors

This commit is contained in:
Jacob Fletcher
2024-03-20 15:01:46 -04:00
parent 9f690c1f5d
commit b3d28bac6a
48 changed files with 512 additions and 322 deletions

View File

@@ -6,8 +6,8 @@ import type { FieldBase, Option } from 'payload/types'
import { optionIsObject } from 'payload/types'
import React, { useCallback } from 'react'
import { FieldLabel } from '../../forms/FieldLabel/index.js'
import { useForm } from '../../forms/Form/context.js'
import { Label as LabelComp } from '../../forms/Label/index.js'
import { useField } from '../../forms/useField/index.js'
import { withCondition } from '../../forms/withCondition/index.js'
import { fieldBaseClass } from '../shared/index.js'
@@ -16,6 +16,9 @@ import './index.scss'
const baseClass = 'radio-group'
import { FieldDescription } from '@payloadcms/ui/forms/FieldDescription'
import { FieldError } from '@payloadcms/ui/forms/FieldError'
import type { FormFieldBase } from '../shared/index.js'
export type RadioFieldProps = FormFieldBase & {
@@ -34,11 +37,13 @@ export type OnChange<T = string> = (value: T) => void
const RadioGroupField: React.FC<RadioFieldProps> = (props) => {
const {
name,
Description,
Error,
Label: LabelFromProps,
CustomDescription,
CustomError,
CustomLabel,
className,
label,
descriptionProps,
errorProps,
labelProps,
layout = 'horizontal',
onChange: onChangeFromProps,
options = [],
@@ -53,8 +58,6 @@ const RadioGroupField: React.FC<RadioFieldProps> = (props) => {
const { uuid } = useForm()
const Label = LabelFromProps || <LabelComp label={label} required={required} />
const memoizedValidate = useCallback(
(value, validationOptions) => {
if (typeof validate === 'function')
@@ -92,8 +95,10 @@ const RadioGroupField: React.FC<RadioFieldProps> = (props) => {
width,
}}
>
<div className={`${baseClass}__error-wrap`}>{Error}</div>
{Label}
<div className={`${baseClass}__error-wrap`}>
{CustomError !== undefined ? CustomError : <FieldError {...(errorProps || {})} />}
</div>
{CustomLabel !== undefined ? CustomLabel : <FieldLabel {...(labelProps || {})} />}
<ul className={`${baseClass}--group`} id={`field-${path.replace(/\./g, '__')}`}>
{options.map((option) => {
let optionValue = ''
@@ -130,7 +135,11 @@ const RadioGroupField: React.FC<RadioFieldProps> = (props) => {
)
})}
</ul>
{Description}
{CustomDescription !== undefined ? (
CustomDescription
) : (
<FieldDescription {...(descriptionProps || {})} />
)}
</div>
)
}