@mantine/core
Version:
React components library focused on usability, accessibility and developer experience
65 lines (64 loc) • 2.51 kB
JavaScript
"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/Checkbox/CheckboxGroup/CheckboxGroup.tsx
const CheckboxGroupContext = createContext(null);
const defaultProps = { hiddenInputValuesSeparator: "," };
const CheckboxGroup = genericFactory(((props) => {
const { value, defaultValue, onChange, size, wrapperProps, children, readOnly, name, hiddenInputValuesSeparator, hiddenInputProps, maxSelectedValues, disabled, ...others } = useProps("CheckboxGroup", defaultProps, props);
const [_value, setValue] = useUncontrolled({
value,
defaultValue,
finalValue: [],
onChange
});
const handleChange = (event) => {
const itemValue = typeof event === "string" ? event : 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 = (checkboxValue) => {
if (disabled) return true;
if (!maxSelectedValues) return false;
const isCurrentlySelected = _value.includes(checkboxValue);
const hasReachedLimit = _value.length >= maxSelectedValues;
return !isCurrentlySelected && hasReachedLimit;
};
const hiddenInputValue = _value.join(hiddenInputValuesSeparator);
return /* @__PURE__ */ jsx(CheckboxGroupContext, {
value: {
value: _value,
onChange: handleChange,
size,
isDisabled
},
children: /* @__PURE__ */ jsxs(Input.Wrapper, {
size,
...wrapperProps,
...others,
labelElement: "div",
__staticSelector: "CheckboxGroup",
children: [/* @__PURE__ */ jsx(InputsGroupFieldset, {
role: "group",
children
}), /* @__PURE__ */ jsx("input", {
type: "hidden",
name,
value: hiddenInputValue,
...hiddenInputProps
})]
})
});
}));
CheckboxGroup.classes = Input.Wrapper.classes;
CheckboxGroup.displayName = "@mantine/core/CheckboxGroup";
//#endregion
export { CheckboxGroup, CheckboxGroupContext };
//# sourceMappingURL=CheckboxGroup.mjs.map