-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[changed] Deprecating Input type=radio in favor of new RadioButton an…
…d RadioGroup components
- Loading branch information
Showing
12 changed files
with
410 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const SimpleRadioGroup = React.createClass({ | ||
getInitialState() { | ||
return {message: undefined}; | ||
}, | ||
|
||
handleChange(newValue) { | ||
this.setState({message: newValue}); | ||
}, | ||
|
||
render() { | ||
const message = this.state.message ? | ||
<FormControls.Static>{this.state.message}</FormControls.Static> : | ||
null; | ||
|
||
const handleChange = this.handleChange; | ||
|
||
return ( | ||
<form> | ||
{message} | ||
<FormControls.RadioGroup name="greeting" legend="Greetings" srOnly> | ||
<FormControls.RadioButton label="Hi!" value="Hi!" /> | ||
<FormControls.RadioButton label="Hello!" value="Hello!" /> | ||
</FormControls.RadioGroup> | ||
<FormControls.RadioGroup name="farewell" onChange={handleChange}> | ||
<FormControls.RadioButton label="Bye!" value="Bye!" /> | ||
<FormControls.RadioButton label="Goodbye!" value="Goodbye!" /> | ||
</FormControls.RadioGroup> | ||
<FormControls.RadioGroup name="thing" defaultValue="2"> | ||
<FormControls.RadioButton label="1" value="1" disabled /> | ||
<FormControls.RadioButton label="2" value="2" disabled /> | ||
<FormControls.RadioButton label="3" value="3" disabled /> | ||
</FormControls.RadioGroup> | ||
</form> | ||
); | ||
} | ||
}); | ||
|
||
React.render(<SimpleRadioGroup />, mountNode); |
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,74 @@ | ||
import React from 'react'; | ||
import classNames from 'classnames'; | ||
|
||
export default class RadioButton extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
} | ||
|
||
getInputDOMNode() { | ||
return React.findDOMNode(this.refs.input); | ||
} | ||
|
||
getValue() { | ||
return this.props.value; | ||
} | ||
|
||
getChecked() { | ||
return this.getInputDOMNode().checked; | ||
} | ||
|
||
renderLabel(input) { | ||
if (!this.props.label) { | ||
return input; | ||
} | ||
|
||
const {id, label} = this.props; | ||
|
||
const classes = { | ||
'radio': !this.props.inline, | ||
'radio-inline': this.props.inline | ||
}; | ||
|
||
return ( | ||
<label className={classNames(classes)} htmlFor={id}> | ||
{input} | ||
{label} | ||
</label> | ||
); | ||
} | ||
|
||
render() { | ||
const {inline, label, ...other} = this.props; | ||
return this.renderLabel( | ||
<input type="radio" {...other} ref="input" /> | ||
); | ||
} | ||
} | ||
|
||
RadioButton.propTypes = { | ||
/** | ||
* Disables the radio button | ||
*/ | ||
disabled: React.PropTypes.bool, | ||
/** | ||
* Applies the .radio-inline class to the wrapping label | ||
*/ | ||
inline: React.PropTypes.bool, | ||
/** | ||
* Wraps with radio button in a label along with the provided content | ||
*/ | ||
label: React.PropTypes.node, | ||
/** | ||
* Applies a name to associate the radio button with a group. | ||
*/ | ||
name: React.PropTypes.string, | ||
/** | ||
* Calls this when the checked state changes | ||
*/ | ||
onChange: React.PropTypes.func, | ||
/** | ||
* Applies a value to this radio button | ||
*/ | ||
value: React.PropTypes.any | ||
}; |
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,85 @@ | ||
import React from 'react'; | ||
import classNames from 'classnames'; | ||
|
||
export default class RadioGroup extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = {value: this.props.defaultValue}; | ||
this.handleChange = this.handleChange.bind(this); | ||
} | ||
|
||
getValue() { | ||
return this.state.value; | ||
} | ||
|
||
renderLegend() { | ||
const {legend, srOnly} = this.props; | ||
const classes = { | ||
'sr-only': srOnly | ||
}; | ||
|
||
return legend ? | ||
<legend className={classNames(classes)}>{legend}</legend> : | ||
null; | ||
} | ||
|
||
handleChange(evt) { | ||
if (!evt.target) { | ||
return; | ||
} | ||
|
||
const {value} = evt.target; | ||
this.setState({value}); | ||
this.props.onChange(value); | ||
} | ||
|
||
renderChildren() { | ||
const {name, inline} = this.props; | ||
return React.Children.map(this.props.children, child => { | ||
const {value} = child.props; | ||
const checked = this.state.value ? this.state.value === value : false; | ||
return React.cloneElement(child, {name, inline, checked, onChange: this.handleChange}); | ||
}); | ||
} | ||
|
||
render() { | ||
const children = this.renderChildren(); | ||
return ( | ||
<fieldset ref="radioGroup" className="radio"> | ||
{this.renderLegend()} | ||
{children} | ||
</fieldset> | ||
); | ||
} | ||
} | ||
|
||
RadioGroup.propTypes = { | ||
/** | ||
* The default value of the radio group. The RadioButton in this group with the provided value will be checked by default. | ||
*/ | ||
defaultValue: React.PropTypes.any, | ||
/** | ||
* Formats the radio group with the class .radio-inline | ||
*/ | ||
inline: React.PropTypes.bool, | ||
/** | ||
* Populates a legend element at the top of the fieldset | ||
*/ | ||
legend: React.PropTypes.node, | ||
/** | ||
* Applies the provided name prop to all the given children. | ||
*/ | ||
name: React.PropTypes.string.isRequired, | ||
/** | ||
* This is called when the selected value changes, with the new value as a parameter | ||
*/ | ||
onChange: React.PropTypes.func, | ||
/** | ||
* Applies the legend, if present, with the .sr-only class | ||
*/ | ||
srOnly: React.PropTypes.bool | ||
}; | ||
|
||
RadioGroup.defaultProps = { | ||
onChange: () => {} | ||
}; |
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 |
---|---|---|
@@ -1,5 +1,9 @@ | ||
import RadioButton from './RadioButton'; | ||
import RadioGroup from './RadioGroup'; | ||
import Static from './Static'; | ||
|
||
export default { | ||
Static | ||
Static, | ||
RadioGroup, | ||
RadioButton | ||
}; |
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 |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
"globals": { | ||
"assert": true, | ||
"expect": true, | ||
"should": true, | ||
"sinon": true | ||
}, | ||
"plugins": [ | ||
|
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,49 @@ | ||
import React from 'react'; | ||
import ReactTestUtils from 'react/lib/ReactTestUtils'; | ||
import RadioButton from '../../src/FormControls/RadioButton'; | ||
|
||
describe('FormControls.RadioButton', function () { | ||
it('does not apply htmlFor to the label if no id is provided', function () { | ||
const instance = ReactTestUtils.renderIntoDocument( | ||
<RadioButton label="test" value="test" /> | ||
); | ||
|
||
const label = ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'label'); | ||
should.not.exist(label.props.htmlFor); | ||
}); | ||
|
||
it('does not apply htmlFor to the label if no id is provided', function () { | ||
const testId = 'thing'; | ||
const instance = ReactTestUtils.renderIntoDocument( | ||
<RadioButton id={testId} label="test" value="test" /> | ||
); | ||
|
||
const label = ReactTestUtils.findRenderedDOMComponentWithTag(instance, 'label'); | ||
label.props.htmlFor.should.eql(testId); | ||
}); | ||
|
||
it('does not render a label if no label prop is provided', function () { | ||
const instance = ReactTestUtils.renderIntoDocument( | ||
<RadioButton value="test" /> | ||
); | ||
|
||
React.findDOMNode(instance).tagName.toLowerCase().should.eql('input'); | ||
}); | ||
|
||
it('renders a label if label prop is provided', function () { | ||
const instance = ReactTestUtils.renderIntoDocument( | ||
<RadioButton label="thing" value="test" /> | ||
); | ||
|
||
React.findDOMNode(instance).tagName.toLowerCase().should.eql('label'); | ||
}); | ||
|
||
it('should render the label content', function () { | ||
const testText = 'thing'; | ||
const instance = ReactTestUtils.renderIntoDocument( | ||
<RadioButton label={testText} value="test" /> | ||
); | ||
|
||
React.findDOMNode(instance).textContent.should.eql(testText); | ||
}); | ||
}); |
Oops, something went wrong.