UNPKG

@mantine/core

Version:

React components library focused on usability, accessibility and developer experience

65 lines (64 loc) 2.44 kB
"use client"; import { useProps } from "../../../core/MantineProvider/use-props/use-props.mjs"; import { genericFactory } from "../../../core/factory/factory.mjs"; import { Input } from "../../Input/Input.mjs"; import { InputsGroupFieldset } from "../../../utils/InputsGroupFieldset/InputsGroupFieldset.mjs"; import { createContext } from "react"; import { useUncontrolled } from "@mantine/hooks"; import { jsx, jsxs } from "react/jsx-runtime"; //#region packages/@mantine/core/src/components/Switch/SwitchGroup/SwitchGroup.tsx const SwitchGroupContext = createContext(null); const defaultProps = { hiddenInputValuesSeparator: "," }; const SwitchGroup = genericFactory(((props) => { const { value, defaultValue, onChange, size, wrapperProps, children, readOnly, name, hiddenInputValuesSeparator, hiddenInputProps, maxSelectedValues, disabled, ...others } = useProps("SwitchGroup", defaultProps, props); const [_value, setValue] = 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__ */ jsx(SwitchGroupContext, { value: { value: _value, onChange: handleChange, size, isDisabled }, children: /* @__PURE__ */ jsxs(Input.Wrapper, { size, ...wrapperProps, ...others, labelElement: "div", __staticSelector: "SwitchGroup", children: [/* @__PURE__ */ jsx(InputsGroupFieldset, { role: "group", children }), /* @__PURE__ */ jsx("input", { type: "hidden", name, value: hiddenInputValue, ...hiddenInputProps })] }) }); })); SwitchGroup.classes = Input.Wrapper.classes; SwitchGroup.displayName = "@mantine/core/SwitchGroup"; //#endregion export { SwitchGroup, SwitchGroupContext }; //# sourceMappingURL=SwitchGroup.mjs.map