Commit 9123797f by Sarah Fischmann

modified variables, got rid of form tags, added function story

parent 8009d271
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types';
import { inputProps } from './utils/asInput'; import { inputProps } from './utils/asInput';
...@@ -7,33 +6,31 @@ import { inputProps } from './utils/asInput'; ...@@ -7,33 +6,31 @@ import { inputProps } from './utils/asInput';
class CheckBox extends React.Component { class CheckBox extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.handleClick = this.handleClick.bind(this);
if (props.checked === 'true') { if (props.checked === 'true') {
this.state = { this.state = {
pressed: props.checked,
checked: true, checked: true,
}; };
} else { } else {
this.state = { this.state = {
pressed: props.checked,
checked: false, checked: false,
}; };
} }
} }
handleClick() { handleClick() {
if (this.state.pressed === 'true') { if (this.state.checked === true) {
this.setState({ this.setState({
pressed: 'false',
checked: false, checked: false,
}); });
} else { } else {
this.setState({ this.setState({
pressed: 'true',
checked: true, checked: true,
}); });
} }
if (this.props.fun) { if (this.props.onChange) {
this.props.fun(); this.props.onChange();
} }
} }
...@@ -42,29 +39,23 @@ class CheckBox extends React.Component { ...@@ -42,29 +39,23 @@ class CheckBox extends React.Component {
const props = { ...this.props }; const props = { ...this.props };
return ( return (
<form> <label htmlFor={props.checkLabel}>
<label htmlFor={props.checkLabel} className="form-check-label"> <input
<input name={props.name}
name={props.name} type="checkbox"
type="checkbox" defaultChecked={this.state.checked}
defaultChecked={this.state.checked} aria-describedby={props.describedby}
className="form-check-input" aria-checked={this.state.checked}
aria-describedby={props.describedby} tabIndex="0"
aria-checked={this.state.pressed} onClick={this.handleClick}
tabIndex="0" disabled={props.disabled}
onClick={() => this.handleClick()} />
disabled={props.disable} {props.label}
/> </label>
{props.checkLabel}
</label>
</form>
); );
} }
} }
CheckBox.propTypes = { CheckBox.propTypes = inputProps;
...inputProps,
checkLabel: PropTypes.string.isRequired,
};
export default CheckBox; export default CheckBox;
...@@ -130,28 +130,34 @@ storiesOf('CheckBox', module) ...@@ -130,28 +130,34 @@ storiesOf('CheckBox', module)
.add('basic usage', () => ( .add('basic usage', () => (
<CheckBox <CheckBox
name="checkbox" name="checkbox"
label="CheckBox"
describedBy="checkbox" describedBy="checkbox"
checkLabel="check me out!" label="check me out!"
checked="false" checked="false"
/> />
)) ))
.add('disabled', () => ( .add('disabled', () => (
<CheckBox <CheckBox
name="checkbox" name="checkbox"
label="CheckBox"
describedBy="checkbox" describedBy="checkbox"
checkLabel="you cannot check me out" label="you cannot check me out"
checked="false" checked="false"
disable="true" disabled="true"
/> />
)) ))
.add('default checked', () => ( .add('default checked', () => (
<CheckBox <CheckBox
name="checkbox" name="checkbox"
label="CheckBox"
describedBy="checkbox" describedBy="checkbox"
checkLabel="(un)check me out" label="(un)check me out"
checked="true" checked="true"
/> />
))
.add('call a function', () => (
<CheckBox
name="checkbox"
describedBy="checkbox"
label="check out the console"
checked="false"
onChange={() => console.log('the checkbox changed state')}
/>
)); ));
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