@mantine/core
Version:
React components library focused on usability, accessibility and developer experience
289 lines (288 loc) • 9.96 kB
JavaScript
"use client";
import { useProps } from "../../core/MantineProvider/use-props/use-props.mjs";
import { useResolvedStylesApi } from "../../core/styles-api/use-resolved-styles-api/use-resolved-styles-api.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 { genericFactory } from "../../core/factory/factory.mjs";
import { InputBase } from "../InputBase/InputBase.mjs";
import { getParsedComboboxData } from "../Combobox/get-parsed-combobox-data/get-parsed-combobox-data.mjs";
import { getOptionsLockup } from "../Combobox/get-options-lockup/get-options-lockup.mjs";
import { useCombobox } from "../Combobox/use-combobox/use-combobox.mjs";
import { Combobox } from "../Combobox/Combobox.mjs";
import { OptionsDropdown } from "../Combobox/OptionsDropdown/OptionsDropdown.mjs";
import { Pill } from "../Pill/Pill.mjs";
import { PillsInput } from "../PillsInput/PillsInput.mjs";
import { filterPickedValues } from "./filter-picked-values.mjs";
import { Fragment, useEffect, useRef } from "react";
import { useId as useId$1, useUncontrolled } from "@mantine/hooks";
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
//#region packages/@mantine/core/src/components/MultiSelect/MultiSelect.tsx
const clearSectionOffset = {
xs: 41,
sm: 50,
md: 60,
lg: 72,
xl: 89
};
const defaultProps = {
maxValues: Infinity,
withCheckIcon: true,
checkIconPosition: "left",
hiddenInputValuesDivider: ",",
clearSearchOnChange: true,
openOnFocus: true,
size: "sm"
};
const MultiSelect = genericFactory((_props) => {
const props = useProps("MultiSelect", defaultProps, _props);
const { classNames, className, style, styles, unstyled, vars, size, value, defaultValue, onChange, onKeyDown, variant, data, dropdownOpened, defaultDropdownOpened, onDropdownOpen, onDropdownClose, selectFirstOptionOnChange, selectFirstOptionOnDropdownOpen, onOptionSubmit, comboboxProps, filter, limit, withScrollArea, maxDropdownHeight, searchValue, defaultSearchValue, onSearchChange, readOnly, disabled, onFocus, onBlur, radius, rightSection, rightSectionWidth, rightSectionPointerEvents, rightSectionProps, leftSection, leftSectionWidth, leftSectionPointerEvents, leftSectionProps, inputContainer, inputWrapperOrder, withAsterisk, labelProps, descriptionProps, errorProps, wrapperProps, description, label, error, maxValues, searchable, nothingFoundMessage, withCheckIcon, withAlignedLabels, checkIconPosition, hidePickedOptions, withErrorStyles, name, form, id, clearable, clearSectionMode, clearButtonProps, hiddenInputProps, placeholder, hiddenInputValuesDivider, required, mod, renderOption, renderPill, onRemove, onClear, onMaxValues, scrollAreaProps, chevronColor, attributes, clearSearchOnChange, openOnFocus, ...others } = props;
const _id = useId$1(id);
const parsedData = getParsedComboboxData(data);
const optionsLockup = getOptionsLockup(parsedData);
const retainedSelectedOptions = useRef({});
const combobox = useCombobox({
opened: dropdownOpened,
defaultOpened: defaultDropdownOpened,
onDropdownOpen: () => {
onDropdownOpen?.();
if (selectFirstOptionOnDropdownOpen) combobox.selectFirstOption();
},
onDropdownClose: () => {
onDropdownClose?.();
combobox.resetSelectedOption();
}
});
const { styleProps, rest: { type, autoComplete, ...rest } } = extractStyleProps(others);
const [_value, setValue] = useUncontrolled({
value,
defaultValue,
finalValue: [],
onChange
});
const [_searchValue, setSearchValue] = useUncontrolled({
value: searchValue,
defaultValue: defaultSearchValue,
finalValue: "",
onChange: onSearchChange
});
const handleSearchChange = (value) => {
setSearchValue(value);
combobox.resetSelectedOption();
};
const getStyles = useStyles({
name: "MultiSelect",
classes: {},
props,
classNames,
styles,
unstyled,
attributes
});
const { resolvedClassNames, resolvedStyles } = useResolvedStylesApi({
props,
styles,
classNames
});
const handleInputKeydown = (event) => {
onKeyDown?.(event);
if (event.key === " " && !searchable) {
event.preventDefault();
combobox.toggleDropdown();
}
if (event.key === "Backspace" && _searchValue.length === 0 && _value.length > 0) {
onRemove?.(_value[_value.length - 1]);
setValue(_value.slice(0, _value.length - 1));
}
};
const values = _value.map((item, index) => {
const optionData = optionsLockup[`${item}`] || retainedSelectedOptions.current[`${item}`];
if (renderPill) return /* @__PURE__ */ jsx(Fragment, { children: renderPill({
option: optionData,
value: item,
onRemove: () => {
setValue(_value.filter((i) => item !== i));
onRemove?.(item);
},
disabled
}) }, `${item}-${index}`);
return /* @__PURE__ */ jsx(Pill, {
withRemoveButton: !readOnly && !optionsLockup[`${item}`]?.disabled,
onRemove: () => {
setValue(_value.filter((i) => item !== i));
onRemove?.(item);
},
unstyled,
disabled,
...getStyles("pill"),
children: optionData?.label || item
}, `${item}-${index}`);
});
useEffect(() => {
if (selectFirstOptionOnChange) combobox.selectFirstOption();
}, [selectFirstOptionOnChange, _searchValue]);
useEffect(() => {
_value.forEach((val) => {
if (`${val}` in optionsLockup) retainedSelectedOptions.current[`${val}`] = optionsLockup[`${val}`];
});
}, [optionsLockup, _value]);
const clearButton = /* @__PURE__ */ jsx(Combobox.ClearButton, {
...clearButtonProps,
onClear: () => {
onClear?.();
setValue([]);
handleSearchChange("");
}
});
const filteredData = filterPickedValues({
data: parsedData,
value: _value
});
const _clearable = clearable && _value.length > 0 && !disabled && !readOnly;
const pillsListStyle = _clearable ? { paddingInlineEnd: clearSectionOffset[size] ?? clearSectionOffset.sm } : void 0;
return /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsxs(Combobox, {
store: combobox,
classNames: resolvedClassNames,
styles: resolvedStyles,
unstyled,
size,
readOnly,
__staticSelector: "MultiSelect",
attributes,
onOptionSubmit: (val) => {
onOptionSubmit?.(val);
if (clearSearchOnChange) handleSearchChange("");
combobox.updateSelectedOptionIndex("selected");
if (_value.includes(optionsLockup[`${val}`].value)) {
setValue(_value.filter((v) => v !== optionsLockup[`${val}`].value));
onRemove?.(optionsLockup[`${val}`].value);
} else if (_value.length < maxValues) setValue([..._value, optionsLockup[`${val}`].value]);
else onMaxValues?.();
},
...comboboxProps,
children: [/* @__PURE__ */ jsx(Combobox.DropdownTarget, { children: /* @__PURE__ */ jsx(PillsInput, {
...styleProps,
__staticSelector: "MultiSelect",
classNames: resolvedClassNames,
styles: resolvedStyles,
unstyled,
size,
className,
style,
variant,
disabled,
radius,
__defaultRightSection: /* @__PURE__ */ jsx(Combobox.Chevron, {
size,
error,
unstyled,
color: chevronColor
}),
__clearSection: clearButton,
__clearable: _clearable,
__clearSectionMode: clearSectionMode,
rightSection,
rightSectionPointerEvents: rightSectionPointerEvents || "none",
rightSectionWidth,
rightSectionProps,
leftSection,
leftSectionWidth,
leftSectionPointerEvents,
leftSectionProps,
inputContainer,
inputWrapperOrder,
withAsterisk,
labelProps,
descriptionProps,
errorProps,
wrapperProps,
description,
label,
error,
withErrorStyles,
__stylesApiProps: {
...props,
rightSectionPointerEvents: rightSectionPointerEvents || "none",
multiline: true
},
pointer: !searchable,
onClick: () => searchable ? combobox.openDropdown() : combobox.toggleDropdown(),
"data-expanded": combobox.dropdownOpened || void 0,
id: _id,
required,
mod,
attributes,
children: /* @__PURE__ */ jsxs(Pill.Group, {
attributes,
disabled,
unstyled,
...getStyles("pillsList", { style: pillsListStyle }),
children: [values, /* @__PURE__ */ jsx(Combobox.EventsTarget, {
autoComplete,
withExpandedAttribute: true,
children: /* @__PURE__ */ jsx(PillsInput.Field, {
...rest,
id: _id,
placeholder,
type: !searchable && !placeholder ? "hidden" : "visible",
...getStyles("inputField"),
unstyled,
onFocus: (event) => {
onFocus?.(event);
openOnFocus && searchable && combobox.openDropdown();
},
onBlur: (event) => {
onBlur?.(event);
combobox.closeDropdown();
handleSearchChange("");
},
onKeyDown: handleInputKeydown,
value: _searchValue,
onChange: (event) => {
handleSearchChange(event.currentTarget.value);
searchable && combobox.openDropdown();
selectFirstOptionOnChange && combobox.selectFirstOption();
},
disabled,
readOnly: readOnly || !searchable,
pointer: !searchable
})
})]
})
}) }), /* @__PURE__ */ jsx(OptionsDropdown, {
data: hidePickedOptions ? filteredData : parsedData,
hidden: readOnly || disabled,
filter,
search: _searchValue,
limit,
hiddenWhenEmpty: !nothingFoundMessage,
withScrollArea,
maxDropdownHeight,
filterOptions: searchable,
value: _value,
checkIconPosition,
withCheckIcon,
withAlignedLabels,
nothingFoundMessage,
unstyled,
labelId: label ? `${_id}-label` : void 0,
"aria-label": label ? void 0 : others["aria-label"],
renderOption,
scrollAreaProps
})]
}), /* @__PURE__ */ jsx(Combobox.HiddenInput, {
name,
valuesDivider: hiddenInputValuesDivider,
value: _value,
form,
disabled,
...hiddenInputProps
})] });
});
MultiSelect.classes = {
...InputBase.classes,
...Combobox.classes
};
MultiSelect.displayName = "@mantine/core/MultiSelect";
//#endregion
export { MultiSelect };
//# sourceMappingURL=MultiSelect.mjs.map