@porsche-design-system/components-react
Version:
Porsche Design System is a component library designed to help developers create the best experience for software or services distributed by Dr. Ing. h.c. F. Porsche AG.
82 lines (79 loc) • 7.64 kB
JavaScript
import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
import { Component } from 'react';
import '../../provider.mjs';
import { splitChildren } from '../../splitChildren.mjs';
import { minifyCss } from '../../minifyCss.mjs';
import { getMultiSelectCss as getComponentCss$y } from '../../../../../../components/dist/styles/esm/styles-entry.mjs';
import { getHasNativePopoverSupport, descriptionId, getComboboxAriaAttributes, labelId, getListboxAriaAttributes } from '../../../../../../components/dist/utils/esm/utils-entry.mjs';
import { PButtonPure } from '../components/button-pure.wrapper.mjs';
import { Label } from './label.mjs';
import { messageId, StateMessage } from './state-message.mjs';
import { PIcon } from '../components/icon.wrapper.mjs';
import { PInputSearch } from '../components/input-search.wrapper.mjs';
import { NoResultsOption } from './no-results-option.mjs';
/**
* @slot {"name": "label", "description": "Shows a label. Only [phrasing content](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Content_categories#Phrasing_content) is allowed." }
* @slot {"name": "label-after", "description": "Places additional content after the label text (for content that should not be part of the label, e.g. external links or `p-popover`)."}
* @slot {"name": "description", "description": "Shows a description. Only [phrasing content](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Content_categories#Phrasing_content) is allowed." }
* @slot {"name": "selected", "description": "Use this slot to provide custom markup for the selected options display in the button area." }
* @slot {"name": "", "description": "Default slot for the p-multi-select-option tags." }
* @slot {"name": "options-status", "description": "When implementing a custom filter with the `filter` slot, use this slot for loading, error and no results status." }
* @slot {"name": "message", "description": "Shows a state message. Only [phrasing content](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Content_categories#Phrasing_content) is allowed." }
* @slot {"name": "filter", "description": "Optional slot for providing a custom `p-input-search` input. When used, the default filter input is replaced and the built-in filter logic is disabled, giving full control over filtering behavior." }
*
* @controlled { "props": ["value"], "event": "change", "isInternallyMutated": true }
*/
class DSRMultiSelect extends Component {
host;
// The "name" property is reflected as an attribute to ensure compatibility with native form submission.
// In the React wrapper, all props are synced as properties on the element ref, so reflecting "name" as an attribute ensures it is properly handled in the form submission process.
isOpen = false;
hasFilterResults = true;
selectedOptions = [];
internals;
defaultValue;
multiSelectOptions = [];
multiSelectOptgroups = [];
buttonElement;
inputSearchElement;
filterSlot;
listboxElement;
resetButtonElement;
preventOptionUpdate = false; // Used to prevent value watcher from updating options when options are already updated
popoverElement;
hasNativePopoverSupport = getHasNativePopoverSupport();
cleanUpAutoUpdate;
currentlyHighlightedOption = null;
setFormValue() {
if (this.props.value == null || this.props.value.length === 0) {
// null excludes the control from form submission (mirrors native behavior)
this.props.internals?.setFormValue(null);
return;
}
const formData = new FormData();
for (const val of this.props.value) {
formData.append(this.props.name, String(val));
}
this.props.internals?.setFormValue(formData);
}
formDisabledCallback() {
// Called when a parent fieldset is disabled or enabled
}
formStateRestoreCallback() {
}
formResetCallback() {
// triggers value watcher which syncs form value
}
render() {
const { namedSlotChildren} = splitChildren(this.props.children);
const hasCustomFilterSlot = namedSlotChildren.filter(({ props: { slot } }) => slot === 'filter').length > 0;
const hasCustomSelectedSlot = namedSlotChildren.filter(({ props: { slot } }) => slot === 'selected').length > 0;
const buttonId = 'button';
const listboxId = 'listbox';
const selectDescriptionId = (this.props.description || namedSlotChildren.filter(({ props: { slot } }) => slot === 'description').length > 0) ? descriptionId : undefined;
const selectMessageId = (this.props.message || namedSlotChildren.filter(({ props: { slot } }) => slot === 'message').length > 0) && ['success', 'error'].includes(this.props.state) ? messageId : undefined;
const style = minifyCss(getComponentCss$y(this.props.isOpen, this.props.disabled, this.props.hideLabel, this.props.state, this.props.compact));
return (jsxs(Fragment, { children: [jsxs("template", { shadowroot: "open", shadowrootmode: "open", shadowrootdelegatesfocus: "true", children: [jsx("style", { dangerouslySetInnerHTML: { __html: style } }), jsxs("div", { className: "root", children: [jsx(Label, { hasLabel: this.props.label || namedSlotChildren.filter(({ props: { slot } }) => slot === 'label').length > 0, hasDescription: this.props.description || namedSlotChildren.filter(({ props: { slot } }) => slot === 'description').length > 0, host: null, label: this.props.label, description: this.props.description, htmlFor: buttonId, isRequired: this.props.required, isDisabled: this.props.disabled }), jsxs("button", { "aria-invalid": this.props.state === 'error' ? 'true' : null, type: "button", role: "combobox", id: buttonId, tabIndex: 0, ...getComboboxAriaAttributes(this.props.isOpen, this.props.required, (this.props.label || namedSlotChildren.filter(({ props: { slot } }) => slot === 'label').length > 0) && labelId, selectMessageId, selectDescriptionId, listboxId), disabled: this.props.disabled, onBlur: this.props.onComboBlur, children: [hasCustomSelectedSlot ? (jsx("slot", { name: "selected" })) : (jsx("span", { children: this.selectedOptions.map((option) => (option.textContent ?? '').toString().trim()).join(', ') })), this.props.value?.length > 0 && (jsx(PButtonPure, { type: "button", className: "button", icon: "close", hideLabel: true, disabled: this.props.disabled, children: "Reset selection" })), jsx(PIcon, { className: "icon", name: "arrow-head-down", color: "primary", "aria-hidden": "true" })] }), jsxs("div", { popover: "manual", tabIndex: 0, children: [hasCustomFilterSlot ? (jsx("slot", { name: "filter" })) : (jsx(PInputSearch, { className: "filter", name: "filter", label: "Filter options", hideLabel: true, autoComplete: "off", clear: true, indicator: true, compact: true, onBlur: (e) => e.stopPropagation() })), jsxs("div", { id: listboxId, className: "options", ...getListboxAriaAttributes(this.props.required, (this.props.label || namedSlotChildren.filter(({ props: { slot } }) => slot === 'label').length > 0) && labelId, selectMessageId, selectDescriptionId, true), onPointerMove: this.props.onPointerMove, onBlur: (e) => e.stopPropagation(), children: [!this.props.hasFilterResults && jsx(NoResultsOption, {}), jsx("slot", { name: "options-status" }), jsx("slot", {})] })] }), jsx(StateMessage, { hasMessage: (this.props.message || namedSlotChildren.filter(({ props: { slot } }) => slot === 'message').length > 0) && ['success', 'error'].includes(this.props.state), state: this.props.state, message: this.props.message, host: null })] })] }), this.props.children] }));
}
}
export { DSRMultiSelect };