Commit b82b0a76 by Sarah Fischmann

cleaning up checkbox and README

parent 59e6684f
...@@ -12,14 +12,12 @@ storiesOf('CheckBox', module) ...@@ -12,14 +12,12 @@ storiesOf('CheckBox', module)
<CheckBox <CheckBox
name="checkbox" name="checkbox"
label="check me out!" label="check me out!"
checked="false"
/> />
)) ))
.add('disabled', () => ( .add('disabled', () => (
<CheckBox <CheckBox
name="checkbox" name="checkbox"
label="you cannot check me out" label="you cannot check me out"
checked="false"
disabled={boolean('disabled', true)} disabled={boolean('disabled', true)}
/> />
)) ))
...@@ -27,14 +25,13 @@ storiesOf('CheckBox', module) ...@@ -27,14 +25,13 @@ storiesOf('CheckBox', module)
<CheckBox <CheckBox
name="checkbox" name="checkbox"
label="(un)check me out" label="(un)check me out"
checked="true" checked
/> />
)) ))
.add('call a function', () => ( .add('call a function', () => (
<CheckBox <CheckBox
name="checkbox" name="checkbox"
label="check out the console" label="check out the console"
checked="false"
onChangeState={() => console.log('the checkbox changed state')} onChangeState={() => console.log('the checkbox changed state')}
/> />
)); ));
...@@ -9,7 +9,6 @@ describe('<CheckBox />', () => { ...@@ -9,7 +9,6 @@ describe('<CheckBox />', () => {
<CheckBox <CheckBox
name="checkbox" name="checkbox"
label="check me out!" label="check me out!"
checked="false"
/>, />,
); );
...@@ -24,7 +23,6 @@ describe('<CheckBox />', () => { ...@@ -24,7 +23,6 @@ describe('<CheckBox />', () => {
<CheckBox <CheckBox
name="checkbox" name="checkbox"
label="check me out!" label="check me out!"
checked="false"
/>, />,
); );
...@@ -45,7 +43,6 @@ describe('<CheckBox />', () => { ...@@ -45,7 +43,6 @@ describe('<CheckBox />', () => {
<CheckBox <CheckBox
name="checkbox" name="checkbox"
label="check me out!" label="check me out!"
checked="false"
onChangeState={spy} onChangeState={spy}
/>, />,
); );
...@@ -60,7 +57,7 @@ describe('<CheckBox />', () => { ...@@ -60,7 +57,7 @@ describe('<CheckBox />', () => {
<CheckBox <CheckBox
name="checkbox" name="checkbox"
label="I start checked" label="I start checked"
checked="true" checked
/>, />,
); );
...@@ -77,7 +74,6 @@ describe('<CheckBox />', () => { ...@@ -77,7 +74,6 @@ describe('<CheckBox />', () => {
<CheckBox <CheckBox
name="checkbox" name="checkbox"
label="I am disabled" label="I am disabled"
checked="false"
disabled disabled
/>, />,
); );
......
...@@ -2,13 +2,13 @@ ...@@ -2,13 +2,13 @@
Checkbox based off of the [WAI-ARIA authoring guidelines for the checkbox component](https://www.w3.org/TR/wai-aria-1.1/#checkbox). The checkbox is an HTMl input element with added attributes to ensure proper functionality and accessibility. The checkbox is wrapped in an `asInput` component. The `asInput` wrapper is passed the input element as well as a second parameter set to `false` to ensure that the label is placed to the right of the checkbox. Checkbox based off of the [WAI-ARIA authoring guidelines for the checkbox component](https://www.w3.org/TR/wai-aria-1.1/#checkbox). The checkbox is an HTMl input element with added attributes to ensure proper functionality and accessibility. The checkbox is wrapped in an `asInput` component. The `asInput` wrapper is passed the input element as well as a second parameter set to `false` to ensure that the label is placed to the right of the checkbox.
The following inputs should be passed into every checkbox component: The following parameters should be passed into every checkbox component:
* `name`: `name` attribute - type `String` * `name` (`String`): `name` attribute
* `label`: label to be placed next to the checkbox - type `String` * `label` (`String`): label to be placed next to the checkbox
* `checked`: `"true"` if the default state should be checked, `"false"` otherwise - type `String`
The following input can optionally be passed into a checkbox component: The following parameters can optionally be passed into a checkbox component:
* `disabled`: `true` if the checkbox should be disabled, `false` otherwise - type `Boolean` * `checked` (`Boolean`): `true` if the default state should be checked, `false` otherwise
* `disabled` (`Boolean`): `true` if the checkbox should be disabled, `false` otherwise
* `onChangeState`: function to be called when the checkbox changes state * `onChangeState`: function to be called when the checkbox changes state
The implementation of the checkbox contains the following functions: The implementation of the checkbox contains the following functions:
......
...@@ -13,29 +13,16 @@ class Check extends React.Component { ...@@ -13,29 +13,16 @@ class Check extends React.Component {
} }
const id = newId('checkbox'); const id = newId('checkbox');
if (props.checked === 'true') { this.state = {
this.state = { id,
id, checked: props.checked || false,
checked: true, };
};
} else {
this.state = {
id,
checked: false,
};
}
} }
handleClick() { handleClick() {
if (this.state.checked === true) { this.setState({
this.setState({ checked: !this.state.checked,
checked: false, });
});
} else {
this.setState({
checked: true,
});
}
if (this.onChangeState) { if (this.onChangeState) {
this.onChangeState(); this.onChangeState();
} }
...@@ -63,9 +50,8 @@ Check.propTypes = inputProps; ...@@ -63,9 +50,8 @@ Check.propTypes = inputProps;
const CheckBox = asInput(Check, false); const CheckBox = asInput(Check, false);
CheckBox.propTupes = { CheckBox.propTypes = {
...Check.propTypes, ...Check.propTypes,
...CheckBox.propTypes,
}; };
export default CheckBox; export default CheckBox;
...@@ -24,7 +24,7 @@ export const inputProps = { ...@@ -24,7 +24,7 @@ export const inputProps = {
className: PropTypes.arrayOf(PropTypes.string), className: PropTypes.arrayOf(PropTypes.string),
}; };
const asInput = (WrappedComponent, order = true) => { const asInput = (WrappedComponent, labelFirst = true) => {
class NewComponent extends React.Component { class NewComponent extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
...@@ -85,28 +85,9 @@ const asInput = (WrappedComponent, order = true) => { ...@@ -85,28 +85,9 @@ const asInput = (WrappedComponent, order = true) => {
render() { render() {
const { description, error, describedBy } = this.getDescriptions(); const { description, error, describedBy } = this.getDescriptions();
if (order) {
return (
<div className={styles['form-group']}>
<label htmlFor={this.state.id}>{this.props.label}</label>
<WrappedComponent
{...this.props}
{...this.state}
className={[
styles['form-control'],
...this.props.className,
]}
describedBy={describedBy}
onChange={this.handleChange}
onBlur={this.handleBlur}
/>
{error}
{description}
</div>
);
}
return ( return (
<div className={styles['form-group']}> <div className={styles['form-group']}>
{labelFirst && <label htmlFor={this.state.id}>{this.props.label}</label>}
<WrappedComponent <WrappedComponent
{...this.props} {...this.props}
{...this.state} {...this.state}
...@@ -118,7 +99,7 @@ const asInput = (WrappedComponent, order = true) => { ...@@ -118,7 +99,7 @@ const asInput = (WrappedComponent, order = true) => {
onChange={this.handleChange} onChange={this.handleChange}
onBlur={this.handleBlur} onBlur={this.handleBlur}
/> />
<label htmlFor={this.state.id}>{this.props.label}</label> {!labelFirst && <label htmlFor={this.state.id}>{this.props.label}</label>}
{error} {error}
{description} {description}
</div> </div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment