Skip to content

Commit 631d57b

Browse files
committed
Merge pull request react-bootstrap#1237 from pivotal-cf/add-accessible-close-button-to-alerts
[fixed] screen-reader accessible dismiss button on alerts
2 parents 5f0ce27 + 533530a commit 631d57b

3 files changed

Lines changed: 27 additions & 10 deletions

File tree

docs/src/ComponentsPage.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,11 @@ const ComponentsPage = React.createClass({
623623
<p>just pass in a <code>onDismiss</code> function.</p>
624624
<ReactPlayground codeText={Samples.AlertDismissable} />
625625

626+
<div className="bs-callout bs-callout-info">
627+
<h4>Screen Reader Accessibility</h4>
628+
<p>Unlike regular Bootstrap, alerts have an sr-only dismiss button after the content.</p>
629+
</div>
630+
626631
<h4><Anchor id="alerts-auto-closeable">Auto closeable</Anchor></h4>
627632
<p>Auto close after a set time with <code>dismissAfter</code> prop.</p>
628633
<ReactPlayground codeText={Samples.AlertAutoDismissable} />

src/Alert.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,20 @@ const Alert = React.createClass({
2424
<button
2525
type="button"
2626
className="close"
27-
aria-label={this.props.closeLabel}
27+
onClick={this.props.onDismiss}
28+
aria-hidden="true">
29+
<span>&times;</span>
30+
</button>
31+
);
32+
},
33+
34+
renderSrOnlyDismissButton() {
35+
return (
36+
<button
37+
type="button"
38+
className="close sr-only"
2839
onClick={this.props.onDismiss}>
29-
<span aria-hidden="true">&times;</span>
40+
{this.props.closeLabel}
3041
</button>
3142
);
3243
},
@@ -41,6 +52,7 @@ const Alert = React.createClass({
4152
<div {...this.props} role="alert" className={classNames(this.props.className, classes)}>
4253
{isDismissable ? this.renderDismissButton() : null}
4354
{this.props.children}
55+
{isDismissable ? this.renderSrOnlyDismissButton() : null}
4456
</div>
4557
);
4658
},

test/AlertSpec.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,16 @@ describe('Alert', function () {
8181
assert.equal(React.findDOMNode(instance).getAttribute('role'), 'alert');
8282
});
8383

84-
it('Should have add ARIAs to button', function () {
84+
it('Should call onDismiss callback when the sr-only dismiss link is activated', function(done) {
85+
let doneOp = function () {
86+
done();
87+
};
8588
let instance = ReactTestUtils.renderIntoDocument(
86-
<Alert onDismiss={()=>{}} closeLabel='close'>Message</Alert>
89+
<Alert onDismiss={doneOp}>
90+
Message
91+
</Alert>
8792
);
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+
ReactTestUtils.Simulate.click(React.findDOMNode(instance).getElementsByClassName('sr-only')[0]);
9394
});
94-
9595
});
9696
});

0 commit comments

Comments
 (0)