import classNames from 'classnames' import * as React from 'react' import { types, Text, Plain } from 'react-bricks/frontend' import { FieldErrorsImpl, UseFormRegister } from 'react-hook-form' import blockNames from '../../blockNames' import { useAdminContext } from 'react-bricks/frontend' import { textColors } from '../../colors' export interface FormCheckboxProps { register: UseFormRegister fieldName: string label: types.TextValue isRequired: boolean errors: FieldErrorsImpl<{ [x: string]: any }> requiredError?: string columns: '1' | '2' } const FormCheckbox: types.Brick = ({ register, isRequired = true, fieldName, label, errors, requiredError, columns, }) => { const labelTextContent = typeof label === 'string' ? label : Plain.serialize(label) const { isAdmin } = useAdminContext() const registerAttributes = fieldName ? register( fieldName?.replace(/\s/g, '').toLowerCase(), //@ts-ignore { required: isRequired, } ) : {} return (
{errors[fieldName] && (
{errors[fieldName]?.type === 'required' && requiredError} {errors[fieldName]?.message && `This field ${errors[fieldName]?.message}`}
)}
) } FormCheckbox.schema = { name: blockNames.FormCheckbox, label: 'Checkbox', 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/FormCheckbox.tsx', getDefaultProps: () => ({ label: 'I accept the processing of my data', columns: '2', isRequired: true, fieldName: 'privacy', }), 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 FormCheckbox