Skip to content

Commit

Permalink
Update accessibility for button groups
Browse files Browse the repository at this point in the history
  • Loading branch information
cchaos committed Jan 16, 2019
1 parent a3a9a3c commit e11e4de
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 43 deletions.
7 changes: 7 additions & 0 deletions src-docs/src/views/button/button_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
EuiCode,
EuiButtonGroup,
EuiButtonToggle,
EuiCallOut,
} from '../../../../src/components';

import Button from './button';
Expand Down Expand Up @@ -251,6 +252,12 @@ export const ButtonExample = {
(default) or <EuiCode>&quot;primary&quot;</EuiCode>. If your just displaying a group of
icons, add the prop <EuiCode>isIconOnly</EuiCode>.
</p>
<EuiCallOut title="Accessibility">
<p>
In order for groups to be properly read as groups with a title, add the <EuiCode>legend</EuiCode> prop.
This is only for accessiblity, however, so it will be visibly hidden.
</p>
</EuiCallOut>
</div>
),
demo: <ButtonGroup />,
Expand Down
21 changes: 16 additions & 5 deletions src-docs/src/views/button/button_group.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default class extends Component {
this.toggleButtons = [{
id: `${idPrefix}0`,
label: 'Option one',
isDisabled: true,
}, {
id: `${idPrefix}1`,
label: 'Option two is selected by default',
Expand All @@ -33,6 +34,7 @@ export default class extends Component {
this.toggleButtonsMulti = [{
id: `${idPrefix2}0`,
label: 'Option 1',
isDisabled: true,
}, {
id: `${idPrefix2}1`,
label: 'Option 2 is selected by default',
Expand All @@ -45,6 +47,7 @@ export default class extends Component {
id: `${idPrefix3}0`,
label: 'Align left',
iconType: 'editorAlignLeft',
isDisabled: true,
}, {
id: `${idPrefix3}1`,
label: 'Align center',
Expand All @@ -58,18 +61,23 @@ export default class extends Component {
this.toggleButtonsIconsMulti = [{
id: `${idPrefix3}3`,
label: 'Bold',
name: 'bold',
iconType: 'editorBold',
isDisabled: true,
}, {
id: `${idPrefix3}4`,
label: 'Italic',
name: 'italic',
iconType: 'editorItalic',
}, {
id: `${idPrefix3}5`,
label: 'Underline',
name: 'underline',
iconType: 'editorUnderline',
}, {
id: `${idPrefix3}6`,
label: 'Strikethrough',
name: 'strikethrough',
iconType: 'editorStrike',
}];

Expand Down Expand Up @@ -119,7 +127,7 @@ export default class extends Component {
return (
<Fragment>
<EuiButtonGroup
name="Basic"
legend="This is a basic group"
options={this.toggleButtons}
idSelected={this.state.toggleIdSelected}
onChange={this.onChange}
Expand All @@ -132,7 +140,8 @@ export default class extends Component {
<EuiSpacer size="s" />

<EuiButtonGroup
name="Primary"
legend="This is a primary group"
name="primary"
options={this.toggleButtonsMulti}
idToSelectedMap={this.state.toggleIdToSelectedMap}
onChange={this.onChangeMulti}
Expand All @@ -147,7 +156,8 @@ export default class extends Component {
<EuiSpacer size="s" />

<EuiButtonGroup
name="Disabled"
legend="This is a disabled group"
name="disabledGroup"
options={this.toggleButtons}
idSelected={this.state.toggleIdSelected}
onChange={this.onChange}
Expand All @@ -162,7 +172,8 @@ export default class extends Component {
<EuiSpacer size="s" />

<EuiButtonGroup
name="Text align"
legend="Text align"
name="textAlign"
className="eui-displayInlineBlock"
options={this.toggleButtonsIcons}
idSelected={this.state.toggleIconIdSelected}
Expand All @@ -173,7 +184,7 @@ export default class extends Component {
&nbsp;&nbsp;

<EuiButtonGroup
name="Text style"
legend="Text style"
className="eui-displayInlineBlock"
options={this.toggleButtonsIconsMulti}
idToSelectedMap={this.state.toggleIconIdToSelectedMap}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`EuiButtonGroup is rendered 1`] = `
<div
aria-label="aria-label"
class="euiButtonGroup testClass1 testClass2"
data-test-subj="test subject string"
/>
<fieldset>
<div
aria-label="aria-label"
class="euiButtonGroup testClass1 testClass2"
data-test-subj="test subject string"
/>
</fieldset>
`;
83 changes: 50 additions & 33 deletions src/components/button/button_group/button_group.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';

import { EuiScreenReaderOnly } from '../../accessibility';
import { EuiButtonToggle } from '../button_toggle';
import { TOGGLE_TYPES } from '../../toggle';

Expand All @@ -15,6 +16,7 @@ export const EuiButtonGroup = ({
isFullWidth,
isIconOnly,
name,
legend,
onChange,
options,
type,
Expand All @@ -29,40 +31,50 @@ export const EuiButtonGroup = ({
className,
);

let legendNode;
if (legend) {
legendNode = (
<EuiScreenReaderOnly><legend>{legend}</legend></EuiScreenReaderOnly>
);
}

return (
<div className={classes} {...rest}>
{options.map((option, index) => {

let isSelectedState;
if (type === 'multi') {
isSelectedState = idToSelectedMap[option.id] || false;
} else {
isSelectedState = option.id === idSelected;
}

return (
<EuiButtonToggle
className="euiButtonGroup__button"
color={color}
fill={isSelectedState}
iconSide={option.iconSide}
iconType={option.iconType}
id={option.id}
isDisabled={isDisabled || option.isDisabled}
isIconOnly={isIconOnly}
isSelected={isSelectedState}
key={index}
label={option.label}
name={name}
onChange={onChange.bind(null, option.id, option.value)}
size={buttonSize}
toggleClassName="euiButtonGroup__toggle"
type={type}
value={option.value}
/>
);
})}
</div>
<fieldset>
{legendNode}

<div className={classes} {...rest}>
{options.map((option, index) => {
let isSelectedState;
if (type === 'multi') {
isSelectedState = idToSelectedMap[option.id] || false;
} else {
isSelectedState = option.id === idSelected;
}

return (
<EuiButtonToggle
className="euiButtonGroup__button"
color={color}
fill={isSelectedState}
iconSide={option.iconSide}
iconType={option.iconType}
id={option.id}
isDisabled={isDisabled || option.isDisabled}
isIconOnly={isIconOnly}
isSelected={isSelectedState}
key={index}
label={option.label}
name={option.name || name}
onChange={onChange.bind(null, option.id, option.value)}
size={buttonSize}
toggleClassName="euiButtonGroup__toggle"
type={type}
value={option.value}
/>
);
})}
</div>
</fieldset>
);
};

Expand Down Expand Up @@ -112,6 +124,11 @@ EuiButtonGroup.propTypes = {
* Map of ids of selected options for `type="multi"`
*/
idToSelectedMap: PropTypes.objectOf(PropTypes.bool),

/**
* Adds a hidden legend to the group for accessiblity
*/
legend: PropTypes.node,
};

EuiButtonGroup.defaultProps = {
Expand Down

0 comments on commit e11e4de

Please sign in to comment.