@eightshift/ui-components 1.0.0 → 1.0.1

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.
@@ -32,6 +32,7 @@ import { $ as $e72dd72e1c76a225$export$2f645645f7bca764 } from "../../useListSta
32
32
  * @param {boolean} [props.hidden] - If `true`, the component is not rendered.
33
33
  * @param {JSX.Element} [props.addButton] - If provided, overrides the default add button. `(props: { addItem: (additional: Object<string, any>?) => void, disabled: Boolean }) => JSX.Element`.
34
34
  * @param {string} [props.className] - Classes to pass to the item wrapper.
35
+ * @param {JSX.Element} [props.emptyState] - Allows overriding the default empty state.
35
36
  *
36
37
  * @returns {JSX.Element} The Repeater component.
37
38
  *
@@ -82,6 +83,7 @@ const Repeater = (props) => {
82
83
  minItems,
83
84
  addButton,
84
85
  className,
86
+ emptyState,
85
87
  hidden,
86
88
  ...rest
87
89
  } = props;
@@ -112,10 +114,10 @@ const Repeater = (props) => {
112
114
  items[index] = { ...items[index], ...newValue };
113
115
  onChange(items);
114
116
  },
115
- move: (sourceKey, targetKeys) => {
117
+ move: (sourceKey, targetKeys, direction = "before") => {
116
118
  const sourceIndex = items.findIndex((item) => item.key === sourceKey);
117
119
  const targetIndices = [...targetKeys].map((key) => items.findIndex((item) => item.key === key));
118
- onChange(arrayMoveMultiple(items, targetIndices, sourceIndex));
120
+ onChange(arrayMoveMultiple(items, targetIndices, sourceIndex, direction));
119
121
  },
120
122
  insert: (targetKey, ...newItems) => {
121
123
  const targetIndex = items.findIndex((item) => item.key === targetKey);
@@ -138,7 +140,7 @@ const Repeater = (props) => {
138
140
  if (e.target.dropPosition === "before") {
139
141
  list.move(e.target.key, e.keys);
140
142
  } else if (e.target.dropPosition === "after") {
141
- list.move(e.target.key, e.keys);
143
+ list.move(e.target.key, e.keys, "after");
142
144
  }
143
145
  },
144
146
  renderDropIndicator(target) {
@@ -260,7 +262,7 @@ const Repeater = (props) => {
260
262
  setCanReorder
261
263
  })),
262
264
  dragAndDropHooks,
263
- renderEmptyState: () => hideEmptyState ? null : /* @__PURE__ */ jsx("div", { className: "es-uic-rounded-md es-uic-border es-uic-border-dashed es-uic-border-gray-300 es-uic-p-2 es-uic-text-sm es-uic-text-gray-400", children: __("No items", "eightshift-ui-components") }),
265
+ renderEmptyState: () => hideEmptyState ? null : emptyState ?? /* @__PURE__ */ jsx("div", { className: "es-uic-rounded-md es-uic-border es-uic-border-dashed es-uic-border-gray-300 es-uic-p-2 es-uic-text-sm es-uic-text-gray-400", children: __("No items", "eightshift-ui-components") }),
264
266
  className,
265
267
  ...rest,
266
268
  children
@@ -5,6 +5,7 @@ import { r as restrictToParentElement } from "../../modifiers.esm-BuJQPI1X.js";
5
5
  import { CustomSelectDefaultMultiValueRemove, CustomSelectDefaultDropdownIndicator, CustomSelectDefaultClearIndicator } from "./custom-select-default-components.js";
6
6
  import { BaseControl } from "../base-control/base-control.js";
7
7
  import { eightshiftSelectClasses } from "./styles.js";
8
+ import { fixIds } from "./shared.js";
8
9
  import { c as components } from "../../index-a301f526.esm-BzTuHLdr.js";
9
10
  /**
10
11
  * Multi-select menu with async loading and re-ordering.
@@ -95,6 +96,7 @@ const AsyncMultiSelect = (props) => {
95
96
  const results = await loadOptions(searchText);
96
97
  return processLoadedOptions((results == null ? void 0 : results.map((item) => ({ id: item.value, ...item }))) ?? []);
97
98
  };
99
+ fixIds(value, onChange);
98
100
  return /* @__PURE__ */ jsx(
99
101
  BaseControl,
100
102
  {
@@ -3,7 +3,7 @@ import { S as StateManagedSelect$1 } from "../../react-select.esm-3tyTZmrx.js";
3
3
  import { D as DndContext, g as getDragEndHandler, S as SortableContext, a as getMultiValue, b as getMultiValueRemove } from "../../multi-select-components-nORKdJ-2.js";
4
4
  import { r as restrictToParentElement } from "../../modifiers.esm-BuJQPI1X.js";
5
5
  import { CustomSelectDefaultMultiValueRemove, CustomSelectDefaultDropdownIndicator, CustomSelectDefaultClearIndicator } from "./custom-select-default-components.js";
6
- import { getValue, customOnChange } from "./shared.js";
6
+ import { fixIds, getValue, customOnChange } from "./shared.js";
7
7
  import { BaseControl } from "../base-control/base-control.js";
8
8
  import { eightshiftSelectClasses } from "./styles.js";
9
9
  import { c as components } from "../../index-a301f526.esm-BzTuHLdr.js";
@@ -86,6 +86,7 @@ const MultiSelect = (props) => {
86
86
  if (hidden) {
87
87
  return null;
88
88
  }
89
+ fixIds(value, onChange);
89
90
  return /* @__PURE__ */ jsx(
90
91
  BaseControl,
91
92
  {
@@ -23,7 +23,7 @@ const getValue = (simpleValue, value, options) => {
23
23
  *
24
24
  * @param {boolean} simpleValue - Whether `simpleValue` is set.
25
25
  * @param {string|{label: string, value: string, metadata: Object<string, any>[]}} newValue - The new value to be set.
26
- * @param {Function} - onChange The `onChange` callback passed to the component.
26
+ * @param {Function} onChange - The `onChange` callback passed to the component.
27
27
  * @returns {void}
28
28
  *
29
29
  * @preserve
@@ -39,7 +39,32 @@ const customOnChange = (simpleValue, newValue, onChange) => {
39
39
  }
40
40
  onChange(newValue == null ? void 0 : newValue.value);
41
41
  };
42
+ /**
43
+ * Handles the `onChange` callback.
44
+ *
45
+ * @param {object[]} items - Current items.
46
+ * @param {Function} onChange - The `onChange` callback passed to the component.
47
+ * @returns {void}
48
+ *
49
+ * @preserve
50
+ */
51
+ const fixIds = (items, onChange) => {
52
+ const allIds = (items == null ? void 0 : items.map(({ id }) => id)) ?? [];
53
+ const hasDuplicates = (input) => {
54
+ var _a;
55
+ return ((_a = new Set(input)) == null ? void 0 : _a.size) !== (input == null ? void 0 : input.length);
56
+ };
57
+ const hasMissingIds = items == null ? void 0 : items.some(({ id }) => typeof id === "undefined" || id === null || id === "");
58
+ if (hasDuplicates(allIds) && (items == null ? void 0 : items.length) > 0 || hasMissingIds) {
59
+ const newItems = [...items].map((item, index) => ({
60
+ ...item,
61
+ id: index + 1
62
+ }));
63
+ onChange(newItems);
64
+ }
65
+ };
42
66
  export {
43
67
  customOnChange,
68
+ fixIds,
44
69
  getValue
45
70
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eightshift/ui-components",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",