UNPKG

@dr.pogodin/react-utils

Version:

Collection of generic ReactJS components and utils

49 lines 1.4 kB
import themed from '@dr.pogodin/react-themes'; import { optionValueName } from "../common"; import defaultTheme from "./theme.scss"; 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