Skip to content

Instantly share code, notes, and snippets.

@pandevim
Forked from insin/README.md
Created May 26, 2019 22:36
Show Gist options
  • Save pandevim/ed2b2acaec1deed694b31be0dd22c570 to your computer and use it in GitHub Desktop.
Save pandevim/ed2b2acaec1deed694b31be0dd22c570 to your computer and use it in GitHub Desktop.
<RadioGroup/> for React Bootstrap - https://react-bootstrap-radio-group.surge.sh

To run the example:

npm install -g nwb
react run example.js --auto-install
import 'bootstrap/dist/css/bootstrap.css'
import React from 'react'
import Grid from 'react-bootstrap/lib/Grid'
import Col from 'react-bootstrap/lib/Col'
import RadioGroup from './RadioGroup'
export default class Example extends React.Component {
state = {
test: ''
}
handleChange = (e) => {
this.setState({[e.target.name]: e.target.value})
}
render() {
return <Grid>
<Col sm={12} style={{marginBottom: '1em'}}>
<h1>&lt;RadioGroup/&gt;</h1>
<RadioGroup
name="test"
onChange={this.handleChange}
options={[
['dairy', 'Cheese'],
['fruit', 'Apple'],
['meat', 'Ham'],
]}
value={this.state.test}
/>
</Col>
<Col sm={12}>
<pre>{`<RadioGroup
name="test"
onChange={this.handleChange}
options={[
['dairy', 'Cheese'],
['fruit', 'Apple'],
['meat', 'Ham'],
]}
value={this.state.test}
/>`}</pre>
</Col>
</Grid>
}
}
import React, {PropTypes as t} from 'react'
import Button from 'react-bootstrap/lib/Button'
import ButtonGroup from 'react-bootstrap/lib/ButtonGroup'
/**
* A ButtonGroup whose buttons act like a radio button.
* Options should be passed as a list of [value, display] tuples.
* Buttons are set up so you can use e.target.name and e.target.value in your
* onChange handler like you would with regular form inputs.
*/
let RadioGroup = React.createClass({
propTypes: {
name: t.string.isRequired,
onChange: t.func.isRequired,
options: t.arrayOf(t.arrayOf(t.string)),
value: t.string.isRequired,
},
render() {
let {disabled, name, onChange, options, value, ...props} = this.props
return <ButtonGroup {...props}>
{options.map(option =>
<Button
key={option[0]}
bsStyle={option[0] === value ? 'primary' : 'default'}
children={option[1]}
disabled={disabled}
name={name}
onClick={onChange}
value={option[0]}
/>
)}
</ButtonGroup>
}
})
export default RadioGroup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment