Skip to content

Instantly share code, notes, and snippets.

@viniciusdacal
Last active April 8, 2017 11:29
Show Gist options
  • Select an option

  • Save viniciusdacal/eb341bbe1ddfd0353adcee1f64fc8608 to your computer and use it in GitHub Desktop.

Select an option

Save viniciusdacal/eb341bbe1ddfd0353adcee1f64fc8608 to your computer and use it in GitHub Desktop.
import React from 'react';
import ReactDOM from 'react-dom';
class GroupButton extends React.Component {
constructor(props) {
super(props);
this.state = {
activeIndex: props.initialIndex,
};
}
onSelectButton(index) {
this.setState({
activeIndex: index
});
}
isActive(index) {
return index === this.state.activeIndex;
}
render() {
return (
<div className='btn-group'>
<button
className=`btn ${this.isActive(0) ? 'btn-active': ''}`
onClick={() => this.onSelectButton(0)}
>
First
</button>
<button
className=`btn ${this.isActive(1) ? 'btn-active': ''}`
onClick={() => this.onSelectButton(1)}
>
Second
</button>
</div>
);
}
}
ReactDOM.render(
<GroupButton initialIndex={0} />,
document.getElementById('root')
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment