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

fix(hotfix/5.135.6): Add ariaLabel prop for ChoiceGroupOption #32900

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "office-ui-fabric-react",
"comment": "Add ariaLabel prop for ChoiceGroupOption.",
"type": "patch"
}
],
"packageName": "office-ui-fabric-react",
"email": "jiagao@microsoft.com"
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,27 @@ describe('ChoiceGroup', () => {
expect(extraAttributeGetter(0)).toEqual('auto1');
expect(extraAttributeGetter(1)).toBeNull();
});

it('can assign a custom aria label', () => {
const option4: IChoiceGroupOption[] = [{ key: '4', text: '4', ariaLabel: 'Custom aria label' }];
const choiceGroup = ReactTestUtils.renderIntoDocument<ChoiceGroup>(
<ChoiceGroup
label='testgroup'
options={ TEST_OPTIONS.concat(option4) }
required={ true }
/>
);

const renderedDOM = ReactDOM.findDOMNode(choiceGroup as React.ReactInstance) as Element;
const choiceOptions = renderedDOM.querySelectorAll(QUERY_SELECTOR);

expect(choiceOptions.length).toBe(4);

expect((choiceOptions[0] as HTMLInputElement).getAttribute('aria-label')).toBeNull();
expect((choiceOptions[1] as HTMLInputElement).getAttribute('aria-label')).toBeNull();
expect((choiceOptions[2] as HTMLInputElement).getAttribute('aria-label')).toBeNull();

expect((choiceOptions[3] as HTMLInputElement).getAttribute('aria-labelledby')).toBeNull();
expect((choiceOptions[3] as HTMLInputElement).getAttribute('aria-label')).toEqual('Custom aria label');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class ChoiceGroup extends BaseComponent<IChoiceGroupProps, IChoiceGroupSt
private _labelId: string;
private _inputElement = createRef<HTMLInputElement>();

constructor(props: IChoiceGroupProps, ) {
constructor(props: IChoiceGroupProps,) {
super(props);

this._warnDeprecations({ 'onChanged': 'onChange' });
Expand Down Expand Up @@ -122,7 +122,8 @@ export class ChoiceGroup extends BaseComponent<IChoiceGroupProps, IChoiceGroupSt
onChange={ this._onChange.bind(this, option) }
onFocus={ this._onFocus.bind(this, option) }
onBlur={ this._onBlur.bind(this, option) }
aria-labelledby={ option.labelId }
aria-labelledby={ option.ariaLabel ? undefined : option.labelId }
aria-label={ option.ariaLabel }
{ ...getNativeProps(option, inputProperties) }
/>
{ onRenderField(option, this._onRenderField) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,9 @@ export interface IChoiceGroupOption extends React.HTMLAttributes<HTMLElement | H
* This value is maintained by the component and is accessible during onRenderField
*/
labelId?: string;

/**
* The aria label of the ChoiceGroupOption for the benefit of screen readers.
*/
ariaLabel?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ exports[`ChoiceGroup renders ChoiceGroup correctly 1`] = `
className="ms-ChoiceField-wrapper"
>
<input
aria-label={undefined}
aria-labelledby="ChoiceGroupLabel1-1"
checked={false}
className="ms-ChoiceField-input"
Expand Down Expand Up @@ -53,6 +54,7 @@ exports[`ChoiceGroup renders ChoiceGroup correctly 1`] = `
className="ms-ChoiceField-wrapper"
>
<input
aria-label={undefined}
aria-labelledby="ChoiceGroupLabel1-2"
checked={false}
className="ms-ChoiceField-input"
Expand Down Expand Up @@ -85,6 +87,7 @@ exports[`ChoiceGroup renders ChoiceGroup correctly 1`] = `
className="ms-ChoiceField-wrapper"
>
<input
aria-label={undefined}
aria-labelledby="ChoiceGroupLabel1-3"
checked={false}
className="ms-ChoiceField-input"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class ChoiceGroupBasicExample extends React.Component<{}, IChoiceGroupBas
{
key: 'B',
text: 'Option B',
ariaLabel: 'Custom aria-label'
},
{
key: 'C',
Expand Down