-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react-alerts): create react alerts
- React alerts can be dismissable (adds a close button) - Close button has optional callback - React alerts can have icons [Finishes #85646924] Signed-off-by: Nicole Sullivan <nsullivan@pivotal.io>
- Loading branch information
Geoff Pleiss
committed
Jan 14, 2015
1 parent
f50c235
commit 265e99d
Showing
4 changed files
with
364 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
'use strict'; | ||
|
||
var React = require('react'); | ||
var BsAlert = require('react-bootstrap/Alert'); | ||
var Media = require('./media.jsx').Media; | ||
|
||
var Alert = React.createClass({ | ||
getInitialState: function() { | ||
return { | ||
alertVisible: true | ||
}; | ||
}, | ||
|
||
render: function() { | ||
if (this.state.alertVisible) { | ||
var onDismiss = this.props.dismissable ? this.handleAlertDismiss : null; | ||
|
||
var children; | ||
|
||
if (this.props.withIcon) { | ||
var icon = <i className={"fa " + this.props.alertIcon}></i>; | ||
children = ( | ||
<Media leftImage={icon}> | ||
{this.props.children} | ||
</Media> | ||
); | ||
} else { | ||
children = this.props.children; | ||
} | ||
return ( | ||
<BsAlert onDismiss={onDismiss} {...this.props }> | ||
{children} | ||
</BsAlert> | ||
); | ||
} | ||
|
||
return ( | ||
<span /> | ||
); | ||
}, | ||
|
||
handleAlertDismiss: function() { | ||
if (typeof this.props.dismissable === "function") { | ||
this.props.dismissable(); | ||
} | ||
this.setState({alertVisible: false}); | ||
} | ||
}); | ||
|
||
var SuccessAlert = React.createClass({ | ||
render: function() { | ||
return ( | ||
<Alert bsStyle="success" alertIcon="fa-check-circle" {...this.props} /> | ||
); | ||
} | ||
}); | ||
|
||
var InfoAlert = React.createClass({ | ||
render: function() { | ||
return ( | ||
<Alert bsStyle="info" alertIcon="fa-info-circle" {...this.props } /> | ||
); | ||
} | ||
}); | ||
|
||
var WarningAlert = React.createClass({ | ||
render: function() { | ||
return ( | ||
<Alert bsStyle="warning" alertIcon="fa-exclamation-triangle" {...this.props } /> | ||
); | ||
} | ||
}); | ||
|
||
var ErrorAlert = React.createClass({ | ||
render: function() { | ||
return ( | ||
<Alert bsStyle="danger" alertIcon="fa-exclamation-triangle" {...this.props } /> | ||
); | ||
} | ||
}); | ||
|
||
module.exports = { | ||
SuccessAlert: SuccessAlert, | ||
InfoAlert: InfoAlert, | ||
WarningAlert: WarningAlert, | ||
ErrorAlert: ErrorAlert | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
'use strict'; | ||
|
||
var $ = require('jquery'); | ||
var React = require('react/addons'); | ||
var TestUtils = React.addons.TestUtils; | ||
|
||
var SuccessAlert = require('../../../src/pivotal-ui/javascripts/alerts.jsx').SuccessAlert; | ||
var InfoAlert = require('../../../src/pivotal-ui/javascripts/alerts.jsx').InfoAlert; | ||
var WarningAlert = require('../../../src/pivotal-ui/javascripts/alerts.jsx').WarningAlert; | ||
var ErrorAlert = require('../../../src/pivotal-ui/javascripts/alerts.jsx').ErrorAlert; | ||
|
||
describe('Alert', function() { | ||
beforeEach(function() { | ||
this.node = $('<div id="container"></div>').appendTo('body').get(0); | ||
}); | ||
|
||
afterEach(function() { | ||
React.unmountComponentAtNode(this.node); | ||
document.body.removeChild(this.node); | ||
}); | ||
|
||
describe('when dismissable is set to true', function() { | ||
beforeEach(function() { | ||
React.render( | ||
<SuccessAlert dismissable>alert body</SuccessAlert>, | ||
this.node | ||
); | ||
}); | ||
|
||
it('has a close button', function() { | ||
expect($('#container button.close')).toExist(); | ||
}); | ||
|
||
it('disappears when close button is clicked', function() { | ||
TestUtils.Simulate.click($('#container button.close').get(0)); | ||
expect($('#container .alert')).not.toExist(); | ||
}); | ||
}); | ||
|
||
describe('when dismissable is set to a callback', function() { | ||
beforeEach(function() { | ||
this.callback = jasmine.createSpy('dismissable callback'); | ||
React.render( | ||
<SuccessAlert dismissable={this.callback}>alert body</SuccessAlert>, | ||
this.node | ||
); | ||
}); | ||
|
||
it('has a close button', function() { | ||
expect($('#container button.close')).toExist(); | ||
}); | ||
|
||
describe('when close button is clicked', function() { | ||
beforeEach(function() { | ||
TestUtils.Simulate.click($('#container button.close').get(0)); | ||
}); | ||
|
||
it('disappears', function() { | ||
expect($('#container .alert')).not.toExist(); | ||
}); | ||
|
||
it('calls the callback passed in', function() { | ||
expect(this.callback).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when dismissable is not present', function() { | ||
beforeEach(function() { | ||
React.render( | ||
<SuccessAlert>alert body</SuccessAlert>, | ||
this.node | ||
); | ||
}); | ||
|
||
it('does not have a close button', function() { | ||
expect($('#container button.close')).not.toExist(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('SuccessAlert', function() { | ||
describe('when withIcon is set to true', function() { | ||
beforeEach(function() { | ||
this.node = $('<div id="container"></div>').appendTo('body').get(0); | ||
React.render( | ||
<SuccessAlert withIcon>alert body</SuccessAlert>, | ||
this.node | ||
); | ||
}); | ||
|
||
afterEach(function() { | ||
React.unmountComponentAtNode(this.node); | ||
document.body.removeChild(this.node); | ||
}); | ||
|
||
it('renders an icon in the alert', function() { | ||
expect($('#container i')).toHaveClass('fa-check-circle'); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('InfoAlert', function() { | ||
describe('when withIcon is set to true', function() { | ||
beforeEach(function() { | ||
this.node = $('<div id="container"></div>').appendTo('body').get(0); | ||
React.render( | ||
<InfoAlert withIcon>alert body</InfoAlert>, | ||
this.node | ||
); | ||
}); | ||
|
||
afterEach(function() { | ||
React.unmountComponentAtNode(this.node); | ||
document.body.removeChild(this.node); | ||
}); | ||
|
||
it('renders an icon in the alert', function() { | ||
expect($('#container i')).toHaveClass('fa-info-circle'); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('WarningAlert', function() { | ||
describe('when withIcon is set to true', function() { | ||
beforeEach(function() { | ||
this.node = $('<div id="container"></div>').appendTo('body').get(0); | ||
React.render( | ||
<WarningAlert withIcon>alert body</WarningAlert>, | ||
this.node | ||
); | ||
}); | ||
|
||
afterEach(function() { | ||
React.unmountComponentAtNode(this.node); | ||
document.body.removeChild(this.node); | ||
}); | ||
|
||
it('renders an icon in the alert', function() { | ||
expect($('#container i')).toHaveClass('fa-exclamation-triangle'); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('ErrorAlert', function() { | ||
describe('when withIcon is set to true', function() { | ||
beforeEach(function() { | ||
this.node = $('<div id="container"></div>').appendTo('body').get(0); | ||
React.render( | ||
<ErrorAlert withIcon>alert body</ErrorAlert>, | ||
this.node | ||
); | ||
}); | ||
|
||
afterEach(function() { | ||
React.unmountComponentAtNode(this.node); | ||
document.body.removeChild(this.node); | ||
}); | ||
|
||
it('renders an icon in the alert', function() { | ||
expect($('#container i')).toHaveClass('fa-exclamation-triangle'); | ||
}); | ||
}); | ||
}); |