-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathInputRadioGroup.js
More file actions
63 lines (59 loc) · 2.24 KB
/
InputRadioGroup.js
File metadata and controls
63 lines (59 loc) · 2.24 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
52
53
54
55
56
57
58
59
60
61
62
63
import React, {useState, useEffect} from 'react';
/**
* InputRadioGroup component
* @param {string} id - Base ID for radio inputs
* @param {string} name - Name attribute for all radio inputs
* @param {string|number} value - Currently selected value
* @param {Array} options - Array of options: [{value: '1', label: 'On'}, {value: '0', label: 'Off'}]
* @param {function} onChange - Callback when selection changes
* @param {string} className - Additional CSS class for each radio option wrapper
* @param {boolean} disabled - Disable all radio inputs
* @param {JSX.Element} separator - Separator element between radio options
* @return {JSX.Element} Radio button group
*/
export default function InputRadioGroup({
id,
name,
value: initialValue,
options = [],
onChange,
className = 'spbc_radio_option',
disabled = false,
separator = <> </>,
}) {
const [selectedValue, setSelectedValue] = useState(String(initialValue ?? ''));
useEffect(() => {
setSelectedValue(String(initialValue ?? ''));
}, [initialValue]);
const handleChange = (event) => {
const newValue = event.target.value;
setSelectedValue(newValue);
if (onChange) {
onChange(event);
}
};
return (
<>
{options.map((option, index) => (
<React.Fragment key={`${id}-${option.value}`}>
<span className={className}>
<label>
<input
type="radio"
id={`${id}_${option.value}`}
name={name}
value={option.value}
checked={selectedValue === String(option.value)}
onChange={handleChange}
disabled={disabled || option.disabled}
className="spbc_setting_field"
/>
<span>{option.label}</span>
</label>
</span>
{index < options.length - 1 && separator}
</React.Fragment>
))}
</>
);
}