import * as React from 'react' import { types, Plain, Text } from 'react-bricks/frontend' import { FieldErrorsImpl, UseFormRegister } from 'react-hook-form' import classNames from 'classnames' import blockNames from '../../blockNames' import { useAdminContext } from 'react-bricks/frontend' import { textColors } from '../../colors' export interface FormSelectProps { register: UseFormRegister fieldName?: string label: types.TextValue options?: string isRequired: boolean errors: FieldErrorsImpl<{ [x: string]: any }> requiredError?: string columns: '1' | '2' } const FormSelect: types.Brick = ({ options, isRequired, register, fieldName = 'select', 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}`} )}
) } FormSelect.schema = { name: blockNames.FormSelect, label: 'Select', 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/FormSelect.tsx', getDefaultProps: () => ({ fieldName: 'select', label: 'Choose a fruit', columns: '2', options: 'orange: Orange\napple: Apple', isRequired: false, }), 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: 'options', label: 'Options', type: types.SideEditPropType.Textarea, helperText: 'Each line should have "value:label"', }, { name: 'isRequired', type: types.SideEditPropType.Boolean, label: 'Field required', }, { name: 'requiredError', type: types.SideEditPropType.Text, label: 'Required error message', }, ], } export default FormSelect