import { FormsyInjectedProps, withFormsy } from 'formsy-react'; import React, { cloneElement, Component, createElement } from 'react'; import { Checkbox, Form, Radio, StrictFormFieldProps, StrictRadioProps, } from 'semantic-ui-react'; import { filterSuirElementProps } from './utils'; type CheckboxRadioValueType = boolean | number | string; export interface IFormsyCheckboxProps extends FormsyInjectedProps, Pick< StrictFormFieldProps, 'as' | 'className' | 'error' | 'width' | 'inline' | 'disabled' >, Omit { inputClassName?: string; passRequiredToField?: boolean; inputAs?: | typeof Form.Checkbox | typeof Form.Radio | typeof Checkbox | typeof Radio; defaultChecked?: boolean; errorLabel?: React.ReactElement; label?: string | React.ReactNode; onChange?( event: React.FormEvent, data: Omit & { value: CheckboxRadioValueType } ): void; } class FormsyCheckbox extends Component { componentDidMount() { const { defaultChecked, setValue } = this.props; setValue(!!defaultChecked, false); } handleChange = ( e: React.FormEvent, data: Omit & { value: CheckboxRadioValueType; checked: boolean; } ) => { const { checked } = data; this.props.setValue(checked); if (this.props.onChange) { this.props.onChange(e, data); } }; render() { const { inputAs = Checkbox, required, isValid, isPristine, errorLabel, errorMessage, value, // Form.Field props as, width, className, disabled, inline, passRequiredToField, } = this.props; const error = !isPristine && !isValid; const checkboxProps: Record = { ...filterSuirElementProps(this.props), label: this.props.label, checked: !!value, onChange: this.handleChange, }; if (inputAs === Checkbox || inputAs === Radio) { checkboxProps.error = undefined; } return ( {createElement(inputAs as any, { ...checkboxProps })} {error && errorLabel && cloneElement(errorLabel, {}, errorMessage)} ); } } export default withFormsy(FormsyCheckbox);