import React, { useState, useEffect } from "react"; import * as _ from "lodash"; import classNames from "classnames"; import { StringUtility } from "../../utility"; export interface CheckboxProps { flatKey: string; name: string; label?: string; value: boolean; rawData: any; error: boolean; errorMessage?: string; onChange?: Function; updateData: Function; } export const Checkbox = (props: CheckboxProps) => { const [value, setValue] = useState(false); useEffect(() => { setValue(props.value); }, [props.value]); const label: string = props.label || StringUtility.camelCaseToNormal(props.name); const classes: string = classNames("rlf-checkbox rlf-input", { error: props.error }); const updateData = (value: boolean): void => { _.set(props.rawData, props.flatKey, value); props.updateData(props.rawData); setValue(value); if (props.onChange) { props.onChange(value); } }; const getError = (): JSX.Element | null => { if (props.error && props.errorMessage) { return

{props.errorMessage}

; } return null; }; return (
updateData(e.target.checked)} />

{label}

{getError()}
); };