-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added <Switch> component, tests, and storybook (#433)
* Added <Switch> component, tests, and storybook * Update package.json version * Update after code review * Correct table props warning * fix: bump up size limitation to account for new code
- Loading branch information
Showing
10 changed files
with
510 additions
and
280 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
module.exports = [ | ||
{ | ||
path: 'lib/index.js', | ||
limit: '185 KB', | ||
limit: '200 KB', | ||
ignore: ['react-dom'], | ||
}, | ||
] |
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,77 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
import { default as BaseSwitch } from 'react-switch' | ||
import { | ||
blurDirty, | ||
hasInputError, | ||
fieldPropTypesWithValue, | ||
omitLabelProps, | ||
replaceEmptyStringValue, | ||
} from '../helpers' | ||
import { LabeledField } from '../labels' | ||
import { compose, generateInputErrorId } from '../../utils' | ||
|
||
/** | ||
* | ||
* A switch input that can be used in a `redux-forms`-controlled form. | ||
* | ||
* This input only accepts and stores boolean values. | ||
* | ||
* See the [react-switch](https://github.com/markusenglund/react-switch) documentation for additional styling properties. | ||
* | ||
* @name Switch | ||
* @type Function | ||
* @param {Object} input - A `redux-forms` [input](http://redux-form.com/6.5.0/docs/api/Field.md/#input-props) object | ||
* @param {Object} meta - A `redux-forms` [meta](http://redux-form.com/6.5.0/docs/api/Field.md/#meta-props) object | ||
* @param {Element | Boolean} checkedIcon - An icon displayed when the switch is checked. Set to `false` if no check icon is desired. | ||
* @param {Element | Boolean} uncheckedIcon - An icon displayed when the switch is unchecked. Set to `false` if no uncheck icon is desired. | ||
* | ||
* @example | ||
* | ||
* function CoolPersonForm ({ handleSubmit, pristine, invalid, submitting }) { | ||
* return ( | ||
* <form onSubmit={ handleSubmit }> | ||
* <Field name="isCool" component={ Switch } /> | ||
* <SubmitButton {...{ pristine, invalid, submitting }}> | ||
* Submit | ||
* </SubmitButton> | ||
* </form> | ||
* ) | ||
* } | ||
* | ||
* export default CoolPersonForm | ||
*/ | ||
|
||
const propTypes = { | ||
...fieldPropTypesWithValue(PropTypes.bool), | ||
label: PropTypes.node, | ||
} | ||
|
||
function Switch (props) { | ||
const { | ||
input: { name, value, onBlur, onChange }, | ||
meta, // eslint-disable-line no-unused-vars | ||
...rest | ||
} = omitLabelProps(props) | ||
return ( | ||
<LabeledField className="switch" { ...props }> | ||
<BaseSwitch {...{ | ||
id: name, | ||
name, | ||
checked: value, | ||
onBlur, | ||
onChange: (checked) => onChange(checked), | ||
'aria-describedby': hasInputError(meta) ? generateInputErrorId(name) : null, | ||
...rest | ||
}} | ||
/> | ||
</LabeledField> | ||
) | ||
} | ||
|
||
Switch.propTypes = propTypes | ||
|
||
export default compose( | ||
blurDirty(), | ||
replaceEmptyStringValue(false), | ||
)(Switch) |
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,64 @@ | ||
import React from 'react' | ||
import { storiesOf } from '@storybook/react' | ||
import { action } from '@storybook/addon-actions' | ||
import { Switch as StaticSwitch } from 'src' | ||
import dynamicInput from '../../dynamic-input' | ||
|
||
const Switch = dynamicInput({ | ||
initialValue: false, | ||
valuePath: 'input.value', | ||
onChangePath: 'input.onChange' | ||
})(StaticSwitch) | ||
|
||
const inputProps = { | ||
name: 'person.selected', | ||
onChange: action('switch clicked') | ||
} | ||
|
||
storiesOf('Switch', module) | ||
.add('with default label', () => ( | ||
<Switch | ||
input={ inputProps } | ||
meta={{}} | ||
/> | ||
)) | ||
.add('with custom label', () => ( | ||
<Switch | ||
input={ inputProps } | ||
meta={{}} | ||
label="Custom Label" | ||
/> | ||
)) | ||
.add('with no label', () => ( | ||
<Switch | ||
input={ inputProps } | ||
meta={{}} | ||
label={false} | ||
/> | ||
)) | ||
.add('with no icons', () => ( | ||
<Switch | ||
input={ inputProps } | ||
meta={{}} | ||
checkedIcon={false} | ||
uncheckedIcon={false} | ||
/> | ||
)) | ||
.add('with error', () => ( | ||
<Switch | ||
input={ inputProps } | ||
meta={{ | ||
invalid: true, | ||
touched: true, | ||
error: 'Invalid input' | ||
}} | ||
value="0000" | ||
/> | ||
)) | ||
.add('with a tooltip', () => ( | ||
<Switch | ||
input={ inputProps } | ||
meta={{}} | ||
tooltip="I am a tooltip" | ||
/> | ||
)) |
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,35 @@ | ||
import React from 'react' | ||
import { mount } from 'enzyme' | ||
import { Switch } from '../../../src' | ||
|
||
test('Switch toggles value when clicked', () => { | ||
const onChange = jest.fn() | ||
const props = { | ||
input: { | ||
name: 'test', | ||
value: false, | ||
onChange, | ||
}, | ||
meta: {} | ||
} | ||
const wrapper = mount(<Switch { ...props }/>) | ||
wrapper.find('input').simulate('change') | ||
const newValue = onChange.mock.calls[0][0] | ||
expect(newValue).toEqual(true) | ||
}) | ||
|
||
test('Switch is given an aria-describedby attribute when there is an input error', () => { | ||
const name = "test" | ||
const props = { | ||
input: { | ||
name, | ||
value: false, | ||
}, | ||
meta: { | ||
touched: true, | ||
invalid: true, | ||
} | ||
} | ||
const wrapper = mount(<Switch { ...props }/>) | ||
expect(wrapper.find('input').prop('aria-describedby')).toContain(name) | ||
}) |
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