Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFR] Display errors on mui BooleanInput #3115

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/ra-ui-materialui/src/input/BooleanInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { Component } from 'react';
import PropTypes from 'prop-types';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import FormGroup from '@material-ui/core/FormGroup';
import FormHelperText from '@material-ui/core/FormHelperText';
import Switch from '@material-ui/core/Switch';
import { addField, FieldTitle } from 'ra-core';

Expand All @@ -23,6 +24,7 @@ export class BooleanInput extends Component {
resource,
options,
fullWidth,
meta,
...rest
} = this.props;

Expand Down Expand Up @@ -51,6 +53,13 @@ export class BooleanInput extends Component {
/>
}
/>
{meta.error
? meta.error.map((error, index) => (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<FormHelperText key={index} error>
{error}
</FormHelperText>
))
: null}
</FormGroup>
);
}
Expand Down
42 changes: 38 additions & 4 deletions packages/ra-ui-materialui/src/input/BooleanInput.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { BooleanInput } from './BooleanInput';

describe('<BooleanInput />', () => {
it('should render as a mui Toggle', () => {
const wrapper = shallow(<BooleanInput source="foo" input={{}} />)
const wrapper = shallow(
<BooleanInput source="foo" input={{}} meta={{}} />
)
.find('WithStyles(FormControlLabel)')
.shallow()
.dive();
Expand All @@ -16,7 +18,7 @@ describe('<BooleanInput />', () => {

it('should be checked if the value is true', () => {
const wrapper = shallow(
<BooleanInput source="foo" input={{ value: true }} />
<BooleanInput source="foo" input={{ value: true }} meta={{}} />
)
.find('WithStyles(FormControlLabel)')
.shallow()
Expand All @@ -26,7 +28,7 @@ describe('<BooleanInput />', () => {

it('should not be checked if the value is false', () => {
const wrapper = shallow(
<BooleanInput source="foo" input={{ value: false }} />
<BooleanInput source="foo" input={{ value: false }} meta={{}} />
)
.find('WithStyles(FormControlLabel)')
.shallow()
Expand All @@ -35,10 +37,42 @@ describe('<BooleanInput />', () => {
});

it('should not be checked if the value is undefined', () => {
const wrapper = shallow(<BooleanInput source="foo" input={{}} />)
const wrapper = shallow(
<BooleanInput source="foo" input={{}} meta={{}} />
)
.find('WithStyles(FormControlLabel)')
.shallow()
.dive();
assert.equal(wrapper.find('WithStyles(Switch)').prop('checked'), false);
});

it('should displays errors', () => {
const wrapper = shallow(
<BooleanInput
source="foo"
input={{}}
meta={{ error: ['foo', 'bar'] }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error should be a string

/>
)
.find('WithStyles(FormGroup)')
.shallow()
.dive();
assert.equal(wrapper.find('WithStyles(FormHelperText)').length, 2);
assert.equal(
wrapper
.find('WithStyles(FormHelperText)')
.at(0)
.children()
.text(),
'foo'
);
assert.equal(
wrapper
.find('WithStyles(FormHelperText)')
.at(1)
.children()
.text(),
'bar'
);
});
});