/* eslint-disable react/destructuring-assignment */ /* eslint-disable react-hooks/exhaustive-deps */ 'use client' import type { 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 { useField } from '../../forms/useField/index.js' import { withCondition } from '../../forms/withCondition/index.js' import { fieldBaseClass } from '../shared/index.js' import { Radio } from './Radio/index.js' import './index.scss' const baseClass = 'radio-group' import { FieldDescription } from '@payloadcms/ui/forms/FieldDescription' import { FieldError } from '@payloadcms/ui/forms/FieldError' import { useFieldProps } from '@payloadcms/ui/forms/FieldPropsProvider' import type { FormFieldBase } from '../shared/index.js' export type RadioFieldProps = FormFieldBase & { layout?: 'horizontal' | 'vertical' name?: string onChange?: OnChange options?: Option[] path?: string value?: string width?: string } export type OnChange = (value: T) => void const RadioGroupField: React.FC = (props) => { const { name, CustomDescription, CustomError, CustomLabel, className, descriptionProps, errorProps, label, labelProps, layout = 'horizontal', onChange: onChangeFromProps, options = [], path: pathFromProps, readOnly: readOnlyFromProps, required, style, validate, value: valueFromProps, width, } = props const { uuid } = useForm() const memoizedValidate = useCallback( (value, validationOptions) => { if (typeof validate === 'function') return validate(value, { ...validationOptions, options, required }) }, [validate, options, required], ) const { path: pathFromContext, readOnly: readOnlyFromContext } = useFieldProps() const readOnly = readOnlyFromProps || readOnlyFromContext const { path, setValue, showError, value: valueFromContext, } = useField({ path: pathFromContext || pathFromProps || name, validate: memoizedValidate, }) const value = valueFromContext || valueFromProps return (
    {options.map((option) => { let optionValue = '' if (optionIsObject(option)) { optionValue = option.value } else { optionValue = option } const isSelected = String(optionValue) === String(value) const id = `field-${path}-${optionValue}${uuid ? `-${uuid}` : ''}` return (
  • { if (typeof onChangeFromProps === 'function') { onChangeFromProps(optionValue) } if (!readOnly) { setValue(optionValue) } }} option={optionIsObject(option) ? option : { label: option, value: option }} path={path} uuid={uuid} />
  • ) })}
{CustomDescription !== undefined ? ( CustomDescription ) : ( )}
) } export const RadioGroup = withCondition(RadioGroupField)