-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Display validation errors on toggles
- Loading branch information
Showing
5 changed files
with
116 additions
and
24 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,42 @@ | ||
import React, { PropTypes } from 'react'; | ||
import MaterialToggle from 'material-ui/Toggle'; | ||
import createComponent from './createComponent'; | ||
import mapError from './mapError'; | ||
|
||
const errorStyle = { | ||
color: 'red', | ||
}; | ||
|
||
// Toggle implementation with an error displayed at the bottom. | ||
const Toggle = function ({ errorText, ...props }) { | ||
return ( | ||
<div> | ||
<MaterialToggle {...props} /> | ||
{ | ||
errorText && | ||
<div style={errorStyle}>{errorText}</div> | ||
} | ||
</div> | ||
); | ||
}; | ||
|
||
Toggle.propTypes = { | ||
errorText: PropTypes.string, | ||
}; | ||
|
||
export default createComponent( | ||
Toggle, | ||
({ | ||
input: { | ||
onChange, | ||
value, | ||
...inputProps | ||
}, | ||
...props | ||
}) => ({ | ||
// Take our the required fields and send the rest of the props to mapError(). | ||
...mapError({ ...props, input: inputProps }), | ||
toggled: !!value, | ||
onToggle: onChange, | ||
}) | ||
); |
46 changes: 46 additions & 0 deletions
46
client/app/lib/components/redux-form/__test__/Toggle.test.js
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,46 @@ | ||
import { shallow } from 'enzyme'; | ||
import React from 'react'; | ||
import Toggle from '../Toggle'; | ||
|
||
describe('<Toggle />', () => { | ||
it('renders a toggled toggle', () => { | ||
const ToggleInput = shallow( | ||
<Toggle | ||
input={{ | ||
name: 'Awesome toggle', | ||
value: true, | ||
onChange: jest.fn(), | ||
}} | ||
/>, | ||
{ | ||
context: { intl }, | ||
childContextTypes: { intl: intlShape }, | ||
} | ||
).first().shallow(); | ||
|
||
expect(ToggleInput).toMatchSnapshot(); | ||
}); | ||
|
||
|
||
it('renders a toggle with an error', () => { | ||
const ToggleInput = shallow( | ||
<Toggle | ||
input={{ | ||
name: 'Not very awesome toggle', | ||
value: false, | ||
onChange: jest.fn(), | ||
}} | ||
meta={{ | ||
error: 'Ooops', | ||
touched: true, | ||
}} | ||
/>, | ||
{ | ||
context: { intl }, | ||
childContextTypes: { intl: intlShape }, | ||
} | ||
).first().shallow(); | ||
|
||
expect(ToggleInput).toMatchSnapshot(); | ||
}); | ||
}); |
18 changes: 18 additions & 0 deletions
18
client/app/lib/components/redux-form/__test__/__snapshots__/Toggle.test.js.snap
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,18 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`<Toggle /> renders a toggle with an error 1`] = ` | ||
<Toggle | ||
errorText="Ooops" | ||
name="Not very awesome toggle" | ||
onToggle={[Function]} | ||
toggled={false} | ||
/> | ||
`; | ||
|
||
exports[`<Toggle /> renders a toggled toggle 1`] = ` | ||
<Toggle | ||
name="Awesome toggle" | ||
onToggle={[Function]} | ||
toggled={true} | ||
/> | ||
`; |
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