@via-profit/ui-kit 0.1.0-rc.42 → 0.1.0-rc.44
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,5 +1,5 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import { Value,
|
|
2
|
+
import { Value, OnRequestClose, GetOptionSelected } from '../Menu';
|
|
3
3
|
import type { MenuItemCommonProps } from '../Menu/MenuItem';
|
|
4
4
|
export type AutocompleteProps<T, Multiple extends boolean | undefined = undefined> = {
|
|
5
5
|
readonly value: Value<T, Multiple> | null;
|
|
@@ -21,8 +21,9 @@ export type AutocompleteProps<T, Multiple extends boolean | undefined = undefine
|
|
|
21
21
|
readonly clearIfNotSelected?: boolean;
|
|
22
22
|
readonly children: Children<T>;
|
|
23
23
|
readonly filterItems?: FilterItems<T>;
|
|
24
|
-
readonly
|
|
24
|
+
readonly onChange?: OnChange<T, Multiple>;
|
|
25
25
|
readonly selectedItemToString: ItemToString<T, Multiple>;
|
|
26
|
+
readonly getOptionSelected?: GetOptionSelected<T>;
|
|
26
27
|
readonly onRequestClose?: OnRequestClose;
|
|
27
28
|
readonly onInputChange?: React.ChangeEventHandler<HTMLInputElement>;
|
|
28
29
|
readonly onRequestOpen?: (event: React.KeyboardEvent<HTMLInputElement> | React.MouseEvent<HTMLInputElement> | React.FocusEvent<HTMLInputElement, Element>) => void;
|
|
@@ -33,6 +34,7 @@ export type Children<T> = (data: {
|
|
|
33
34
|
inputValue: string;
|
|
34
35
|
}, itemProps: MenuItemCommonProps) => React.ReactNode;
|
|
35
36
|
export type ItemToString<T, Multiple extends boolean | undefined = undefined> = (item: Value<T, Multiple>) => string;
|
|
37
|
+
export type OnChange<T, Multiple extends boolean | undefined = undefined> = (item: Value<T, Multiple> | null) => void;
|
|
36
38
|
export type FilterItems<T> = (items: readonly T[], data: {
|
|
37
39
|
readonly query: string;
|
|
38
40
|
readonly inputValue: string;
|
|
@@ -34,15 +34,15 @@ const Spinner_1 = __importDefault(require("../LoadingIndicator/Spinner"));
|
|
|
34
34
|
const IconClear_1 = __importDefault(require("./IconClear"));
|
|
35
35
|
const context_1 = __importStar(require("./context"));
|
|
36
36
|
const Autocomplete = react_1.default.forwardRef((props, ref) => {
|
|
37
|
-
const { items, value, multiple, isOpen = false, isLoading = false, clearIfNotSelected = true, filterItems, children,
|
|
37
|
+
const { items, value, multiple, isOpen = false, isLoading = false, clearIfNotSelected = true, filterItems, children, onChange, selectedItemToString, onInputChange, getOptionSelected, onRequestOpen = () => undefined, onRequestClose = () => undefined, } = props;
|
|
38
38
|
const menuRef = react_1.default.useRef(null);
|
|
39
39
|
const inputRef = react_1.default.useRef(null);
|
|
40
40
|
const isFocusedRef = react_1.default.useRef(false);
|
|
41
41
|
const { state, dispatch } = (0, context_1.default)();
|
|
42
42
|
const { currentOpen, filteredItems, inputValue, currentValue, anchorElement, currentLoading } = state;
|
|
43
43
|
const clear = react_1.default.useCallback(() => {
|
|
44
|
-
if (
|
|
45
|
-
|
|
44
|
+
if (onChange) {
|
|
45
|
+
onChange(null);
|
|
46
46
|
}
|
|
47
47
|
dispatch((0, context_1.actionSetPartial)({
|
|
48
48
|
filteredItems: items,
|
|
@@ -53,7 +53,7 @@ const Autocomplete = react_1.default.forwardRef((props, ref) => {
|
|
|
53
53
|
var _a;
|
|
54
54
|
(_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus();
|
|
55
55
|
}, 15);
|
|
56
|
-
}, [dispatch,
|
|
56
|
+
}, [dispatch, onChange, items]);
|
|
57
57
|
/**
|
|
58
58
|
* API
|
|
59
59
|
*/
|
|
@@ -198,9 +198,9 @@ const Autocomplete = react_1.default.forwardRef((props, ref) => {
|
|
|
198
198
|
filterItems,
|
|
199
199
|
items,
|
|
200
200
|
]),
|
|
201
|
-
react_1.default.useMemo(() => (react_1.default.createElement(Menu_1.default, { ref: menuRef, anchorPos: "left-bottom-right", multiple: multiple, items: filteredItems, value: currentValue, isOpen: currentOpen && filteredItems.length > 0, autofocus: false, anchorElement: anchorElement, closeOutsideClick: true, onSelectItem: item => {
|
|
202
|
-
if (typeof
|
|
203
|
-
|
|
201
|
+
react_1.default.useMemo(() => (react_1.default.createElement(Menu_1.default, { ref: menuRef, anchorPos: "left-bottom-right", multiple: multiple, items: filteredItems, value: currentValue, isOpen: currentOpen && filteredItems.length > 0, autofocus: false, anchorElement: anchorElement, closeOutsideClick: true, getOptionSelected: getOptionSelected, onSelectItem: item => {
|
|
202
|
+
if (typeof onChange === 'function') {
|
|
203
|
+
onChange(item);
|
|
204
204
|
}
|
|
205
205
|
if (!multiple) {
|
|
206
206
|
dispatch((0, context_1.actionSetPartial)({
|
|
@@ -221,7 +221,8 @@ const Autocomplete = react_1.default.forwardRef((props, ref) => {
|
|
|
221
221
|
children,
|
|
222
222
|
dispatch,
|
|
223
223
|
onRequestClose,
|
|
224
|
-
|
|
224
|
+
onChange,
|
|
225
|
+
getOptionSelected,
|
|
225
226
|
selectedItemToString,
|
|
226
227
|
])));
|
|
227
228
|
});
|