UNPKG

@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.

67 lines (64 loc) 5.6 kB
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 { stripFocusAndHoverStyles } from '../../stripFocusAndHoverStyles.mjs'; import { getMultiSelectCss as getComponentCss$C } from '../../../../../../components/dist/styles/esm/styles-entry.mjs'; import { getHasNativePopoverSupport, getSelectedOptionValues, getSelectedOptions } from '../../../../../../components/dist/utils/esm/utils-entry.mjs'; import { PButtonPure } from '../components/button-pure.wrapper.mjs'; import { Label } from './label.mjs'; import { StateMessage } from './state-message.mjs'; import { PIcon } from '../components/icon.wrapper.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": "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": "", "description": "Default slot for the p-multi-select-option tags." } * @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." } * * @controlled { "props": ["value"], "event": "update", "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; srHighlightedOptionText = ''; hasFilterResults = true; internals; defaultValue; multiSelectOptions = []; multiSelectOptgroups = []; inputElement; resetButtonElement; preventOptionUpdate = false; // Used to prevent value watcher from updating options when options are already updated popoverElement; hasNativePopoverSupport = getHasNativePopoverSupport(); cleanUpAutoUpdate; get currentValue() { return getSelectedOptionValues(this.props.multiSelectOptions); } setFormValue(value) { const formData = new FormData(); for (const val of value) { formData.append(this.props.name, val); } this.props.internals?.setFormValue(formData); } formDisabledCallback() { } formStateRestoreCallback() { } formResetCallback() { this.props.setFormValue(this.props.defaultValue); } render() { const { children, namedSlotChildren, otherChildren } = splitChildren(this.props.children); const inputId = 'filter'; const popoverId = 'list'; const optionsSelectedId = 'options-selected'; const style = minifyCss(stripFocusAndHoverStyles(getComponentCss$C(this.props.isOpen, this.props.disabled, this.props.hideLabel, this.props.state, this.props.theme))); 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: inputId, isRequired: this.props.required, isDisabled: this.props.disabled }), this.props.currentValue && (jsxs("span", { id: optionsSelectedId, className: "sr-only", children: [getSelectedOptions(this.props.multiSelectOptions).length, " options selected"] })), jsxs("div", { className: `wrapper${this.props.disabled ? ' disabled' : ''}`, children: [jsx("input", { id: inputId, role: "combobox", autoComplete: "off", disabled: this.props.disabled, required: this.props.required, "aria-invalid": this.props.state === 'error' ? 'true' : null }), jsx(PIcon, { className: "icon", name: "arrow-head-down", theme: this.props.theme, "aria-hidden": "true" }), this.props.currentValue && (jsx(PButtonPure, { type: "button", className: "button", icon: "close", hideLabel: true, theme: this.props.theme, disabled: this.props.disabled, children: "Reset selection" })), jsxs("div", { id: popoverId, popover: "manual", tabIndex: -1, children: [!this.props.hasFilterResults && (jsxs("div", { className: "no-results", role: "option", children: [jsx("span", { "aria-hidden": "true", children: "---" }), jsx("span", { className: "sr-only", children: "No results found" })] })), 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, theme: this.props.theme, host: null }), jsx("span", { className: "sr-only", role: "status", "aria-live": "assertive", "aria-relevant": "additions text", children: this.props.hasFilterResults ? this.props.srHighlightedOptionText : 'No results found' })] })] }), this.props.children] })); } } export { DSRMultiSelect };