Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(checkbox): adding checkbox component, using material design p…
…ackage for icons
- Loading branch information
kamilmateusiak
committed
Sep 4, 2018
1 parent
0878272
commit eb3d6d8
Showing
11 changed files
with
240 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import classNames from 'classnames/bind'; | ||
import { CheckIcon } from 'react-material-icon-svg'; | ||
import FieldDescription from '../FieldDescription'; | ||
import styles from './style.scss'; | ||
import getMergedClassNames from '../../utils/getMergedClassNames'; | ||
|
||
const cx = classNames.bind(styles); | ||
|
||
const CheckboxField = props => { | ||
const { | ||
className, | ||
children, | ||
checked, | ||
disabled, | ||
id, | ||
description, | ||
...restProps | ||
} = props; | ||
|
||
const baseClass = 'checkbox'; | ||
const mergedClassNames = getMergedClassNames( | ||
cx({ | ||
[baseClass]: true, | ||
[`${baseClass}--selected`]: checked, | ||
[`${baseClass}--disabled`]: disabled | ||
}), | ||
className | ||
); | ||
|
||
return ( | ||
<div className={mergedClassNames}> | ||
<label className={styles[`${baseClass}__label`]} htmlFor={id}> | ||
<div className={styles[`${baseClass}__square`]}> | ||
<CheckIcon className={styles[`${baseClass}__checkmark`]} /> | ||
<input | ||
className={styles[`${baseClass}__input`]} | ||
{...restProps} | ||
type="radio" | ||
id={id} | ||
checked={checked} | ||
disabled={disabled} | ||
/> | ||
</div> | ||
<div className={styles[`${baseClass}__text`]}>{children}</div> | ||
</label> | ||
{description && ( | ||
<FieldDescription className={styles[`${baseClass}__helper`]}> | ||
{description} | ||
</FieldDescription> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
CheckboxField.propTypes = { | ||
className: PropTypes.string, | ||
id: PropTypes.string.isRequired, | ||
onClick: PropTypes.func, | ||
onChange: PropTypes.func, | ||
checked: PropTypes.bool.isRequired, | ||
children: PropTypes.node.isRequired, | ||
description: PropTypes.string, | ||
disabled: PropTypes.bool | ||
}; | ||
|
||
CheckboxField.defaultProps = { | ||
onChange: () => {} | ||
}; | ||
|
||
export default CheckboxField; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
Use checkboxes when there is a short list of options and the user can select multiple options, all or none. | ||
|
||
<h3>Checkbox Field</h3> | ||
|
||
```js | ||
initialState = { checked: false }; | ||
|
||
<CheckboxField | ||
checked={state.checked} | ||
onClick={() => setState({checked: !state.checked})} | ||
id='checkbox-example-1' | ||
description='checkbox helper text' | ||
> | ||
Checkbox label | ||
</CheckboxField> | ||
``` | ||
```js noeditor | ||
<ComponentHtmlMarkup> | ||
<CheckboxField | ||
checked | ||
id='checkbox-example-1' | ||
description='checkbox helper text' | ||
> | ||
Checkbox label | ||
</CheckboxField> | ||
</ComponentHtmlMarkup> | ||
``` | ||
|
||
<h3>CheckboxField Disabled</h3> | ||
|
||
```js | ||
<CheckboxField | ||
checked | ||
disabled | ||
id='checkbox-example-2' | ||
> | ||
Checkbox Field label | ||
</CheckboxField> | ||
``` | ||
```js noeditor | ||
<ComponentHtmlMarkup> | ||
<CheckboxField | ||
checked | ||
disabled | ||
id='checkbox-example-2' | ||
> | ||
Checkbox Field label | ||
</CheckboxField> | ||
</ComponentHtmlMarkup> | ||
``` | ||
|
||
<h3>Best practices</h3> | ||
<ul> | ||
<li>Labels appear to the right of checkboxes, and start with a captital letter.</li> | ||
<li>Labels state clearly what will happen if the box is checked.</li> | ||
<li>Use positive wording (“Turn on notifications” instead of “Turn off notifications”).</li> | ||
<li>Don’t use commas at the end of each line.</li> | ||
<li>Users can select the checkbox by clicking on the box or its label.</li> | ||
<li>Each checkbox is independent – selecting one doesn't uncheck others in the list.</li> | ||
<li>List options from most likely to least likely to be selected.</li> | ||
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import CheckboxField from './CheckboxField'; | ||
|
||
export default CheckboxField; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
@import '../../scss/variables/colors'; | ||
|
||
$base-class: 'checkbox'; | ||
|
||
.#{$base-class} { | ||
display: inline-block; | ||
|
||
&__label { | ||
cursor: pointer; | ||
display: inline-flex; | ||
align-items: center; | ||
} | ||
|
||
&__square { | ||
position: relative; | ||
width: 16px; | ||
height: 16px; | ||
border-radius: 4px; | ||
background-color:#f3f7f9; | ||
border: 1px solid #bcc6d0; | ||
margin-right: 8px; | ||
transition: all 0.2s ease; | ||
box-sizing: border-box; | ||
} | ||
|
||
&__checkmark { | ||
opacity: 0; | ||
position: absolute; | ||
top: 2px; | ||
left: 2px; | ||
width: 10px; | ||
height: 10px; | ||
fill: #fff; | ||
transition: all 0.2s ease; | ||
} | ||
|
||
&--selected { | ||
.#{$base-class}__square { | ||
background-color: $primary-color; | ||
border-color: $primary-color; | ||
} | ||
|
||
.#{$base-class}__checkmark { | ||
opacity: 1; | ||
} | ||
} | ||
|
||
&__text { | ||
font-size: 15px; | ||
color: $font-primary-color; | ||
} | ||
|
||
&__input { | ||
width: 100%; | ||
height: 100%; | ||
opacity: 0; | ||
cursor: pointer; | ||
} | ||
|
||
&--disabled { | ||
opacity: 0.3; | ||
.#{$base-class} { | ||
&__square { | ||
border-color: $font-tertiary-color; | ||
background-color: $font-tertiary-color; | ||
} | ||
&__label { | ||
color: $font-primary-color; | ||
cursor: not-allowed; | ||
} | ||
&__input { | ||
cursor: not-allowed; | ||
} | ||
} | ||
} | ||
|
||
&__helper { | ||
cursor: default; | ||
margin-left: 24px; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters