-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathRadioButton.jsx
162 lines (143 loc) · 3.8 KB
/
RadioButton.jsx
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* @component
*/
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import classNames from 'clsx';
let currentId = 0;
const PREFIX = 'CC_RB_';
/**
* A radio button that allows to select one of multiple options.
*/
export default class RadioButton extends Component {
constructor() {
super();
currentId += 1;
this.id = PREFIX + currentId;
}
/**
* Handles check event and passes the checked state to the handler.
* @param event
*/
handleChange = (event) => {
const { disabled, onChange, value } = this.props;
if (!disabled && onChange) {
if (value !== undefined) {
onChange(value);
} else {
onChange(event.target.value);
}
}
};
/**
* Renders a radio button.
* @returns {XML}
*/
render() {
const {
checked,
id,
children,
disabled,
name,
className,
stopPropagation,
style,
...props
} = this.props;
return (
<div className={classNames('cc__radio-button', className)} style={style}>
<input
{...props}
id={id || this.id}
type="radio"
className="radio"
checked={checked}
onChange={this.handleChange}
name={name}
disabled={disabled}
onClick={
stopPropagation
? (event) => event.stopPropagation()
: null
}
/>
<label
htmlFor={id || this.id}
onClick={
stopPropagation
? (event) => event.stopPropagation()
: null
}
>
{children}
</label>
</div>
);
}
}
RadioButton.propTypes = {
/**
* The HTML id of the `<input>`-element.
*/
id: PropTypes.string,
/**
* Multiple radio buttons with the same name belong to one group. Only one
* radio button in a group can be active at a time.
*/
name: PropTypes.string,
/**
* Wether the radio button is currently active.
*/
checked: PropTypes.bool,
/**
* A function that is called when the radio button gets checked. Receives
* the `value`-prop as its first argument.
*/
onChange: PropTypes.func,
/**
* Disables any user interaction and renders the radio button in a disabled
* style.
*/
disabled: PropTypes.bool,
/**
* A string or `ReactNode` that will be the label.
*/
children: PropTypes.node,
/**
* A value that will be sent to the `onChange`-callback as its first
* argument.
*/
value: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
PropTypes.bool,
]),
/**
* A classname string that will be applied to the container element.
*/
className: PropTypes.string,
/**
* Wether to stop propagation of click events to parent elements.
*/
stopPropagation: PropTypes.bool,
/**
* A React style object that will be applied to the container element.
*/
style: PropTypes.objectOf(
PropTypes.oneOfType([PropTypes.string, PropTypes.number])
),
};
RadioButton.defaultProps = {
id: null,
name: null,
checked: undefined,
onChange: null,
disabled: false,
children: null,
value: undefined,
className: null,
stopPropagation: false,
style: null,
};
RadioButton.displayName = 'RadioButton';