@steroidsjs/core 2.2.9 → 2.2.13

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.
@@ -29,6 +29,12 @@ export interface IDataSelectConfig {
29
29
  * @example id
30
30
  */
31
31
  primaryKey?: string;
32
+ /**
33
+ * Атрибут, в котором должны лежать дочерние элементы списка (для группировки)
34
+ * Если аттрибут не задан - группировка не производится
35
+ * @example items
36
+ */
37
+ groupAttribute?: string;
32
38
  /**
33
39
  * Значение поля в форме
34
40
  */
@@ -49,5 +55,6 @@ export interface IDataSelectResult {
49
55
  setSelectedIds: (ids: PrimaryKey | PrimaryKey[], skipToggle?: boolean) => void;
50
56
  selectedItems: IDataSelectItem[];
51
57
  }
58
+ export declare const getLinearItems: (items: any, groupAttribute: any) => any[];
52
59
  export default function useDataSelect(config: IDataSelectConfig): IDataSelectResult;
53
60
  export {};
@@ -8,6 +8,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
8
8
  return (mod && mod.__esModule) ? mod : { "default": mod };
9
9
  };
10
10
  exports.__esModule = true;
11
+ exports.getLinearItems = void 0;
11
12
  var isEqual_1 = __importDefault(require("lodash-es/isEqual"));
12
13
  var isArray_1 = __importDefault(require("lodash-es/isArray"));
13
14
  var isNil_1 = __importDefault(require("lodash-es/isNil"));
@@ -16,9 +17,23 @@ var react_use_1 = require("react-use");
16
17
  var defaultProps = {
17
18
  primaryKey: 'id'
18
19
  };
20
+ var getLinearItems = function (items, groupAttribute) {
21
+ var result = [];
22
+ items.forEach(function (item) {
23
+ if (groupAttribute && Array.isArray(item[groupAttribute])) {
24
+ result = result.concat(exports.getLinearItems(item[groupAttribute], groupAttribute));
25
+ }
26
+ else {
27
+ result.push(item);
28
+ }
29
+ });
30
+ return result;
31
+ };
32
+ exports.getLinearItems = getLinearItems;
19
33
  function useDataSelect(config) {
20
34
  // Get primary key
21
35
  var primaryKey = config.primaryKey || defaultProps.primaryKey;
36
+ var linearItems = react_1.useMemo(function () { return exports.getLinearItems(config.items, config.groupAttribute); }, [config.groupAttribute, config.items]);
22
37
  // Initial select
23
38
  var initialSelectedIds = react_1.useMemo(function () {
24
39
  var _a;
@@ -28,16 +43,16 @@ function useDataSelect(config) {
28
43
  if (!isNil_1["default"](config.inputValue)) {
29
44
  return [].concat(isArray_1["default"](config.inputValue) ? config.inputValue : [config.inputValue]);
30
45
  }
31
- return config.selectFirst && config.items.length > 0
32
- ? [config.items[0][primaryKey]]
46
+ return config.selectFirst && linearItems.length > 0
47
+ ? [linearItems[0][primaryKey]]
33
48
  : [];
34
- }, [config.items, config.selectFirst, config.selectedIds, primaryKey, config.inputValue]);
35
- var initialSelectedItems = react_1.useMemo(function () { return config.items.length > 0
49
+ }, [config.selectedIds, config.inputValue, config.selectFirst, linearItems, primaryKey]);
50
+ var initialSelectedItems = react_1.useMemo(function () { return linearItems.length > 0
36
51
  && initialSelectedIds.length > 0
37
52
  ? initialSelectedIds
38
- .map(function (selectedId) { return config.items.find(function (item) { return item.id === selectedId; }); })
53
+ .map(function (selectedId) { return linearItems.find(function (item) { return item.id === selectedId; }); })
39
54
  .filter(Boolean)
40
- : []; }, [initialSelectedIds, config.items]);
55
+ : []; }, [initialSelectedIds, linearItems]);
41
56
  // State
42
57
  var _a = react_1.useState(false), isOpened = _a[0], setIsOpened = _a[1];
43
58
  var _b = react_1.useState(false), isFocused = _b[0], setIsFocused = _b[1];
@@ -82,7 +97,7 @@ function useDataSelect(config) {
82
97
  var newSelectedItems = [];
83
98
  var hasChanges = false;
84
99
  selectedIds.forEach(function (selectedId) {
85
- var finedItem = config.items.find(function (item) { return item[primaryKey] === selectedId; });
100
+ var finedItem = linearItems.find(function (item) { return item[primaryKey] === selectedId; });
86
101
  if (!finedItem && config.sourceItems) {
87
102
  finedItem = config.sourceItems.find(function (item) { return item[primaryKey] === selectedId; });
88
103
  }
@@ -98,14 +113,14 @@ function useDataSelect(config) {
98
113
  if (hasChanges || prevSelectedIdsLength !== selectedIds.length) {
99
114
  setSelectedItemsInternal(newSelectedItems);
100
115
  }
101
- }, [config.items, config.sourceItems, primaryKey, selectedIds, selectedItems, prevSelectedIdsLength]);
116
+ }, [linearItems, config.sourceItems, primaryKey, selectedIds, selectedItems, prevSelectedIdsLength]);
102
117
  // Select first after fetch data
103
- var prevItemsLength = react_use_1.usePrevious(config.items.length);
118
+ var prevItemsLength = react_use_1.usePrevious(linearItems.length);
104
119
  react_use_1.useUpdateEffect(function () {
105
- if (config.selectFirst && prevItemsLength === 0 && config.items.length > 0) {
106
- setSelectedIdsInternal([config.items[0][primaryKey]]);
120
+ if (config.selectFirst && prevItemsLength === 0 && linearItems.length > 0) {
121
+ setSelectedIdsInternal([linearItems[0][primaryKey]]);
107
122
  }
108
- }, [config.items, config.selectFirst, prevItemsLength, primaryKey]);
123
+ }, [linearItems, config.selectFirst, prevItemsLength, primaryKey]);
109
124
  // Update selected items on change value
110
125
  var prevConfigSelectedIds = react_use_1.usePrevious(config.selectedIds || []);
111
126
  react_use_1.useUpdateEffect(function () {
@@ -121,7 +136,7 @@ function useDataSelect(config) {
121
136
  && !isEqual_1["default"](selectedIds, newSelectedIds) && newSelectedIds.length !== 0) {
122
137
  setSelectedIdsInternal(newSelectedIds);
123
138
  }
124
- }, [config.items, config.selectedIds, primaryKey, prevConfigSelectedIds,
139
+ }, [linearItems, config.selectedIds, primaryKey, prevConfigSelectedIds,
125
140
  selectedItems, config.sourceItems, selectedIds]);
126
141
  // Global key down handler for navigate on items
127
142
  // Support keys:
@@ -151,9 +166,9 @@ function useDataSelect(config) {
151
166
  // Select first selected
152
167
  setSelectedIds(selectedIds[0], true);
153
168
  }
154
- else if (config.items.length > 0) {
169
+ else if (linearItems.length > 0) {
155
170
  // Select first result
156
- setSelectedIds(config.items[0], true);
171
+ setSelectedIds(linearItems[0], true);
157
172
  }
158
173
  setIsOpened(false);
159
174
  }
@@ -178,7 +193,7 @@ function useDataSelect(config) {
178
193
  else {
179
194
  // Navigate on items by keys
180
195
  var direction = isDown ? 1 : -1;
181
- var keys = config.items.map(function (item) { return item.id; });
196
+ var keys = linearItems.map(function (item) { return item.id; });
182
197
  // Get current index
183
198
  var index = hoveredId ? keys.indexOf(hoveredId) : -1;
184
199
  if (index === -1 && selectedIds.length === 1) {
@@ -192,7 +207,7 @@ function useDataSelect(config) {
192
207
  setHoveredId(keys[newIndex]);
193
208
  }
194
209
  }
195
- }, [config.items, hoveredId, isFocused, isOpened, setSelectedIds, selectedIds]);
210
+ }, [isFocused, isOpened, hoveredId, selectedIds, linearItems, setSelectedIds]);
196
211
  react_use_1.useEvent('keydown', onKeyDown);
197
212
  return {
198
213
  isOpened: isOpened,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@steroidsjs/core",
3
- "version": "2.2.9",
3
+ "version": "2.2.13",
4
4
  "description": "",
5
5
  "author": "Vladimir Kozhin <hello@kozhindev.com>",
6
6
  "repository": {
package/reducers/form.js CHANGED
@@ -50,7 +50,7 @@ function reducerItem(state, action) {
50
50
  case form_1.FORM_SUBMIT:
51
51
  return dot_prop_immutable_1.set(state, 'submitCounter', (state.submitCounter || 0) + 1);
52
52
  case form_1.FORM_RESET:
53
- return dot_prop_immutable_1.set(state, 'values', cloneDeep_1["default"](state.initialValues || {}));
53
+ return __assign(__assign({}, state), { values: cloneDeep_1["default"](state.initialValues || {}) });
54
54
  case form_1.FORM_ARRAY_ADD:
55
55
  // eslint-disable-next-line no-case-declarations
56
56
  var newValue = [].concat(get_1["default"](state, 'values.' + action.name) || []);
@@ -69,8 +69,8 @@ var buildUrl = function (path, params) {
69
69
  var pathKeys = [];
70
70
  try {
71
71
  pathKeys = path_to_regexp_1.parse(path)
72
- .slice(1)
73
- .map(function (p) { return p.name; });
72
+ .map(function (p) { return typeof p === 'object' && p.name; })
73
+ .filter(Boolean);
74
74
  url = path_to_regexp_1.compile(path)(__assign({}, params));
75
75
  }
76
76
  catch (e) {
@@ -90,7 +90,7 @@ export interface IButtonProps {
90
90
  * автоматически будет переключаться в режим загрузки (`loading`) на время выполнения `Promise`.
91
91
  * @param e => fetch(...)
92
92
  */
93
- onClick?: (e: Event | React.MouseEvent) => Promise<any> | void;
93
+ onClick?: (e: Event | React.MouseEvent) => Promise<any> | any;
94
94
  /**
95
95
  * Переводит кнопку в состояние "не активна"
96
96
  * @example true
@@ -15,6 +15,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
15
15
  };
16
16
  exports.__esModule = true;
17
17
  var react_1 = require("react");
18
+ var react_use_1 = require("react-use");
18
19
  var hooks_1 = require("../../../hooks");
19
20
  var fieldWrapper_1 = __importDefault(require("../../../ui/form/Field/fieldWrapper"));
20
21
  function CheckboxListField(props) {
@@ -42,6 +43,17 @@ function CheckboxListField(props) {
42
43
  react_1.useEffect(function () {
43
44
  props.input.onChange.call(null, selectedIds);
44
45
  }, [props.input.onChange, selectedIds]);
46
+ var onReset = react_1.useCallback(function () {
47
+ setSelectedIds([]);
48
+ }, [setSelectedIds]);
49
+ // Reset selected ids on form reset
50
+ var prevInputValue = react_use_1.usePrevious(props.input.value);
51
+ react_use_1.useUpdateEffect(function () {
52
+ // if form reset
53
+ if (prevInputValue && props.input.value === undefined && selectedIds.length > 0) {
54
+ onReset();
55
+ }
56
+ }, [onReset, prevInputValue, props.input.value, selectedIds.length]);
45
57
  return components.ui.renderView(props.view || 'form.CheckboxListFieldView', __assign(__assign({}, props), { items: items,
46
58
  inputProps: inputProps,
47
59
  onItemSelect: onItemSelect,
@@ -31,6 +31,12 @@ export interface IDropDownFieldProps extends IFieldWrapperInputProps, IDataProvi
31
31
  * @example true
32
32
  */
33
33
  showReset?: boolean;
34
+ /**
35
+ * Атрибут, в котором должны лежать дочерние элементы списка (для группировки)
36
+ * Если аттрибут не задан - группировка не производится
37
+ * @example items
38
+ */
39
+ groupAttribute?: string;
34
40
  /**
35
41
  * Включает стиль без 'border'
36
42
  * @example true
@@ -39,6 +39,7 @@ function DropDownField(props) {
39
39
  selectFirst: props.selectFirst,
40
40
  selectedIds: inputSelectedIds,
41
41
  primaryKey: props.primaryKey,
42
+ groupAttribute: props.groupAttribute,
42
43
  items: items,
43
44
  sourceItems: sourceItems,
44
45
  inputValue: props.input.value
@@ -21,9 +21,9 @@ function NumberField(props) {
21
21
  var components = hooks_1.useComponents();
22
22
  props.inputProps = react_1.useMemo(function () {
23
23
  var _a;
24
- return (__assign({ name: props.input.name, value: (_a = props.input.value) !== null && _a !== void 0 ? _a : undefined, onChange: function (e) { var _a, _b; return props.input.onChange(((_a = e.target) === null || _a === void 0 ? void 0 : _a.value) || ((_b = e.nativeEvent) === null || _b === void 0 ? void 0 : _b.text)); }, type: 'number', min: props.min, max: props.max, step: props.step, placeholder: props.placeholder, disabled: props.disabled }, props.inputProps));
24
+ return (__assign({ name: props.input.name, value: (_a = props.input.value) !== null && _a !== void 0 ? _a : '', onChange: function (e) { var _a, _b; return props.input.onChange(((_a = e.target) === null || _a === void 0 ? void 0 : _a.value) || ((_b = e.nativeEvent) === null || _b === void 0 ? void 0 : _b.text)); }, type: 'number', min: props.min, max: props.max, step: props.step, placeholder: props.placeholder, disabled: props.disabled }, props.inputProps));
25
25
  }, [props.disabled, props.input, props.inputProps, props.placeholder, props.min, props.max, props.step]);
26
- return components.ui.renderView(props.view || 'form.NumberFieldView' || 'form.InputFieldView', props);
26
+ return components.ui.renderView(props.view || 'form.NumberFieldView', props);
27
27
  }
28
28
  NumberField.defaultProps = {
29
29
  disabled: false,