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

[TextField] Fix label not being associated with native select #18141

Merged
merged 2 commits into from
Nov 1, 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
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"
Copy link
Member Author

@eps1lon eps1lon Nov 1, 2019

Choose a reason for hiding this comment

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

Otherwise we got duplicate ids. Including this here so that we recover lighthouse a11y score for this page completely (except contrast).

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 @@ -120,7 +120,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');
Copy link
Member Author

Choose a reason for hiding this comment

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

This was never correct. See A11yance/aria-query#24


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