@via-profit/ui-kit 0.1.0-rc.50 → 0.1.0-rc.52
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.
|
@@ -3,7 +3,9 @@ import { Value, OnRequestClose, GetOptionSelected } from '../Menu';
|
|
|
3
3
|
import { TextFieldProps } from '../TextField';
|
|
4
4
|
import type { AnchorPos } from '../Popper';
|
|
5
5
|
import type { MenuItemCommonProps } from '../Menu/MenuItem';
|
|
6
|
-
|
|
6
|
+
type NativeInputProps = React.InputHTMLAttributes<HTMLInputElement>;
|
|
7
|
+
type AutocompleteInputProps = Omit<NativeInputProps, 'onChange' | 'value' | 'children'>;
|
|
8
|
+
export interface AutocompleteProps<T, Multiple extends boolean | undefined = undefined> extends AutocompleteInputProps {
|
|
7
9
|
readonly items: readonly T[];
|
|
8
10
|
readonly value: Value<T, Multiple>;
|
|
9
11
|
/**
|
|
@@ -42,11 +44,39 @@ export interface AutocompleteProps<T, Multiple extends boolean | undefined = und
|
|
|
42
44
|
* Field placeholder
|
|
43
45
|
*/
|
|
44
46
|
readonly placeholder?: string;
|
|
47
|
+
/**
|
|
48
|
+
* If is true then the asterisk will be displayed in label\
|
|
49
|
+
* If is ReactNode then ReactNode will be displayed in label
|
|
50
|
+
*/
|
|
51
|
+
readonly requiredAsterisk?: boolean | React.ReactNode;
|
|
52
|
+
/**
|
|
53
|
+
* If true then TextField will be filled in full width on horizontal
|
|
54
|
+
*/
|
|
55
|
+
readonly fullWidth?: boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Native <input> element reference
|
|
58
|
+
*/
|
|
59
|
+
readonly inputRef?: React.MutableRefObject<HTMLInputElement | null> | React.RefCallback<HTMLInputElement>;
|
|
60
|
+
/**
|
|
61
|
+
* End icon component
|
|
62
|
+
*/
|
|
63
|
+
readonly endIcon?: React.ReactElement;
|
|
64
|
+
/**
|
|
65
|
+
* Start icon component
|
|
66
|
+
*/
|
|
67
|
+
readonly startIcon?: React.ReactElement;
|
|
45
68
|
/**
|
|
46
69
|
* Field label text or element
|
|
47
70
|
*/
|
|
48
71
|
readonly label?: TextFieldProps['label'];
|
|
72
|
+
/**
|
|
73
|
+
* If true then `errorText` value will be displayed under the TextField element
|
|
74
|
+
*/
|
|
49
75
|
readonly error?: TextFieldProps['error'];
|
|
76
|
+
/**
|
|
77
|
+
* Text or ReactNode to show in error element\
|
|
78
|
+
* Will be displaed only if `error` property is tru
|
|
79
|
+
*/
|
|
50
80
|
readonly errorText?: TextFieldProps['errorText'];
|
|
51
81
|
readonly filterItems?: FilterItems<T>;
|
|
52
82
|
readonly onChange?: OnChange<T, Multiple>;
|
|
@@ -22,6 +22,17 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
22
22
|
__setModuleDefault(result, mod);
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
26
|
+
var t = {};
|
|
27
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
28
|
+
t[p] = s[p];
|
|
29
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
30
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
31
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
32
|
+
t[p[i]] = s[p[i]];
|
|
33
|
+
}
|
|
34
|
+
return t;
|
|
35
|
+
};
|
|
25
36
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
37
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
38
|
};
|
|
@@ -34,9 +45,9 @@ const Spinner_1 = __importDefault(require("../LoadingIndicator/Spinner"));
|
|
|
34
45
|
const IconClear_1 = __importDefault(require("./IconClear"));
|
|
35
46
|
const context_1 = __importStar(require("./context"));
|
|
36
47
|
const Autocomplete = react_1.default.forwardRef((props, ref) => {
|
|
37
|
-
const { items, value, multiple, isOpen = false, isLoading = false, clearIfNotSelected = true, anchorPos = 'auto-start-end', placeholder, label, error, errorText, filterItems, children, onChange, selectedItemToString, onInputChange, getOptionSelected, onRequestOpen = () => undefined, onRequestClose = () => undefined
|
|
48
|
+
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 } = 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"]);
|
|
38
49
|
const menuRef = react_1.default.useRef(null);
|
|
39
|
-
const
|
|
50
|
+
const fieldInputRef = react_1.default.useRef(null);
|
|
40
51
|
const isFocusedRef = react_1.default.useRef(false);
|
|
41
52
|
const { state, dispatch } = (0, context_1.default)();
|
|
42
53
|
const { currentOpen, filteredItems, inputValue, currentValue, anchorElement, currentLoading } = state;
|
|
@@ -51,7 +62,7 @@ const Autocomplete = react_1.default.forwardRef((props, ref) => {
|
|
|
51
62
|
}));
|
|
52
63
|
setTimeout(() => {
|
|
53
64
|
var _a;
|
|
54
|
-
(_a =
|
|
65
|
+
(_a = fieldInputRef.current) === null || _a === void 0 ? void 0 : _a.focus();
|
|
55
66
|
}, 15);
|
|
56
67
|
}, [dispatch, onChange, items, multiple]);
|
|
57
68
|
/**
|
|
@@ -144,11 +155,19 @@ const Autocomplete = react_1.default.forwardRef((props, ref) => {
|
|
|
144
155
|
dispatch((0, context_1.actionSetPartial)({ currentOpen: isOpen, currentLoading: isLoading, filteredItems: items }));
|
|
145
156
|
}, [isOpen, isLoading, items, dispatch]);
|
|
146
157
|
return (react_1.default.createElement("div", null,
|
|
147
|
-
react_1.default.useMemo(() => (react_1.default.createElement(TextField_1.default, { placeholder: placeholder, label: label, error: error, errorText: errorText, 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)), onKeyDown: inputKeydownEvent, ref: el => {
|
|
158
|
+
react_1.default.useMemo(() => (react_1.default.createElement(TextField_1.default, Object.assign({}, nativeInputProps, { placeholder: placeholder, label: label, error: error, requiredAsterisk: requiredAsterisk, errorText: errorText, fullWidth: fullWidth, startIcon: startIcon, 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)), onKeyDown: inputKeydownEvent, ref: el => {
|
|
148
159
|
if (anchorElement !== el) {
|
|
149
160
|
dispatch((0, context_1.actionSetPartial)({ anchorElement: el }));
|
|
150
161
|
}
|
|
151
|
-
}, inputRef:
|
|
162
|
+
}, inputRef: el => {
|
|
163
|
+
fieldInputRef.current = el;
|
|
164
|
+
if (typeof inputRef === 'function') {
|
|
165
|
+
inputRef(el);
|
|
166
|
+
}
|
|
167
|
+
if (inputRef && typeof inputRef === 'object') {
|
|
168
|
+
inputRef.current = el;
|
|
169
|
+
}
|
|
170
|
+
}, value: inputValue, onBlur: () => {
|
|
152
171
|
isFocusedRef.current = false;
|
|
153
172
|
if (clearIfNotSelected) {
|
|
154
173
|
dispatch((0, context_1.actionSetPartial)({
|
|
@@ -179,19 +198,24 @@ const Autocomplete = react_1.default.forwardRef((props, ref) => {
|
|
|
179
198
|
dispatch((0, context_1.actionSetPartial)({
|
|
180
199
|
filteredItems: newItems,
|
|
181
200
|
}));
|
|
182
|
-
} })), [
|
|
201
|
+
} }))), [
|
|
183
202
|
placeholder,
|
|
184
203
|
label,
|
|
185
|
-
multiple,
|
|
186
204
|
error,
|
|
205
|
+
requiredAsterisk,
|
|
187
206
|
errorText,
|
|
207
|
+
fullWidth,
|
|
208
|
+
startIcon,
|
|
188
209
|
currentLoading,
|
|
189
210
|
inputKeydownEvent,
|
|
190
211
|
inputValue,
|
|
191
212
|
clear,
|
|
192
213
|
anchorElement,
|
|
193
214
|
dispatch,
|
|
215
|
+
inputRef,
|
|
194
216
|
clearIfNotSelected,
|
|
217
|
+
multiple,
|
|
218
|
+
nativeInputProps,
|
|
195
219
|
value,
|
|
196
220
|
selectedItemToString,
|
|
197
221
|
onRequestOpen,
|