// @flow import React, { PureComponent, type Node } from 'react' import { any, curry, filter, prop, propEq, map } from 'ramda' import CheckboxGroup from '../CheckboxGroup' import Box from '../../elements/Box' import { type InputType, type MetaType } from '../../types' const eqFunc = curry((vals, obj) => any(v => propEq('showSupplementalValue', v)(obj), vals)) type Props = { change?: (string | () => string, Array | () => Array) => void, direction?: string, gridStyles?: {}, input?: InputType, label: string, labelModifier?: ?string, meta?: MetaType, modifier?: string, noLabel?: boolean, readOnly?: boolean, supplementaryFields: Array<{ showSupplementalValue: string, render: () => Node, }>, values: Array<{ label: string, value: string, id?: ?string, }>, } type State = { showingSupplementals: [], } class CheckboxGroupWithNSupFields extends PureComponent { static defaultProps = { change: undefined, gridStyles: undefined, labelModifier: undefined, noLabel: false, direction: 'horizontal', readOnly: false, input: { name: null }, meta: { touched: false, error: '' }, modifier: 'horizontal', } state = { showingSupplementals: [], } componentWillMount() { const { supplementaryFields, input } = this.props const values = prop('value', input) let selectedValues if (values != null) { selectedValues = filter(eqFunc(values), supplementaryFields) } if (selectedValues != null) { this.setState(() => ({ showingSupplementals: selectedValues, })) } } handleShowSupplemental = (isChecked: boolean, values: Array) => { const { supplementaryFields } = this.props const selectedValues = filter(eqFunc(values), supplementaryFields) this.setState(() => ({ showingSupplementals: selectedValues, })) } render() { const { change, direction, gridStyles, label, labelModifier, input, meta, modifier, noLabel, readOnly, supplementaryFields, values, ...styles } = this.props const { showingSupplementals } = this.state return ( { map(obj => ( { obj.render() } ), showingSupplementals) } ) } } export default CheckboxGroupWithNSupFields