forked from reactstrap/reactstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButtonStateful.js
More file actions
51 lines (43 loc) · 1.78 KB
/
ButtonStateful.js
File metadata and controls
51 lines (43 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import React, { Component } from 'react';
import { Button, ButtonGroup } from 'reactstrap';
class Example extends Component {
constructor (props) {
super(props);
this.state = { cSelected: [] };
this.onRadioBtnClick = this.onRadioBtnClick.bind(this);
this.onCheckboxBtnClick = this.onCheckboxBtnClick.bind(this);
}
onRadioBtnClick(rSelected) {
this.setState({ rSelected });
}
onCheckboxBtnClick(selected) {
const index = this.state.cSelected.indexOf(selected);
if (index < 0) {
this.state.cSelected.push(selected);
} else {
this.state.cSelected.splice(index, 1);
}
this.setState({ cSelected: [...this.state.cSelected] });
}
render() {
return (
<div>
<h5>Radio Buttons</h5>
<ButtonGroup>
<Button color="primary" onClick={() => this.onRadioBtnClick(1)} active={this.state.rSelected === 1}>One</Button>
<Button color="primary" onClick={() => this.onRadioBtnClick(2)} active={this.state.rSelected === 2}>Two</Button>
<Button color="primary" onClick={() => this.onRadioBtnClick(3)} active={this.state.rSelected === 3}>Three</Button>
</ButtonGroup>
<p>Selected: {this.state.rSelected}</p>
<h5>Checkbox Buttons</h5>
<ButtonGroup>
<Button color="primary" onClick={() => this.onCheckboxBtnClick(1)} active={this.state.cSelected.includes(1)}>One</Button>
<Button color="primary" onClick={() => this.onCheckboxBtnClick(2)} active={this.state.cSelected.includes(2)}>Two</Button>
<Button color="primary" onClick={() => this.onCheckboxBtnClick(3)} active={this.state.cSelected.includes(3)}>Three</Button>
</ButtonGroup>
<p>Selected: {JSON.stringify(this.state.cSelected)}</p>
</div>
);
}
}
export default Example;