Skip to content

Commit 71d5f9d

Browse files
committed
Display validation errors on toggles
1 parent 6151d19 commit 71d5f9d

File tree

5 files changed

+136
-24
lines changed

5 files changed

+136
-24
lines changed

client/app/lib/components/redux-form/Toggle.js

-21
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React, { PropTypes } from 'react';
2+
import MaterialToggle from 'material-ui/Toggle';
3+
import { red500 } from 'material-ui/styles/colors';
4+
import createComponent from './createComponent';
5+
import mapError from './mapError';
6+
7+
const errorStyle = {
8+
color: red500,
9+
};
10+
11+
// Toggle implementation with an error displayed at the bottom.
12+
const Toggle = function ({ errorText, ...props }) {
13+
return (
14+
<div>
15+
<MaterialToggle {...props} />
16+
{
17+
errorText &&
18+
<div style={errorStyle}>{errorText}</div>
19+
}
20+
</div>
21+
);
22+
};
23+
24+
Toggle.propTypes = {
25+
errorText: PropTypes.string,
26+
};
27+
28+
export default createComponent(
29+
Toggle,
30+
({
31+
input: {
32+
onChange,
33+
value,
34+
...inputProps
35+
},
36+
...props
37+
}) => ({
38+
// Take out the required fields and send the rest of the props to mapError().
39+
...mapError({ ...props, input: inputProps }),
40+
toggled: !!value,
41+
onToggle: onChange,
42+
})
43+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import React from 'react';
2+
import shallowUntil from 'utils/shallowUntil';
3+
import Toggle from '../Toggle';
4+
5+
describe('<Toggle />', () => {
6+
it('renders a toggled toggle', () => {
7+
const ToggleInput = shallowUntil(
8+
<Toggle
9+
input={{
10+
name: 'Awesome toggle',
11+
value: true,
12+
onChange: jest.fn(),
13+
}}
14+
/>,
15+
{
16+
context: { intl },
17+
childContextTypes: { intl: intlShape },
18+
},
19+
'Toggle'
20+
);
21+
22+
expect(ToggleInput).toMatchSnapshot();
23+
});
24+
25+
it('renders a toggle with an error', () => {
26+
const ToggleInput = shallowUntil(
27+
<Toggle
28+
input={{
29+
name: 'Not very awesome toggle',
30+
value: false,
31+
onChange: jest.fn(),
32+
}}
33+
meta={{
34+
error: 'Ooops',
35+
touched: true,
36+
}}
37+
/>,
38+
{
39+
context: { intl },
40+
childContextTypes: { intl: intlShape },
41+
},
42+
'Toggle'
43+
);
44+
45+
expect(ToggleInput).toMatchSnapshot();
46+
});
47+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`<Toggle /> renders a toggle with an error 1`] = `
4+
<div>
5+
<Toggle
6+
defaultToggled={false}
7+
disabled={false}
8+
labelPosition="left"
9+
name="Not very awesome toggle"
10+
onToggle={[Function]}
11+
toggled={false}
12+
/>
13+
<div
14+
style={
15+
Object {
16+
"color": "#f44336",
17+
}
18+
}
19+
>
20+
Ooops
21+
</div>
22+
</div>
23+
`;
24+
25+
exports[`<Toggle /> renders a toggled toggle 1`] = `
26+
<div>
27+
<Toggle
28+
defaultToggled={false}
29+
disabled={false}
30+
labelPosition="left"
31+
name="Awesome toggle"
32+
onToggle={[Function]}
33+
toggled={true}
34+
/>
35+
</div>
36+
`;

client/app/lib/components/redux-form/createComponent.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@ import { injectIntl } from 'react-intl';
33
/**
44
* Creates a component class that renders the given Material UI component
55
*
6-
* @param MaterialUIComponent The material ui component to render
7-
* @param mapProps A mapping of props provided by redux-form to the props the Material UI
8-
* component needs
6+
* @param {React.Component} MaterialUIComponent The material ui component to render
7+
* @param {function} mapProps A function that maps props provided by redux-form to the props the
8+
* Material UI component needs.
9+
* Format of the redux-from props (input of the mapProps function):
10+
* {
11+
* input: {name: stirng, value: any, onChange: function, ...},
12+
* meta: {touched: boolean, pristine: boolean, error: string, ...}
13+
* otherProps: {} // Other props that passed to the <Field /> input: label, intl, etc...
14+
* }
15+
* See http://redux-form.com/6.5.0/docs/api/Field.md/ for the detailed props.
916
*/
1017
export default function createComponent(MaterialUIComponent, mapProps) {
1118
class InputComponent extends Component {

0 commit comments

Comments
 (0)