Skip to content
This repository has been archived by the owner on Jun 20, 2022. It is now read-only.

Commit

Permalink
feat: add control option to Checkbox (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
ividic authored and gregberge committed Jan 22, 2019
1 parent 2dad70f commit f26186c
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
45 changes: 42 additions & 3 deletions docs/components/Checkbox.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ Set sizes using `size` prop like `"sm"`, `"md"` or `"lg"`.
<Checkbox
size="sm"
id="sizeCheckbox1"
name="sizeCheckboxs"
name="sizeCheckboxes"
value="option1"
/>
<FormCheckLabel htmlFor="sizeCheckbox1">Small</FormCheckLabel>
</FormCheck>
<FormCheck>
<Checkbox id="sizeCheckbox2" name="sizeCheckboxs" value="option2" />
<Checkbox id="sizeCheckbox2" name="sizeCheckboxes" value="option2" />
<FormCheckLabel htmlFor="sizeCheckbox2">Medium</FormCheckLabel>
</FormCheck>
<FormCheck>
<Checkbox
size="lg"
id="sizeCheckbox3"
name="sizeCheckboxs"
name="sizeCheckboxes"
value="option3"
/>
<FormCheckLabel htmlFor="sizeCheckbox3">Large</FormCheckLabel>
Expand All @@ -54,16 +54,55 @@ Disable using `disabled` prop.
</FormCheck>
</Playground>

## Control & Validation

* Set `display` to "block" and `width` to "100%" using `control` prop.
* Set validation using `valid` or `valid={false}`

<Playground>
<FormCheck>
<Checkbox
id="controlCheckbox1"
name="controlCheckboxes"
value="option1"
control
/>
<FormCheckLabel htmlFor="controlCheckbox1">Control</FormCheckLabel>
</FormCheck>
<FormCheck>
<Checkbox
id="controlCheckbox2"
name="controlCheckboxes"
value="option2"
control
valid
/>
<FormCheckLabel htmlFor="controlCheckbox2">Valid Control</FormCheckLabel>
</FormCheck>
<FormCheck>
<Checkbox
id="controlCheckbox3"
name="controlCheckboxes"
value="option3"
control
valid={false}
/>
<FormCheckLabel htmlFor="controlCheckbox3">Invalid Control</FormCheckLabel>
</FormCheck>
</Playground>

## API

### Checkbox

<PropsTable
of={PropDesc({
checked: PropDesc.bool,
control: PropDesc.bool,
disabled: PropDesc.bool,
onChange: PropDesc.func,
size: PropDesc.oneOf(['sm', 'lg']),
valid: PropDesc.bool,
value: PropDesc.string,
...getSystemPropDesc(Checkbox),
})}
Expand Down
48 changes: 47 additions & 1 deletion packages/shared/core/Checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,48 @@ const sizeStyle = {
`,
}

const validStyle = css`
input + .sui-checkbox-content,
input:checked + .sui-checkbox-content {
border-color: ${th('success')};
}
input:focus + .sui-checkbox-content {
border-color: ${th('success')};
box-shadow: ${mixin('controlFocusBoxShadow', 'success')};
}
`

const invalidStyle = css`
input + .sui-checkbox-content,
input:checked + .sui-checkbox-content {
border-color: ${th('danger')};
}
input:focus + .sui-checkbox-content {
border-color: ${th('danger')};
box-shadow: ${mixin('controlFocusBoxShadow', 'danger')};
}
`

const controlStyle = css`
input:focus + .sui-checkbox-content {
border-color: ${th('controlFocusBorderColor')};
box-shadow: ${mixin('controlFocusBoxShadow', 'primary')};
}
${p => {
switch (p.valid) {
case true:
return validStyle
case false:
return invalidStyle
default:
return null
}
}};
`

const containerSystem = compose(
basics,
dimensions,
Expand All @@ -75,6 +117,7 @@ const system = compose(

const Checkbox = createComponent(() => ({
name: 'checkbox',
omitProps: ['control', 'valid'],
system,
applySystem: null,
render: ({ Component, ref, className, size, ...props }) => (
Expand Down Expand Up @@ -134,7 +177,7 @@ const Checkbox = createComponent(() => ({
}
}
input:focused + .sui-checkbox-content {
input:focus + .sui-checkbox-content {
${mixin('controlFocus')};
}
Expand All @@ -143,6 +186,7 @@ const Checkbox = createComponent(() => ({
}
${p => sizeStyle[p.size]};
${p => p.control && controlStyle};
${containerSystem.props};
Expand All @@ -152,9 +196,11 @@ const Checkbox = createComponent(() => ({
`,
propTypes: {
checked: PropTypes.bool,
control: PropTypes.bool,
disabled: PropTypes.bool,
onChange: PropTypes.func,
size: PropTypes.oneOf(['sm', 'md', 'lg']),
valid: PropTypes.bool,
value: PropTypes.string,
},
defaultProps: {
Expand Down

0 comments on commit f26186c

Please sign in to comment.