import classNames from 'classnames' import * as React from 'react' import { Repeater, types, Plain, Text } from 'react-bricks/frontend' import { FieldErrorsImpl, UseFormRegister } from 'react-hook-form' import { textColors } from '../../colors' import blockNames from '../../blockNames' export interface FormRadiobuttonsProps { register?: UseFormRegister fieldName: string label: types.TextValue isRequired: boolean requiredError?: string columns: '1' | '2' errors: FieldErrorsImpl<{ [x: string]: any }> radioButtons: types.RepeaterItems } const FormRadiobuttons: types.Brick = ({ register, fieldName, label, isRequired, requiredError, columns, errors, radioButtons, }) => { const labelTextContent = typeof label === 'string' ? label : Plain.serialize(label) return (
( {props.children} )} /> {isRequired && (labelTextContent === '' ? null : ( * ))}
{errors[fieldName] && (
{errors[fieldName]?.type === 'required' && requiredError} {errors[fieldName]?.message && `This field ${errors[fieldName]?.message}`}
)}
) } FormRadiobuttons.schema = { name: blockNames.FormRadiobuttons, label: 'Radio buttons', category: 'contact', hideFromAddMenu: true, playgroundLinkLabel: 'View source code on Github', playgroundLinkUrl: 'https://github.com/ReactBricks/reactbricks-starters/blob/main/packages/reactbricks-ui/nextjs-pages/src/contacts/FormBuilder/FormRadiobuttons.tsx', // Defaults when a new brick is added getDefaultProps: () => ({ columns: '2', fieldName: 'color', label: 'Choose a color', radiobuttons: [ { label: 'Blue', value: 'blue', }, { label: 'Green', value: 'green', }, ], isRequired: false, }), repeaterItems: [ { name: 'radiobuttons', itemType: blockNames.FormSingleRadio, min: 1, }, ], // Sidebar Edit controls for props sideEditProps: [ { name: 'columns', label: 'Columns', type: types.SideEditPropType.Select, selectOptions: { display: types.OptionsDisplay.Radio, options: [ { value: '1', label: 'One' }, { value: '2', label: 'Two' }, ], }, }, { name: 'fieldName', type: types.SideEditPropType.Text, label: 'Field Name', }, { name: 'isRequired', type: types.SideEditPropType.Boolean, label: 'Field required', }, { name: 'requiredError', type: types.SideEditPropType.Text, label: 'Required error message', }, ], } export default FormRadiobuttons