UNPKG

11.4 kBJavaScriptView Raw
1import {ButtonContext as $d2b4bc8c273e7be6$export$24d547caef80ccd1} from "./Button.mjs";
2import {Provider as $64fa3d84918910a7$export$2881499e37b75b9a, removeDataAttributes as $64fa3d84918910a7$export$ef03459518577ad4, useContextProps as $64fa3d84918910a7$export$29f1550f4b0d4415, useRenderProps as $64fa3d84918910a7$export$4d86445c2cf5e3, useSlot as $64fa3d84918910a7$export$9d4c57ee4c6ffdd8, useSlottedContext as $64fa3d84918910a7$export$fabf2dc03a41866e} from "./utils.mjs";
3import {FieldErrorContext as $ee014567cb39d3f0$export$ff05c3ac10437e03} from "./FieldError.mjs";
4import {FormContext as $d3e0e05bdfcf66bd$export$c24727297075ec6a} from "./Form.mjs";
5import {GroupContext as $a049562f99e7db0e$export$f9c6924e160136d1} from "./Group.mjs";
6import {InputContext as $3985021b0ad6602f$export$37fb8590cf2c088c} from "./Input.mjs";
7import {LabelContext as $01b77f81d0f07f68$export$75b6ee27786ba447} from "./Label.mjs";
8import {ListBoxContext as $eed445e0843c11d0$export$7ff8f37d2d81a48d, ListStateContext as $eed445e0843c11d0$export$7c5906fe4f1f2af2} from "./ListBox.mjs";
9import {OverlayTriggerStateContext as $de32f1b87079253c$export$d2f961adcb0afbe} from "./Dialog.mjs";
10import {PopoverContext as $07b14b47974efb58$export$9b9a0cd73afb7ca4} from "./Popover.mjs";
11import {TextContext as $514c0188e459b4c0$export$9afb8bc826b033ea} from "./Text.mjs";
12import {useFilter as $cP7hC$useFilter, useComboBox as $cP7hC$useComboBox} from "react-aria";
13import {useComboBoxState as $cP7hC$useComboBoxState} from "react-stately";
14import {CollectionBuilder as $cP7hC$CollectionBuilder} from "@react-aria/collections";
15import {useResizeObserver as $cP7hC$useResizeObserver, filterDOMProps as $cP7hC$filterDOMProps} from "@react-aria/utils";
16import $cP7hC$react, {createContext as $cP7hC$createContext, useMemo as $cP7hC$useMemo, useRef as $cP7hC$useRef, useState as $cP7hC$useState, useCallback as $cP7hC$useCallback, forwardRef as $cP7hC$forwardRef} from "react";
17
18/*
19 * Copyright 2022 Adobe. All rights reserved.
20 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
21 * you may not use this file except in compliance with the License. You may obtain a copy
22 * of the License at http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Unless required by applicable law or agreed to in writing, software distributed under
25 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
26 * OF ANY KIND, either express or implied. See the License for the specific language
27 * governing permissions and limitations under the License.
28 */
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44const $d01f2c01039c0eec$export$d414ccceff7063c3 = /*#__PURE__*/ (0, $cP7hC$createContext)(null);
45const $d01f2c01039c0eec$export$c02625b26074192c = /*#__PURE__*/ (0, $cP7hC$createContext)(null);
46function $d01f2c01039c0eec$var$ComboBox(props, ref) {
47 [props, ref] = (0, $64fa3d84918910a7$export$29f1550f4b0d4415)(props, ref, $d01f2c01039c0eec$export$d414ccceff7063c3);
48 let { children: children, isDisabled: isDisabled = false, isInvalid: isInvalid = false, isRequired: isRequired = false } = props;
49 let content = (0, $cP7hC$useMemo)(()=>/*#__PURE__*/ {
50 var _props_items;
51 return (0, $cP7hC$react).createElement((0, $eed445e0843c11d0$export$7ff8f37d2d81a48d).Provider, {
52 value: {
53 items: (_props_items = props.items) !== null && _props_items !== void 0 ? _props_items : props.defaultItems
54 }
55 }, typeof children === 'function' ? children({
56 isOpen: false,
57 isDisabled: isDisabled,
58 isInvalid: isInvalid,
59 isRequired: isRequired,
60 defaultChildren: null
61 }) : children);
62 }, [
63 children,
64 isDisabled,
65 isInvalid,
66 isRequired,
67 props.items,
68 props.defaultItems
69 ]);
70 return /*#__PURE__*/ (0, $cP7hC$react).createElement((0, $cP7hC$CollectionBuilder), {
71 content: content
72 }, (collection)=>/*#__PURE__*/ (0, $cP7hC$react).createElement($d01f2c01039c0eec$var$ComboBoxInner, {
73 props: props,
74 collection: collection,
75 comboBoxRef: ref
76 }));
77}
78function $d01f2c01039c0eec$var$ComboBoxInner({ props: props, collection: collection, comboBoxRef: ref }) {
79 let { name: name, formValue: formValue = 'key', allowsCustomValue: allowsCustomValue } = props;
80 if (allowsCustomValue) formValue = 'text';
81 let { validationBehavior: formValidationBehavior } = (0, $64fa3d84918910a7$export$fabf2dc03a41866e)((0, $d3e0e05bdfcf66bd$export$c24727297075ec6a)) || {};
82 var _props_validationBehavior, _ref;
83 let validationBehavior = (_ref = (_props_validationBehavior = props.validationBehavior) !== null && _props_validationBehavior !== void 0 ? _props_validationBehavior : formValidationBehavior) !== null && _ref !== void 0 ? _ref : 'native';
84 let { contains: contains } = (0, $cP7hC$useFilter)({
85 sensitivity: 'base'
86 });
87 let state = (0, $cP7hC$useComboBoxState)({
88 defaultFilter: props.defaultFilter || contains,
89 ...props,
90 // If props.items isn't provided, rely on collection filtering (aka listbox.items is provided or defaultItems provided to Combobox)
91 items: props.items,
92 children: undefined,
93 collection: collection,
94 validationBehavior: validationBehavior
95 });
96 let buttonRef = (0, $cP7hC$useRef)(null);
97 let inputRef = (0, $cP7hC$useRef)(null);
98 let listBoxRef = (0, $cP7hC$useRef)(null);
99 let popoverRef = (0, $cP7hC$useRef)(null);
100 let [labelRef, label] = (0, $64fa3d84918910a7$export$9d4c57ee4c6ffdd8)();
101 let { buttonProps: buttonProps, inputProps: inputProps, listBoxProps: listBoxProps, labelProps: labelProps, descriptionProps: descriptionProps, errorMessageProps: errorMessageProps, ...validation } = (0, $cP7hC$useComboBox)({
102 ...(0, $64fa3d84918910a7$export$ef03459518577ad4)(props),
103 label: label,
104 inputRef: inputRef,
105 buttonRef: buttonRef,
106 listBoxRef: listBoxRef,
107 popoverRef: popoverRef,
108 name: formValue === 'text' ? name : undefined,
109 validationBehavior: validationBehavior
110 }, state);
111 // Make menu width match input + button
112 let [menuWidth, setMenuWidth] = (0, $cP7hC$useState)(null);
113 let onResize = (0, $cP7hC$useCallback)(()=>{
114 if (inputRef.current) {
115 var _buttonRef_current;
116 let buttonRect = (_buttonRef_current = buttonRef.current) === null || _buttonRef_current === void 0 ? void 0 : _buttonRef_current.getBoundingClientRect();
117 let inputRect = inputRef.current.getBoundingClientRect();
118 let minX = buttonRect ? Math.min(buttonRect.left, inputRect.left) : inputRect.left;
119 let maxX = buttonRect ? Math.max(buttonRect.right, inputRect.right) : inputRect.right;
120 setMenuWidth(maxX - minX + 'px');
121 }
122 }, [
123 buttonRef,
124 inputRef,
125 setMenuWidth
126 ]);
127 (0, $cP7hC$useResizeObserver)({
128 ref: inputRef,
129 onResize: onResize
130 });
131 // Only expose a subset of state to renderProps function to avoid infinite render loop
132 let renderPropsState = (0, $cP7hC$useMemo)(()=>({
133 isOpen: state.isOpen,
134 isDisabled: props.isDisabled || false,
135 isInvalid: validation.isInvalid || false,
136 isRequired: props.isRequired || false
137 }), [
138 state.isOpen,
139 props.isDisabled,
140 validation.isInvalid,
141 props.isRequired
142 ]);
143 let renderProps = (0, $64fa3d84918910a7$export$4d86445c2cf5e3)({
144 ...props,
145 values: renderPropsState,
146 defaultClassName: 'react-aria-ComboBox'
147 });
148 let DOMProps = (0, $cP7hC$filterDOMProps)(props);
149 delete DOMProps.id;
150 var _state_selectedKey;
151 return /*#__PURE__*/ (0, $cP7hC$react).createElement((0, $64fa3d84918910a7$export$2881499e37b75b9a), {
152 values: [
153 [
154 $d01f2c01039c0eec$export$c02625b26074192c,
155 state
156 ],
157 [
158 (0, $01b77f81d0f07f68$export$75b6ee27786ba447),
159 {
160 ...labelProps,
161 ref: labelRef
162 }
163 ],
164 [
165 (0, $d2b4bc8c273e7be6$export$24d547caef80ccd1),
166 {
167 ...buttonProps,
168 ref: buttonRef,
169 isPressed: state.isOpen
170 }
171 ],
172 [
173 (0, $3985021b0ad6602f$export$37fb8590cf2c088c),
174 {
175 ...inputProps,
176 ref: inputRef
177 }
178 ],
179 [
180 (0, $de32f1b87079253c$export$d2f961adcb0afbe),
181 state
182 ],
183 [
184 (0, $07b14b47974efb58$export$9b9a0cd73afb7ca4),
185 {
186 ref: popoverRef,
187 triggerRef: inputRef,
188 scrollRef: listBoxRef,
189 placement: 'bottom start',
190 isNonModal: true,
191 trigger: 'ComboBox',
192 style: {
193 '--trigger-width': menuWidth
194 }
195 }
196 ],
197 [
198 (0, $eed445e0843c11d0$export$7ff8f37d2d81a48d),
199 {
200 ...listBoxProps,
201 ref: listBoxRef
202 }
203 ],
204 [
205 (0, $eed445e0843c11d0$export$7c5906fe4f1f2af2),
206 state
207 ],
208 [
209 (0, $514c0188e459b4c0$export$9afb8bc826b033ea),
210 {
211 slots: {
212 description: descriptionProps,
213 errorMessage: errorMessageProps
214 }
215 }
216 ],
217 [
218 (0, $a049562f99e7db0e$export$f9c6924e160136d1),
219 {
220 isInvalid: validation.isInvalid,
221 isDisabled: props.isDisabled || false
222 }
223 ],
224 [
225 (0, $ee014567cb39d3f0$export$ff05c3ac10437e03),
226 validation
227 ]
228 ]
229 }, /*#__PURE__*/ (0, $cP7hC$react).createElement("div", {
230 ...DOMProps,
231 ...renderProps,
232 ref: ref,
233 slot: props.slot || undefined,
234 "data-focused": state.isFocused || undefined,
235 "data-open": state.isOpen || undefined,
236 "data-disabled": props.isDisabled || undefined,
237 "data-invalid": validation.isInvalid || undefined,
238 "data-required": props.isRequired || undefined
239 }), name && formValue === 'key' && /*#__PURE__*/ (0, $cP7hC$react).createElement("input", {
240 type: "hidden",
241 name: name,
242 value: (_state_selectedKey = state.selectedKey) !== null && _state_selectedKey !== void 0 ? _state_selectedKey : ''
243 }));
244}
245/**
246 * A combo box combines a text input with a listbox, allowing users to filter a list of options to items matching a query.
247 */ const $d01f2c01039c0eec$export$72b9695b8216309a = /*#__PURE__*/ (0, $cP7hC$forwardRef)($d01f2c01039c0eec$var$ComboBox);
248
249
250export {$d01f2c01039c0eec$export$d414ccceff7063c3 as ComboBoxContext, $d01f2c01039c0eec$export$c02625b26074192c as ComboBoxStateContext, $d01f2c01039c0eec$export$72b9695b8216309a as ComboBox};
251//# sourceMappingURL=ComboBox.module.js.map