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] Fix RadioButtonGroupInput and CheckboxGroupInput Error Display #3735

Merged
merged 1 commit into from
Sep 25, 2019
Merged
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
22 changes: 13 additions & 9 deletions packages/ra-ui-materialui/src/input/CheckboxGroupInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ describe('<CheckboxGroupInput />', () => {
render={() => <CheckboxGroupInput {...defaultProps} />}
/>
);
const input1 = getByLabelText('Angular');
const input1 = getByLabelText('Angular') as HTMLInputElement;
expect(input1.type).toBe('checkbox');
expect(input1.value).toBe('ang');
expect(input1.checked).toBe(false);
const input2 = getByLabelText('React');
const input2 = getByLabelText('React') as HTMLInputElement;
expect(input2.type).toBe('checkbox');
expect(input2.value).toBe('rct');
expect(input2.checked).toBe(false);
Expand All @@ -49,9 +49,9 @@ describe('<CheckboxGroupInput />', () => {
)}
/>
);
const input1 = getByLabelText('Angular');
const input1 = getByLabelText('Angular') as HTMLInputElement;
expect(input1.checked).toEqual(true);
const input2 = getByLabelText('React');
const input2 = getByLabelText('React') as HTMLInputElement;
expect(input2.checked).toEqual(false);
});

Expand All @@ -68,7 +68,8 @@ describe('<CheckboxGroupInput />', () => {
)}
/>
);
expect(getByLabelText('Bar').value).toBe('foo');
const input = getByLabelText('Bar') as HTMLInputElement;
expect(input.value).toBe('foo');
});

it('should use optionValue including "." as value identifier', () => {
Expand All @@ -84,7 +85,8 @@ describe('<CheckboxGroupInput />', () => {
)}
/>
);
expect(getByLabelText('Bar').value).toBe('foo');
const input = getByLabelText('Bar') as HTMLInputElement;
expect(input.value).toBe('foo');
});

it('should use optionText with a string value as text identifier', () => {
Expand Down Expand Up @@ -238,7 +240,7 @@ describe('<CheckboxGroupInput />', () => {
// This validator always returns an error
const validate = () => 'ra.validation.error';

const { queryByLabelText, queryByText } = render(
const { queryByLabelText, getByText } = render(
<Form
onSubmit={jest.fn}
validateOnBlur
Expand All @@ -250,12 +252,14 @@ describe('<CheckboxGroupInput />', () => {
)}
/>
);
const input = queryByLabelText('Angular');
const input = queryByLabelText('Angular') as HTMLInputElement;
fireEvent.click(input);
expect(input.checked).toBe(true);

fireEvent.blur(input);
expect(queryByText('ra.validation.error')).not.toBeNull();
const error = getByText('ra.validation.error');
expect(error).toBeDefined();
expect(error.classList.contains('Mui-error')).toEqual(true);
});
});
});
15 changes: 12 additions & 3 deletions packages/ra-ui-materialui/src/input/CheckboxGroupInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import FormControl, { FormControlProps } from '@material-ui/core/FormControl';
import FormGroup from '@material-ui/core/FormGroup';
import FormHelperText from '@material-ui/core/FormHelperText';
import { makeStyles } from '@material-ui/core/styles';
import { CheckboxProps } from '@material-ui/core/Checkbox';
import { FieldTitle, useInput, InputProps, ChoicesProps } from 'ra-core';

import defaultSanitizeRestProps from './sanitizeRestProps';
import CheckboxGroupInputItem from './CheckboxGroupInputItem';
import { CheckboxProps } from '@material-ui/core/Checkbox';
import InputHelperText from './InputHelperText';

const sanitizeRestProps = ({
setFilter,
Expand Down Expand Up @@ -150,6 +151,7 @@ const CheckboxGroupInput: FunctionComponent<
<FormControl
component="fieldset"
margin="normal"
error={touched && !!error}
{...sanitizeRestProps(rest)}
>
<FormLabel component="legend" className={classes.label}>
Expand All @@ -175,8 +177,15 @@ const CheckboxGroupInput: FunctionComponent<
/>
))}
</FormGroup>
{touched && error && <FormHelperText error>{error}</FormHelperText>}
{helperText && <FormHelperText>{helperText}</FormHelperText>}
{(touched && error) || helperText ? (
<FormHelperText>
<InputHelperText
touched={touched}
error={error}
helperText={helperText}
/>
</FormHelperText>
) : null}
</FormControl>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,9 @@ describe('<RadioButtonGroupInput />', () => {

fireEvent.blur(input);

expect(getByText('ra.validation.error')).toBeDefined();
const error = getByText('ra.validation.error');
expect(error).toBeDefined();
expect(error.classList.contains('Mui-error')).toEqual(true);
expect(queryByText('Can I help you?')).toBeNull();
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export const RadioButtonGroupInput: FunctionComponent<
<FormControl
component="fieldset"
margin="normal"
error={touched && !!error}
{...sanitizeRestProps(rest)}
>
<FormLabel component="legend" className={classes.label}>
Expand Down