Skip to content

Commit

Permalink
[TextField] Fix label not being associated with native select (#18141)
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon authored Nov 1, 2019
1 parent 13b3a0d commit 54d1603
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
6 changes: 3 additions & 3 deletions docs/src/pages/components/text-fields/ValidationTextFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default function ValidationTextFields() {
/>
<TextField
error
id="standard-error"
id="standard-error-helper-text"
label="Error"
defaultValue="Hello World"
helperText="Incorrect entry."
Expand All @@ -50,7 +50,7 @@ export default function ValidationTextFields() {
/>
<TextField
error
id="filled-error"
id="filled-error-helper-text"
label="Error"
defaultValue="Hello World"
helperText="Incorrect entry."
Expand All @@ -71,7 +71,7 @@ export default function ValidationTextFields() {
/>
<TextField
error
id="outlined-error"
id="outlined-error-helper-text"
label="Error"
defaultValue="Hello World"
helperText="Incorrect entry."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default function ValidationTextFields() {
/>
<TextField
error
id="standard-error"
id="standard-error-helper-text"
label="Error"
defaultValue="Hello World"
helperText="Incorrect entry."
Expand All @@ -52,7 +52,7 @@ export default function ValidationTextFields() {
/>
<TextField
error
id="filled-error"
id="filled-error-helper-text"
label="Error"
defaultValue="Hello World"
helperText="Incorrect entry."
Expand All @@ -73,7 +73,7 @@ export default function ValidationTextFields() {
/>
<TextField
error
id="outlined-error"
id="outlined-error-helper-text"
label="Error"
defaultValue="Hello World"
helperText="Incorrect entry."
Expand Down
4 changes: 3 additions & 1 deletion packages/material-ui/src/TextField/TextField.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ const TextField = React.forwardRef(function TextField(props, ref) {
}
if (select) {
// unset defaults from textbox inputs
InputMore.id = undefined;
if (!SelectProps || !SelectProps.native) {
InputMore.id = undefined;
}
InputMore['aria-describedby'] = undefined;
}

Expand Down
25 changes: 20 additions & 5 deletions packages/material-ui/src/TextField/TextField.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ describe('<TextField />', () => {
});

describe('prop: select', () => {
it('should be able to render a select as expected', () => {
it('can render a <select /> when `native`', () => {
const currencies = [{ value: 'USD', label: '$' }, { value: 'BTC', label: '฿' }];

const { getByRole } = render(
const { container } = render(
<TextField select SelectProps={{ native: true }}>
{currencies.map(option => (
<option key={option.value} value={option.value}>
Expand All @@ -130,10 +130,25 @@ describe('<TextField />', () => {
</TextField>,
);

const select = getByRole('listbox');

const select = container.querySelector('select');
expect(select).to.be.ok;
expect(select.querySelectorAll('option')).to.have.lengthOf(2);
expect(select.options).to.have.lengthOf(2);
});

it('associates the label with the <select /> when `native={true}` and `id`', () => {
const { getByLabelText } = render(
<TextField
label="Currency:"
id="labelled-select"
select
SelectProps={{ native: true }}
value="$"
>
<option value="dollar">$</option>
</TextField>,
);

expect(getByLabelText('Currency:')).to.have.property('value', 'dollar');
});

it('renders a combobox with the appropriate accessible name', () => {
Expand Down

0 comments on commit 54d1603

Please sign in to comment.