2022-09-29 07:04:09 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2022-01-14 08:27:31 +00:00
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
2021-08-13 06:30:53 +00:00
|
|
|
|
|
|
|
|
export const RadioButton = function RadioButton({
|
|
|
|
|
id,
|
|
|
|
|
height,
|
2021-12-14 03:40:42 +00:00
|
|
|
properties,
|
|
|
|
|
styles,
|
|
|
|
|
fireEvent,
|
|
|
|
|
setExposedVariable,
|
2022-07-13 10:20:45 +00:00
|
|
|
registerAction,
|
2022-09-12 04:05:42 +00:00
|
|
|
darkMode,
|
2023-01-24 11:52:35 +00:00
|
|
|
dataCy,
|
2021-08-13 06:30:53 +00:00
|
|
|
}) {
|
2021-12-14 03:40:42 +00:00
|
|
|
const { label, value, values, display_values } = properties;
|
2022-09-12 04:05:42 +00:00
|
|
|
const { visibility, disabledState, activeColor } = styles;
|
|
|
|
|
const textColor = darkMode && styles.textColor === '#000' ? '#fff' : styles.textColor;
|
2022-09-29 07:04:09 +00:00
|
|
|
const [checkedValue, setValue] = useState(() => value);
|
|
|
|
|
useEffect(() => setValue(value), [value]);
|
2022-08-09 09:05:50 +00:00
|
|
|
|
2021-08-13 06:30:53 +00:00
|
|
|
let selectOptions = [];
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
selectOptions = [
|
2021-12-14 03:40:42 +00:00
|
|
|
...values.map((value, index) => {
|
|
|
|
|
return { name: display_values[index], value: value };
|
2021-09-21 13:48:28 +00:00
|
|
|
}),
|
2021-08-13 06:30:53 +00:00
|
|
|
];
|
2021-09-21 13:48:28 +00:00
|
|
|
} catch (err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
}
|
2021-08-13 06:30:53 +00:00
|
|
|
|
2021-10-07 06:46:08 +00:00
|
|
|
function onSelect(selection) {
|
2022-09-29 07:04:09 +00:00
|
|
|
setValue(selection);
|
2022-02-25 09:50:34 +00:00
|
|
|
setExposedVariable('value', selection).then(() => fireEvent('onSelectionChange'));
|
2021-08-13 06:30:53 +00:00
|
|
|
}
|
|
|
|
|
|
2021-10-07 06:46:08 +00:00
|
|
|
useEffect(() => {
|
2021-12-14 03:40:42 +00:00
|
|
|
setExposedVariable('value', value);
|
2021-10-07 06:46:08 +00:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2021-12-14 03:40:42 +00:00
|
|
|
}, [value]);
|
2021-10-07 06:46:08 +00:00
|
|
|
|
2022-09-27 05:25:51 +00:00
|
|
|
registerAction(
|
|
|
|
|
'selectOption',
|
|
|
|
|
async function (option) {
|
|
|
|
|
onSelect(option);
|
|
|
|
|
},
|
2022-09-29 07:04:09 +00:00
|
|
|
[setValue]
|
2022-09-27 05:25:51 +00:00
|
|
|
);
|
2022-07-13 10:20:45 +00:00
|
|
|
|
2021-08-13 06:30:53 +00:00
|
|
|
return (
|
2023-01-24 11:52:35 +00:00
|
|
|
<div
|
|
|
|
|
data-disabled={disabledState}
|
|
|
|
|
className="row py-1"
|
|
|
|
|
style={{ height, display: visibility ? '' : 'none' }}
|
|
|
|
|
data-cy={dataCy}
|
|
|
|
|
>
|
2021-10-16 03:14:29 +00:00
|
|
|
<span className="form-check-label col-auto py-0" style={{ color: textColor }}>
|
2021-09-21 13:48:28 +00:00
|
|
|
{label}
|
|
|
|
|
</span>
|
2021-10-16 03:14:29 +00:00
|
|
|
<div className="col px-1 py-0 mt-0">
|
2021-08-13 06:30:53 +00:00
|
|
|
{selectOptions.map((option, index) => (
|
2021-09-21 13:48:28 +00:00
|
|
|
<label key={index} className="form-check form-check-inline">
|
|
|
|
|
<input
|
2022-01-09 08:42:40 +00:00
|
|
|
style={{
|
|
|
|
|
marginTop: '1px',
|
2022-08-09 09:05:50 +00:00
|
|
|
backgroundColor: checkedValue === option.value ? `${activeColor}` : 'white',
|
2022-01-09 08:42:40 +00:00
|
|
|
}}
|
2021-09-21 13:48:28 +00:00
|
|
|
className="form-check-input"
|
2022-08-09 09:05:50 +00:00
|
|
|
checked={checkedValue === option.value}
|
2021-09-21 13:48:28 +00:00
|
|
|
type="radio"
|
|
|
|
|
value={option.value}
|
2022-01-14 08:27:31 +00:00
|
|
|
name={`${id}-${uuidv4()}`}
|
2021-10-07 06:46:08 +00:00
|
|
|
onChange={() => onSelect(option.value)}
|
2021-09-21 13:48:28 +00:00
|
|
|
/>
|
|
|
|
|
<span className="form-check-label" style={{ color: textColor }}>
|
|
|
|
|
{option.name}
|
|
|
|
|
</span>
|
2021-08-13 06:30:53 +00:00
|
|
|
</label>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|