-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
RadioButton.js
188 lines (160 loc) · 6 KB
/
RadioButton.js
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
import * as React from 'react';
import { PrimeReactContext } from '../api/Api';
import { useHandleStyle } from '../componentbase/ComponentBase';
import { useMergeProps, useMountEffect } from '../hooks/Hooks';
import { Tooltip } from '../tooltip/Tooltip';
import { DomHandler, ObjectUtils, classNames } from '../utils/Utils';
import { RadioButtonBase } from './RadioButtonBase';
export const RadioButton = React.memo(
React.forwardRef((inProps, ref) => {
const mergeProps = useMergeProps();
const context = React.useContext(PrimeReactContext);
const props = RadioButtonBase.getProps(inProps, context);
const elementRef = React.useRef(null);
const inputRef = React.useRef(props.inputRef);
const { ptm, cx, isUnstyled } = RadioButtonBase.setMetaData({
props
});
useHandleStyle(RadioButtonBase.css.styles, isUnstyled, { name: 'radiobutton' });
const select = (event) => {
onChange(event);
};
const onChange = (event) => {
if (props.disabled || props.readonly) {
return;
}
if (props.onChange) {
const checked = props.checked;
const radioClicked = event.target instanceof HTMLDivElement;
const inputClicked = event.target === inputRef.current;
const isInputToggled = inputClicked && event.target.checked !== checked;
const isRadioToggled = radioClicked && (DomHandler.hasClass(elementRef.current, 'p-radiobutton-checked') === checked ? !checked : false);
const value = !checked;
const eventData = {
originalEvent: event,
value: props.value,
checked: value,
stopPropagation: () => {
event?.stopPropagation();
},
preventDefault: () => {
event?.preventDefault();
},
target: {
type: 'radio',
name: props.name,
id: props.id,
value: props.value,
checked: value
}
};
if (isInputToggled || isRadioToggled) {
props?.onChange?.(eventData);
// do not continue if the user defined click wants to prevent
if (event.defaultPrevented) {
return;
}
if (isRadioToggled) {
inputRef.current.checked = value;
}
}
DomHandler.focus(inputRef.current);
}
};
const onFocus = (event) => {
props?.onFocus?.(event);
};
const onBlur = (event) => {
props?.onBlur?.(event);
};
React.useImperativeHandle(ref, () => ({
props,
select,
focus: () => DomHandler.focus(inputRef.current),
getElement: () => elementRef.current,
getInput: () => inputRef.current
}));
React.useEffect(() => {
if (inputRef.current) {
inputRef.current.checked = props.checked;
}
}, [props.checked]);
React.useEffect(() => {
ObjectUtils.combinedRefs(inputRef, props.inputRef);
}, [inputRef, props.inputRef]);
useMountEffect(() => {
if (props.autoFocus) {
DomHandler.focus(inputRef.current, props.autoFocus);
}
});
const hasTooltip = ObjectUtils.isNotEmpty(props.tooltip);
const otherProps = RadioButtonBase.getOtherProps(props);
const rootProps = mergeProps(
{
id: props.id,
className: classNames(props.className, cx('root', { context })),
style: props.style,
'data-p-checked': props.checked
},
otherProps,
ptm('root')
);
delete rootProps.input;
delete rootProps.box;
delete rootProps.icon;
const createInputElement = () => {
const ariaProps = ObjectUtils.reduceKeys(otherProps, DomHandler.ARIA_PROPS);
const inputProps = mergeProps(
{
id: props.inputId,
type: 'radio',
name: props.name,
defaultChecked: props.checked,
onFocus: onFocus,
onBlur: onBlur,
onChange: onChange,
disabled: props.disabled,
readOnly: props.readonly,
required: props.required,
tabIndex: props.tabIndex,
className: cx('input'),
...ariaProps
},
inProps.input,
ptm('input')
);
return <input ref={inputRef} {...inputProps} />;
};
const createBoxElement = () => {
const boxProps = mergeProps(
{
className: cx('box')
},
inProps.box,
ptm('box')
);
const iconProps = mergeProps(
{
className: cx('icon')
},
inProps.icon,
ptm('icon')
);
return (
<div {...boxProps}>
<div {...iconProps} />
</div>
);
};
return (
<>
<div ref={elementRef} {...rootProps}>
{createInputElement()}
{createBoxElement()}
</div>
{hasTooltip && <Tooltip target={elementRef} content={props.tooltip} pt={ptm('tooltip')} {...props.tooltipOptions} />}
</>
);
})
);
RadioButton.displayName = 'RadioButton';