UNPKG

@mantine/core

Version:

React components library focused on usability, accessibility and developer experience

67 lines (66 loc) 2.77 kB
"use client"; require("../../../_virtual/_rolldown/runtime.cjs"); const require_use_props = require("../../../core/MantineProvider/use-props/use-props.cjs"); const require_factory = require("../../../core/factory/factory.cjs"); const require_Input = require("../../Input/Input.cjs"); const require_InputsGroupFieldset = require("../../../utils/InputsGroupFieldset/InputsGroupFieldset.cjs"); let react = require("react"); let _mantine_hooks = require("@mantine/hooks"); let react_jsx_runtime = require("react/jsx-runtime"); //#region packages/@mantine/core/src/components/Switch/SwitchGroup/SwitchGroup.tsx const SwitchGroupContext = (0, react.createContext)(null); const defaultProps = { hiddenInputValuesSeparator: "," }; const SwitchGroup = require_factory.genericFactory(((props) => { const { value, defaultValue, onChange, size, wrapperProps, children, readOnly, name, hiddenInputValuesSeparator, hiddenInputProps, maxSelectedValues, disabled, ...others } = require_use_props.useProps("SwitchGroup", defaultProps, props); const [_value, setValue] = (0, _mantine_hooks.useUncontrolled)({ value, defaultValue, finalValue: [], onChange }); const handleChange = (event) => { const itemValue = event.currentTarget.value; if (readOnly) return; const isCurrentlySelected = _value.includes(itemValue); if (!isCurrentlySelected && maxSelectedValues && _value.length >= maxSelectedValues) return; setValue(isCurrentlySelected ? _value.filter((item) => item !== itemValue) : [..._value, itemValue]); }; const isDisabled = (switchValue) => { if (disabled) return true; if (!maxSelectedValues) return false; const isCurrentlySelected = _value.includes(switchValue); const hasReachedLimit = _value.length >= maxSelectedValues; return !isCurrentlySelected && hasReachedLimit; }; const hiddenInputValue = _value.join(hiddenInputValuesSeparator); return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(SwitchGroupContext, { value: { value: _value, onChange: handleChange, size, isDisabled }, children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(require_Input.Input.Wrapper, { size, ...wrapperProps, ...others, labelElement: "div", __staticSelector: "SwitchGroup", children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_InputsGroupFieldset.InputsGroupFieldset, { role: "group", children }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("input", { type: "hidden", name, value: hiddenInputValue, ...hiddenInputProps })] }) }); })); SwitchGroup.classes = require_Input.Input.Wrapper.classes; SwitchGroup.displayName = "@mantine/core/SwitchGroup"; //#endregion exports.SwitchGroup = SwitchGroup; exports.SwitchGroupContext = SwitchGroupContext; //# sourceMappingURL=SwitchGroup.cjs.map