-
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
8 changed files
with
229 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
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"> | ||
<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> | ||
</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,54 @@ | ||
import React from 'react'; | ||
import classNames from 'classnames'; | ||
|
||
export default class RadioButton extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
} | ||
|
||
getDOMNode() { | ||
return React.findDOMNode(this.refs.input); | ||
} | ||
|
||
getValue() { | ||
return this.props.value; | ||
} | ||
|
||
getChecked() { | ||
return this.getDOMNode().checked; | ||
} | ||
|
||
renderLabel(input) { | ||
if (!this.props.label) { | ||
return input; | ||
} | ||
|
||
const classes = { | ||
'radio': !this.props.inline, | ||
'radio-inline': this.props.inline | ||
}; | ||
|
||
return ( | ||
<label className={classNames(classes)}> | ||
{input} | ||
{this.props.label} | ||
</label> | ||
); | ||
} | ||
|
||
render() { | ||
const {inline, label, ...other} = this.props; | ||
return this.renderLabel( | ||
<input type="radio" {...other} ref="input" /> | ||
); | ||
} | ||
} | ||
|
||
RadioButton.propTypes = { | ||
disabled: React.PropTypes.bool, | ||
inline: React.PropTypes.bool, | ||
label: React.PropTypes.node, | ||
name: React.PropTypes.string, | ||
onChange: React.PropTypes.func, | ||
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,59 @@ | ||
import React from 'react'; | ||
|
||
export default class RadioGroup extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = {value: undefined}; | ||
this.handleChange = this.handleChange.bind(this); | ||
} | ||
|
||
getDOMNode() { | ||
return React.findDOMNode(this.refs.radioGroup); | ||
} | ||
|
||
getValue() { | ||
return this.state.value; | ||
} | ||
|
||
renderLegend() { | ||
return this.props.legend ? | ||
<legend>{this.props.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 => React.cloneElement(child, {name, inline, onChange: this.handleChange})); | ||
} | ||
|
||
render() { | ||
const children = this.renderChildren(); | ||
return ( | ||
<fieldset ref="radioGroup" className="radio"> | ||
{this.renderLegend()} | ||
{children} | ||
</fieldset> | ||
); | ||
} | ||
} | ||
|
||
RadioGroup.propTypes = { | ||
inline: React.PropTypes.bool, | ||
legend: React.PropTypes.node, | ||
name: React.PropTypes.string.isRequired, | ||
onChange: React.PropTypes.func | ||
}; | ||
|
||
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