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

[ButtonBase] Fix an accessibility issue #5656

Merged
merged 1 commit into from
Nov 26, 2016
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
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
"keycode": "^2.1.1",
"lodash": "^4.17.2",
"object-assign": "^4.1.0",
"react-addons-create-fragment": "^15.4.0",
"react-addons-transition-group": "^15.4.0",
"react-event-listener": "^0.4.0",
"recompose": "^0.20.2",
Expand Down
44 changes: 21 additions & 23 deletions src/internal/ButtonBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import React, { Component, PropTypes } from 'react';
import ReactDOM from 'react-dom';
import createFragment from 'react-addons-create-fragment';
import { createStyleSheet } from 'jss-theme-reactor';
import classNames from 'classnames';
import keycode from 'keycode';
Expand Down Expand Up @@ -54,7 +53,14 @@ export default class ButtonBase extends Component {
*/
className: PropTypes.string,
component: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
/**
* If `true`, the base button will be disabled.
*/
disabled: PropTypes.bool,
/**
* If `true`, the base button will have a keyboard focus ripple.
* `ripple` must also be true.
*/
focusRipple: PropTypes.bool,
keyboardFocusedClassName: PropTypes.string,
onBlur: PropTypes.func,
Expand All @@ -68,6 +74,9 @@ export default class ButtonBase extends Component {
onMouseUp: PropTypes.func,
onTouchEnd: PropTypes.func,
onTouchStart: PropTypes.func,
/**
* If `true`, the base button will have a ripple.
*/
ripple: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
role: PropTypes.string,
tabIndex: PropTypes.string,
Expand Down Expand Up @@ -211,14 +220,7 @@ export default class ButtonBase extends Component {
children,
className: classNameProp,
component,
/**
* If `true`, the base button will be disabled.
*/
disabled,
/**
* If `true`, the base button will have a keyboard focus ripple.
* Ripple must also be true.
*/
focusRipple, // eslint-disable-line no-unused-vars
keyboardFocusedClassName,
onBlur, // eslint-disable-line no-unused-vars
Expand All @@ -230,10 +232,8 @@ export default class ButtonBase extends Component {
onMouseUp, // eslint-disable-line no-unused-vars
onTouchEnd, // eslint-disable-line no-unused-vars
onTouchStart, // eslint-disable-line no-unused-vars
/**
* If `true`, the base button will have a ripple.
*/
ripple,
tabIndex,
type,
...other
} = this.props;
Expand All @@ -256,31 +256,29 @@ export default class ButtonBase extends Component {
onMouseUp: this.handleMouseUp,
onTouchEnd: this.handleTouchEnd,
onTouchStart: this.handleTouchStart,
tabIndex: this.props.tabIndex,
tabIndex: disabled ? '-1' : tabIndex,
className,
...other,
};

let element = component;
let Element = component;

if (other.href) {
element = 'a';
Element = 'a';
}

if (element === 'button') {
if (Element === 'button') {
buttonProps.type = type;
buttonProps.disabled = disabled;
} else if (element !== 'a') {
} else if (Element !== 'a') {
buttonProps.role = this.props.hasOwnProperty('role') ? this.props.role : 'button';
}

return React.createElement(
element,
buttonProps,
createFragment({
children,
ripple: this.renderRipple(ripple, centerRipple),
}),
return (
<Element {...buttonProps}>
{children}
{this.renderRipple(ripple, centerRipple)}
</Element>
);
}
}
7 changes: 7 additions & 0 deletions src/internal/ButtonBase.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,11 @@ describe('<ButtonBase />', () => {
}, 200);
});
});

describe('prop: disabled', () => {
it('should apply the right tabIndex', () => {
const wrapper = shallow(<ButtonBase disabled>Hello</ButtonBase>);
assert.strictEqual(wrapper.props().tabIndex, '-1', 'should not receive the focus');
});
});
});
2 changes: 0 additions & 2 deletions test/regressions/site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
name="viewport"
content="width=device-width, initial-scale=1, user-scalable=0, maximum-scale=1, minimum-scale=1"
>
<link href="https://fonts.googleapis.com/css?family=Roboto:400,500,700,400italic" rel="stylesheet" type="text/css">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
</head>
<body>
Copy link
Member Author

@oliviertassinari oliviertassinari Nov 26, 2016

Choose a reason for hiding this comment

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

That was making the tests flaky. I hate that 💣 .
cc @pradel in case you were wondering what was going on with your latest PR 🔴 😁 .

Copy link
Member

Choose a reason for hiding this comment

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

@oliviertassinari ohh that's annoying. I saw that happening too.

FOUC! lol.

We could always delay until the font is loaded... or load it locally even. WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

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

We could always delay until the font is loaded

Yes, that's exactly what I'm doing. Oh, I haven't though about having them localy. We would have to keep them up to date.

<div id="app"></div>
Expand Down
3 changes: 2 additions & 1 deletion test/regressions/site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"webpack-dev-server": "^1.16.2"
},
"dependencies": {
"react-router": "^3.0.0"
"react-router": "^3.0.0",
"webfontloader": "^1.6.26"
}
}
23 changes: 17 additions & 6 deletions test/regressions/site/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,27 @@ import { AppContainer } from 'react-hot-loader';
import RedBox from 'redbox-react';
import React from 'react';
import { render } from 'react-dom';
import webFont from 'webfontloader';
import App from './App';

const rootEl = document.getElementById('app');

render(
<AppContainer errorReporter={RedBox}>
<App />
</AppContainer>,
rootEl,
);
webFont.load({
google: {
families: [
'Roboto:400,500,700,400italic',
'Material+Icons',
],
},
active: () => {
render(
<AppContainer errorReporter={RedBox}>
<App />
</AppContainer>,
rootEl,
);
},
});

if (module.hot) {
module.hot.accept('./App', () => {
Expand Down