BetterDiscordApp-rauenzi/renderer/src/ui/settings/components/radio.jsx

31 lines
1.2 KiB
React
Raw Normal View History

2023-05-19 23:14:55 +02:00
import React from "@modules/react";
2022-10-03 05:08:10 +02:00
2023-05-20 00:37:21 +02:00
import RadioIcon from "@ui/icons/radio";
2022-10-03 05:08:10 +02:00
2023-03-07 01:17:28 +01:00
const {useState, useCallback} = React;
2022-10-03 05:08:10 +02:00
2023-03-07 01:17:28 +01:00
export default function Radio({name, value, options, onChange}) {
const [index, setIndex] = useState(options.findIndex(o => o.value === value));
const change = useCallback((e) => {
const newIndex = parseInt(e.target.value);
const newValue = options[newIndex].value;
onChange?.(newValue);
setIndex(newIndex);
2023-03-20 03:23:11 +01:00
}, [options, onChange]);
2023-03-07 01:17:28 +01:00
function renderOption(opt, i) {
const isSelected = index === i;
2022-10-03 05:08:10 +02:00
return <label className={"bd-radio-option" + (isSelected ? " bd-radio-selected" : "")}>
2023-03-07 01:17:28 +01:00
<input onChange={change} type="radio" name={name} checked={isSelected} value={i} />
2022-10-03 05:08:10 +02:00
{/* <span className="bd-radio-button"></span> */}
<RadioIcon className="bd-radio-icon" size="24" checked={isSelected} />
<div className="bd-radio-label-wrap">
<div className="bd-radio-label">{opt.name}</div>
<div className="bd-radio-description">{opt.desc || opt.description}</div>
</div>
</label>;
}
2023-03-07 01:17:28 +01:00
return <div className="bd-radio-group">{options.map(renderOption)}</div>;
}