@dr.pogodin/react-utils
Version:
Collection of generic ReactJS components and utils
57 lines • 2.15 kB
JavaScript
import themed from '@dr.pogodin/react-themes';
import { optionValueName } from "../common.js";
const defaultTheme = {
"context": "-dr-pogodin-react-utils___build-web-shared-components-selectors-Switch-theme___context___akD3dQ",
"ad": "-dr-pogodin-react-utils___build-web-shared-components-selectors-Switch-theme___ad___xcXtdE",
"hoc": "-dr-pogodin-react-utils___build-web-shared-components-selectors-Switch-theme___hoc___N3iziJ",
"container": "-dr-pogodin-react-utils___build-web-shared-components-selectors-Switch-theme___container___BYeWyJ",
"option": "-dr-pogodin-react-utils___build-web-shared-components-selectors-Switch-theme___option___7MFXEN",
"selected": "-dr-pogodin-react-utils___build-web-shared-components-selectors-Switch-theme___selected___CDbWAd",
"options": "-dr-pogodin-react-utils___build-web-shared-components-selectors-Switch-theme___options___9-eOZo"
};
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const BaseSwitch = ({
label,
onChange,
options,
theme,
value
}) => {
if (!options || !theme.option) throw Error('Internal error');
const optionNodes = [];
for (const option of options) {
const [iValue, iName] = optionValueName(option);
let className = theme.option;
let onPress;
if (iValue === value) className += ` ${theme.selected}`;else if (onChange) {
onPress = () => {
onChange(iValue);
};
}
optionNodes.push(onPress ? /*#__PURE__*/_jsx("div", {
className: className,
onClick: onPress,
onKeyDown: e => {
if (e.key === 'Enter') onPress();
},
role: "button",
tabIndex: 0,
children: iName
}, iValue) : /*#__PURE__*/_jsx("div", {
className: className,
children: iName
}, iValue));
}
return /*#__PURE__*/_jsxs("div", {
className: theme.container,
children: [label ? /*#__PURE__*/_jsx("div", {
className: theme.label,
children: label
}) : null, /*#__PURE__*/_jsx("div", {
className: theme.options,
children: optionNodes
})]
});
};
export default themed(BaseSwitch, 'Switch', defaultTheme);
//# sourceMappingURL=index.js.map