@jobber/components 4.7.1-jeff.1 → 4.7.1-mike.2

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,10 +1,11 @@
1
- /// <reference types="react" />
1
+ import React, { RefObject } from "react";
2
2
  import { Option } from "./Option";
3
3
  interface MenuProps {
4
4
  readonly visible: boolean;
5
5
  readonly options: Option[];
6
6
  readonly selectedOption?: Option;
7
+ readonly attachTo: RefObject<Element | null>;
7
8
  onOptionSelect(chosenOption: Option): void;
8
9
  }
9
- export declare function Menu({ visible, options, selectedOption, onOptionSelect, }: MenuProps): JSX.Element;
10
+ export declare function Menu({ visible, options, selectedOption, onOptionSelect, attachTo, }: MenuProps): React.ReactPortal;
10
11
  export {};
@@ -8,6 +8,8 @@ var debounce = require('lodash/debounce');
8
8
  var styleInject_es = require('../style-inject.es-9d2f5f4e.js');
9
9
  var classnames = require('classnames');
10
10
  var useEventListener = require('@use-it/event-listener');
11
+ var ReactDOM = require('react-dom');
12
+ var reactPopper = require('react-popper');
11
13
  var Text = require('../Text-e7ed0974.js');
12
14
  var Icon = require('../Icon-405a216c.js');
13
15
  var Heading = require('../Heading-a1191b15.js');
@@ -39,19 +41,16 @@ var IndexChange;
39
41
  IndexChange[IndexChange["Previous"] = -1] = "Previous";
40
42
  IndexChange[IndexChange["Next"] = 1] = "Next";
41
43
  })(IndexChange || (IndexChange = {}));
42
- function Menu({ visible, options, selectedOption, onOptionSelect, }) {
44
+ function Menu({ visible, options, selectedOption, onOptionSelect, attachTo, }) {
43
45
  const [highlightedIndex, setHighlightedIndex] = React.useState(0);
44
- const optionMenuClass = classnames__default["default"](styles.options, {
45
- [styles.visible]: visible,
46
- });
47
- const menuDiv = React.useRef();
46
+ const { setPositionedElementRef, styles: popperStyles, attributes, targetWidth, } = useRepositionMenu(attachTo);
48
47
  const detectSeparatorCondition = (option) => option.description || option.details;
49
48
  const detectGroups = (option) => option.options;
50
49
  const addSeparators = options.some(detectSeparatorCondition);
51
50
  const initialHighlight = options.some(detectGroups) ? 1 : 0;
52
51
  setupKeyListeners();
53
52
  React.useEffect(() => setHighlightedIndex(initialHighlight), [options]);
54
- return (React__default["default"].createElement("div", { className: optionMenuClass, ref: menuDiv }, options.map((option, index) => {
53
+ const menu = (React__default["default"].createElement("div", Object.assign({ className: classnames__default["default"](styles.options, { [styles.visible]: visible }), ref: setPositionedElementRef, style: Object.assign(Object.assign({}, popperStyles.popper), { width: targetWidth }) }, attributes.popper), options.map((option, index) => {
55
54
  const optionClass = classnames__default["default"](styles.option, {
56
55
  [styles.active]: index === highlightedIndex,
57
56
  [styles.separator]: addSeparators,
@@ -69,40 +68,20 @@ function Menu({ visible, options, selectedOption, onOptionSelect, }) {
69
68
  option.details !== undefined && (React__default["default"].createElement("div", { className: styles.details },
70
69
  React__default["default"].createElement(Text.Text, null, option.details))))));
71
70
  })));
71
+ return ReactDOM.createPortal(menu, document.body);
72
72
  function setupKeyListeners() {
73
73
  useOnKeyDown("ArrowDown", (event) => {
74
74
  const indexChange = arrowKeyPress(event, IndexChange.Next);
75
75
  if (indexChange) {
76
76
  setHighlightedIndex(Math.min(options.length - 1, highlightedIndex + indexChange));
77
77
  }
78
- if (menuDiv.current) {
79
- scrollMenuIfItemNotInView(menuDiv.current, "down");
80
- }
81
78
  });
82
79
  useOnKeyDown("ArrowUp", (event) => {
83
80
  const indexChange = arrowKeyPress(event, IndexChange.Previous);
84
81
  if (indexChange) {
85
82
  setHighlightedIndex(Math.max(0, highlightedIndex + indexChange));
86
83
  }
87
- if (menuDiv.current) {
88
- scrollMenuIfItemNotInView(menuDiv.current, "up");
89
- }
90
84
  });
91
- function scrollMenuIfItemNotInView(menuDivElement, direction) {
92
- const itemDiv = menuDivElement.querySelector(`button.${styles.option}:nth-child(${highlightedIndex + 1})`);
93
- if (!itemDiv)
94
- return;
95
- const menuTop = menuDivElement.getBoundingClientRect().top;
96
- const { top: itemTop, height: itemHeight, bottom: itemBottom, } = itemDiv.getBoundingClientRect();
97
- const itemTrueBottom = itemBottom + itemHeight;
98
- const menuBottom = menuDivElement.getBoundingClientRect().bottom;
99
- if (direction == "up" && itemTop - itemHeight < menuTop) {
100
- menuDivElement.scrollTop -= itemHeight;
101
- }
102
- else if (direction == "down" && itemTrueBottom > menuBottom) {
103
- menuDivElement.scrollTop += itemHeight;
104
- }
105
- }
106
85
  useOnKeyDown("Enter", (event) => {
107
86
  if (!visible)
108
87
  return;
@@ -141,6 +120,18 @@ function isGroup(option) {
141
120
  return true;
142
121
  return false;
143
122
  }
123
+ function useRepositionMenu(attachTo) {
124
+ var _a;
125
+ const [positionElement, setPositionedElementRef] = React.useState();
126
+ const popper = reactPopper.usePopper(attachTo.current, positionElement, {
127
+ modifiers: [
128
+ { name: "offset", options: { offset: [0, 8] } },
129
+ { name: "flip", options: { fallbackPlacements: ["top"] } },
130
+ ],
131
+ });
132
+ const targetWidth = (_a = attachTo.current) === null || _a === void 0 ? void 0 : _a.clientWidth;
133
+ return Object.assign(Object.assign({}, popper), { setPositionedElementRef, targetWidth });
134
+ }
144
135
 
145
136
  /**
146
137
  * Max statements disabled here to make room for the
@@ -152,6 +143,7 @@ function Autocomplete({ initialOptions = [], value, allowFreeForm = true, size =
152
143
  const [options, setOptions] = React.useState(initialOptions);
153
144
  const [menuVisible, setMenuVisible] = React.useState(false);
154
145
  const [inputText, setInputText] = React.useState((_a = value === null || value === void 0 ? void 0 : value.label) !== null && _a !== void 0 ? _a : "");
146
+ const autocompleteRef = React.useRef(null);
155
147
  const delayedSearch = debounce__default["default"](updateSearch, debounceRate);
156
148
  React.useEffect(() => {
157
149
  delayedSearch();
@@ -161,9 +153,9 @@ function Autocomplete({ initialOptions = [], value, allowFreeForm = true, size =
161
153
  var _a;
162
154
  updateInput((_a = value === null || value === void 0 ? void 0 : value.label) !== null && _a !== void 0 ? _a : "");
163
155
  }, [value]);
164
- return (React__default["default"].createElement("div", { className: styles.autocomplete },
156
+ return (React__default["default"].createElement("div", { className: styles.autocomplete, ref: autocompleteRef },
165
157
  React__default["default"].createElement(InputText.InputText, { autocomplete: false, size: size, invalid: invalid, value: inputText, onChange: handleInputChange, placeholder: placeholder, onFocus: handleInputFocus, onBlur: handleInputBlur }),
166
- menuVisible && (React__default["default"].createElement(Menu, { visible: true, options: options, selectedOption: value, onOptionSelect: handleMenuChange }))));
158
+ menuVisible && (React__default["default"].createElement(Menu, { attachTo: autocompleteRef, visible: true, options: options, selectedOption: value, onOptionSelect: handleMenuChange }))));
167
159
  function updateInput(newText) {
168
160
  setInputText(newText);
169
161
  if (newText === "") {
@@ -23,5 +23,5 @@ export declare function useInternalChipDismissibleInput({ options, isLoadingMore
23
23
  handleSetActiveOnMouseOver: (index: number) => () => void;
24
24
  handleSelectOption: (selected: ChipDismissibleInputOptionProps) => void;
25
25
  handleKeyDown: (event: KeyboardEvent<HTMLInputElement>) => void;
26
- handleDebouncedSearch: (() => void) & import("lodash").Cancelable;
26
+ handleDebouncedSearch: import("lodash").DebouncedFunc<() => void>;
27
27
  };
@@ -18,7 +18,7 @@ export declare const InputText: React.ForwardRefExoticComponent<({
18
18
  readonly inline?: boolean | undefined;
19
19
  loading?: boolean | undefined;
20
20
  readonly name?: string | undefined;
21
- onChange?: ((newValue: string | number | boolean | Date, event?: React.ChangeEvent<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement> | undefined) => void) | undefined;
21
+ onChange?: ((newValue: string | number | boolean | Date, event?: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement> | undefined) => void) | undefined;
22
22
  onValidation?: ((message: string) => void) | undefined;
23
23
  readonly placeholder?: string | undefined;
24
24
  readonly size?: "large" | "small" | undefined;
@@ -28,7 +28,7 @@ export declare const InputText: React.ForwardRefExoticComponent<({
28
28
  onFocus?: (() => void) | undefined;
29
29
  onBlur?: (() => void) | undefined;
30
30
  readonly autocomplete?: boolean | import("../FormField").AutocompleteTypes | undefined;
31
- inputRef?: React.RefObject<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement> | undefined;
31
+ inputRef?: React.RefObject<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement> | undefined;
32
32
  readonly keyboard?: "numeric" | "decimal" | undefined;
33
33
  readonly maxLength?: number | undefined;
34
34
  readonly suffix?: {
@@ -66,7 +66,7 @@ export declare const InputText: React.ForwardRefExoticComponent<({
66
66
  readonly inline?: boolean | undefined;
67
67
  loading?: boolean | undefined;
68
68
  readonly name?: string | undefined;
69
- onChange?: ((newValue: string | number | boolean | Date, event?: React.ChangeEvent<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement> | undefined) => void) | undefined;
69
+ onChange?: ((newValue: string | number | boolean | Date, event?: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement> | undefined) => void) | undefined;
70
70
  onValidation?: ((message: string) => void) | undefined;
71
71
  readonly placeholder?: string | undefined;
72
72
  readonly size?: "large" | "small" | undefined;
@@ -76,7 +76,7 @@ export declare const InputText: React.ForwardRefExoticComponent<({
76
76
  onFocus?: (() => void) | undefined;
77
77
  onBlur?: (() => void) | undefined;
78
78
  readonly autocomplete?: boolean | import("../FormField").AutocompleteTypes | undefined;
79
- inputRef?: React.RefObject<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement> | undefined;
79
+ inputRef?: React.RefObject<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement> | undefined;
80
80
  readonly keyboard?: "numeric" | "decimal" | undefined;
81
81
  readonly maxLength?: number | undefined;
82
82
  readonly suffix?: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jobber/components",
3
- "version": "4.7.1-jeff.1+61268033",
3
+ "version": "4.7.1-mike.2+a1a4e681",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -24,7 +24,7 @@
24
24
  "@std-proposal/temporal": "0.0.1",
25
25
  "@tanstack/react-table": "8.5.13",
26
26
  "@types/color": "^3.0.1",
27
- "@types/lodash": "4.14.136",
27
+ "@types/lodash": "^4.14.136",
28
28
  "@types/react-datepicker": "^4.8.0",
29
29
  "@types/react-router": "5.1.7",
30
30
  "@types/react-router-dom": "^5.3.3",
@@ -82,5 +82,5 @@
82
82
  "> 1%",
83
83
  "IE 10"
84
84
  ],
85
- "gitHead": "61268033eb69a2abc7b80657c9f8f047b5e3fcbb"
85
+ "gitHead": "a1a4e6815a6bb4d8ea9904a7465886d300a706d8"
86
86
  }