-
Notifications
You must be signed in to change notification settings - Fork 126
/
RadioGroup.tsx
265 lines (234 loc) · 8.37 KB
/
RadioGroup.tsx
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import React from 'react';
import PropTypes from 'prop-types';
import invariant from 'invariant';
import { ComponentType, getRandomID, isNonNullable } from '../../lib/utils';
import { Radio } from '../Radio';
import { createPropsGetter } from '../../lib/createPropsGetter';
import { Nullable } from '../../typings/utility-types';
import { FocusTrap } from '../../internal/FocusTrap';
import { ThemeContext } from '../../lib/theming/ThemeContext';
import { Theme } from '../../lib/theming/Theme';
import { CommonProps, CommonWrapper } from '../../internal/CommonWrapper';
import { cx } from '../../lib/theming/Emotion';
import { rootNode, TSetRootNode } from '../../lib/rootNode';
import { styles } from './RadioGroup.styles';
import { Prevent } from './Prevent';
import { RadioGroupContext, RadioGroupContextType } from './RadioGroupContext';
export interface RadioGroupProps<T = string | number> extends CommonProps {
/**
* Значение по умолчанию. Должно быть одним из значений дочерних радиокнопок
* или значений из параметра `items`
*/
defaultValue?: T;
/**
* Значение радиогруппы. Должно быть одним из значений радиокнопок.
* Если не указано, то компонент будет работать, как неконтролируемый
*/
value?: T;
/**
* Может быть использовано, если не передан параметр `children`
*
* Массив параметров радиокнопок. Может быть типа `Array<Value>` или
* `Array<[Value, Data]>`, где тип `Value` — значение радиокнопки, а `Data`
* — значение которое будет использовано вторым параметром в `renderItem`.
* Если тип `items: Array<Value>`, то он будет приведен к типу
* `Array<[Value, Value]>`
*/
items?: T[] | Array<[T, React.ReactNode]>;
/**
* Аттрибут name для вложенных радиокнопок. Если не указан, то сгенерируется
* случайное имя
*/
name?: string;
/**
* Дизейблит все радиокнопки
*/
disabled?: boolean;
/**
* Переводит все радиокнопки в состояние валидации: предупреждение.
*/
warning?: boolean;
/**
* Переводит все радиокнопки в состояние валидации: ошибка.
*/
error?: boolean;
/**
* Выравнивает элементы в строку. Не работает с `children`
*/
inline?: boolean;
/**
* Ширина радиогруппы. Не работает с `children`
*/
width?: React.CSSProperties['width'];
/**
* Метод отрисовки контента радиокнопки. Не работает с `children`.
*
* Принимает два аргумента: `(value: Value, data: Data) => React.Node`
*/
renderItem?: (itemValue: T, data: React.ReactNode) => React.ReactNode;
/** Вызывается при изменении `value` */
onValueChange?: (value: T) => void;
onBlur?: (event: FocusEvent) => void;
onMouseLeave?: () => any;
onMouseOver?: () => any;
onMouseEnter?: () => any;
}
export interface RadioGroupState<T> {
activeItem?: T;
}
/**
*
* `children` может содержать любую разметку с компонентами Radio,
* если не передан параметр `items`.
* Каждому компоненту Radio нужно указать параметр `value`, такого же типа
* как и параметр `value` самой радиогруппы.
*
* Значения активного элемента сравниваются по строгому равенству `===`
*/
@rootNode
export class RadioGroup<T> extends React.Component<RadioGroupProps<T>, RadioGroupState<T>> {
public static __KONTUR_REACT_UI__ = 'RadioGroup';
public static propTypes = {
children: ComponentType,
disabled: PropTypes.bool,
error: PropTypes.bool,
inline: PropTypes.bool,
name: PropTypes.string,
warning: PropTypes.bool,
width: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
onBlur: PropTypes.func,
onMouseEnter: PropTypes.func,
onMouseLeave: PropTypes.func,
onMouseOver: PropTypes.func,
};
public static defaultProps = {
renderItem,
};
public static Prevent = Prevent;
private theme!: Theme;
private node: Nullable<HTMLSpanElement>;
private name = getRandomID();
private getProps = createPropsGetter(RadioGroup.defaultProps);
private setRootNode!: TSetRootNode;
constructor(props: RadioGroupProps<T>) {
super(props);
this.state = {
activeItem: this.props.defaultValue,
};
}
private getRadioGroupContextValue = (): RadioGroupContextType<T> => {
return {
activeItem: this.getValue(),
onSelect: this.handleSelect,
name: this.getName(),
disabled: this.props.disabled,
error: this.props.error,
warning: this.props.warning,
};
};
public render() {
return (
<ThemeContext.Consumer>
{(theme) => {
this.theme = theme;
return this.renderMain();
}}
</ThemeContext.Consumer>
);
}
public renderMain() {
const { width, onMouseLeave, onMouseOver, onMouseEnter, onBlur } = this.props;
const style = {
width: width ?? 'auto',
};
const handlers = {
onMouseOver,
onMouseEnter,
onMouseLeave,
};
return (
<CommonWrapper rootNodeRef={this.setRootNode} {...this.props}>
<FocusTrap onBlur={onBlur}>
<span ref={this.ref} style={style} className={styles.root()} {...handlers}>
<RadioGroupContext.Provider value={this.getRadioGroupContextValue()}>
{this.renderChildren()}
</RadioGroupContext.Provider>
</span>
</FocusTrap>
</CommonWrapper>
);
}
/**
* @public
*/
public focus() {
const node = this.node;
if (!node) {
return;
}
let radio = node.querySelector('input[type="radio"]:checked') as Nullable<HTMLInputElement>;
// If no checked radios, try get first radio
if (!radio || radio.disabled) {
radio = node.querySelector('input[type="radio"]:not([disabled])') as Nullable<HTMLInputElement>;
}
if (radio) {
radio.focus();
}
}
private getValue = () => (this.isControlled() ? this.props.value : this.state.activeItem);
private getName = () => this.props.name || this.name;
private isControlled = () => isNonNullable(this.props.value);
private handleSelect = (value: T) => {
if (!this.isControlled()) {
this.setState({ activeItem: value });
}
if (this.props.onValueChange) {
this.props.onValueChange(value);
}
};
private renderChildren() {
const { items, children } = this.props;
invariant((!items && children) || (items && !children), 'Either items or children must be passed, not both');
return items ? mapItems<T>(this.renderRadio, items) : children;
}
private renderRadio = (itemValue: T, data: React.ReactNode, index: number): JSX.Element => {
const itemProps = {
key: typeof itemValue === 'string' || typeof itemValue === 'number' ? itemValue : index,
className: cx({
[styles.item(this.theme)]: true,
[styles.itemFirst()]: index === 0,
[styles.itemInline()]: !!this.props.inline,
}),
};
return (
<span {...itemProps}>
<Radio value={itemValue}>{this.getProps().renderItem<T>(itemValue, data)}</Radio>
</span>
);
};
private ref = (element: HTMLSpanElement) => {
this.node = element;
};
}
function renderItem<T>(_value: T, data: React.ReactNode) {
return data;
}
function mapItems<T>(
fn: (value: T, data: React.ReactNode, index: number) => React.ReactNode,
items: T[] | Array<[T, React.ReactNode]>,
) {
const result: React.ReactNode[] = [];
let index = 0;
for (const entry of items) {
const [value, data] = normalizeEntry<T>(entry);
result.push(fn(value, data, index));
++index;
}
return result;
}
function normalizeEntry<T>(entry: T | [T, React.ReactNode]): [T, React.ReactNode] {
if (!Array.isArray(entry)) {
return [entry, entry];
}
return entry;
}