@jobber/components 6.85.2 → 6.86.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,344 +0,0 @@
1
- import { _ as __rest, a as __awaiter } from './tslib.es6-es.js';
2
- import React__default, { useCallback, useEffect, useState, forwardRef, useMemo, useRef } from 'react';
3
- import { u as useDebounce_2 } from './useDebounce-es.js';
4
- import classnames from 'classnames';
5
- import { u as useIsMounted_2 } from './useIsMounted-es.js';
6
- import { u as useFloating, o as offset, f as flip, e as size, b as autoUpdate, F as FloatingPortal } from './floating-ui.react-es.js';
7
- import { u as useSafeLayoutEffect_1 } from './useSafeLayoutEffect-es.js';
8
- import { c as calculateMaxHeight } from './maxHeight-es.js';
9
- import { H as Heading } from './Heading-es.js';
10
- import { T as Text } from './Text-es.js';
11
- import { I as Icon } from './Icon-es.js';
12
- import { u as useOnKeyDown_2 } from './useOnKeyDown-es.js';
13
- import { InputText } from './InputText/index.mjs';
14
- import { m as mergeRefs } from './FormField-es.js';
15
-
16
- var styles = {"autocomplete":"_7mObJiwfPh4-","options":"dL5JShAJlKM-","heading":"PWZL-94hH7k-","visible":"_2RzcnTdaPyc-","option":"y9zhi8Wr8QA-","active":"_3Xg49dtL1Q8-","separator":"LIeh390F3W8-","icon":"K2phy6IC3TY-","text":"a6-LbUm5WnY-","label":"tQNbuxcE9nU-","details":"qacStG9-XbE-","spinning":"P9cQDL4MZ-s-"};
17
-
18
- const AUTOCOMPLETE_MAX_HEIGHT = 300;
19
-
20
- function useRepositionMenu(attachTo, visible, cssManagedVisibility) {
21
- const { refs, floatingStyles, update } = useFloating(Object.assign({ placement: "bottom", middleware: [
22
- offset(8),
23
- flip({ fallbackPlacements: ["top"] }),
24
- size({
25
- apply({ availableHeight, elements }) {
26
- const maxHeight = calculateMaxHeight(availableHeight, {
27
- maxHeight: AUTOCOMPLETE_MAX_HEIGHT,
28
- });
29
- Object.assign(elements.floating.style, {
30
- maxHeight: `${maxHeight}px`,
31
- });
32
- },
33
- }),
34
- ], elements: {
35
- reference: attachTo,
36
- } }, (!cssManagedVisibility
37
- ? {
38
- whileElementsMounted: autoUpdate,
39
- }
40
- : {})));
41
- // While DefaultMenu leverages conditional rendering, CustomMenu is hidden with CSS
42
- // We need to apply the correct update method to each case
43
- useSafeLayoutEffect_1(() => {
44
- if (cssManagedVisibility && visible && attachTo && refs.floating.current) {
45
- const cleanup = autoUpdate(attachTo, refs.floating.current, update);
46
- return cleanup;
47
- }
48
- return undefined;
49
- }, [
50
- cssManagedVisibility,
51
- visible,
52
- attachTo,
53
- refs.floating.current,
54
- update,
55
- autoUpdate,
56
- ]);
57
- const targetWidth = attachTo === null || attachTo === void 0 ? void 0 : attachTo.clientWidth;
58
- return {
59
- menuRef: refs.floating.current,
60
- setMenuRef: refs.setFloating,
61
- targetWidth,
62
- styles: {
63
- float: floatingStyles,
64
- },
65
- };
66
- }
67
-
68
- function BaseAutocompleteMenuWrapperInternal({ setMenuRef, floatStyles, targetWidth, visible, children, }) {
69
- return (React__default.createElement("div", { className: classnames(styles.options, { [styles.visible]: visible }), ref: setMenuRef, style: Object.assign(Object.assign({}, floatStyles.float), { width: targetWidth }), "data-elevation": "elevated" }, children));
70
- }
71
- /**
72
- * Provides a wrapper for the Autocomplete menu that handles positioning and visibility.
73
- * @param attachTo - The element that the menu should be attached to.
74
- */
75
- function useAutocompleteMenu({ attachTo, }) {
76
- const [menuRef, setMenuRef] = React__default.useState(null);
77
- const AutocompleteMenuWrapper = useCallback(({ children, visible, }) => {
78
- const menuFloatProps = useRepositionMenu(attachTo, visible, true);
79
- useEffect(() => {
80
- setMenuRef(menuFloatProps.menuRef);
81
- }, [menuFloatProps.menuRef]);
82
- return (React__default.createElement(BaseAutocompleteMenuWrapper, { floatStyles: menuFloatProps.styles, setMenuRef: menuFloatProps.setMenuRef, targetWidth: menuFloatProps.targetWidth, visible: visible }, children));
83
- }, [attachTo]);
84
- return { MenuWrapper: AutocompleteMenuWrapper, menuRef };
85
- }
86
- function BaseAutocompleteMenuWrapper(props) {
87
- const mounted = useIsMounted_2();
88
- const menu = React__default.createElement(BaseAutocompleteMenuWrapperInternal, Object.assign({}, props));
89
- return mounted.current ? React__default.createElement(FloatingPortal, null, menu) : menu;
90
- }
91
-
92
- function isOptionSelected(selectedOption, option) {
93
- return Boolean(selectedOption && selectedOption.value === option.value);
94
- }
95
- /**
96
- * Helper function to determine if the option is a group. This is used to
97
- * determine if the option contains a list of options for rendering Section
98
- * Labels in the Autocomplete component.
99
- */
100
- function isOptionGroup(option) {
101
- return "options" in option;
102
- }
103
-
104
- /**
105
- * The rendering of the default MenuOption
106
- */
107
- function MenuOption({ isHighlighted, option, onOptionSelect, isSelected, addSeparators, UNSAFE_className = {}, UNSAFE_style = {}, }) {
108
- if (isOptionGroup(option)) {
109
- return (React__default.createElement(MenuGroupOptions, { UNSAFE_className: UNSAFE_className.groupOption, option: option, UNSAFE_style: UNSAFE_style.groupOption }));
110
- }
111
- return (React__default.createElement(BaseMenuOption, { UNSAFE_className: UNSAFE_className.option, UNSAFE_style: UNSAFE_style.option, option: option, isHighlighted: isHighlighted, onOptionSelect: onOptionSelect, addSeparators: addSeparators },
112
- React__default.createElement(MenuOptionContent, { option: option, isSelected: isSelected, UNSAFE_className: UNSAFE_className.content, UNSAFE_style: UNSAFE_style.content })));
113
- }
114
- function MenuOptionContent({ option, isSelected, UNSAFE_className = {}, UNSAFE_style = {}, }) {
115
- const iconClassName = classnames(styles.icon, UNSAFE_className.icon);
116
- const textClassName = classnames(styles.text, UNSAFE_className.text);
117
- const labelClassName = classnames(styles.label, UNSAFE_className.label);
118
- const detailsClassName = classnames(styles.details, UNSAFE_className.details);
119
- return (React__default.createElement(React__default.Fragment, null,
120
- React__default.createElement("div", { className: iconClassName, style: UNSAFE_style.icon }, isSelected && React__default.createElement(Icon, { name: "checkmark", size: "small" })),
121
- React__default.createElement("div", { className: textClassName, style: UNSAFE_style.text },
122
- React__default.createElement("div", { className: labelClassName, style: UNSAFE_style.label },
123
- React__default.createElement(Text, null, option.label),
124
- option.description !== undefined && (React__default.createElement(Text, { variation: "subdued" }, option.description))),
125
- option.details !== undefined && (React__default.createElement("div", { className: detailsClassName, style: UNSAFE_style.details },
126
- React__default.createElement(Text, null, option.details))))));
127
- }
128
- /**
129
- * The rendering of the default MenuGroupOption
130
- */
131
- function MenuGroupOptions({ option, UNSAFE_className = {}, UNSAFE_style = {}, }) {
132
- return (React__default.createElement(BaseMenuGroupOption, { UNSAFE_className: UNSAFE_className.heading, UNSAFE_style: UNSAFE_style.heading },
133
- React__default.createElement(Heading, { level: 5 }, option.label)));
134
- }
135
- function BaseMenuGroupOption({ children, UNSAFE_className = "", UNSAFE_style = {}, }) {
136
- const headingClassName = classnames(styles.heading, UNSAFE_className);
137
- return (React__default.createElement("div", { className: headingClassName, style: UNSAFE_style }, children));
138
- }
139
- /**
140
- * Renders the base option component. The component takes children and renders them inside a button.
141
- */
142
- function BaseMenuOption({ option, isHighlighted, onOptionSelect, addSeparators, children, UNSAFE_className = "", UNSAFE_style = {}, }) {
143
- const optionClass = classnames(styles.option, {
144
- [styles.active]: isHighlighted,
145
- [styles.separator]: addSeparators,
146
- }, UNSAFE_className);
147
- return (React__default.createElement("button", { role: "option", type: "button", className: optionClass, onMouseDown: onOptionSelect === null || onOptionSelect === void 0 ? void 0 : onOptionSelect.bind(undefined, option), style: UNSAFE_style }, children));
148
- }
149
-
150
- var KeyboardAction;
151
- (function (KeyboardAction) {
152
- KeyboardAction[KeyboardAction["Previous"] = -1] = "Previous";
153
- KeyboardAction[KeyboardAction["Next"] = 1] = "Next";
154
- KeyboardAction[KeyboardAction["Select"] = 0] = "Select";
155
- })(KeyboardAction || (KeyboardAction = {}));
156
- /**
157
- * Hook to handle custom keyboard navigation for the Autocomplete component.
158
- * Use this hook if you are using components in the menu that aren't MenuOption or BaseMenuOption.
159
- */
160
- function useCustomKeyboardNavigation({ onRequestHighlightChange, }) {
161
- useOnKeyDown_2((event) => {
162
- onRequestHighlightChange === null || onRequestHighlightChange === void 0 ? void 0 : onRequestHighlightChange(event, KeyboardAction.Next);
163
- }, "ArrowDown");
164
- useOnKeyDown_2((event) => {
165
- onRequestHighlightChange === null || onRequestHighlightChange === void 0 ? void 0 : onRequestHighlightChange(event, KeyboardAction.Previous);
166
- }, "ArrowUp");
167
- useOnKeyDown_2((event) => {
168
- onRequestHighlightChange === null || onRequestHighlightChange === void 0 ? void 0 : onRequestHighlightChange(event, KeyboardAction.Select);
169
- }, "Enter");
170
- }
171
- /**
172
- * Hook to handle keyboard navigation for the Menu in the Autocomplete component.
173
- * If using components in the menu that aren't MenuOption or BaseMenuOption, you should use the `useCustomKeyboardNavigation` hook.
174
- */
175
- function useKeyboardNavigation({ options, onOptionSelect, menuRef, visible, }) {
176
- const [highlightedIndex, setHighlightedIndex] = useState(0);
177
- const initialHighlight = options.some(isOptionGroup) ? 1 : 0;
178
- useEffect(() => setHighlightedIndex(initialHighlight), [options]);
179
- useEffect(() => {
180
- var _a, _b;
181
- (_b = (_a = menuRef === null || menuRef === void 0 ? void 0 : menuRef.children[highlightedIndex]) === null || _a === void 0 ? void 0 : _a.scrollIntoView) === null || _b === void 0 ? void 0 : _b.call(_a, {
182
- behavior: "smooth",
183
- block: "nearest",
184
- inline: "start",
185
- });
186
- }, [highlightedIndex]);
187
- const onRequestHighlightChange = useCallback((event, direction) => {
188
- if (!visible)
189
- return;
190
- const indexChange = getRequestedIndexChange({
191
- event,
192
- options,
193
- direction,
194
- highlightedIndex,
195
- });
196
- switch (direction) {
197
- case KeyboardAction.Previous:
198
- setHighlightedIndex(prev => Math.max(0, prev + indexChange));
199
- break;
200
- case KeyboardAction.Next:
201
- setHighlightedIndex(prev => Math.min(options.length - 1, prev + indexChange));
202
- break;
203
- case KeyboardAction.Select:
204
- if (isOptionGroup(options[highlightedIndex]))
205
- return;
206
- onOptionSelect(options[highlightedIndex]);
207
- break;
208
- }
209
- }, [highlightedIndex, options, onOptionSelect, visible]);
210
- useCustomKeyboardNavigation({ onRequestHighlightChange });
211
- return { highlightedIndex };
212
- }
213
- /**
214
- * Function to get the requested index change based on the current highlighted index and the direction of the keyboard action.
215
- * Accounts for groups in the options array.
216
- */
217
- function getRequestedIndexChange({ event, options, direction, highlightedIndex, }) {
218
- event.preventDefault();
219
- const requestedIndex = options[highlightedIndex + direction];
220
- return requestedIndex && isOptionGroup(requestedIndex)
221
- ? direction + direction
222
- : direction;
223
- }
224
-
225
- /**
226
- * Renders the default Menu for the Autocomplete component.
227
- */
228
- function DefaultMenu({ options, selectedOption, onOptionSelect, attachTo, visible, }) {
229
- const { menuRef, setMenuRef, styles: floatStyles, targetWidth, } = useRepositionMenu(attachTo, visible, false);
230
- const detectSeparatorCondition = (option) => option.description || option.details;
231
- const addSeparators = options.some(detectSeparatorCondition);
232
- const { highlightedIndex } = useKeyboardNavigation({
233
- onOptionSelect,
234
- options,
235
- visible,
236
- menuRef,
237
- });
238
- return (React__default.createElement(BaseAutocompleteMenuWrapper, { setMenuRef, floatStyles, targetWidth, visible }, options === null || options === void 0 ? void 0 : options.map((option, index) => {
239
- return (React__default.createElement(MenuOption, { key: index, option: option, isHighlighted: index === highlightedIndex, onOptionSelect: onOptionSelect, isSelected: isOptionSelected(selectedOption, option), addSeparators: addSeparators }));
240
- })));
241
- }
242
-
243
- function Menu({ options, selectedOption, onOptionSelect, inputFocused, attachTo, inputRef, customRenderMenu, }) {
244
- if (customRenderMenu) {
245
- return (React__default.createElement(CustomMenu, { attachTo: attachTo, inputFocused: inputFocused, inputRef: inputRef, customRenderMenu: customRenderMenu, options: options, onOptionSelect: onOptionSelect, selectedOption: selectedOption }));
246
- }
247
- if (!inputFocused || !options.length)
248
- return null;
249
- return (React__default.createElement(DefaultMenu, { attachTo: attachTo, options: options, onOptionSelect: onOptionSelect, selectedOption: selectedOption, visible: inputFocused }));
250
- }
251
- /**
252
- * Renders the custom Menu for the Autocomplete component.
253
- * Provides the menuRef and MenuWrapper to the customRenderMenu function.
254
- */
255
- function CustomMenu({ options, selectedOption, onOptionSelect, customRenderMenu, attachTo, inputFocused, inputRef, }) {
256
- const { MenuWrapper, menuRef } = useAutocompleteMenu({ attachTo });
257
- const menuContent = customRenderMenu({
258
- options,
259
- menuRef,
260
- onOptionSelect,
261
- selectedOption,
262
- inputFocused,
263
- MenuWrapper,
264
- inputRef,
265
- });
266
- return menuContent;
267
- }
268
-
269
- // Max statements increased to make room for the debounce functions
270
- // eslint-disable-next-line max-statements
271
- function AutocompleteInternal(_a, ref) {
272
- var _b;
273
- var { initialOptions = [], value, allowFreeForm = true, size = undefined, debounce: debounceRate = 300, onChange, getOptions, placeholder, onBlur, onFocus, validations, customRenderMenu } = _a, inputProps = __rest(_a, ["initialOptions", "value", "allowFreeForm", "size", "debounce", "onChange", "getOptions", "placeholder", "onBlur", "onFocus", "validations", "customRenderMenu"]);
274
- const initialOptionsMemo = useMemo(() => mapToOptions(initialOptions), [initialOptions]);
275
- const [options, setOptions] = useState(initialOptionsMemo);
276
- const [inputFocused, setInputFocused] = useState(false);
277
- const [inputText, setInputText] = useState((_b = value === null || value === void 0 ? void 0 : value.label) !== null && _b !== void 0 ? _b : "");
278
- const [autocompleteRef, setAutocompleteRef] = useState(null);
279
- const delayedSearch = useDebounce_2(updateSearch, debounceRate);
280
- const inputRef = useRef(null);
281
- useEffect(() => {
282
- delayedSearch();
283
- }, [inputText]);
284
- useEffect(() => {
285
- var _a;
286
- updateInput((_a = value === null || value === void 0 ? void 0 : value.label) !== null && _a !== void 0 ? _a : "");
287
- }, [value]);
288
- return (React__default.createElement("div", { className: styles.autocomplete, ref: setAutocompleteRef },
289
- React__default.createElement(InputText, Object.assign({ ref: mergeRefs([ref, inputRef]), autocomplete: false, size: size, value: inputText, onChange: handleInputChange, placeholder: placeholder, onFocus: handleInputFocus, onBlur: handleInputBlur, validations: validations }, inputProps)),
290
- React__default.createElement(Menu, { attachTo: autocompleteRef, inputRef: inputRef, inputFocused: inputFocused, options: options, customRenderMenu: customRenderMenu, selectedOption: value, onOptionSelect: handleMenuChange })));
291
- function updateInput(newText) {
292
- setInputText(newText);
293
- if (newText === "") {
294
- setOptions(mapToOptions(initialOptions));
295
- }
296
- }
297
- function updateSearch() {
298
- return __awaiter(this, void 0, void 0, function* () {
299
- const updatedOptions = yield getOptions(inputText);
300
- const filteredOptions = updatedOptions.filter(option => isOptionGroup(option) ? option.options.length > 0 : true);
301
- setOptions(mapToOptions(filteredOptions));
302
- });
303
- }
304
- function handleMenuChange(chosenOption) {
305
- var _a;
306
- onChange(chosenOption);
307
- updateInput((_a = chosenOption === null || chosenOption === void 0 ? void 0 : chosenOption.label) !== null && _a !== void 0 ? _a : "");
308
- setInputFocused(false);
309
- }
310
- function handleInputChange(newText) {
311
- updateInput(newText);
312
- if (allowFreeForm) {
313
- onChange({ label: newText });
314
- }
315
- }
316
- function handleInputBlur() {
317
- setInputFocused(false);
318
- if (value == undefined || value.label !== inputText) {
319
- setInputText("");
320
- onChange(undefined);
321
- }
322
- onBlur && onBlur();
323
- }
324
- function handleInputFocus() {
325
- setInputFocused(true);
326
- if (onFocus) {
327
- onFocus();
328
- }
329
- }
330
- }
331
- function mapToOptions(items) {
332
- const retVal = items.reduce((result, item) => {
333
- result.push(item);
334
- if (isOptionGroup(item) && item.options) {
335
- result = result.concat(item.options);
336
- }
337
- return result;
338
- }, []);
339
- return retVal;
340
- }
341
- // Casts the Generics to the forward ref so autocomplete works as expected for consumers
342
- const Autocomplete = forwardRef(AutocompleteInternal);
343
-
344
- export { Autocomplete as A, BaseMenuOption as B, KeyboardAction as K, MenuOption as M, BaseMenuGroupOption as a, useCustomKeyboardNavigation as b, useRepositionMenu as c, BaseAutocompleteMenuWrapper as d, useAutocompleteMenu as e, isOptionGroup as f, getRequestedIndexChange as g, isOptionSelected as i, useKeyboardNavigation as u };