@panneau/field-resource-item 3.0.310 → 4.0.0

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.
Files changed (2) hide show
  1. package/es/index.js +73 -117
  2. package/package.json +13 -13
package/es/index.js CHANGED
@@ -6,12 +6,10 @@ import classNames from 'classnames';
6
6
  import isArray from 'lodash-es/isArray';
7
7
  import isEmpty from 'lodash-es/isEmpty';
8
8
  import uniqBy from 'lodash-es/uniqBy';
9
- import PropTypes from 'prop-types';
10
9
  import React, { useMemo, useState, useCallback, useEffect } from 'react';
11
10
  import { useIntl, FormattedMessage } from 'react-intl';
12
- import { PropTypes as PropTypes$1 } from '@panneau/core';
13
11
  import { usePanneauResource } from '@panneau/core/contexts';
14
- import { isMessage, getPathValue } from '@panneau/core/utils';
12
+ import { getPathValue, isMessage } from '@panneau/core/utils';
15
13
  import { useResourceItems } from '@panneau/data';
16
14
  import Button from '@panneau/element-button';
17
15
  import ResourceCard from '@panneau/element-resource-card';
@@ -23,109 +21,69 @@ import ResourceItemsModal from '@panneau/modal-resource-items';
23
21
 
24
22
  // TODO: improve the modals (esc key) and switch to FormModal
25
23
 
26
- var propTypes = {
27
- name: PropTypes.string,
28
- value: PropTypes.object,
29
- // eslint-disable-line react/forbid-prop-types
30
- errors: PropTypes$1.formErrors,
31
- resource: PropTypes.string,
32
- resourceType: PropTypes.string,
33
- paginated: PropTypes.bool,
34
- query: PropTypes.object,
35
- // eslint-disable-line react/forbid-prop-types
36
- page: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
37
- // eslint-disable-line react/forbid-prop-types
38
- count: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
39
- // eslint-disable-line react/forbid-prop-types
40
- options: PropTypes.object,
41
- // eslint-disable-line react/forbid-prop-types
42
- searchParamName: PropTypes.string,
43
- getItemLabel: PropTypes.func,
44
- getItemDescription: PropTypes.func,
45
- getItemImage: PropTypes.func,
46
- itemLabelPath: PropTypes.string,
47
- itemDescriptionPath: PropTypes.string,
48
- itemImagePath: PropTypes.string,
49
- itemLabelWithId: PropTypes.bool,
50
- placeholder: PropTypes.string,
51
- canCreate: PropTypes.bool,
52
- canEdit: PropTypes.bool,
53
- canFind: PropTypes.bool,
54
- withoutModal: PropTypes.bool,
55
- createButtonLabel: PropTypes$1.message,
56
- editButtonLabel: PropTypes$1.message,
57
- findButtonLabel: PropTypes$1.message,
58
- multiple: PropTypes.bool,
59
- disabled: PropTypes.bool,
60
- className: PropTypes.string,
61
- inputClassName: PropTypes.string,
62
- onChange: PropTypes.func
63
- };
64
- var defaultProps = {
65
- name: null,
66
- value: null,
67
- errors: null,
68
- resource: null,
69
- resourceType: null,
70
- paginated: true,
71
- query: null,
72
- page: null,
73
- count: null,
74
- options: null,
75
- searchParamName: 'search',
76
- getItemLabel: getPathValue,
77
- getItemDescription: getPathValue,
78
- getItemImage: getPathValue,
79
- itemLabelPath: 'label',
80
- itemDescriptionPath: null,
81
- itemImagePath: 'image.thumbnail_url',
82
- itemLabelWithId: false,
83
- placeholder: null,
84
- canCreate: false,
85
- canEdit: false,
86
- canFind: false,
87
- withoutModal: false,
88
- createButtonLabel: null,
89
- editButtonLabel: null,
90
- findButtonLabel: null,
91
- multiple: false,
92
- disabled: false,
93
- className: null,
94
- inputClassName: null,
95
- onChange: null
96
- };
97
- var ResourceItemField = function ResourceItemField(_ref) {
98
- var name = _ref.name,
99
- value = _ref.value,
100
- errors = _ref.errors,
101
- resourceId = _ref.resource,
102
- resourceType = _ref.resourceType,
103
- paginated = _ref.paginated,
104
- initialQuery = _ref.query,
105
- initialPage = _ref.page,
106
- initialCount = _ref.count,
107
- initialOptions = _ref.options,
108
- searchParamName = _ref.searchParamName,
109
- initialGetItemLabel = _ref.getItemLabel,
110
- getItemDescription = _ref.getItemDescription,
111
- getItemImage = _ref.getItemImage,
112
- itemLabelPath = _ref.itemLabelPath,
113
- itemDescriptionPath = _ref.itemDescriptionPath,
114
- itemImagePath = _ref.itemImagePath,
115
- itemLabelWithId = _ref.itemLabelWithId,
116
- placeholder = _ref.placeholder,
117
- canCreate = _ref.canCreate,
118
- canEdit = _ref.canEdit,
119
- canFind = _ref.canFind,
120
- withoutModal = _ref.withoutModal,
121
- createButtonLabel = _ref.createButtonLabel,
122
- editButtonLabel = _ref.editButtonLabel,
123
- findButtonLabel = _ref.findButtonLabel,
124
- multiple = _ref.multiple,
125
- disabled = _ref.disabled,
126
- className = _ref.className,
127
- inputClassName = _ref.inputClassName,
128
- onChange = _ref.onChange;
24
+ function ResourceItemField(_ref) {
25
+ var _ref$name = _ref.name,
26
+ name = _ref$name === void 0 ? null : _ref$name,
27
+ _ref$value = _ref.value,
28
+ value = _ref$value === void 0 ? null : _ref$value,
29
+ _ref$errors = _ref.errors,
30
+ errors = _ref$errors === void 0 ? null : _ref$errors,
31
+ _ref$resource = _ref.resource,
32
+ resourceId = _ref$resource === void 0 ? null : _ref$resource,
33
+ _ref$resourceType = _ref.resourceType,
34
+ resourceType = _ref$resourceType === void 0 ? null : _ref$resourceType,
35
+ _ref$paginated = _ref.paginated,
36
+ paginated = _ref$paginated === void 0 ? true : _ref$paginated,
37
+ _ref$query = _ref.query,
38
+ initialQuery = _ref$query === void 0 ? null : _ref$query,
39
+ _ref$page = _ref.page,
40
+ initialPage = _ref$page === void 0 ? null : _ref$page,
41
+ _ref$count = _ref.count,
42
+ initialCount = _ref$count === void 0 ? null : _ref$count,
43
+ _ref$options = _ref.options,
44
+ initialOptions = _ref$options === void 0 ? null : _ref$options,
45
+ _ref$searchParamName = _ref.searchParamName,
46
+ searchParamName = _ref$searchParamName === void 0 ? 'search' : _ref$searchParamName,
47
+ _ref$getItemLabel = _ref.getItemLabel,
48
+ initialGetItemLabel = _ref$getItemLabel === void 0 ? getPathValue : _ref$getItemLabel,
49
+ _ref$getItemDescripti = _ref.getItemDescription,
50
+ getItemDescription = _ref$getItemDescripti === void 0 ? getPathValue : _ref$getItemDescripti,
51
+ _ref$getItemImage = _ref.getItemImage,
52
+ getItemImage = _ref$getItemImage === void 0 ? getPathValue : _ref$getItemImage,
53
+ _ref$itemLabelPath = _ref.itemLabelPath,
54
+ itemLabelPath = _ref$itemLabelPath === void 0 ? 'label' : _ref$itemLabelPath,
55
+ _ref$itemDescriptionP = _ref.itemDescriptionPath,
56
+ itemDescriptionPath = _ref$itemDescriptionP === void 0 ? null : _ref$itemDescriptionP,
57
+ _ref$itemImagePath = _ref.itemImagePath,
58
+ itemImagePath = _ref$itemImagePath === void 0 ? 'image.thumbnail_url' : _ref$itemImagePath,
59
+ _ref$itemLabelWithId = _ref.itemLabelWithId,
60
+ itemLabelWithId = _ref$itemLabelWithId === void 0 ? false : _ref$itemLabelWithId,
61
+ _ref$placeholder = _ref.placeholder,
62
+ placeholder = _ref$placeholder === void 0 ? null : _ref$placeholder,
63
+ _ref$canCreate = _ref.canCreate,
64
+ canCreate = _ref$canCreate === void 0 ? false : _ref$canCreate,
65
+ _ref$canEdit = _ref.canEdit,
66
+ canEdit = _ref$canEdit === void 0 ? false : _ref$canEdit,
67
+ _ref$canFind = _ref.canFind,
68
+ canFind = _ref$canFind === void 0 ? false : _ref$canFind,
69
+ _ref$withoutModal = _ref.withoutModal,
70
+ withoutModal = _ref$withoutModal === void 0 ? false : _ref$withoutModal,
71
+ _ref$createButtonLabe = _ref.createButtonLabel,
72
+ createButtonLabel = _ref$createButtonLabe === void 0 ? null : _ref$createButtonLabe,
73
+ _ref$editButtonLabel = _ref.editButtonLabel,
74
+ editButtonLabel = _ref$editButtonLabel === void 0 ? null : _ref$editButtonLabel,
75
+ _ref$findButtonLabel = _ref.findButtonLabel,
76
+ findButtonLabel = _ref$findButtonLabel === void 0 ? null : _ref$findButtonLabel,
77
+ _ref$multiple = _ref.multiple,
78
+ multiple = _ref$multiple === void 0 ? false : _ref$multiple,
79
+ _ref$disabled = _ref.disabled,
80
+ disabled = _ref$disabled === void 0 ? false : _ref$disabled,
81
+ _ref$className = _ref.className,
82
+ className = _ref$className === void 0 ? null : _ref$className,
83
+ _ref$inputClassName = _ref.inputClassName,
84
+ inputClassName = _ref$inputClassName === void 0 ? null : _ref$inputClassName,
85
+ _ref$onChange = _ref.onChange,
86
+ onChange = _ref$onChange === void 0 ? null : _ref$onChange;
129
87
  var intl = useIntl();
130
88
  var resource = usePanneauResource(resourceId);
131
89
  var resourceValues = useResourceValues(resource);
@@ -196,7 +154,7 @@ var ResourceItemField = function ResourceItemField(_ref) {
196
154
  return _objectSpread(_objectSpread(_objectSpread({}, query), !isEmpty(queryTextValue) ? _defineProperty({}, searchParamName, queryTextValue) : null), {}, {
197
155
  paginated: paginated
198
156
  });
199
- }, [queryTextValue, paginated]);
157
+ }, [queryTextValue, paginated, query, searchParamName]);
200
158
  var _useResourceItems = useResourceItems(queryResource, finalQuery, paginated ? page : null, paginated ? count : null, resourceOptions),
201
159
  _useResourceItems$all = _useResourceItems.allItems,
202
160
  partialItems = _useResourceItems$all === void 0 ? null : _useResourceItems$all,
@@ -215,10 +173,10 @@ var ResourceItemField = function ResourceItemField(_ref) {
215
173
  return id;
216
174
  });
217
175
  var onScrollEnd = useCallback(function () {
218
- if (page !== null && page >= lastPage) {
176
+ if (page !== null && typeof page === 'number' && typeof lastPage === 'number' && page >= lastPage) {
219
177
  return;
220
178
  }
221
- if (paginated) {
179
+ if (paginated && typeof page === 'number') {
222
180
  setPage(page + 1);
223
181
  }
224
182
  }, [paginated, page, setPage, lastPage]);
@@ -256,14 +214,14 @@ var ResourceItemField = function ResourceItemField(_ref) {
256
214
  });
257
215
  var onValueChange = useCallback(function (newId) {
258
216
  if (onChange === null) return;
259
- if (multiple) {
217
+ if (multiple && isArray(newId)) {
260
218
  var newValue = items.filter(function (_ref8) {
261
219
  var _ref8$id = _ref8.id,
262
220
  id = _ref8$id === void 0 ? null : _ref8$id;
263
221
  return newId.indexOf(id) !== -1;
264
222
  }) || [];
265
223
  onChange(newValue);
266
- } else {
224
+ } else if (!isArray(newId)) {
267
225
  var _newValue = items.filter(function (_ref9) {
268
226
  var _ref9$id = _ref9.id,
269
227
  id = _ref9$id === void 0 ? null : _ref9$id;
@@ -293,7 +251,7 @@ var ResourceItemField = function ResourceItemField(_ref) {
293
251
  onChange(finalNewValue);
294
252
  }
295
253
  setFormOpen(false);
296
- }, [onChange, multiple, value, setFormOpen, resourceType, paginated, initialPage]);
254
+ }, [onChange, multiple, value, setFormOpen, resourceType]);
297
255
  var onOpenList = useCallback(function () {
298
256
  setListOpen(false); // TODO: fix this
299
257
  }, [setListOpen]);
@@ -322,9 +280,9 @@ var ResourceItemField = function ResourceItemField(_ref) {
322
280
  // Clear the page and be good
323
281
  if (paginated) {
324
282
  setPage(defaultPage);
325
- reload();
283
+ if (reload) reload();
326
284
  } else {
327
- reload();
285
+ if (reload) reload();
328
286
  }
329
287
  }, [onChange, paginated, defaultPage, reload]);
330
288
  var form = formOpen ? /*#__PURE__*/React.createElement(ResourceForm, {
@@ -430,9 +388,7 @@ var ResourceItemField = function ResourceItemField(_ref) {
430
388
  }
431
389
  }
432
390
  }) : null);
433
- };
434
- ResourceItemField.propTypes = propTypes;
435
- ResourceItemField.defaultProps = defaultProps;
391
+ }
436
392
 
437
393
  var definition = {
438
394
  id: 'resource-item',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@panneau/field-resource-item",
3
- "version": "3.0.310",
3
+ "version": "4.0.0",
4
4
  "description": "An item mapping a resource",
5
5
  "keywords": [
6
6
  "javascript"
@@ -49,23 +49,23 @@
49
49
  },
50
50
  "dependencies": {
51
51
  "@babel/runtime": "^7.12.5",
52
- "@panneau/core": "^3.0.307",
53
- "@panneau/data": "^3.0.307",
54
- "@panneau/element-button": "^3.0.308",
55
- "@panneau/element-resource-card": "^3.0.308",
56
- "@panneau/element-select": "^3.0.310",
57
- "@panneau/form-resource": "^3.0.308",
58
- "@panneau/intl": "^3.0.307",
59
- "@panneau/modal-dialog": "^3.0.308",
60
- "@panneau/modal-resource-form": "^3.0.308",
61
- "@panneau/modal-resource-items": "^3.0.308",
52
+ "@panneau/core": "^4.0.0",
53
+ "@panneau/data": "^4.0.0",
54
+ "@panneau/element-button": "^4.0.0",
55
+ "@panneau/element-resource-card": "^4.0.0",
56
+ "@panneau/element-select": "^4.0.0",
57
+ "@panneau/form-resource": "^4.0.0",
58
+ "@panneau/intl": "^4.0.0",
59
+ "@panneau/modal-dialog": "^4.0.0",
60
+ "@panneau/modal-resource-form": "^4.0.0",
61
+ "@panneau/modal-resource-items": "^4.0.0",
62
62
  "classnames": "^2.5.1",
63
63
  "lodash-es": "^4.17.21",
64
64
  "prop-types": "^15.7.2",
65
- "react-intl": "^5.15.8||^6.0.0"
65
+ "react-intl": "^5.15.8 || ^6.0.0 || ^7.0.0"
66
66
  },
67
67
  "publishConfig": {
68
68
  "access": "public"
69
69
  },
70
- "gitHead": "d7d8937d48a4f972a8fa742256267f6b4aaf3f4f"
70
+ "gitHead": "2cca874e0388b4a20c39aadb205c0e67d5a946fc"
71
71
  }