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

Allow SelectArrayInput to be variant=outlined #4511

Merged
merged 4 commits into from
May 7, 2020
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
19 changes: 6 additions & 13 deletions packages/ra-ui-materialui/src/input/SelectArrayInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,14 @@ describe('<SelectArrayInput />', () => {
});

it('should use the input parameter value as the initial input value', () => {
const { getByLabelText } = render(
const { getByDisplayValue } = render(
<Form
initialValues={{ categories: ['programming', 'lifestyle'] }}
onSubmit={jest.fn()}
render={() => <SelectArrayInput {...defaultProps} />}
/>
);
const input = getByLabelText(
'resources.posts.fields.categories'
) as HTMLInputElement;
expect(input.value).toBe('programming,lifestyle');
expect(getByDisplayValue('programming,lifestyle')).not.toBeNull();
});

it('should reveal choices on click', () => {
Expand All @@ -60,7 +57,7 @@ describe('<SelectArrayInput />', () => {
});

it('should use optionValue as value identifier', () => {
const { getByRole, getByText, getByLabelText } = render(
const { getByRole, getByText, getByDisplayValue } = render(
<Form
onSubmit={jest.fn()}
render={() => (
Expand All @@ -76,13 +73,11 @@ describe('<SelectArrayInput />', () => {
);
fireEvent.mouseDown(getByRole('button'));
fireEvent.click(getByText('Programming'));
expect(getByLabelText('resources.posts.fields.categories').value).toBe(
'programming'
);
expect(getByDisplayValue('programming')).not.toBeNull();
});

it('should use optionValue including "." as value identifier', () => {
const { getByRole, getByText, getByLabelText } = render(
const { getByRole, getByText, getByDisplayValue } = render(
<Form
onSubmit={jest.fn()}
render={() => (
Expand All @@ -101,9 +96,7 @@ describe('<SelectArrayInput />', () => {
);
fireEvent.mouseDown(getByRole('button'));
fireEvent.click(getByText('Programming'));
expect(getByLabelText('resources.posts.fields.categories').value).toBe(
'programming'
);
expect(getByDisplayValue('programming')).not.toBeNull();
});

it('should use optionText with a string value as text identifier', () => {
Expand Down
34 changes: 17 additions & 17 deletions packages/ra-ui-materialui/src/input/SelectArrayInput.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import React, { FunctionComponent, useCallback } from 'react';
import React, {
FunctionComponent,
useCallback,
useRef,
useState,
useEffect,
} from 'react';
import PropTypes from 'prop-types';
import {
makeStyles,
Select,
MenuItem,
InputLabel,
Input,
FilledInput,
FormHelperText,
FormControl,
Chip,
Expand Down Expand Up @@ -161,14 +165,18 @@ const SelectArrayInput: FunctionComponent<
...rest
} = props;
const classes = useStyles(props);
const inputLabel = useRef(null);
const [labelWidth, setLabelWidth] = useState(0);
useEffect(() => {
setLabelWidth(inputLabel.current.offsetWidth);
Copy link
Contributor

Choose a reason for hiding this comment

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

inputLabel.current can be null at mounting. Can you add a check?

Copy link
Member

Choose a reason for hiding this comment

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

I don't think so. the effect will run after the first render, and at that time, the ref will be defined.

}, []);

const { getChoiceText, getChoiceValue } = useChoices({
optionText,
optionValue,
translateChoice,
});

const {
id,
input,
isRequired,
meta: { error, touched },
Expand Down Expand Up @@ -201,7 +209,6 @@ const SelectArrayInput: FunctionComponent<
},
[getChoiceValue, renderMenuItemOption]
);

return (
<FormControl
margin={margin}
Expand All @@ -211,9 +218,8 @@ const SelectArrayInput: FunctionComponent<
{...sanitizeRestProps(rest)}
>
<InputLabel
htmlFor={id}
shrink
variant={variant}
ref={inputLabel}
id={`${label}-outlined-label`}
error={touched && !!error}
>
<FieldTitle
Expand All @@ -225,14 +231,8 @@ const SelectArrayInput: FunctionComponent<
</InputLabel>
<Select
autoWidth
labelId={`${label}-outlined-label`}
multiple
input={
variant === 'standard' ? (
<Input id={id} />
) : (
<FilledInput id={id} />
)
}
error={!!(touched && error)}
renderValue={(selected: any[]) => (
<div className={classes.chips}>
Expand All @@ -252,10 +252,10 @@ const SelectArrayInput: FunctionComponent<
</div>
)}
data-testid="selectArray"
variant={variant}
{...input}
value={input.value || []}
{...options}
labelWidth={labelWidth}
>
{choices.map(renderMenuItem)}
</Select>
Expand Down