-
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-radio): add radio and radio group components
[Finishes #86529336] Signed-off-by: Ryan Dy <rdy@pivotal.io>
- Loading branch information
Geoff Pleiss
committed
Jan 22, 2015
1 parent
3605113
commit 17a0325
Showing
6 changed files
with
233 additions
and
2 deletions.
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
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,32 @@ | ||
'use strict'; | ||
|
||
var _ = require('lodash'); | ||
var React = require('react/addons'); | ||
var cloneWithProps = React.addons.cloneWithProps; | ||
|
||
var RadioGroup = React.createClass({ | ||
propTypes: { | ||
id: React.PropTypes.string, | ||
name: React.PropTypes.string.isRequired, | ||
onChange: React.PropTypes.func | ||
}, | ||
|
||
getDefaultProps: function() { | ||
return {onChange: _.noop}; | ||
}, | ||
|
||
onChange: function(e) { | ||
this.props.onChange(e.target.value); | ||
}, | ||
|
||
render: function() { | ||
var {name} = this.props; | ||
var children = React.Children.map(this.props.children, (child) => cloneWithProps(child, {name, onChange: this.onChange})); | ||
|
||
return ( | ||
<div className="radio-group" id={this.props.id}>{children}</div> | ||
); | ||
}, | ||
}); | ||
|
||
module.exports = {RadioGroup}; |
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,29 @@ | ||
'use strict'; | ||
|
||
var React = require('react/addons'); | ||
|
||
var Radio = React.createClass({ | ||
propTypes: { | ||
checked: React.PropTypes.bool, | ||
defaultChecked: React.PropTypes.bool, | ||
name: React.PropTypes.string, | ||
value: React.PropTypes.string.isRequired, | ||
onChange: React.PropTypes.func, | ||
id: React.PropTypes.string | ||
}, | ||
|
||
render: function() { | ||
var {id, checked, defaultChecked, name, value, onChange} = this.props; | ||
return ( | ||
<div className='radio'> | ||
<label> | ||
<input type='radio' {...{id, checked, defaultChecked, name, value, onChange}}> | ||
{this.props.children} | ||
</input> | ||
</label> | ||
</div> | ||
); | ||
} | ||
}); | ||
|
||
module.exports = {Radio}; |
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,52 @@ | ||
'use strict'; | ||
|
||
require('./spec_helper'); | ||
|
||
var $ = require('jquery'); | ||
var React = require('react/addons'); | ||
|
||
var RadioGroup = require('../../../src/pivotal-ui/javascripts/radio-group').RadioGroup; | ||
var Radio = require('../../../src/pivotal-ui/javascripts/radio').Radio; | ||
|
||
describe('RadioGroup', function() { | ||
var changeSpy; | ||
beforeEach(function() { | ||
this.node = $('<div id="container"></div>').appendTo('body').get(0); | ||
changeSpy = jasmine.createSpy('change'); | ||
React.render( | ||
<RadioGroup name='bananas' onChange={changeSpy} id="clear-channel"> | ||
<Radio value="1">One!!!</Radio> | ||
<Radio value="2">The two value</Radio> | ||
<Radio value="3">Three</Radio> | ||
</RadioGroup>, | ||
this.node | ||
); | ||
}); | ||
|
||
afterEach(function() { | ||
React.unmountComponentAtNode(this.node); | ||
document.body.removeChild(this.node); | ||
}); | ||
|
||
it("renders the radio group", function() { | ||
expect($('#container .radio-group#clear-channel')).toExist(); | ||
}); | ||
|
||
|
||
it('renders 3 radiobuttons', function() { | ||
expect($('#container .radio-group .radio label :radio[name=bananas]')).toHaveLength(3); | ||
expect($('#container .radio-group .radio label :radio[name=bananas]').eq(0)).toHaveValue('1'); | ||
expect($('#container .radio-group .radio label :radio[name=bananas]').eq(1)).toHaveValue('2'); | ||
expect($('#container .radio-group .radio label :radio[name=bananas]').eq(2)).toHaveValue('3'); | ||
}); | ||
|
||
describe("when the radio button is changed", function() { | ||
beforeEach(function() { | ||
$('#container .radio-group :radio').eq(0).simulate('change').simulate('click'); | ||
}); | ||
|
||
it("calls the change callback", function() { | ||
expect(changeSpy).toHaveBeenCalledWith('1'); | ||
}); | ||
}); | ||
}); |
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,56 @@ | ||
'use strict'; | ||
|
||
require('./spec_helper'); | ||
|
||
var $ = require('jquery'); | ||
var React = require('react/addons'); | ||
|
||
var Radio = require('../../../src/pivotal-ui/javascripts/radio').Radio; | ||
|
||
describe('Radio', function() { | ||
beforeEach(function() { | ||
this.node = $('<div id="container"></div>').appendTo('body').get(0); | ||
}); | ||
|
||
afterEach(function() { | ||
React.unmountComponentAtNode(this.node); | ||
document.body.removeChild(this.node); | ||
}); | ||
|
||
it("renders a radio", function() { | ||
React.render(<Radio value="1" name="bananas" id="npr">One!!!</Radio>, this.node); | ||
expect($('#container .radio label :radio[name=bananas]')).toHaveValue('1'); | ||
expect($('#container .radio label')).toContainText('One!!!'); | ||
expect($('#container #npr:radio')).toExist(); | ||
}); | ||
|
||
describe('when the checked property is passed', function() { | ||
beforeEach(function() { | ||
React.render(<Radio value="1" name="bananas" checked onChange={jasmine.createSpy('change')}>One!!!</Radio>, this.node); | ||
}); | ||
|
||
it('renders a checked radio', function() { | ||
expect($('#container .radio label :radio[name=bananas]:checked')).toExist(); | ||
}); | ||
}); | ||
|
||
describe('when the defaultChecked property is passed', function() { | ||
var changeSpy; | ||
|
||
beforeEach(function() { | ||
changeSpy = jasmine.createSpy('change'); | ||
React.render(<Radio value="1" name="bananas" onChange={changeSpy} defaultChecked>One!!!</Radio>, this.node); | ||
}); | ||
|
||
it('renders a checked radio', function() { | ||
expect($('#container .radio label :radio[name=bananas]:checked')).toExist(); | ||
}); | ||
|
||
describe('changing the value of the radio button', function() { | ||
it('triggers the onChange callback', function() { | ||
$('#container .radio label :radio').simulate('click'); | ||
expect(changeSpy).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); | ||
}); |