@via-profit/ui-kit 0.1.0-rc.39 → 0.1.0-rc.40
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.
- package/Autocomplete/AutocompleteContainer.d.ts +46 -0
- package/Autocomplete/AutocompleteContainer.js +229 -0
- package/Autocomplete/IconClear.js +9 -0
- package/Autocomplete/context.d.ts +27 -0
- package/Autocomplete/context.js +48 -0
- package/Autocomplete/index.d.ts +4 -43
- package/Autocomplete/index.js +31 -177
- package/Calendar/index.d.ts +1 -1
- package/Calendar/index.js +4 -8
- package/Calendar/use-calendar.js +1 -1
- package/Color/index.d.ts +4 -4
- package/Color/index.js +9 -9
- package/Highlighted/HighlightedContainer.d.ts +4 -0
- package/Highlighted/HighlightedContainer.js +22 -0
- package/Highlighted/HighlightedMark.d.ts +4 -0
- package/Highlighted/HighlightedMark.js +30 -0
- package/Highlighted/HighlightedText.d.ts +4 -0
- package/Highlighted/HighlightedText.js +22 -0
- package/Highlighted/index.d.ts +45 -0
- package/Highlighted/index.js +39 -0
- package/LoadingIndicator/Spinner.d.ts +4 -0
- package/LoadingIndicator/Spinner.js +42 -0
- package/LoadingIndicator/index.d.ts +3 -0
- package/LoadingIndicator/index.js +9 -0
- package/Menu/MenuContainer.d.ts +165 -0
- package/Menu/MenuContainer.js +432 -0
- package/Menu/MenuItem.d.ts +9 -7
- package/Menu/MenuItem.js +39 -30
- package/Menu/MenuList.d.ts +6 -0
- package/Menu/{MenuWrapper.js → MenuList.js} +9 -22
- package/Menu/MenuListPopper.d.ts +7 -0
- package/Menu/MenuListPopper.js +30 -0
- package/Menu/context.d.ts +23 -0
- package/Menu/context.js +46 -0
- package/Menu/index.d.ts +3 -192
- package/Menu/index.js +32 -420
- package/Modal/BaseModal/context.d.ts +1 -1
- package/Modal/BaseModal/context.js +1 -1
- package/Modal/BaseModal/index.js +1 -1
- package/Modal/ConfirmBox/ConfirmBoxFooter.d.ts +1 -1
- package/Modal/Drawer/DrawerHeader.d.ts +1 -1
- package/Modal/MessageBox/MessageBoxFooter.d.ts +1 -1
- package/README.md +1 -2
- package/package.json +28 -35
- package/Autocomplete/popper-options.d.ts +0 -4
- package/Autocomplete/popper-options.js +0 -33
- package/Menu/MenuItemInner.d.ts +0 -7
- package/Menu/MenuItemInner.js +0 -48
- package/Menu/MenuWrapper.d.ts +0 -10
- package/Menu/popper-options.d.ts +0 -4
- package/Menu/popper-options.js +0 -33
- package/Menu/reducer.d.ts +0 -14
- package/Menu/reducer.js +0 -19
- package/SelectBox/IconChevronDown.js +0 -9
- package/SelectBox/SelectButton.d.ts +0 -10
- package/SelectBox/SelectButton.js +0 -78
- package/SelectBox/index.d.ts +0 -40
- package/SelectBox/index.js +0 -188
- package/SelectBox/popper-options.d.ts +0 -4
- package/SelectBox/popper-options.js +0 -33
- /package/{SelectBox/IconChevronDown.d.ts → Autocomplete/IconClear.d.ts} +0 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { Value, OnSelectItem, OnRequestClose } from '@via-profit/ui-kit/src/Menu';
|
|
3
|
+
import type { MenuItemCommonProps } from '@via-profit/ui-kit/src/Menu/MenuItem';
|
|
4
|
+
export type AutocompleteProps<T, Multiple extends boolean | undefined = undefined> = {
|
|
5
|
+
readonly value: Value<T, Multiple> | null;
|
|
6
|
+
/**
|
|
7
|
+
* Array of items. If will be array of objects or array of strings
|
|
8
|
+
*/
|
|
9
|
+
readonly items: T[];
|
|
10
|
+
readonly multiple?: Multiple;
|
|
11
|
+
/**
|
|
12
|
+
* Menu open state\
|
|
13
|
+
* If `true` then menu is open, otherwise - closed
|
|
14
|
+
*/
|
|
15
|
+
readonly isOpen?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Loading indicator visiblility\
|
|
18
|
+
* If `true` then visible, otherwise - hidden
|
|
19
|
+
*/
|
|
20
|
+
readonly isLoading?: boolean;
|
|
21
|
+
readonly clearIfNotSelected?: boolean;
|
|
22
|
+
readonly children: Children<T>;
|
|
23
|
+
readonly filterItems?: FilterItems<T>;
|
|
24
|
+
readonly onSelectItem?: OnSelectItem<T>;
|
|
25
|
+
readonly selectedItemToString: ItemToString<T, Multiple>;
|
|
26
|
+
readonly onRequestClose?: OnRequestClose;
|
|
27
|
+
readonly onInputChange?: React.ChangeEventHandler<HTMLInputElement>;
|
|
28
|
+
readonly onRequestOpen?: (event: React.KeyboardEvent<HTMLInputElement> | React.MouseEvent<HTMLInputElement> | React.FocusEvent<HTMLInputElement, Element>) => void;
|
|
29
|
+
};
|
|
30
|
+
export type Children<T> = (data: {
|
|
31
|
+
item: T;
|
|
32
|
+
index: number;
|
|
33
|
+
inputValue: string;
|
|
34
|
+
}, itemProps: MenuItemCommonProps) => React.ReactNode;
|
|
35
|
+
export type ItemToString<T, Multiple extends boolean | undefined = undefined> = (item: Value<T, Multiple>) => string;
|
|
36
|
+
export type FilterItems<T> = (items: readonly T[], data: {
|
|
37
|
+
readonly query: string;
|
|
38
|
+
readonly inputValue: string;
|
|
39
|
+
}) => readonly T[];
|
|
40
|
+
export type AutocompleteRef = {
|
|
41
|
+
clear: () => void;
|
|
42
|
+
};
|
|
43
|
+
declare const _default: <T, Multiple extends boolean | undefined = undefined>(props: AutocompleteProps<T, Multiple> & {
|
|
44
|
+
ref?: React.Ref<AutocompleteRef> | undefined;
|
|
45
|
+
}) => JSX.Element;
|
|
46
|
+
export default _default;
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
const react_1 = __importDefault(require("react"));
|
|
30
|
+
const Menu_1 = __importDefault(require("@via-profit/ui-kit/src/Menu"));
|
|
31
|
+
const TextField_1 = __importDefault(require("@via-profit/ui-kit/src/TextField"));
|
|
32
|
+
const Button_1 = __importDefault(require("@via-profit/ui-kit/src/Button"));
|
|
33
|
+
const Spinner_1 = __importDefault(require("@via-profit/ui-kit/src/LoadingIndicator/Spinner"));
|
|
34
|
+
const IconClear_1 = __importDefault(require("./IconClear"));
|
|
35
|
+
const context_1 = __importStar(require("./context"));
|
|
36
|
+
const Autocomplete = react_1.default.forwardRef((props, ref) => {
|
|
37
|
+
const { items, value, multiple, isOpen = false, isLoading = false, clearIfNotSelected = true, filterItems, children, onSelectItem, selectedItemToString, onInputChange, onRequestOpen = () => undefined, onRequestClose = () => undefined, } = props;
|
|
38
|
+
const menuRef = react_1.default.useRef(null);
|
|
39
|
+
const inputRef = react_1.default.useRef(null);
|
|
40
|
+
const isFocusedRef = react_1.default.useRef(false);
|
|
41
|
+
const { state, dispatch } = (0, context_1.default)();
|
|
42
|
+
const { currentOpen, filteredItems, inputValue, currentValue, anchorElement, currentLoading } = state;
|
|
43
|
+
const clear = react_1.default.useCallback(() => {
|
|
44
|
+
if (onSelectItem) {
|
|
45
|
+
onSelectItem(null);
|
|
46
|
+
}
|
|
47
|
+
dispatch((0, context_1.actionSetPartial)({
|
|
48
|
+
filteredItems: items,
|
|
49
|
+
currentValue: null,
|
|
50
|
+
inputValue: '',
|
|
51
|
+
}));
|
|
52
|
+
setTimeout(() => {
|
|
53
|
+
var _a;
|
|
54
|
+
(_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus();
|
|
55
|
+
}, 15);
|
|
56
|
+
}, [dispatch, onSelectItem, items]);
|
|
57
|
+
/**
|
|
58
|
+
* API
|
|
59
|
+
*/
|
|
60
|
+
react_1.default.useImperativeHandle(ref, () => ({
|
|
61
|
+
clear: () => clear(),
|
|
62
|
+
}), [clear]);
|
|
63
|
+
const inputKeydownEvent = react_1.default.useCallback((event) => {
|
|
64
|
+
var _a, _b, _c, _d, _e;
|
|
65
|
+
if (!isFocusedRef.current) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
switch (event.code) {
|
|
69
|
+
case 'Enter':
|
|
70
|
+
case 'NumpadEnter':
|
|
71
|
+
event.preventDefault();
|
|
72
|
+
(_a = menuRef.current) === null || _a === void 0 ? void 0 : _a.selectHightlightedItem();
|
|
73
|
+
break;
|
|
74
|
+
case 'ArrowUp':
|
|
75
|
+
{
|
|
76
|
+
event.preventDefault();
|
|
77
|
+
(_b = menuRef.current) === null || _b === void 0 ? void 0 : _b.hightlightPrevItem();
|
|
78
|
+
}
|
|
79
|
+
break;
|
|
80
|
+
case 'ArrowDown':
|
|
81
|
+
{
|
|
82
|
+
event.preventDefault();
|
|
83
|
+
(_c = menuRef.current) === null || _c === void 0 ? void 0 : _c.hightlightNextItem();
|
|
84
|
+
if (!currentOpen) {
|
|
85
|
+
onRequestOpen(event);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
break;
|
|
89
|
+
case 'Home':
|
|
90
|
+
{
|
|
91
|
+
event.preventDefault();
|
|
92
|
+
(_d = menuRef.current) === null || _d === void 0 ? void 0 : _d.hightlightFirstItem();
|
|
93
|
+
}
|
|
94
|
+
break;
|
|
95
|
+
case 'End':
|
|
96
|
+
{
|
|
97
|
+
event.preventDefault();
|
|
98
|
+
(_e = menuRef.current) === null || _e === void 0 ? void 0 : _e.hightlightLastItem();
|
|
99
|
+
}
|
|
100
|
+
break;
|
|
101
|
+
// case 'PageUp':
|
|
102
|
+
// case 'PageDown':
|
|
103
|
+
// event.preventDefault();
|
|
104
|
+
// break;
|
|
105
|
+
case 'Escape':
|
|
106
|
+
case 'Tab':
|
|
107
|
+
if (currentOpen) {
|
|
108
|
+
onRequestClose(event);
|
|
109
|
+
}
|
|
110
|
+
break;
|
|
111
|
+
default:
|
|
112
|
+
// do nothing
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
}, [currentOpen, onRequestClose, onRequestOpen]);
|
|
116
|
+
/**
|
|
117
|
+
* Only affected value change action
|
|
118
|
+
*/
|
|
119
|
+
react_1.default.useEffect(() => {
|
|
120
|
+
if (value === currentValue) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
if (multiple) {
|
|
124
|
+
if (Array.isArray(value)) {
|
|
125
|
+
dispatch((0, context_1.actionSetPartial)({
|
|
126
|
+
currentValue: value,
|
|
127
|
+
inputValue: value === null ? inputValue : selectedItemToString(value),
|
|
128
|
+
}));
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (!multiple) {
|
|
132
|
+
if (!Array.isArray(value)) {
|
|
133
|
+
dispatch((0, context_1.actionSetPartial)({
|
|
134
|
+
currentValue: value,
|
|
135
|
+
inputValue: value === null ? inputValue : selectedItemToString(value),
|
|
136
|
+
}));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}, [value, multiple, currentValue, inputValue, dispatch, selectedItemToString]);
|
|
140
|
+
react_1.default.useEffect(() => {
|
|
141
|
+
if (currentOpen && menuRef.current) {
|
|
142
|
+
menuRef.current.scrollToFirstSelected();
|
|
143
|
+
}
|
|
144
|
+
}, [currentOpen]);
|
|
145
|
+
react_1.default.useEffect(() => {
|
|
146
|
+
var _a;
|
|
147
|
+
if (filteredItems.length === 1) {
|
|
148
|
+
(_a = menuRef.current) === null || _a === void 0 ? void 0 : _a.highlightIndex(0);
|
|
149
|
+
}
|
|
150
|
+
}, [filteredItems]);
|
|
151
|
+
react_1.default.useEffect(() => {
|
|
152
|
+
dispatch((0, context_1.actionSetPartial)({ currentOpen: isOpen, currentLoading: isLoading, filteredItems: items }));
|
|
153
|
+
}, [isOpen, isLoading, items, dispatch]);
|
|
154
|
+
return (react_1.default.createElement("div", null,
|
|
155
|
+
react_1.default.useMemo(() => (react_1.default.createElement(TextField_1.default, { endIcon: react_1.default.createElement(Button_1.default, { iconOnly: true, 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 => {
|
|
156
|
+
if (anchorElement !== el) {
|
|
157
|
+
dispatch((0, context_1.actionSetPartial)({ anchorElement: el }));
|
|
158
|
+
}
|
|
159
|
+
}, inputRef: inputRef, value: inputValue, onBlur: () => {
|
|
160
|
+
isFocusedRef.current = false;
|
|
161
|
+
if (clearIfNotSelected) {
|
|
162
|
+
dispatch((0, context_1.actionSetPartial)({
|
|
163
|
+
inputValue: value !== null ? selectedItemToString(value) : '',
|
|
164
|
+
}));
|
|
165
|
+
}
|
|
166
|
+
}, onFocus: event => {
|
|
167
|
+
isFocusedRef.current = true;
|
|
168
|
+
onRequestOpen(event);
|
|
169
|
+
}, onClick: event => {
|
|
170
|
+
if (isFocusedRef.current) {
|
|
171
|
+
onRequestOpen(event);
|
|
172
|
+
}
|
|
173
|
+
}, onChange: event => {
|
|
174
|
+
dispatch((0, context_1.actionSetPartial)({ inputValue: event.currentTarget.value }));
|
|
175
|
+
if (typeof onInputChange === 'function') {
|
|
176
|
+
onInputChange(event);
|
|
177
|
+
}
|
|
178
|
+
const query = event.currentTarget.value.toLowerCase().trim();
|
|
179
|
+
// Apply filter
|
|
180
|
+
const newItems = typeof filterItems !== 'function' || query.length === 0
|
|
181
|
+
? items
|
|
182
|
+
: filterItems(items, { query, inputValue });
|
|
183
|
+
dispatch((0, context_1.actionSetPartial)({
|
|
184
|
+
filteredItems: newItems,
|
|
185
|
+
}));
|
|
186
|
+
} })), [
|
|
187
|
+
currentLoading,
|
|
188
|
+
inputKeydownEvent,
|
|
189
|
+
inputValue,
|
|
190
|
+
clear,
|
|
191
|
+
anchorElement,
|
|
192
|
+
dispatch,
|
|
193
|
+
clearIfNotSelected,
|
|
194
|
+
value,
|
|
195
|
+
selectedItemToString,
|
|
196
|
+
onRequestOpen,
|
|
197
|
+
onInputChange,
|
|
198
|
+
filterItems,
|
|
199
|
+
items,
|
|
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 onSelectItem === 'function') {
|
|
203
|
+
onSelectItem(item);
|
|
204
|
+
}
|
|
205
|
+
if (!multiple) {
|
|
206
|
+
dispatch((0, context_1.actionSetPartial)({
|
|
207
|
+
inputValue: selectedItemToString(item),
|
|
208
|
+
}));
|
|
209
|
+
}
|
|
210
|
+
}, closeOnSelect: multiple ? false : true, onRequestClose: evt => {
|
|
211
|
+
if ((evt === null || evt === void 0 ? void 0 : evt.target) !== anchorElement) {
|
|
212
|
+
onRequestClose(evt);
|
|
213
|
+
}
|
|
214
|
+
} }, ({ index, item }, itemProps) => children({ index, item: item, inputValue }, itemProps))), [
|
|
215
|
+
anchorElement,
|
|
216
|
+
currentOpen,
|
|
217
|
+
currentValue,
|
|
218
|
+
filteredItems,
|
|
219
|
+
inputValue,
|
|
220
|
+
multiple,
|
|
221
|
+
children,
|
|
222
|
+
dispatch,
|
|
223
|
+
onRequestClose,
|
|
224
|
+
onSelectItem,
|
|
225
|
+
selectedItemToString,
|
|
226
|
+
])));
|
|
227
|
+
});
|
|
228
|
+
Autocomplete.displayName = 'Autocomplete';
|
|
229
|
+
exports.default = Autocomplete;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const react_1 = __importDefault(require("react"));
|
|
7
|
+
const IconClear = (props, ref) => (react_1.default.createElement("svg", Object.assign({ xmlns: "http://www.w3.org/2000/svg", version: "1.1", width: "1em", height: "1em", viewBox: "0 0 512 512" }, props, { ref: ref }),
|
|
8
|
+
react_1.default.createElement("path", { d: "m139.86 128.54a16 16 0 0 0-7.1738 4.1406 16 16 0 0 0 0 22.629l100.69 100.69-100.69 100.69a16 16 0 0 0 0 22.629 16 16 0 0 0 22.629 0l100.69-100.69 100.69 100.69a16 16 0 0 0 22.629 0 16 16 0 0 0 0-22.629l-100.69-100.69 100.69-100.69a16 16 0 0 0 0-22.629 16 16 0 0 0-22.629 0l-100.69 100.69-100.69-100.69a16 16 0 0 0-15.455-4.1406z", fill: "currentColor" })));
|
|
9
|
+
exports.default = react_1.default.forwardRef(IconClear);
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { AnchorElement } from '@via-profit/ui-kit/src/Menu';
|
|
3
|
+
type State = {
|
|
4
|
+
readonly currentOpen: boolean;
|
|
5
|
+
readonly currentLoading: boolean;
|
|
6
|
+
readonly anchorElement: AnchorElement | null;
|
|
7
|
+
readonly filteredItems: readonly unknown[];
|
|
8
|
+
readonly inputValue: string;
|
|
9
|
+
readonly currentValue: unknown | null;
|
|
10
|
+
};
|
|
11
|
+
type Action = ActionSetPartial;
|
|
12
|
+
type ActionSetPartial = {
|
|
13
|
+
readonly type: 'setPartial';
|
|
14
|
+
readonly payload: Partial<State>;
|
|
15
|
+
};
|
|
16
|
+
export declare const actionSetPartial: (payload: ActionSetPartial['payload']) => ActionSetPartial;
|
|
17
|
+
export declare const defaultState: State;
|
|
18
|
+
export declare const reducer: React.Reducer<State, Action>;
|
|
19
|
+
interface ContextProviderProps {
|
|
20
|
+
readonly children: React.ReactNode | readonly React.ReactNode[];
|
|
21
|
+
}
|
|
22
|
+
export declare const ContextProvider: React.FC<ContextProviderProps>;
|
|
23
|
+
export declare const useContext: () => {
|
|
24
|
+
state: State;
|
|
25
|
+
dispatch: React.Dispatch<ActionSetPartial>;
|
|
26
|
+
};
|
|
27
|
+
export default useContext;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.useContext = exports.ContextProvider = exports.reducer = exports.defaultState = exports.actionSetPartial = void 0;
|
|
7
|
+
const react_1 = __importDefault(require("react"));
|
|
8
|
+
const actionSetPartial = (payload) => ({
|
|
9
|
+
type: 'setPartial',
|
|
10
|
+
payload,
|
|
11
|
+
});
|
|
12
|
+
exports.actionSetPartial = actionSetPartial;
|
|
13
|
+
exports.defaultState = {
|
|
14
|
+
currentOpen: false,
|
|
15
|
+
currentLoading: false,
|
|
16
|
+
anchorElement: null,
|
|
17
|
+
filteredItems: [],
|
|
18
|
+
currentValue: null,
|
|
19
|
+
inputValue: '',
|
|
20
|
+
};
|
|
21
|
+
const reducer = (state, action) => {
|
|
22
|
+
switch (action.type) {
|
|
23
|
+
case 'setPartial':
|
|
24
|
+
return Object.assign(Object.assign({}, state), action.payload);
|
|
25
|
+
default:
|
|
26
|
+
return state;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
exports.reducer = reducer;
|
|
30
|
+
const context = react_1.default.createContext({
|
|
31
|
+
state: exports.defaultState,
|
|
32
|
+
dispatch: () => undefined,
|
|
33
|
+
});
|
|
34
|
+
const ContextProvider = props => {
|
|
35
|
+
const { children } = props;
|
|
36
|
+
const [state, dispatch] = react_1.default.useReducer(exports.reducer, exports.defaultState);
|
|
37
|
+
return react_1.default.createElement(context.Provider, { value: { state, dispatch } }, children);
|
|
38
|
+
};
|
|
39
|
+
exports.ContextProvider = ContextProvider;
|
|
40
|
+
const useContext = () => {
|
|
41
|
+
const { state, dispatch } = react_1.default.useContext(context);
|
|
42
|
+
return {
|
|
43
|
+
state,
|
|
44
|
+
dispatch,
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
exports.useContext = useContext;
|
|
48
|
+
exports.default = exports.useContext;
|
package/Autocomplete/index.d.ts
CHANGED
|
@@ -1,46 +1,7 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import {
|
|
3
|
-
export
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
readonly containerRef?: React.Ref<AutocompleteContainerRef>;
|
|
7
|
-
readonly itemToString: ItemToString<T, Multiple>;
|
|
8
|
-
readonly renderInput: (params: RenderInputProps<T, Multiple>) => React.ReactNode;
|
|
9
|
-
readonly onRequestOpen: (event?: FocusEvent | KeyboardEvent | MouseEvent | React.FocusEvent | React.KeyboardEvent | React.MouseEvent) => void;
|
|
10
|
-
readonly onInputChange?: React.ChangeEventHandler<HTMLInputElement>;
|
|
11
|
-
readonly filterItems?: (items: readonly T[], data: {
|
|
12
|
-
readonly query: string;
|
|
13
|
-
readonly inputValue: string;
|
|
14
|
-
}) => readonly T[];
|
|
15
|
-
};
|
|
16
|
-
export type ItemToString<T, Multiple> = Multiple extends undefined | false ? (item: T) => string : undefined;
|
|
17
|
-
export type RenderInputProps<T, Multiple extends boolean | undefined = undefined> = {
|
|
18
|
-
readonly selected: Value<T, Multiple> | null;
|
|
19
|
-
readonly value: string;
|
|
20
|
-
readonly inputRef: React.Ref<HTMLInputElement>;
|
|
21
|
-
readonly onClick: React.MouseEventHandler<HTMLInputElement>;
|
|
22
|
-
readonly onFocus: React.FocusEventHandler<HTMLInputElement>;
|
|
23
|
-
readonly onBlur: React.FocusEventHandler<HTMLInputElement>;
|
|
24
|
-
readonly onKeyDown: React.KeyboardEventHandler<HTMLInputElement>;
|
|
25
|
-
readonly onChange: React.ChangeEventHandler<HTMLInputElement>;
|
|
26
|
-
};
|
|
27
|
-
export type AutocompleteRef<T> = Pick<MenuRef<T>, 'scrollToItem' | 'scrollTo' | 'scrollToFirstSelected' | 'selectItem' | 'highlightIndex' | 'hightlightPrevItem' | 'hightlightNextItem' | 'hightlightFirstItem' | 'hightlightLastItem' | 'selectHightlightedItem' | 'updatePopper'> & {
|
|
28
|
-
readonly clear: () => void;
|
|
29
|
-
readonly focus: () => void;
|
|
30
|
-
readonly blur: () => void;
|
|
31
|
-
readonly clearInput: () => void;
|
|
32
|
-
};
|
|
33
|
-
declare const _default: <T, Multiple extends boolean | undefined = undefined>(props: Pick<MenuProps<T, Multiple>, "zIndex" | "isOpen" | "estimatedItemSize" | "overscanCount" | "items" | "value" | "endThreshold" | "multiple" | "getOptionSelected" | "onEndReached" | "onSelectItem" | "renderItem" | "onRequestClose"> & {
|
|
34
|
-
readonly containerRef?: React.Ref<HTMLDivElement> | undefined;
|
|
35
|
-
readonly itemToString: ItemToString<T, Multiple>;
|
|
36
|
-
readonly renderInput: (params: RenderInputProps<T, Multiple>) => React.ReactNode;
|
|
37
|
-
readonly onRequestOpen: (event?: MouseEvent | KeyboardEvent | FocusEvent | React.FocusEvent<Element, Element> | React.KeyboardEvent<Element> | React.MouseEvent<Element, MouseEvent> | undefined) => void;
|
|
38
|
-
readonly onInputChange?: React.ChangeEventHandler<HTMLInputElement> | undefined;
|
|
39
|
-
readonly filterItems?: ((items: readonly T[], data: {
|
|
40
|
-
readonly query: string;
|
|
41
|
-
readonly inputValue: string;
|
|
42
|
-
}) => readonly T[]) | undefined;
|
|
43
|
-
} & {
|
|
44
|
-
ref?: React.Ref<AutocompleteRef<T>> | undefined;
|
|
2
|
+
import { AutocompleteProps, AutocompleteRef } from './AutocompleteContainer';
|
|
3
|
+
export * from './AutocompleteContainer';
|
|
4
|
+
declare const _default: <T, Multiple extends boolean | undefined = undefined>(props: AutocompleteProps<T, Multiple> & {
|
|
5
|
+
ref?: React.Ref<AutocompleteRef> | undefined;
|
|
45
6
|
}) => JSX.Element;
|
|
46
7
|
export default _default;
|
package/Autocomplete/index.js
CHANGED
|
@@ -1,187 +1,41 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
17
|
+
var t = {};
|
|
18
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
19
|
+
t[p] = s[p];
|
|
20
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
21
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
22
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
23
|
+
t[p[i]] = s[p[i]];
|
|
24
|
+
}
|
|
25
|
+
return t;
|
|
26
|
+
};
|
|
2
27
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
28
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
29
|
};
|
|
5
30
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
31
|
const react_1 = __importDefault(require("react"));
|
|
7
|
-
const
|
|
8
|
-
const
|
|
32
|
+
const AutocompleteContainer_1 = __importDefault(require("./AutocompleteContainer"));
|
|
33
|
+
const context_1 = require("./context");
|
|
34
|
+
__exportStar(require("./AutocompleteContainer"), exports);
|
|
9
35
|
const Autocomplete = react_1.default.forwardRef((props, ref) => {
|
|
10
|
-
const {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const isFocusedRef = react_1.default.useRef(false);
|
|
14
|
-
const [currentOpen, setCurrentOpen] = react_1.default.useState(isOpen);
|
|
15
|
-
const [currentValue, setCurrentValue] = react_1.default.useState(value);
|
|
16
|
-
const [anchorElement, setAnchorElement] = react_1.default.useState(null);
|
|
17
|
-
const [filteredItems, setFilteredItems] = react_1.default.useState(items);
|
|
18
|
-
/**
|
|
19
|
-
* API
|
|
20
|
-
*/
|
|
21
|
-
react_1.default.useImperativeHandle(ref, () => ({
|
|
22
|
-
highlightIndex: (index) => { var _a; return (_a = menuRef.current) === null || _a === void 0 ? void 0 : _a.highlightIndex(index); },
|
|
23
|
-
hightlightPrevItem: () => { var _a; return (_a = menuRef.current) === null || _a === void 0 ? void 0 : _a.hightlightPrevItem(); },
|
|
24
|
-
hightlightNextItem: () => { var _a; return (_a = menuRef.current) === null || _a === void 0 ? void 0 : _a.hightlightNextItem(); },
|
|
25
|
-
hightlightFirstItem: () => { var _a; return (_a = menuRef.current) === null || _a === void 0 ? void 0 : _a.hightlightFirstItem(); },
|
|
26
|
-
hightlightLastItem: () => { var _a; return (_a = menuRef.current) === null || _a === void 0 ? void 0 : _a.hightlightLastItem(); },
|
|
27
|
-
selectHightlightedItem: () => { var _a; return (_a = menuRef.current) === null || _a === void 0 ? void 0 : _a.selectHightlightedItem(); },
|
|
28
|
-
updatePopper: () => { var _a; return (_a = menuRef.current) === null || _a === void 0 ? void 0 : _a.updatePopper(); },
|
|
29
|
-
scrollToFirstSelected: () => { var _a; return (_a = menuRef.current) === null || _a === void 0 ? void 0 : _a.scrollToFirstSelected(); },
|
|
30
|
-
selectItem: (index) => { var _a; return (_a = menuRef.current) === null || _a === void 0 ? void 0 : _a.selectItem(index); },
|
|
31
|
-
scrollTo: (scrollOffset) => { var _a; return (_a = menuRef.current) === null || _a === void 0 ? void 0 : _a.scrollTo(scrollOffset); },
|
|
32
|
-
scrollToItem: (index, align) => { var _a; return (_a = menuRef.current) === null || _a === void 0 ? void 0 : _a.scrollToItem(index, align); },
|
|
33
|
-
clear: () => {
|
|
34
|
-
setCurrentValue(null);
|
|
35
|
-
setInputValue('');
|
|
36
|
-
anchorElement === null || anchorElement === void 0 ? void 0 : anchorElement.focus();
|
|
37
|
-
setFilteredItems(items);
|
|
38
|
-
},
|
|
39
|
-
focus: () => anchorElement === null || anchorElement === void 0 ? void 0 : anchorElement.focus(),
|
|
40
|
-
blur: () => anchorElement === null || anchorElement === void 0 ? void 0 : anchorElement.blur(),
|
|
41
|
-
clearInput: () => setInputValue(''),
|
|
42
|
-
}), [anchorElement, items]);
|
|
43
|
-
const inputKeydownEvent = react_1.default.useCallback((event) => {
|
|
44
|
-
var _a, _b, _c, _d, _e;
|
|
45
|
-
if (!isFocusedRef.current) {
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
switch (event.code) {
|
|
49
|
-
case 'Enter':
|
|
50
|
-
case 'NumpadEnter':
|
|
51
|
-
event.preventDefault();
|
|
52
|
-
(_a = menuRef.current) === null || _a === void 0 ? void 0 : _a.selectHightlightedItem();
|
|
53
|
-
break;
|
|
54
|
-
case 'ArrowUp':
|
|
55
|
-
{
|
|
56
|
-
event.preventDefault();
|
|
57
|
-
(_b = menuRef.current) === null || _b === void 0 ? void 0 : _b.hightlightPrevItem();
|
|
58
|
-
}
|
|
59
|
-
break;
|
|
60
|
-
case 'ArrowDown':
|
|
61
|
-
{
|
|
62
|
-
event.preventDefault();
|
|
63
|
-
(_c = menuRef.current) === null || _c === void 0 ? void 0 : _c.hightlightNextItem();
|
|
64
|
-
if (!currentOpen) {
|
|
65
|
-
onRequestOpen(event);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
break;
|
|
69
|
-
case 'Home':
|
|
70
|
-
{
|
|
71
|
-
event.preventDefault();
|
|
72
|
-
(_d = menuRef.current) === null || _d === void 0 ? void 0 : _d.hightlightFirstItem();
|
|
73
|
-
}
|
|
74
|
-
break;
|
|
75
|
-
case 'End':
|
|
76
|
-
{
|
|
77
|
-
event.preventDefault();
|
|
78
|
-
(_e = menuRef.current) === null || _e === void 0 ? void 0 : _e.hightlightLastItem();
|
|
79
|
-
}
|
|
80
|
-
break;
|
|
81
|
-
case 'PageUp':
|
|
82
|
-
case 'PageDown':
|
|
83
|
-
event.preventDefault();
|
|
84
|
-
break;
|
|
85
|
-
case 'Escape':
|
|
86
|
-
case 'Tab':
|
|
87
|
-
onRequestClose(event);
|
|
88
|
-
break;
|
|
89
|
-
default:
|
|
90
|
-
if (!currentOpen) {
|
|
91
|
-
onRequestOpen(event);
|
|
92
|
-
}
|
|
93
|
-
// do nothing
|
|
94
|
-
break;
|
|
95
|
-
}
|
|
96
|
-
}, [currentOpen, onRequestClose, onRequestOpen]);
|
|
97
|
-
const onSelectItemF = react_1.default.useCallback(selected => {
|
|
98
|
-
setCurrentValue(selected);
|
|
99
|
-
if (typeof onSelectItem === 'function') {
|
|
100
|
-
if (!Array.isArray(selected) && typeof itemToString === 'function') {
|
|
101
|
-
setInputValue(itemToString(selected));
|
|
102
|
-
}
|
|
103
|
-
onSelectItem(selected);
|
|
104
|
-
}
|
|
105
|
-
}, [itemToString, onSelectItem]);
|
|
106
|
-
react_1.default.useEffect(() => {
|
|
107
|
-
if (value === null) {
|
|
108
|
-
setCurrentValue(null);
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
if (multiple) {
|
|
112
|
-
if (Array.isArray(value)) {
|
|
113
|
-
setCurrentValue(value);
|
|
114
|
-
}
|
|
115
|
-
if (!Array.isArray(value)) {
|
|
116
|
-
setCurrentValue([value]);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
if (!multiple) {
|
|
120
|
-
if (Array.isArray(value)) {
|
|
121
|
-
setCurrentValue(value[0]);
|
|
122
|
-
}
|
|
123
|
-
if (!Array.isArray(value)) {
|
|
124
|
-
setCurrentValue(value);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}, [value, multiple]);
|
|
128
|
-
react_1.default.useEffect(() => {
|
|
129
|
-
setCurrentOpen(isOpen);
|
|
130
|
-
}, [isOpen]);
|
|
131
|
-
const renderInputOnChange = react_1.default.useCallback(event => {
|
|
132
|
-
setInputValue(event.currentTarget.value);
|
|
133
|
-
if (typeof onInputChange === 'function') {
|
|
134
|
-
onInputChange(event);
|
|
135
|
-
}
|
|
136
|
-
const query = event.currentTarget.value.toLowerCase().trim();
|
|
137
|
-
// Apply filter
|
|
138
|
-
const newItems = typeof filterItems !== 'function' || query.length === 0
|
|
139
|
-
? items
|
|
140
|
-
: filterItems(items, { query, inputValue });
|
|
141
|
-
setFilteredItems(newItems);
|
|
142
|
-
}, [inputValue, items, onInputChange, filterItems]);
|
|
143
|
-
react_1.default.useEffect(() => {
|
|
144
|
-
var _a;
|
|
145
|
-
if (filteredItems.length === 1) {
|
|
146
|
-
(_a = menuRef.current) === null || _a === void 0 ? void 0 : _a.highlightIndex(0);
|
|
147
|
-
}
|
|
148
|
-
}, [filteredItems]);
|
|
149
|
-
const inputElement = react_1.default.useMemo(() => {
|
|
150
|
-
const elem = renderInput({
|
|
151
|
-
inputRef: setAnchorElement,
|
|
152
|
-
value: inputValue,
|
|
153
|
-
selected: currentValue,
|
|
154
|
-
onBlur: () => {
|
|
155
|
-
isFocusedRef.current = false;
|
|
156
|
-
},
|
|
157
|
-
onFocus: event => {
|
|
158
|
-
isFocusedRef.current = true;
|
|
159
|
-
onRequestOpen(event);
|
|
160
|
-
},
|
|
161
|
-
onKeyDown: inputKeydownEvent,
|
|
162
|
-
onChange: renderInputOnChange,
|
|
163
|
-
onClick: event => {
|
|
164
|
-
if (isFocusedRef.current) {
|
|
165
|
-
onRequestOpen(event);
|
|
166
|
-
}
|
|
167
|
-
},
|
|
168
|
-
});
|
|
169
|
-
return elem;
|
|
170
|
-
}, [
|
|
171
|
-
inputKeydownEvent,
|
|
172
|
-
inputValue,
|
|
173
|
-
onRequestOpen,
|
|
174
|
-
renderInput,
|
|
175
|
-
renderInputOnChange,
|
|
176
|
-
currentValue,
|
|
177
|
-
]);
|
|
178
|
-
return (react_1.default.createElement("div", { ref: containerRef },
|
|
179
|
-
inputElement,
|
|
180
|
-
react_1.default.createElement(Menu_1.default, { ref: menuRef, items: filteredItems, value: currentValue, isOpen: currentOpen && filteredItems.length > 0, autofocus: false, anchorElement: anchorElement, estimatedItemSize: estimatedItemSize, endThreshold: endThreshold, overscanCount: overscanCount, getOptionSelected: getOptionSelected, onEndReached: onEndReached, onSelectItem: onSelectItemF, renderItem: renderItem, multiple: multiple, zIndex: zIndex, closeOutsideClick: true, popperOptions: popper_options_1.default, onRequestClose: evt => {
|
|
181
|
-
if ((evt === null || evt === void 0 ? void 0 : evt.target) !== anchorElement) {
|
|
182
|
-
onRequestClose(evt);
|
|
183
|
-
}
|
|
184
|
-
} })));
|
|
36
|
+
const { children } = props, restProps = __rest(props, ["children"]);
|
|
37
|
+
return (react_1.default.createElement(context_1.ContextProvider, null,
|
|
38
|
+
react_1.default.createElement(AutocompleteContainer_1.default, Object.assign({}, restProps, { ref: ref }), children)));
|
|
185
39
|
});
|
|
186
40
|
Autocomplete.displayName = 'Autocomplete';
|
|
187
41
|
exports.default = Autocomplete;
|
package/Calendar/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import React from 'react';
|
|
|
2
2
|
import { WeekDayName } from './use-calendar';
|
|
3
3
|
import { WeekNameLabelFormat } from './CalendarWeekDaysBar';
|
|
4
4
|
export interface CalendarProps {
|
|
5
|
-
readonly date
|
|
5
|
+
readonly date: Date;
|
|
6
6
|
readonly locale?: string;
|
|
7
7
|
readonly selected?: Date[];
|
|
8
8
|
readonly onSelectDate?: (date: Date) => void;
|