Skip to content
Open
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
3 changes: 3 additions & 0 deletions packages/react-select/src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ type FormatOptionLabelMeta = {
};

export type Props = {
Copy link

@kylemh kylemh Dec 8, 2020

Choose a reason for hiding this comment

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

An alternative suggestion:

import { AriaAttributes } from 'react';

// ...

// aria attributes makes the JSX "noisy", separated for clarity
const ariaAttributes = {
  'aria-autocomplete': 'list',
  'aria-describedby': this.props['aria-describedby'],
  'aria-label': this.props['aria-label'],
  'aria-labelledby': this.props['aria-labelledby'],
  'aria-required': this.props['aria-required'],
  'aria-invalid': this.props['aria-invalid'],
};

export type Props = Pick<AriaAttributes, keyof typeof ariaAttributes> & {
  // ... the other props
};
type Demo = keyof typeof ariaAttributes;
// Demo --> 'aria-autocomplete' | 'aria-describedby' | 'aria-label' | 'aria-labelledby'

/* HTML ID of an element that should be used to describe this input (for assistive tech) */
Copy link

Choose a reason for hiding this comment

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

This and labelledby should actually not have language in the comments that suggests a single element, as both of those props accept a separated LIST of IDs.

https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-labelledby_attribute

A space-separated list of element IDs

Suggested change
/* HTML ID of an element that should be used to describe this input (for assistive tech) */
/* A space-separated list of element IDs that should be used to describe this input (for assistive tech) */

'aria-describedby'?: string,
/* Aria label (for assistive tech) */
'aria-label'?: string,
/* HTML ID of an element that should be used as the label (for assistive tech) */
Copy link

Choose a reason for hiding this comment

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

Suggested change
/* HTML ID of an element that should be used as the label (for assistive tech) */
/* A space-separated list of element IDs that should be used as the label (for assistive tech) */

Expand Down Expand Up @@ -1422,6 +1424,7 @@ export default class Select extends Component<Props, State> {
// aria attributes makes the JSX "noisy", separated for clarity
const ariaAttributes = {
'aria-autocomplete': 'list',
'aria-describedby': this.props['aria-describedby'],
'aria-label': this.props['aria-label'],
'aria-labelledby': this.props['aria-labelledby'],
};
Expand Down
22 changes: 22 additions & 0 deletions packages/react-select/src/__tests__/Select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,28 @@ cases(
}
);

cases(
'accessibility > passes through aria-describedby prop',
({ props = { ...BASIC_PROPS, 'aria-describedby': 'testing' } }) => {
let { container } = render(<Select {...props} />);
expect(
container
.querySelector('.react-select__input input')
.getAttribute('aria-describedby')
).toBe('testing');
},
{
'single select > should pass aria-describedby prop down to input': {},
'multi select > should pass aria-describedby prop down to input': {
props: {
...BASIC_PROPS,
'aria-describedby': 'testing',
isMulti: true,
},
},
}
);

cases(
'accessibility > passes through aria-labelledby prop',
({ props = { ...BASIC_PROPS, 'aria-labelledby': 'testing' } }) => {
Expand Down