UNPKG

@mantine/core

Version:

React components library focused on usability, accessibility and developer experience

100 lines (97 loc) 3.47 kB
'use client'; import React from 'react'; import cx from 'clsx'; import '../../Checkbox/Checkbox.mjs'; import '../../Checkbox/CheckboxGroup/CheckboxGroup.mjs'; import { CheckIcon } from '../../Checkbox/CheckIcon.mjs'; import { ScrollArea } from '../../ScrollArea/ScrollArea.mjs'; import { Combobox } from '../Combobox.mjs'; import { defaultOptionsFilter } from './default-options-filter.mjs'; import { isEmptyComboboxData } from './is-empty-combobox-data.mjs'; import { isOptionsGroup } from './is-options-group.mjs'; import { validateOptions } from './validate-options.mjs'; import classes from '../Combobox.module.css.mjs'; function isValueChecked(value, optionValue) { return Array.isArray(value) ? value.includes(optionValue) : value === optionValue; } function Option({ data, withCheckIcon, value, checkIconPosition, unstyled }) { if (!isOptionsGroup(data)) { const check = withCheckIcon && isValueChecked(value, data.value) && /* @__PURE__ */ React.createElement(CheckIcon, { className: classes.optionsDropdownCheckIcon }); return /* @__PURE__ */ React.createElement( Combobox.Option, { value: data.value, disabled: data.disabled, className: cx({ [classes.optionsDropdownOption]: !unstyled }), "data-reverse": checkIconPosition === "right" || void 0, "data-checked": isValueChecked(value, data.value) || void 0, "aria-selected": isValueChecked(value, data.value) }, checkIconPosition === "left" && check, /* @__PURE__ */ React.createElement("span", null, data.label), checkIconPosition === "right" && check ); } const options = data.items.map((item) => /* @__PURE__ */ React.createElement( Option, { data: item, value, key: item.value, unstyled, withCheckIcon, checkIconPosition } )); return /* @__PURE__ */ React.createElement(Combobox.Group, { label: data.group }, options); } function OptionsDropdown({ data, hidden, hiddenWhenEmpty, filter, search, limit, maxDropdownHeight, withScrollArea = true, filterOptions = true, withCheckIcon = false, value, checkIconPosition, nothingFoundMessage, unstyled, labelId }) { validateOptions(data); const shouldFilter = typeof search === "string"; const filteredData = shouldFilter ? (filter || defaultOptionsFilter)({ options: data, search: filterOptions ? search : "", limit: limit ?? Infinity }) : data; const isEmpty = isEmptyComboboxData(filteredData); const options = filteredData.map((item) => /* @__PURE__ */ React.createElement( Option, { data: item, key: isOptionsGroup(item) ? item.group : item.value, withCheckIcon, value, checkIconPosition, unstyled } )); return /* @__PURE__ */ React.createElement(Combobox.Dropdown, { hidden: hidden || hiddenWhenEmpty && isEmpty }, /* @__PURE__ */ React.createElement(Combobox.Options, { labelledBy: labelId }, withScrollArea ? /* @__PURE__ */ React.createElement( ScrollArea.Autosize, { mah: maxDropdownHeight ?? 220, type: "scroll", scrollbarSize: "var(--_combobox-padding)", offsetScrollbars: "y", className: classes.optionsDropdownScrollArea }, options ) : options, isEmpty && nothingFoundMessage && /* @__PURE__ */ React.createElement(Combobox.Empty, null, nothingFoundMessage))); } export { OptionsDropdown }; //# sourceMappingURL=OptionsDropdown.mjs.map