Skip to content

Commit 4adaa70

Browse files
committed
[added] Accessibility: role 'alert' and aria-label to Alert component
1 parent 3423e70 commit 4adaa70

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/Alert.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ const Alert = React.createClass({
77

88
propTypes: {
99
onDismiss: React.PropTypes.func,
10-
dismissAfter: React.PropTypes.number
10+
dismissAfter: React.PropTypes.number,
11+
closeLabel: React.PropTypes.string
1112
},
1213

1314
getDefaultProps() {
1415
return {
1516
bsClass: 'alert',
16-
bsStyle: 'info'
17+
bsStyle: 'info',
18+
closeLabel: 'Close Alert'
1719
};
1820
},
1921

@@ -22,9 +24,9 @@ const Alert = React.createClass({
2224
<button
2325
type="button"
2426
className="close"
25-
onClick={this.props.onDismiss}
26-
aria-hidden="true">
27-
&times;
27+
aria-label={this.props.closeLabel}
28+
onClick={this.props.onDismiss}>
29+
<span aria-hidden="true">&times;</span>
2830
</button>
2931
);
3032
},
@@ -36,7 +38,7 @@ const Alert = React.createClass({
3638
classes['alert-dismissable'] = isDismissable;
3739

3840
return (
39-
<div {...this.props} className={classNames(this.props.className, classes)}>
41+
<div {...this.props} role='alert' className={classNames(this.props.className, classes)}>
4042
{isDismissable ? this.renderDismissButton() : null}
4143
{this.props.children}
4244
</div>

test/AlertSpec.js

+22
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,26 @@ describe('Alert', function () {
7171
);
7272
assert.ok(React.findDOMNode(instance).className.match(/\balert-danger\b/));
7373
});
74+
75+
describe('Web Accessibility', function(){
76+
it('Should have alert role', function () {
77+
let instance = ReactTestUtils.renderIntoDocument(
78+
<Alert>Message</Alert>
79+
);
80+
81+
assert.equal(React.findDOMNode(instance).getAttribute('role'), 'alert');
82+
});
83+
84+
it('Should have add ARIAs to button', function () {
85+
let instance = ReactTestUtils.renderIntoDocument(
86+
<Alert onDismiss={()=>{}} closeLabel='close'>Message</Alert>
87+
);
88+
89+
let button = ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'button');
90+
91+
assert.equal(React.findDOMNode(button).getAttribute('aria-label'), 'close');
92+
assert.equal(React.findDOMNode(button).children[0].getAttribute('aria-hidden'), 'true');
93+
});
94+
95+
});
7496
});

0 commit comments

Comments
 (0)