// @flow import React, { PureComponent } from 'react' import any from 'ramda/src/any' import append from 'ramda/src/append' import equals from 'ramda/src/equals' import map from 'ramda/src/map' import pathOr from 'ramda/src/pathOr' import prop from 'ramda/src/prop' import propOr from 'ramda/src/propOr' import reject from 'ramda/src/reject' import uniq from 'ramda/src/uniq' import Box from '../../elements/Box' import Checkbox from '../../elements/Checkbox' import ErrorMessage from '../../elements/ErrorMessage' import Grid from '../../elements/Grid' import Label from '../../elements/Label' import { isNotEmptyOrNotNil } from '../../utils' import { type EventType, type InputType, type MetaType } from '../../types' const checkboxGridModifierProps = (modifier, columns) => { switch (modifier) { case 'vertical': return null default: return { gridTemplateColumns: [ '1fr', `repeat(${columns != null ? columns : 4}, 1fr)`, `repeat(${columns != null ? columns : 4}, 1fr)`, ], gridTemplateRows: [ `repeat(${columns != null ? columns : 4}, 1fr)`, '1fr', '1fr', ], } } } type Props = { change?: (string | () => string, Array | () => Array) => void, columns?: number, input?: InputType, gridStyles?: {}, label: string, labelColor?: ?string, labelModifier?: ?string, labelTextStyle?: string, meta?: MetaType, modifier?: string, noLabel?: boolean, onChange?: ?(boolean, Array) => void, onChangeDouble?: ?(boolean, Array) => void, readOnly?: boolean, values: Array<{ label: string, value: string, id?: ?string, }>, } type State = { values: Array, } class CheckboxGroup extends PureComponent { static defaultProps = { change: undefined, columns: 4, gridStyles: undefined, input: undefined, labelColor: undefined, labelModifier: undefined, labelTextStyle: '', meta: undefined, modifier: 'horizontal', noLabel: false, onChange: undefined, onChangeDouble: undefined, readOnly: false, } // eslint-disable-next-line react/no-unused-state state = { values: [] } componentWillMount() { this.setState((state, props) => { const { change, input } = props const inputVals = pathOr([], ['input', 'value'], props) const uniqVals = uniq(inputVals) if (input && change && uniqVals !== inputVals) { change(input.name, uniqVals) } return { values: uniqVals, } }) } handleOnChange = (e: EventType) => { const { target } = e this.setState((state, props) => { let newValues = state.values const { change, input, onChange, onChangeDouble } = props if (target && target.checked && any(equals(target.value), newValues)) { newValues = state.values } else if (target && target.checked) { newValues = append(target.value, newValues) } else if (target && !target.checked) { newValues = reject(equals(target.value), newValues) } if (change && input) { change(input.name, newValues) } if (onChangeDouble != null) { onChangeDouble(target.checked, newValues) } else if (onChange) { onChange(target.checked, newValues) } return { values: uniq(newValues), } }) } render() { const { columns, input, label, labelColor, labelModifier, labelTextStyle, meta, modifier, noLabel, onChange, onChangeDouble, readOnly, values, gridStyles, ...styles } = this.props return ( { map(({ label: optionLabel, value: optionValue }) => ( ), values) } { prop('touched', meta) === true && isNotEmptyOrNotNil(prop('error', meta)) && ( ) } ) } } export default CheckboxGroup