Skip to content

Commit

Permalink
feat(CHeckbox Group): Checkbox Group
Browse files Browse the repository at this point in the history
  • Loading branch information
ddsilva committed Apr 4, 2019
1 parent 9a80c63 commit d799516
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 69 deletions.
6 changes: 4 additions & 2 deletions components/Checkbox/Checkbox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const Checkbox = ({ label, error, id, ...rest }) => (
<CheckIcon />
<CheckboxLabel htmlFor={id}>{label}</CheckboxLabel>
</CheckboxWrapper>
{error && <ErrorMessage>{error}</ErrorMessage>}
{error && typeof error === 'string' && <ErrorMessage>{error}</ErrorMessage>}
</Wrapper>
);

Expand All @@ -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;
86 changes: 86 additions & 0 deletions components/Checkbox/CheckboxGroup.jsx
Original file line number Diff line number Diff line change
@@ -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 => <Checkbox {...props} />);

return (
<Group {...rest}>
{items}
{error && <ErrorLabel>{error}</ErrorLabel>}
</Group>
);
};

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;
7 changes: 7 additions & 0 deletions components/Checkbox/__snapshots__/Checkbox.unit.test.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ exports[`<Checkbox /> should match the snapshot 1`] = `
disabled={false}
id=""
type="checkbox"
value=""
/>
<span
aria-hidden="true"
Expand Down Expand Up @@ -294,6 +295,7 @@ exports[`<Checkbox /> should match the snapshot 2`] = `
disabled={false}
id=""
type="checkbox"
value=""
/>
<span
aria-hidden="true"
Expand Down Expand Up @@ -496,6 +498,7 @@ exports[`<Checkbox /> should match the snapshot 3`] = `
disabled={false}
id=""
type="checkbox"
value=""
/>
<span
aria-hidden="true"
Expand Down Expand Up @@ -690,6 +693,7 @@ exports[`<Checkbox /> should match the snapshot 4`] = `
disabled={false}
id=""
type="checkbox"
value=""
/>
<span
aria-hidden="true"
Expand Down Expand Up @@ -879,6 +883,7 @@ exports[`<Checkbox /> should match the snapshot 5`] = `
disabled={true}
id=""
type="checkbox"
value=""
/>
<span
aria-hidden="true"
Expand Down Expand Up @@ -1068,6 +1073,7 @@ exports[`<Checkbox /> should match the snapshot 6`] = `
disabled={false}
id=""
type="checkbox"
value=""
/>
<span
aria-hidden="true"
Expand Down Expand Up @@ -1258,6 +1264,7 @@ exports[`<Checkbox /> should match the snapshot 7`] = `
disabled={true}
id=""
type="checkbox"
value=""
/>
<span
aria-hidden="true"
Expand Down
83 changes: 18 additions & 65 deletions stories/Checkbox/Checkbox.story.jsx
Original file line number Diff line number Diff line change
@@ -1,79 +1,32 @@
import React from 'react';
import { storiesOf } from '@storybook/react';
import {
TabbedView,
Heading,
Tab,
Title,
SimpleHighlight,
AutoExample,
StoryContainer,
ComponentPanel,
AutoPropsApi,
} from '@catho/quantum-storybook-ui';
import Checkbox from '../../components/Checkbox';
import FieldGroup from '../../components/shared/FieldGroup';

const exampleUsage = `<Checkbox label="Default" checked={false} onChange={() => {}} />
<Checkbox label="Checked" checked onChange={() => {}} />
<Checkbox label="Disabled" disabled checked={false} onChange={() => {}} />
<Checkbox label="Disabled & Checked" disabled checked onChange={() => {}} />
<Checkbox label="With error" error="Message" checked={false} onChange={() => {}} />
<Checkbox label="With error & Checked" error="Message" checked onChange={() => {}} />`;

const exampleTab = (
<Tab title="Example">
<StoryContainer>
<Title as="h2">Examples</Title>
<FieldGroup>
<Checkbox label="Default" checked={false} onChange={() => {}} />
</FieldGroup>
<FieldGroup>
<Checkbox label="Checked" checked onChange={() => {}} />
</FieldGroup>
<FieldGroup>
<Checkbox
label="Disabled"
disabled
checked={false}
onChange={() => {}}
/>
</FieldGroup>
<FieldGroup>
<Checkbox
label="Disabled & Checked"
disabled
checked
onChange={() => {}}
/>
</FieldGroup>
<FieldGroup>
<Checkbox
label="With error"
error="Message"
checked={false}
onChange={() => {}}
/>
</FieldGroup>
<FieldGroup>
<Checkbox
label="With error & Checked"
error="Message"
checked
onChange={() => {}}
/>
</FieldGroup>
<Title as="h2">Usage</Title>
<SimpleHighlight>{exampleUsage}</SimpleHighlight>
</StoryContainer>
</Tab>
);
import checkboxGroup from './sub-components/checkboxGroup';

const description = `Checkboxes are used when there is a list of options and
the user may select multiple options, including all or none.`;

storiesOf('Forms', module).add('Checkbox', () => (
<>
<AutoExample
description={description}
component={Checkbox}
additionalTabs={exampleTab}
/>
<Heading name="Checkbox">{description}</Heading>

<TabbedView>
<Tab title="Usage">
<ComponentPanel component={<Checkbox />} importModules="Checkbox" />
</Tab>

<Tab title="API">
<AutoPropsApi component={Checkbox} />
</Tab>

<Tab title="Checkbox group">{checkboxGroup}</Tab>
</TabbedView>
</>
));
23 changes: 23 additions & 0 deletions stories/Checkbox/sub-components/checkboxGroup.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react';
import { Title, StoryContainer } from '@catho/quantum-storybook-ui';
import CheckboxGroup from '../../../components/Checkbox/CheckboxGroup';

export default (
<StoryContainer>
<Title as="h2">Checkbox Group</Title>

<CheckboxGroup name="group-test" error="Select one">
<CheckboxGroup.Checkbox label="Option 1" value="Option 1" />
<CheckboxGroup.Checkbox label="Option 2" value="Option 2" checked />
<CheckboxGroup.Checkbox label="Option 3" value="Option 3" />
<CheckboxGroup.Checkbox
label="Option 4"
value="Option 4"
disabled
checked
/>
<CheckboxGroup.Checkbox label="Option 5" value="Option 5" />
<CheckboxGroup.Checkbox label="Option 6" value="Option 6" />
</CheckboxGroup>
</StoryContainer>
);
4 changes: 2 additions & 2 deletions stories/RadioGroup/RadioGroup.story.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ stories.add('Radio group', () => (
<br />

<p>
It&apos;s also possible to render it inline, <code>inline</code>{' '}
prop.
It&apos;s also possible to render it inline, with{' '}
<code>inline</code> prop.
</p>
<br />

Expand Down
1 change: 1 addition & 0 deletions stories/RadioGroup/sub-components/buttonGroupError.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Group extends React.Component {
options={options}
type="button"
error="Example error message"
inline
/>
);
}
Expand Down

0 comments on commit d799516

Please sign in to comment.