@via-profit/ui-kit 0.1.0-rc.57 → 0.1.0-rc.58
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.
|
@@ -44,6 +44,7 @@ const Button_1 = __importDefault(require("../Button"));
|
|
|
44
44
|
const Spinner_1 = __importDefault(require("../LoadingIndicator/Spinner"));
|
|
45
45
|
const context_1 = __importStar(require("./context"));
|
|
46
46
|
const IconClear_1 = __importDefault(require("./IconClear"));
|
|
47
|
+
const ClickOutside_1 = require("../ClickOutside");
|
|
47
48
|
const Autocomplete = react_1.default.forwardRef((props, ref) => {
|
|
48
49
|
const { items, value, multiple, isOpen = false, isLoading = false, clearIfNotSelected = true, anchorPos = 'auto-start-end', requiredAsterisk, startIcon, fullWidth, placeholder, label, error, errorText, inputRef, filterItems, children, onChange, selectedItemToString, onInputChange, getOptionSelected, onRequestOpen = () => undefined, onRequestClose = () => undefined, overrides } = props, nativeInputProps = __rest(props, ["items", "value", "multiple", "isOpen", "isLoading", "clearIfNotSelected", "anchorPos", "requiredAsterisk", "startIcon", "fullWidth", "placeholder", "label", "error", "errorText", "inputRef", "filterItems", "children", "onChange", "selectedItemToString", "onInputChange", "getOptionSelected", "onRequestOpen", "onRequestClose", "overrides"]);
|
|
49
50
|
const menuRef = react_1.default.useRef(null);
|
|
@@ -155,6 +156,27 @@ const Autocomplete = react_1.default.forwardRef((props, ref) => {
|
|
|
155
156
|
react_1.default.useEffect(() => {
|
|
156
157
|
dispatch((0, context_1.actionSetPartial)({ currentOpen: isOpen, currentLoading: isLoading, filteredItems: items }));
|
|
157
158
|
}, [isOpen, isLoading, items, dispatch]);
|
|
159
|
+
react_1.default.useEffect(() => {
|
|
160
|
+
const mouseDownEvent = (event) => {
|
|
161
|
+
var _a;
|
|
162
|
+
let parentElem = event.target;
|
|
163
|
+
let needToClose = true;
|
|
164
|
+
while (parentElem && 'parentNode' in parentElem) {
|
|
165
|
+
if (parentElem === anchorElement || parentElem === ((_a = menuRef.current) === null || _a === void 0 ? void 0 : _a.getListElement())) {
|
|
166
|
+
needToClose = false;
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
parentElem = parentElem.parentNode;
|
|
170
|
+
}
|
|
171
|
+
if (needToClose) {
|
|
172
|
+
onRequestClose(event);
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
window.document.addEventListener(ClickOutside_1.mouseEventMap.onMouseDown, mouseDownEvent);
|
|
176
|
+
return () => {
|
|
177
|
+
window.document.removeEventListener(ClickOutside_1.mouseEventMap.onMouseDown, mouseDownEvent);
|
|
178
|
+
};
|
|
179
|
+
}, [onRequestClose, anchorElement]);
|
|
158
180
|
return (react_1.default.createElement("div", null,
|
|
159
181
|
react_1.default.useMemo(() => (react_1.default.createElement(overridesMap.TextField, Object.assign({ placeholder: placeholder, label: label, error: error, requiredAsterisk: requiredAsterisk, errorText: errorText, fullWidth: fullWidth, startIcon: startIcon, isLoading: currentLoading, endIcon: react_1.default.createElement(Button_1.default, { iconOnly: true, type: "button", onClick: () => (currentLoading ? 'undefined' : clear()) }, currentLoading ? react_1.default.createElement(Spinner_1.default, null) : react_1.default.createElement(IconClear_1.default, null)) }, nativeInputProps, { onKeyDown: event => {
|
|
160
182
|
inputKeydownEvent(event);
|
|
@@ -175,15 +197,6 @@ const Autocomplete = react_1.default.forwardRef((props, ref) => {
|
|
|
175
197
|
}
|
|
176
198
|
}, value: inputValue, onBlur: event => {
|
|
177
199
|
isFocusedRef.current = false;
|
|
178
|
-
if (clearIfNotSelected) {
|
|
179
|
-
dispatch((0, context_1.actionSetPartial)({
|
|
180
|
-
inputValue: multiple
|
|
181
|
-
? ''
|
|
182
|
-
: value !== null
|
|
183
|
-
? selectedItemToString(value)
|
|
184
|
-
: '',
|
|
185
|
-
}));
|
|
186
|
-
}
|
|
187
200
|
if (typeof nativeInputProps.onBlur === 'function') {
|
|
188
201
|
nativeInputProps.onBlur(event);
|
|
189
202
|
}
|
|
@@ -230,36 +243,45 @@ const Autocomplete = react_1.default.forwardRef((props, ref) => {
|
|
|
230
243
|
anchorElement,
|
|
231
244
|
dispatch,
|
|
232
245
|
inputRef,
|
|
233
|
-
clearIfNotSelected,
|
|
234
|
-
multiple,
|
|
235
246
|
nativeInputProps,
|
|
236
|
-
value,
|
|
237
|
-
selectedItemToString,
|
|
238
247
|
onRequestOpen,
|
|
239
248
|
onInputChange,
|
|
240
249
|
filterItems,
|
|
241
250
|
items,
|
|
242
251
|
]),
|
|
243
|
-
react_1.default.useMemo(() => (react_1.default.createElement(Menu_1.default, { ref: menuRef, anchorPos: anchorPos, multiple: multiple, items: filteredItems, value: currentValue, isOpen: currentOpen && filteredItems.length > 0, autofocus: false, anchorElement: anchorElement, closeOutsideClick:
|
|
252
|
+
react_1.default.useMemo(() => (react_1.default.createElement(Menu_1.default, { ref: menuRef, anchorPos: anchorPos, multiple: multiple, items: filteredItems, value: currentValue, isOpen: currentOpen && filteredItems.length > 0, autofocus: false, anchorElement: anchorElement, closeOutsideClick: false, getOptionSelected: getOptionSelected, onSelectItem: item => {
|
|
244
253
|
if (typeof onChange === 'function') {
|
|
245
254
|
onChange(item);
|
|
246
255
|
}
|
|
247
256
|
}, closeOnSelect: multiple ? false : true, onRequestClose: evt => {
|
|
257
|
+
if (clearIfNotSelected) {
|
|
258
|
+
dispatch((0, context_1.actionSetPartial)({
|
|
259
|
+
inputValue: multiple
|
|
260
|
+
? ''
|
|
261
|
+
: value !== null
|
|
262
|
+
? selectedItemToString(value)
|
|
263
|
+
: '',
|
|
264
|
+
}));
|
|
265
|
+
}
|
|
248
266
|
if ((evt === null || evt === void 0 ? void 0 : evt.target) !== anchorElement) {
|
|
249
267
|
onRequestClose(evt);
|
|
250
268
|
}
|
|
251
269
|
} }, ({ index, item }, itemProps) => children({ index, item: item, inputValue }, itemProps))), [
|
|
252
|
-
anchorElement,
|
|
253
|
-
currentOpen,
|
|
254
|
-
currentValue,
|
|
255
|
-
filteredItems,
|
|
256
|
-
inputValue,
|
|
257
|
-
multiple,
|
|
258
270
|
anchorPos,
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
271
|
+
multiple,
|
|
272
|
+
filteredItems,
|
|
273
|
+
currentValue,
|
|
274
|
+
currentOpen,
|
|
275
|
+
anchorElement,
|
|
262
276
|
getOptionSelected,
|
|
277
|
+
onChange,
|
|
278
|
+
clearIfNotSelected,
|
|
279
|
+
dispatch,
|
|
280
|
+
value,
|
|
281
|
+
selectedItemToString,
|
|
282
|
+
onRequestClose,
|
|
283
|
+
children,
|
|
284
|
+
inputValue,
|
|
263
285
|
])));
|
|
264
286
|
});
|
|
265
287
|
Autocomplete.displayName = 'Autocomplete';
|
package/ClickOutside/index.d.ts
CHANGED
|
@@ -17,6 +17,7 @@ export interface ClickOutsideProps {
|
|
|
17
17
|
readonly mouseEvent?: OutsideMouseEventName | false;
|
|
18
18
|
}
|
|
19
19
|
type OutsideMouseEventName = 'onClick' | 'onMouseDown' | 'onMouseUp';
|
|
20
|
+
export declare const mouseEventMap: Record<OutsideMouseEventName, 'click' | 'mousedown' | 'mouseup'>;
|
|
20
21
|
export type OnOutsideClick = (event?: React.MouseEvent<HTMLElement> | MouseEvent) => void;
|
|
21
22
|
declare const ClickOutside: React.FC<ClickOutsideProps>;
|
|
22
23
|
export default ClickOutside;
|
package/ClickOutside/index.js
CHANGED
|
@@ -3,8 +3,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.mouseEventMap = void 0;
|
|
6
7
|
const react_1 = __importDefault(require("react"));
|
|
7
|
-
|
|
8
|
+
exports.mouseEventMap = {
|
|
8
9
|
onClick: 'click',
|
|
9
10
|
onMouseDown: 'mousedown',
|
|
10
11
|
onMouseUp: 'mouseup',
|
|
@@ -28,11 +29,11 @@ const ClickOutside = props => {
|
|
|
28
29
|
}
|
|
29
30
|
};
|
|
30
31
|
if (mouseEvent) {
|
|
31
|
-
window.document.addEventListener(mouseEventMap[mouseEvent], mouseDownEvent);
|
|
32
|
+
window.document.addEventListener(exports.mouseEventMap[mouseEvent], mouseDownEvent);
|
|
32
33
|
}
|
|
33
34
|
return () => {
|
|
34
35
|
if (mouseEvent) {
|
|
35
|
-
window.document.removeEventListener(mouseEventMap[mouseEvent], mouseDownEvent);
|
|
36
|
+
window.document.removeEventListener(exports.mouseEventMap[mouseEvent], mouseDownEvent);
|
|
36
37
|
}
|
|
37
38
|
};
|
|
38
39
|
}, [onOutsideClick, mouseEvent]);
|
package/Menu/MenuContainer.d.ts
CHANGED
|
@@ -185,6 +185,10 @@ export type MenuRef = {
|
|
|
185
185
|
* Select highlighted item in list
|
|
186
186
|
*/
|
|
187
187
|
selectHightlightedItem: () => void;
|
|
188
|
+
/**
|
|
189
|
+
* Returns the list HTML container
|
|
190
|
+
*/
|
|
191
|
+
getListElement: () => HTMLDivElement | null;
|
|
188
192
|
};
|
|
189
193
|
export type Value<T, Multiple> = Multiple extends undefined | undefined ? T | null : readonly T[];
|
|
190
194
|
export type GetOptionSelected<T> = (payload: {
|
package/Menu/MenuContainer.js
CHANGED
|
@@ -188,6 +188,7 @@ const MenuContainer = react_1.default.forwardRef((props, ref) => {
|
|
|
188
188
|
scrollToFirstSelected: () => scrollToFirstSelected(),
|
|
189
189
|
selectHightlightedItem: () => selectHightlightedItem(),
|
|
190
190
|
selectItem: (idx) => selectItem(idx),
|
|
191
|
+
getListElement: () => menuListRef.current,
|
|
191
192
|
}), [
|
|
192
193
|
scrollToIndex,
|
|
193
194
|
highlightIndex,
|