@mantine/core
Version:
React components library focused on usability, accessibility and developer experience
112 lines (111 loc) • 3.97 kB
JavaScript
"use client";
import { getFontSize, getRadius, getSize } from "../../core/utils/get-size/get-size.mjs";
import { createVarsResolver } from "../../core/styles-api/create-vars-resolver/create-vars-resolver.mjs";
import { useProps } from "../../core/MantineProvider/use-props/use-props.mjs";
import { useStyles } from "../../core/styles-api/use-styles/use-styles.mjs";
import { extractStyleProps } from "../../core/Box/style-props/extract-style-props/extract-style-props.mjs";
import { factory } from "../../core/factory/factory.mjs";
import { Box } from "../../core/Box/Box.mjs";
import { CheckIcon } from "../Checkbox/CheckIcon.mjs";
import { ChipGroup, ChipGroupContext } from "./ChipGroup/ChipGroup.mjs";
import Chip_module_default from "./Chip.module.mjs";
import { use } from "react";
import { useId as useId$1, useUncontrolled } from "@mantine/hooks";
import { jsx, jsxs } from "react/jsx-runtime";
//#region packages/@mantine/core/src/components/Chip/Chip.tsx
const defaultProps = { type: "checkbox" };
const varsResolver = createVarsResolver((theme, { size, radius, variant, color, autoContrast }) => {
const colors = theme.variantColorResolver({
color: color || theme.primaryColor,
theme,
variant: variant || "filled",
autoContrast
});
return { root: {
"--chip-fz": getFontSize(size),
"--chip-size": getSize(size, "chip-size"),
"--chip-radius": radius === void 0 ? void 0 : getRadius(radius),
"--chip-checked-padding": getSize(size, "chip-checked-padding"),
"--chip-padding": getSize(size, "chip-padding"),
"--chip-icon-size": getSize(size, "chip-icon-size"),
"--chip-bg": color || variant ? colors.background : void 0,
"--chip-hover": color || variant ? colors.hover : void 0,
"--chip-color": color || variant ? colors.color : void 0,
"--chip-bd": color || variant ? colors.border : void 0,
"--chip-spacing": getSize(size, "chip-spacing")
} };
});
const Chip = factory((_props) => {
const props = useProps("Chip", defaultProps, _props);
const { classNames, className, style, styles, unstyled, vars, id, checked, defaultChecked, onChange, value, wrapperProps, type, disabled, children, size, variant, icon, rootRef, autoContrast, mod, attributes, ...others } = props;
const getStyles = useStyles({
name: "Chip",
classes: Chip_module_default,
props,
className,
style,
classNames,
styles,
unstyled,
attributes,
vars,
varsResolver
});
const ctx = use(ChipGroupContext);
const uuid = useId$1(id);
const { styleProps, rest } = extractStyleProps(others);
const [_value, setValue] = useUncontrolled({
value: checked,
defaultValue: defaultChecked,
finalValue: false,
onChange
});
const contextProps = ctx ? {
checked: ctx.isChipSelected(value),
onChange: (event) => {
ctx.onChange(event);
onChange?.(event.currentTarget.checked);
},
type: ctx.multiple ? "checkbox" : "radio"
} : {};
const _checked = contextProps.checked || _value;
return /* @__PURE__ */ jsxs(Box, {
size,
variant,
ref: rootRef,
mod,
...getStyles("root"),
...styleProps,
...wrapperProps,
children: [/* @__PURE__ */ jsx("input", {
type,
...getStyles("input"),
checked: _checked,
onChange: (event) => setValue(event.currentTarget.checked),
id: uuid,
disabled,
value,
...contextProps,
...rest
}), /* @__PURE__ */ jsxs(Box, {
component: "label",
htmlFor: uuid,
mod: {
checked: _checked,
disabled
},
...getStyles("label", { variant: variant || "filled" }),
children: [_checked && icon !== null && icon !== false && /* @__PURE__ */ jsx("span", {
...getStyles("iconWrapper"),
children: icon === void 0 ? /* @__PURE__ */ jsx(CheckIcon, { ...getStyles("checkIcon") }) : icon
}), /* @__PURE__ */ jsx("span", { children })]
})]
});
});
Chip.classes = Chip_module_default;
Chip.varsResolver = varsResolver;
Chip.displayName = "@mantine/core/Chip";
Chip.Group = ChipGroup;
//#endregion
export { Chip };
//# sourceMappingURL=Chip.mjs.map