Commit 74dbe88f authored by Lyza Danger Gardner's avatar Lyza Danger Gardner

Set `aria-label` and `title` attrs only if different from displayed text

Only set `aria-label` and `title` attributes on `<button>` elements if
they are different from the text displayed in the button.
parent 02047ae4
...@@ -37,6 +37,13 @@ export default function Button({ ...@@ -37,6 +37,13 @@ export default function Button({
const baseClassName = buttonText ? 'button--labeled' : 'button--icon-only'; const baseClassName = buttonText ? 'button--labeled' : 'button--icon-only';
const extraProps = {}; const extraProps = {};
// If there is no displayed text in the button, or if the provided `title`
// differs from the displayed text, add `aria-label` and `title` attributes
if (!buttonText || title !== buttonText) {
extraProps.title = title;
extraProps['aria-label'] = title;
}
if (typeof isPressed === 'boolean') { if (typeof isPressed === 'boolean') {
// Indicate that this is a toggle button. // Indicate that this is a toggle button.
extraProps['aria-pressed'] = isPressed; extraProps['aria-pressed'] = isPressed;
...@@ -59,7 +66,6 @@ export default function Button({ ...@@ -59,7 +66,6 @@ export default function Button({
className className
)} )}
onClick={onClick} onClick={onClick}
title={title}
style={style} style={style}
disabled={disabled} disabled={disabled}
{...extraProps} {...extraProps}
......
...@@ -65,18 +65,48 @@ describe('Button', () => { ...@@ -65,18 +65,48 @@ describe('Button', () => {
assert.notProperty(wrapper.find('button').props(), 'aria-pressed'); assert.notProperty(wrapper.find('button').props(), 'aria-pressed');
}); });
it('sets `title` to provided `title` prop', () => { describe('`title` and `aria-label` attributes', () => {
const wrapper = createComponent({}); it('sets attrs to provided `title` prop', () => {
assert.equal(wrapper.find('button').prop('title'), 'My Action'); const wrapper = createComponent({});
}); assert.equal(wrapper.find('button').prop('title'), 'My Action');
assert.equal(wrapper.find('button').prop('aria-label'), 'My Action');
});
it('uses `buttonText` to set `title` attr if `title` missing', () => { it('sets attrs if `title` is different than `buttonText`', () => {
const wrapper = createComponent({ const wrapper = createComponent({
buttonText: 'My Label', buttonText: 'My Label',
title: undefined, title: 'Click here to do something',
});
assert.equal(
wrapper.find('button').prop('title'),
'Click here to do something'
);
assert.equal(
wrapper.find('button').prop('aria-label'),
'Click here to do something'
);
}); });
assert.equal(wrapper.find('button').prop('title'), 'My Label'); it('does not set attrs if no `title` provided and `buttonText` present', () => {
const wrapper = createComponent({
buttonText: 'My Label',
title: undefined,
});
assert.notProperty(wrapper.find('button').props(), 'title');
assert.notProperty(wrapper.find('button').props(), 'aria-label');
});
it('does not set attrs if `title` is the same as `buttonText`', () => {
const wrapper = createComponent({
buttonText: 'My Label',
title: 'My Label',
});
assert.notProperty(wrapper.find('button').props(), 'title');
assert.notProperty(wrapper.find('button').props(), 'aria-label');
});
}); });
it('invokes `onClick` callback when pressed', () => { it('invokes `onClick` callback when pressed', () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment