diff --git a/components/Checkbox/Checkbox.jsx b/components/Checkbox/Checkbox.jsx index 74bc54d79..b2c79ee2c 100644 --- a/components/Checkbox/Checkbox.jsx +++ b/components/Checkbox/Checkbox.jsx @@ -115,7 +115,7 @@ const Checkbox = ({ label, error, id, ...rest }) => ( {label} - {error && {error}} + {error && typeof error === 'string' && {error}} ); @@ -125,14 +125,16 @@ Checkbox.defaultProps = { error: '', id: '', label: '', + value: '', }; Checkbox.propTypes = { checked: PropTypes.bool, disabled: PropTypes.bool, - error: PropTypes.string, + error: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]), id: PropTypes.string, label: PropTypes.string, + value: PropTypes.string, }; export default Checkbox; diff --git a/components/Checkbox/CheckboxGroup.jsx b/components/Checkbox/CheckboxGroup.jsx new file mode 100644 index 000000000..796857e09 --- /dev/null +++ b/components/Checkbox/CheckboxGroup.jsx @@ -0,0 +1,86 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import styled from 'styled-components'; +import { FieldGroup, ErrorMessage } from '../shared'; + +import Checkbox from './Checkbox'; + +const Group = styled(FieldGroup)` + position: relative; +`; + +const ErrorLabel = styled(ErrorMessage)` + margin-left: -3px; +`; + +ErrorLabel.displayName = 'ErrorLabel'; + +const CheckboxGroup = ({ + children, + error, + name, + onChange, + options, + value, + ...rest +}) => { + const _onChange = () => { + const checkedValues = React.Children.map(children, child => child.value); + + console.log(checkedValues); + }; + + const commonProps = { name, error: Boolean(error), onChange: _onChange }; + const checkboxOptions = options.map(option => + Object.assign({}, option, { + key: option.value, + ...commonProps, + }), + ); + + const items = + React.Children.map(children, child => + React.cloneElement(child, { ...commonProps }), + ) || checkboxOptions.map(props => ); + + return ( + + {items} + {error && {error}} + + ); +}; + +CheckboxGroup.Checkbox = Checkbox; + +/** + * Group for Radio components. + */ +CheckboxGroup.defaultProps = { + children: undefined, + error: undefined, + onChange: () => {}, + options: [], + value: undefined, +}; + +CheckboxGroup.propTypes = { + options: PropTypes.arrayOf( + PropTypes.shape({ + label: PropTypes.oneOfType([PropTypes.string, PropTypes.node]), + value: PropTypes.string.isRequired, + disabled: PropTypes.bool, + }), + ), + children: PropTypes.oneOfType([ + PropTypes.arrayOf(PropTypes.element), + PropTypes.element, + ]), + onChange: PropTypes.func, + /** Initialize CheckboxGroup with a value */ + value: PropTypes.string, + name: PropTypes.string.isRequired, + error: PropTypes.string, +}; + +export default CheckboxGroup; diff --git a/components/Checkbox/__snapshots__/Checkbox.unit.test.jsx.snap b/components/Checkbox/__snapshots__/Checkbox.unit.test.jsx.snap index 3814605cb..291e845f8 100644 --- a/components/Checkbox/__snapshots__/Checkbox.unit.test.jsx.snap +++ b/components/Checkbox/__snapshots__/Checkbox.unit.test.jsx.snap @@ -97,6 +97,7 @@ exports[` should match the snapshot 1`] = ` disabled={false} id="" type="checkbox" + value="" />