Commit c913e080 by Sarah Fischmann

fixing onChange and proptypes

parent b82b0a76
...@@ -10,7 +10,7 @@ exports[`Storyshots CheckBox basic usage 1`] = ` ...@@ -10,7 +10,7 @@ exports[`Storyshots CheckBox basic usage 1`] = `
disabled={false} disabled={false}
id="asInput1" id="asInput1"
name="checkbox" name="checkbox"
onClick={[Function]} onChange={[Function]}
type="checkbox" type="checkbox"
/> />
<label <label
...@@ -31,7 +31,7 @@ exports[`Storyshots CheckBox call a function 1`] = ` ...@@ -31,7 +31,7 @@ exports[`Storyshots CheckBox call a function 1`] = `
disabled={false} disabled={false}
id="asInput7" id="asInput7"
name="checkbox" name="checkbox"
onClick={[Function]} onChange={[Function]}
type="checkbox" type="checkbox"
/> />
<label <label
...@@ -52,7 +52,7 @@ exports[`Storyshots CheckBox default checked 1`] = ` ...@@ -52,7 +52,7 @@ exports[`Storyshots CheckBox default checked 1`] = `
disabled={false} disabled={false}
id="asInput5" id="asInput5"
name="checkbox" name="checkbox"
onClick={[Function]} onChange={[Function]}
type="checkbox" type="checkbox"
/> />
<label <label
...@@ -73,7 +73,7 @@ exports[`Storyshots CheckBox disabled 1`] = ` ...@@ -73,7 +73,7 @@ exports[`Storyshots CheckBox disabled 1`] = `
disabled={true} disabled={true}
id="asInput3" id="asInput3"
name="checkbox" name="checkbox"
onClick={[Function]} onChange={[Function]}
type="checkbox" type="checkbox"
/> />
<label <label
......
...@@ -32,6 +32,6 @@ storiesOf('CheckBox', module) ...@@ -32,6 +32,6 @@ storiesOf('CheckBox', module)
<CheckBox <CheckBox
name="checkbox" name="checkbox"
label="check out the console" label="check out the console"
onChangeState={() => console.log('the checkbox changed state')} onChange={() => console.log('the checkbox changed state')}
/> />
)); ));
...@@ -28,11 +28,11 @@ describe('<CheckBox />', () => { ...@@ -28,11 +28,11 @@ describe('<CheckBox />', () => {
expect(wrapper.find('[aria-checked=false]').exists()).toEqual(true); expect(wrapper.find('[aria-checked=false]').exists()).toEqual(true);
wrapper.find('[type="checkbox"]').simulate('click'); wrapper.find('[type="checkbox"]').simulate('change');
expect(wrapper.find('[aria-checked=false]').exists()).toEqual(false); expect(wrapper.find('[aria-checked=false]').exists()).toEqual(false);
expect(wrapper.find('[aria-checked=true]').exists()).toEqual(true); expect(wrapper.find('[aria-checked=true]').exists()).toEqual(true);
wrapper.find('[type="checkbox"]').simulate('click'); wrapper.find('[type="checkbox"]').simulate('change');
expect(wrapper.find('[aria-checked=false]').exists()).toEqual(true); expect(wrapper.find('[aria-checked=false]').exists()).toEqual(true);
expect(wrapper.find('[aria-checked=true]').exists()).toEqual(false); expect(wrapper.find('[aria-checked=true]').exists()).toEqual(false);
}); });
...@@ -43,12 +43,12 @@ describe('<CheckBox />', () => { ...@@ -43,12 +43,12 @@ describe('<CheckBox />', () => {
<CheckBox <CheckBox
name="checkbox" name="checkbox"
label="check me out!" label="check me out!"
onChangeState={spy} onChange={spy}
/>, />,
); );
expect(spy).toHaveBeenCalledTimes(0); expect(spy).toHaveBeenCalledTimes(0);
wrapper.find('input').simulate('click'); wrapper.find('input').simulate('change');
expect(spy).toHaveBeenCalledTimes(1); expect(spy).toHaveBeenCalledTimes(1);
}); });
...@@ -64,7 +64,7 @@ describe('<CheckBox />', () => { ...@@ -64,7 +64,7 @@ describe('<CheckBox />', () => {
expect(wrapper.find('[defaultChecked=true]').exists()).toEqual(true); expect(wrapper.find('[defaultChecked=true]').exists()).toEqual(true);
expect(wrapper.find('[aria-checked=true]').exists()).toEqual(true); expect(wrapper.find('[aria-checked=true]').exists()).toEqual(true);
wrapper.find('input').simulate('click'); wrapper.find('input').simulate('change');
expect(wrapper.find('[defaultChecked=false]').exists()).toEqual(true); expect(wrapper.find('[defaultChecked=false]').exists()).toEqual(true);
expect(wrapper.find('[aria-checked=false]').exists()).toEqual(true); expect(wrapper.find('[aria-checked=false]').exists()).toEqual(true);
}); });
...@@ -78,11 +78,9 @@ describe('<CheckBox />', () => { ...@@ -78,11 +78,9 @@ describe('<CheckBox />', () => {
/>, />,
); );
expect(wrapper.find('[defaultChecked=false]').exists()).toEqual(true); expect(wrapper.props().disabled).toEqual(true);
expect(wrapper.find('[aria-checked=false]').exists()).toEqual(true);
wrapper.find('input').simulate('click'); wrapper.find('input').simulate('change');
expect(wrapper.find('[defaultChecked=false]').exists()).toEqual(true); expect(wrapper.props().disabled).toEqual(true);
expect(wrapper.find('[aria-checked=false]').exists()).toEqual(true);
}); });
}); });
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types';
import asInput, { inputProps } from '../asInput'; import asInput from '../asInput';
import newId from '../utils/newId'; import newId from '../utils/newId';
class Check extends React.Component { class Check extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.handleClick = this.handleClick.bind(this); this.onChange = this.onChange.bind(this);
if (this.props.onChangeState) {
this.onChangeState = this.props.onChangeState.bind(this);
}
const id = newId('checkbox'); const id = newId('checkbox');
this.state = { this.state = {
...@@ -19,13 +17,11 @@ class Check extends React.Component { ...@@ -19,13 +17,11 @@ class Check extends React.Component {
}; };
} }
handleClick() { onChange(event) {
this.setState({ this.setState({
checked: !this.state.checked, checked: !this.state.checked,
}); });
if (this.onChangeState) { this.props.onChange(event);
this.onChangeState();
}
} }
...@@ -39,18 +35,27 @@ class Check extends React.Component { ...@@ -39,18 +35,27 @@ class Check extends React.Component {
name={props.name} name={props.name}
defaultChecked={this.state.checked} defaultChecked={this.state.checked}
aria-checked={this.state.checked} aria-checked={this.state.checked}
onClick={this.handleClick} onChange={this.onChange}
disabled={props.disabled} disabled={props.disabled}
/> />
); );
} }
} }
Check.propTypes = inputProps; Check.propTypes = {
checked: PropTypes.bool,
onChange: PropTypes.func,
};
Check.defaultProps = {
checked: false,
onChange: () => {},
};
const CheckBox = asInput(Check, false); const CheckBox = asInput(Check, false);
CheckBox.propTypes = { CheckBox.propTypes = {
...CheckBox.propTypes,
...Check.propTypes, ...Check.propTypes,
}; };
......
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