Commit e5ea2130 by Sarah Fischmann

fixed checkbox issues, added parameter to HOC

parent 023cdf3b
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Storyshots CheckBox basic usage 1`] = ` exports[`Storyshots CheckBox basic usage 1`] = `
<label <div
htmlFor="checkbox1" className="form-group"
> >
<input <input
aria-checked={false} aria-checked={false}
aria-describedby="checkbox"
defaultChecked={false} defaultChecked={false}
disabled={undefined} disabled={false}
id="checkbox1" id="asInput1"
name="checkbox" name="checkbox"
onClick={[Function]} onClick={[Function]}
tabIndex="0"
type="checkbox" type="checkbox"
/> />
check me out! <label
</label> htmlFor="asInput1"
>
check me out!
</label>
</div>
`; `;
exports[`Storyshots CheckBox call a function 1`] = ` exports[`Storyshots CheckBox call a function 1`] = `
<label <div
htmlFor="checkbox4" className="form-group"
> >
<input <input
aria-checked={false} aria-checked={false}
aria-describedby="checkbox"
defaultChecked={false} defaultChecked={false}
disabled={undefined} disabled={false}
id="checkbox4" id="asInput7"
name="checkbox" name="checkbox"
onClick={[Function]} onClick={[Function]}
tabIndex="0"
type="checkbox" type="checkbox"
/> />
check out the console <label
</label> htmlFor="asInput7"
>
check out the console
</label>
</div>
`; `;
exports[`Storyshots CheckBox default checked 1`] = ` exports[`Storyshots CheckBox default checked 1`] = `
<label <div
htmlFor="checkbox3" className="form-group"
> >
<input <input
aria-checked={true} aria-checked={true}
aria-describedby="checkbox"
defaultChecked={true} defaultChecked={true}
disabled={undefined} disabled={false}
id="checkbox3" id="asInput5"
name="checkbox" name="checkbox"
onClick={[Function]} onClick={[Function]}
tabIndex="0"
type="checkbox" type="checkbox"
/> />
(un)check me out <label
</label> htmlFor="asInput5"
>
(un)check me out
</label>
</div>
`; `;
exports[`Storyshots CheckBox disabled 1`] = ` exports[`Storyshots CheckBox disabled 1`] = `
<label <div
htmlFor="checkbox2" className="form-group"
> >
<input <input
aria-checked={false} aria-checked={false}
aria-describedby="checkbox"
defaultChecked={false} defaultChecked={false}
disabled={true} disabled={true}
id="checkbox2" id="asInput3"
name="checkbox" name="checkbox"
onClick={[Function]} onClick={[Function]}
tabIndex="0"
type="checkbox" type="checkbox"
/> />
you cannot check me out <label
</label> htmlFor="asInput3"
>
you cannot check me out
</label>
</div>
`; `;
exports[`Storyshots Dropdown basic usage 1`] = ` exports[`Storyshots Dropdown basic usage 1`] = `
...@@ -142,14 +150,14 @@ exports[`Storyshots InputSelect basic usage 1`] = ` ...@@ -142,14 +150,14 @@ exports[`Storyshots InputSelect basic usage 1`] = `
className="form-group" className="form-group"
> >
<label <label
htmlFor="asInput5" htmlFor="asInput9"
> >
Fruits Fruits
</label> </label>
<select <select
aria-describedby={undefined} aria-describedby={undefined}
className="form-control" className="form-control"
id="asInput5" id="asInput9"
name="fruits" name="fruits"
onBlur={[Function]} onBlur={[Function]}
onChange={[Function]} onChange={[Function]}
...@@ -185,14 +193,14 @@ exports[`Storyshots InputSelect separate labels and values 1`] = ` ...@@ -185,14 +193,14 @@ exports[`Storyshots InputSelect separate labels and values 1`] = `
className="form-group" className="form-group"
> >
<label <label
htmlFor="asInput6" htmlFor="asInput10"
> >
New England States New England States
</label> </label>
<select <select
aria-describedby={undefined} aria-describedby={undefined}
className="form-control" className="form-control"
id="asInput6" id="asInput10"
name="new-england-states" name="new-england-states"
onBlur={[Function]} onBlur={[Function]}
onChange={[Function]} onChange={[Function]}
...@@ -238,14 +246,14 @@ exports[`Storyshots InputSelect separate option groups 1`] = ` ...@@ -238,14 +246,14 @@ exports[`Storyshots InputSelect separate option groups 1`] = `
className="form-group" className="form-group"
> >
<label <label
htmlFor="asInput7" htmlFor="asInput11"
> >
Northeast States Northeast States
</label> </label>
<select <select
aria-describedby={undefined} aria-describedby={undefined}
className="form-control" className="form-control"
id="asInput7" id="asInput11"
name="northeast-states" name="northeast-states"
onBlur={[Function]} onBlur={[Function]}
onChange={[Function]} onChange={[Function]}
...@@ -339,14 +347,14 @@ exports[`Storyshots InputSelect with validation 1`] = ` ...@@ -339,14 +347,14 @@ exports[`Storyshots InputSelect with validation 1`] = `
className="form-group" className="form-group"
> >
<label <label
htmlFor="asInput8" htmlFor="asInput12"
> >
Favorite Color Favorite Color
</label> </label>
<select <select
aria-describedby={undefined} aria-describedby={undefined}
className="form-control" className="form-control"
id="asInput8" id="asInput12"
name="color" name="color"
onBlur={[Function]} onBlur={[Function]}
onChange={[Function]} onChange={[Function]}
...@@ -397,7 +405,7 @@ exports[`Storyshots InputText minimal usage 1`] = ` ...@@ -397,7 +405,7 @@ exports[`Storyshots InputText minimal usage 1`] = `
className="form-group" className="form-group"
> >
<label <label
htmlFor="asInput9" htmlFor="asInput13"
> >
First Name First Name
</label> </label>
...@@ -406,7 +414,7 @@ exports[`Storyshots InputText minimal usage 1`] = ` ...@@ -406,7 +414,7 @@ exports[`Storyshots InputText minimal usage 1`] = `
aria-invalid={false} aria-invalid={false}
className="form-control" className="form-control"
disabled={false} disabled={false}
id="asInput9" id="asInput13"
name="name" name="name"
onBlur={[Function]} onBlur={[Function]}
onChange={[Function]} onChange={[Function]}
...@@ -423,16 +431,16 @@ exports[`Storyshots InputText validation 1`] = ` ...@@ -423,16 +431,16 @@ exports[`Storyshots InputText validation 1`] = `
className="form-group" className="form-group"
> >
<label <label
htmlFor="asInput10" htmlFor="asInput14"
> >
Username Username
</label> </label>
<input <input
aria-describedby="undefined description-asInput10" aria-describedby="undefined description-asInput14"
aria-invalid={false} aria-invalid={false}
className="form-control" className="form-control"
disabled={false} disabled={false}
id="asInput10" id="asInput14"
name="username" name="username"
onBlur={[Function]} onBlur={[Function]}
onChange={[Function]} onChange={[Function]}
...@@ -443,7 +451,7 @@ exports[`Storyshots InputText validation 1`] = ` ...@@ -443,7 +451,7 @@ exports[`Storyshots InputText validation 1`] = `
/> />
<small <small
className="form-text" className="form-text"
id="description-asInput10" id="description-asInput14"
> >
The unique name that identifies you throughout the site. The unique name that identifies you throughout the site.
</small> </small>
...@@ -498,10 +506,10 @@ exports[`Storyshots Tabs basic usage 1`] = ` ...@@ -498,10 +506,10 @@ exports[`Storyshots Tabs basic usage 1`] = `
> >
<li <li
className="nav-item" className="nav-item"
id="tab-label-tabInterface11-0" id="tab-label-tabInterface15-0"
> >
<a <a
aria-controls="tab-panel-tabInterface11-0" aria-controls="tab-panel-tabInterface15-0"
aria-selected={true} aria-selected={true}
className="nav-link active" className="nav-link active"
onClick={[Function]} onClick={[Function]}
...@@ -513,10 +521,10 @@ exports[`Storyshots Tabs basic usage 1`] = ` ...@@ -513,10 +521,10 @@ exports[`Storyshots Tabs basic usage 1`] = `
</li> </li>
<li <li
className="nav-item" className="nav-item"
id="tab-label-tabInterface11-1" id="tab-label-tabInterface15-1"
> >
<a <a
aria-controls="tab-panel-tabInterface11-1" aria-controls="tab-panel-tabInterface15-1"
aria-selected={false} aria-selected={false}
className="nav-link" className="nav-link"
onClick={[Function]} onClick={[Function]}
...@@ -528,10 +536,10 @@ exports[`Storyshots Tabs basic usage 1`] = ` ...@@ -528,10 +536,10 @@ exports[`Storyshots Tabs basic usage 1`] = `
</li> </li>
<li <li
className="nav-item" className="nav-item"
id="tab-label-tabInterface11-2" id="tab-label-tabInterface15-2"
> >
<a <a
aria-controls="tab-panel-tabInterface11-2" aria-controls="tab-panel-tabInterface15-2"
aria-selected={false} aria-selected={false}
className="nav-link" className="nav-link"
onClick={[Function]} onClick={[Function]}
...@@ -547,9 +555,9 @@ exports[`Storyshots Tabs basic usage 1`] = ` ...@@ -547,9 +555,9 @@ exports[`Storyshots Tabs basic usage 1`] = `
> >
<div <div
aria-hidden={false} aria-hidden={false}
aria-labelledby="tab-label-tabInterface11-0" aria-labelledby="tab-label-tabInterface15-0"
className="tab-pane active" className="tab-pane active"
id="tab-panel-tabInterface11-0" id="tab-panel-tabInterface15-0"
role="tabpanel" role="tabpanel"
> >
<div> <div>
...@@ -558,9 +566,9 @@ exports[`Storyshots Tabs basic usage 1`] = ` ...@@ -558,9 +566,9 @@ exports[`Storyshots Tabs basic usage 1`] = `
</div> </div>
<div <div
aria-hidden={true} aria-hidden={true}
aria-labelledby="tab-label-tabInterface11-1" aria-labelledby="tab-label-tabInterface15-1"
className="tab-pane" className="tab-pane"
id="tab-panel-tabInterface11-1" id="tab-panel-tabInterface15-1"
role="tabpanel" role="tabpanel"
> >
<div> <div>
...@@ -569,9 +577,9 @@ exports[`Storyshots Tabs basic usage 1`] = ` ...@@ -569,9 +577,9 @@ exports[`Storyshots Tabs basic usage 1`] = `
</div> </div>
<div <div
aria-hidden={true} aria-hidden={true}
aria-labelledby="tab-label-tabInterface11-2" aria-labelledby="tab-label-tabInterface15-2"
className="tab-pane" className="tab-pane"
id="tab-panel-tabInterface11-2" id="tab-panel-tabInterface15-2"
role="tabpanel" role="tabpanel"
> >
<div> <div>
......
...@@ -11,7 +11,6 @@ storiesOf('CheckBox', module) ...@@ -11,7 +11,6 @@ storiesOf('CheckBox', module)
.add('basic usage', () => ( .add('basic usage', () => (
<CheckBox <CheckBox
name="checkbox" name="checkbox"
describedBy="checkbox"
label="check me out!" label="check me out!"
checked="false" checked="false"
/> />
...@@ -19,7 +18,6 @@ storiesOf('CheckBox', module) ...@@ -19,7 +18,6 @@ storiesOf('CheckBox', module)
.add('disabled', () => ( .add('disabled', () => (
<CheckBox <CheckBox
name="checkbox" name="checkbox"
describedBy="checkbox"
label="you cannot check me out" label="you cannot check me out"
checked="false" checked="false"
disabled={boolean('disabled', true)} disabled={boolean('disabled', true)}
...@@ -28,7 +26,6 @@ storiesOf('CheckBox', module) ...@@ -28,7 +26,6 @@ storiesOf('CheckBox', module)
.add('default checked', () => ( .add('default checked', () => (
<CheckBox <CheckBox
name="checkbox" name="checkbox"
describedBy="checkbox"
label="(un)check me out" label="(un)check me out"
checked="true" checked="true"
/> />
...@@ -36,9 +33,8 @@ storiesOf('CheckBox', module) ...@@ -36,9 +33,8 @@ storiesOf('CheckBox', module)
.add('call a function', () => ( .add('call a function', () => (
<CheckBox <CheckBox
name="checkbox" name="checkbox"
describedBy="checkbox"
label="check out the console" label="check out the console"
checked="false" checked="false"
onChange={() => console.log('the checkbox changed state')} onChangeState={() => console.log('the checkbox changed state')}
/> />
)); ));
/* eslint-disable import/no-extraneous-dependencies */ /* eslint-disable import/no-extraneous-dependencies */
import React from 'react'; import React from 'react';
import { shallow, mount } from 'enzyme'; import { mount } from 'enzyme';
import CheckBox from './index'; import CheckBox from './index';
describe('<CheckBox />', () => { describe('<CheckBox />', () => {
it('attributes are set correctly', () => { it('attributes are set correctly', () => {
const wrapper = shallow( const wrapper = mount(
<CheckBox <CheckBox
name="checkbox" name="checkbox"
describedBy="this is a checkbox"
label="check me out!" label="check me out!"
checked="false" checked="false"
/>, />,
...@@ -17,16 +16,13 @@ describe('<CheckBox />', () => { ...@@ -17,16 +16,13 @@ describe('<CheckBox />', () => {
expect(wrapper.find('[name="checkbox"]').exists()).toEqual(true); expect(wrapper.find('[name="checkbox"]').exists()).toEqual(true);
expect(wrapper.find('[type="checkbox"]').exists()).toEqual(true); expect(wrapper.find('[type="checkbox"]').exists()).toEqual(true);
expect(wrapper.find('[defaultChecked=false]').exists()).toEqual(true); expect(wrapper.find('[defaultChecked=false]').exists()).toEqual(true);
expect(wrapper.find('[aria-describedby="this is a checkbox"]').exists()).toEqual(true);
expect(wrapper.find('[aria-checked=false]').exists()).toEqual(true); expect(wrapper.find('[aria-checked=false]').exists()).toEqual(true);
expect(wrapper.find('[tabIndex="0"]').exists()).toEqual(true);
}); });
it('aria-label changes after click', () => { it('aria-label changes after click', () => {
const wrapper = shallow( const wrapper = mount(
<CheckBox <CheckBox
name="checkbox" name="checkbox"
descibedBy="checkbox"
label="check me out!" label="check me out!"
checked="false" checked="false"
/>, />,
...@@ -34,24 +30,23 @@ describe('<CheckBox />', () => { ...@@ -34,24 +30,23 @@ describe('<CheckBox />', () => {
expect(wrapper.find('[aria-checked=false]').exists()).toEqual(true); expect(wrapper.find('[aria-checked=false]').exists()).toEqual(true);
wrapper.find('input').simulate('click'); wrapper.find('[type="checkbox"]').simulate('click');
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('input').simulate('click'); wrapper.find('[type="checkbox"]').simulate('click');
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);
}); });
it('check that callback function is triggered when clicked', () => { it('check that callback function is triggered when clicked', () => {
const spy = jest.fn(); const spy = jest.fn();
const wrapper = shallow( const wrapper = mount(
<CheckBox <CheckBox
name="checkbox" name="checkbox"
descibedBy="checkbox"
label="check me out!" label="check me out!"
checked="false" checked="false"
onChange={spy} onChangeState={spy}
/>, />,
); );
...@@ -61,10 +56,9 @@ describe('<CheckBox />', () => { ...@@ -61,10 +56,9 @@ describe('<CheckBox />', () => {
}); });
it('checks if start state can be set to checked', () => { it('checks if start state can be set to checked', () => {
const wrapper = shallow( const wrapper = mount(
<CheckBox <CheckBox
name="checkbox" name="checkbox"
describedBy="checkbox"
label="I start checked" label="I start checked"
checked="true" checked="true"
/>, />,
...@@ -82,7 +76,6 @@ describe('<CheckBox />', () => { ...@@ -82,7 +76,6 @@ describe('<CheckBox />', () => {
const wrapper = mount( const wrapper = mount(
<CheckBox <CheckBox
name="checkbox" name="checkbox"
describedBy="checkbox"
label="I am disabled" label="I am disabled"
checked="false" checked="false"
disabled disabled
......
import React from 'react'; import React from 'react';
import { inputProps } from '../asInput'; import asInput, { inputProps } from '../asInput';
import newId from '../utils/newId'; import newId from '../utils/newId';
class CheckBox extends React.Component { class Check extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.handleClick = this.handleClick.bind(this); this.handleClick = this.handleClick.bind(this);
if (this.props.onChange) { if (this.props.onChangeState) {
this.onChange = this.props.onChange.bind(this); this.onChangeState = this.props.onChangeState.bind(this);
} }
const id = newId('checkbox'); const id = newId('checkbox');
...@@ -36,8 +36,8 @@ class CheckBox extends React.Component { ...@@ -36,8 +36,8 @@ class CheckBox extends React.Component {
checked: true, checked: true,
}); });
} }
if (this.onChange) { if (this.onChangeState) {
this.onChange(); this.onChangeState();
} }
} }
...@@ -46,24 +46,26 @@ class CheckBox extends React.Component { ...@@ -46,24 +46,26 @@ class CheckBox extends React.Component {
const props = { ...this.props }; const props = { ...this.props };
return ( return (
<label htmlFor={this.state.id}> <input
<input id={props.id}
id={this.state.id} type="checkbox"
name={props.name} name={props.name}
type="checkbox" defaultChecked={this.state.checked}
defaultChecked={this.state.checked} aria-checked={this.state.checked}
aria-describedby={props.describedBy} onClick={this.handleClick}
aria-checked={this.state.checked} disabled={props.disabled}
tabIndex="0" />
onClick={this.handleClick}
disabled={props.disabled}
/>
{props.label}
</label>
); );
} }
} }
CheckBox.propTypes = inputProps; Check.propTypes = inputProps;
const CheckBox = asInput(Check, false);
CheckBox.propTupes = {
...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) => { const asInput = (WrappedComponent, order = true) => {
class NewComponent extends React.Component { class NewComponent extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
...@@ -85,10 +85,28 @@ const asInput = (WrappedComponent) => { ...@@ -85,10 +85,28 @@ const asInput = (WrappedComponent) => {
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']}>
<label htmlFor={this.state.id}>{this.props.label}</label>
<WrappedComponent <WrappedComponent
{...this.props} {...this.props}
{...this.state} {...this.state}
...@@ -100,6 +118,7 @@ const asInput = (WrappedComponent) => { ...@@ -100,6 +118,7 @@ const asInput = (WrappedComponent) => {
onChange={this.handleChange} onChange={this.handleChange}
onBlur={this.handleBlur} onBlur={this.handleBlur}
/> />
<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