@bigbinary/neeto-molecules 3.10.6 → 3.11.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.
@@ -1,106 +1,26 @@
1
1
  import _defineProperty from '@babel/runtime/helpers/defineProperty';
2
2
  import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
3
- import { useState, useRef, createElement } from 'react';
3
+ import { useState, useRef } from 'react';
4
4
  import { DragDropContext, Droppable, Draggable } from '@hello-pangea/dnd';
5
- import { findIndexById, isNotEmpty, noop, slugify } from '@bigbinary/neeto-cist';
5
+ import { useField, FieldArray } from 'formik';
6
+ import { isNotPresent, slugify, isNotEmpty, findIndexById, noop } from '@bigbinary/neeto-cist';
6
7
  import Plus from '@bigbinary/neeto-icons/Plus';
7
8
  import Button from '@bigbinary/neetoui/Button';
8
- import Typography from '@bigbinary/neetoui/Typography';
9
- import { reject, prop, isEmpty, assocPath, remove, move, append, pluck } from 'ramda';
9
+ import { reject, prop, pluck, assocPath, remove, move, append } from 'ramda';
10
10
  import { useTranslation, Trans } from 'react-i18next';
11
+ import Modal from '@bigbinary/neetoui/Modal';
12
+ import Textarea from '@bigbinary/neetoui/Textarea';
13
+ import Typography from '@bigbinary/neetoui/Typography';
14
+ import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
15
+ import i18next from 'i18next';
16
+ import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
11
17
  import { withT } from '@bigbinary/neeto-commons-frontend/react-utils';
12
18
  import Delete from '@bigbinary/neeto-icons/Delete';
13
19
  import Edit from '@bigbinary/neeto-icons/Edit';
14
20
  import Drag from '@bigbinary/neeto-icons/Drag';
15
- import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
16
- import Modal from '@bigbinary/neetoui/Modal';
17
- import Textarea from '@bigbinary/neetoui/Textarea';
18
- import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
19
- import i18next from 'i18next';
20
21
  import ImageUpload from '@bigbinary/neeto-icons/ImageUpload';
21
- import Input from '@bigbinary/neetoui/Input';
22
-
23
- function ownKeys$4(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
24
- function _objectSpread$4(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$4(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$4(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
25
- var DragHandle = function DragHandle(props) {
26
- return /*#__PURE__*/jsx("div", _objectSpread$4(_objectSpread$4({
27
- className: "neeto-ui-text-gray-500 flex h-8 flex-shrink-0 cursor-grab items-center",
28
- "data-cy": "drag-handle",
29
- "data-testid": "drag-handle"
30
- }, props), {}, {
31
- children: /*#__PURE__*/jsx(Drag, {
32
- size: 16,
33
- strokeWidth: 2
34
- })
35
- }));
36
- };
37
-
38
- function ownKeys$3(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
39
- function _objectSpread$3(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$3(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$3(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
40
- var BlockOption = withT(function (_ref) {
41
- var t = _ref.t,
42
- innerRef = _ref.innerRef,
43
- _ref$hideDeleteOption = _ref.hideDeleteOption,
44
- hideDeleteOption = _ref$hideDeleteOption === void 0 ? false : _ref$hideDeleteOption,
45
- draggableProps = _ref.draggableProps,
46
- dragHandleProps = _ref.dragHandleProps,
47
- label = _ref.label,
48
- index = _ref.index,
49
- onEdit = _ref.onEdit,
50
- handleDeleteClick = _ref.handleDeleteClick,
51
- id = _ref.id;
52
- return /*#__PURE__*/jsx("div", _objectSpread$3(_objectSpread$3(_objectSpread$3({
53
- ref: innerRef
54
- }, draggableProps), dragHandleProps), {}, {
55
- children: /*#__PURE__*/jsxs("div", {
56
- className: "neeto-ui-border-gray-400 hover:neeto-ui-bg-gray-200 neeto-ui-rounded-sm shadow-xs group relative mb-2 flex h-10 cursor-pointer items-center gap-2 border p-3 transition-colors duration-300 ease-in-out",
57
- "data-cy": "address-field-block",
58
- children: [/*#__PURE__*/jsx(DragHandle, _objectSpread$3({}, dragHandleProps)), /*#__PURE__*/jsx(Typography, {
59
- className: "flex-grow leading-4",
60
- "data-cy": "address-text-field",
61
- "data-testid": "address-text-field",
62
- style: "body2",
63
- children: label
64
- }), /*#__PURE__*/jsxs("div", {
65
- className: "flex items-center justify-end gap-2 opacity-0 group-hover:opacity-100",
66
- children: [/*#__PURE__*/jsx(Button, {
67
- className: "neeto-ui-rounded hover:neeto-ui-bg-gray-400 p-1",
68
- "data-cy": "address-field-edit-button",
69
- "data-testid": "address-field-edit-button",
70
- icon: Edit,
71
- size: "small",
72
- style: "text",
73
- tooltipProps: {
74
- content: t("neetoMolecules.common.actions.edit"),
75
- position: "top",
76
- touch: ["hold", 500]
77
- },
78
- onClick: function onClick() {
79
- return onEdit(index);
80
- }
81
- }), !hideDeleteOption && /*#__PURE__*/jsx(Button, {
82
- className: "neeto-ui-rounded hover:neeto-ui-bg-gray-400 p-1",
83
- "data-cy": "address-field-delete-button",
84
- "data-testid": "address-field-delete-button",
85
- icon: Delete,
86
- size: "small",
87
- style: "text",
88
- tooltipProps: {
89
- content: t("neetoMolecules.common.actions.delete"),
90
- position: "top",
91
- touch: ["hold", 500]
92
- },
93
- onClick: function onClick() {
94
- return handleDeleteClick({
95
- index: index,
96
- id: id
97
- });
98
- }
99
- })]
100
- })]
101
- })
102
- }));
103
- });
22
+ import Input from '@bigbinary/neetoui/formik/Input';
23
+ import Textarea$1 from '@bigbinary/neetoui/formik/Textarea';
104
24
 
105
25
  var BulkOptionModal = function BulkOptionModal(_ref) {
106
26
  var isOpen = _ref.isOpen,
@@ -173,24 +93,56 @@ var BulkOptionModal = function BulkOptionModal(_ref) {
173
93
 
174
94
  var DEFAULT_OPTION_LABEL = i18next.t("neetoMolecules.optionFields.defaultOption");
175
95
 
176
- var getOrderedVisibleOptions = function getOrderedVisibleOptions() {
96
+ var FormError = function FormError(_ref) {
97
+ var error = _ref.error;
98
+ if (isNotPresent(error) || typeof error !== "string") return null;
99
+ return /*#__PURE__*/jsx(Typography, {
100
+ className: "neeto-ui-input__error",
101
+ "data-testid": "validation-error-container",
102
+ style: "body3",
103
+ children: error
104
+ });
105
+ };
106
+
107
+ var getOrderedOptions = function getOrderedOptions() {
177
108
  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
178
- return options.filter(function (option) {
179
- return !option.deleted;
180
- }).sort(function (a, b) {
109
+ return options.sort(function (a, b) {
181
110
  return a.position - b.position;
182
111
  });
183
112
  };
184
- var filterDeletedRecords = reject(prop("deleted"));
113
+ var filterDeletedRecords = function filterDeletedRecords(options, destroyFlagName) {
114
+ return reject(prop(destroyFlagName), options);
115
+ };
185
116
  var randomId = function randomId() {
186
117
  return Math.random().toString(36).substring(2, 13);
187
118
  };
119
+ var getUniqueOptionId = function getUniqueOptionId(option) {
120
+ var _option$id, _option$position, _option$label;
121
+ return option.altId || ((_option$id = option.id) === null || _option$id === void 0 ? void 0 : _option$id.toString()) || ((_option$position = option.position) === null || _option$position === void 0 ? void 0 : _option$position.toString()) || slugify((_option$label = option.label) !== null && _option$label !== void 0 ? _option$label : "");
122
+ };
123
+ var generatePlaceholder = function generatePlaceholder(itemLabel, count) {
124
+ return "".concat(itemLabel, " ").concat(count + 1);
125
+ };
126
+ var _generateLabel = function generateLabel(itemLabel, options, count) {
127
+ var labelCount = count || options.length + 1;
128
+ var tempLabel = "".concat(itemLabel, " ").concat(labelCount);
129
+ var isDuplicateLabel = pluck("label", options).includes(tempLabel);
130
+ return isDuplicateLabel ? _generateLabel(itemLabel, options, labelCount + 1) : tempLabel;
131
+ };
132
+ var resolveFormikValue = function resolveFormikValue(path, values) {
133
+ if (!path || typeof path !== "string") {
134
+ return [];
135
+ }
136
+ return path.replace(/\[|\]/g, ".").split(".").filter(Boolean).reduce(function (acc, key) {
137
+ return acc && acc[key] !== undefined ? acc[key] : undefined;
138
+ }, values);
139
+ };
188
140
 
189
- function ownKeys$2(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
190
- function _objectSpread$2(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$2(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$2(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
141
+ function ownKeys$4(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
142
+ function _objectSpread$4(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$4(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$4(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
191
143
  var attachPosition = function attachPosition(options) {
192
144
  return options.map(function (option, index) {
193
- return _objectSpread$2(_objectSpread$2({}, option), {}, {
145
+ return _objectSpread$4(_objectSpread$4({}, option), {}, {
194
146
  position: index
195
147
  });
196
148
  });
@@ -199,8 +151,11 @@ var useOptionFields = function useOptionFields(_ref) {
199
151
  var options = _ref.options,
200
152
  onChange = _ref.onChange,
201
153
  shouldDestroy = _ref.shouldDestroy,
202
- _ref$itemLabel = _ref.itemLabel,
203
- itemLabel = _ref$itemLabel === void 0 ? DEFAULT_OPTION_LABEL : _ref$itemLabel;
154
+ itemLabel = _ref.itemLabel,
155
+ shouldResetEmptyOptionOnBlur = _ref.shouldResetEmptyOptionOnBlur,
156
+ name = _ref.name,
157
+ destroyFlagName = _ref.destroyFlagName,
158
+ isNewItemsPrefilled = _ref.isNewItemsPrefilled;
204
159
  var _useState = useState(false),
205
160
  _useState2 = _slicedToArray(_useState, 2),
206
161
  shouldFocus = _useState2[0],
@@ -209,11 +164,20 @@ var useOptionFields = function useOptionFields(_ref) {
209
164
  id: "",
210
165
  value: ""
211
166
  });
167
+ var _useField = useField(name),
168
+ _useField2 = _slicedToArray(_useField, 3);
169
+ _useField2[0];
170
+ _useField2[1];
171
+ var helpers = _useField2[2];
172
+ var setValue = helpers.setValue;
212
173
  var handleChange = function handleChange(id, value) {
213
174
  var isOnBlur = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
214
175
  // Ignores onBlur events if the value in the input field is not cleared.
215
176
  // The value will be reset to the initial value if user clicks outside after clearing the value.
216
- if (isOnBlur && !isEmpty(value)) return;
177
+ // This can be controlled using the prop `shouldResetEmptyOptionOnBlur`.
178
+ if (isOnBlur && (isNotEmpty(value) || !shouldResetEmptyOptionOnBlur)) {
179
+ return;
180
+ }
217
181
  var index = findIndexById(id, options);
218
182
  if (editingOptionDetailsRef.current.id !== id) {
219
183
  editingOptionDetailsRef.current = {
@@ -224,49 +188,50 @@ var useOptionFields = function useOptionFields(_ref) {
224
188
 
225
189
  // Resets the value to initial value if the user cleared and blurred the input field.
226
190
  var valueToUpdate = isOnBlur ? editingOptionDetailsRef.current.value : value;
227
- onChange(assocPath([index, "label"], valueToUpdate, options));
228
- };
229
- var _generateLabel = function generateLabel(opts, count) {
230
- var labelCount = count || opts.length + 1;
231
- var tempLabel = "".concat(itemLabel, " ").concat(labelCount);
232
- var isDuplicateLabel = pluck("label", opts).includes(tempLabel);
233
- return isDuplicateLabel ? _generateLabel(opts, labelCount + 1) : tempLabel;
191
+ var nextOptions = assocPath([index, "label"], valueToUpdate, options);
192
+ setValue(nextOptions);
193
+ onChange === null || onChange === void 0 ? void 0 : onChange(nextOptions);
234
194
  };
235
- var handleDelete = function handleDelete(_, id) {
195
+ var handleDelete = function handleDelete(id, arrayHelpers) {
236
196
  var nextOptions;
237
197
  var indexToDelete = findIndexById(id, options);
238
198
  if (shouldDestroy) {
239
- nextOptions = assocPath([indexToDelete, "deleted"], true, options);
199
+ nextOptions = assocPath([indexToDelete, destroyFlagName], true, options);
200
+ setValue(nextOptions);
240
201
  } else {
202
+ arrayHelpers.remove(indexToDelete);
241
203
  nextOptions = attachPosition(remove(indexToDelete, 1, options));
242
204
  }
243
- onChange(nextOptions);
205
+ onChange === null || onChange === void 0 ? void 0 : onChange(nextOptions);
244
206
  };
245
207
  var handleDragEnd = function handleDragEnd(_ref2) {
246
208
  var source = _ref2.source,
247
209
  destination = _ref2.destination;
248
210
  // When dragged outside of draggable
249
211
  if (destination === null) return;
250
- var deletedOptions = options.filter(prop("deleted"));
251
- var filteredOptions = filterDeletedRecords(options);
212
+ var deletedOptions = options.filter(prop(destroyFlagName));
213
+ var filteredOptions = filterDeletedRecords(options, destroyFlagName);
252
214
  var nextOptions = move(source.index, destination.index, filteredOptions);
253
215
  var finalOptions = attachPosition(nextOptions);
254
- onChange([].concat(_toConsumableArray(deletedOptions), _toConsumableArray(finalOptions)));
216
+ setValue([].concat(_toConsumableArray(deletedOptions), _toConsumableArray(finalOptions)));
217
+ onChange === null || onChange === void 0 ? void 0 : onChange([].concat(_toConsumableArray(deletedOptions), _toConsumableArray(finalOptions)));
255
218
  };
256
219
  var handleAddOption = function handleAddOption() {
257
220
  var id = randomId();
258
221
  var newOption = {
259
222
  id: "draft-".concat(id),
260
223
  altId: "alt-".concat(id),
261
- label: _generateLabel(options, null),
224
+ label: isNewItemsPrefilled ? _generateLabel(itemLabel, options) : "",
262
225
  position: options.length
263
226
  };
264
227
  setShouldFocus(true);
265
- onChange(append(newOption, options));
228
+ var nextOptions = append(newOption, options);
229
+ setValue(nextOptions);
230
+ onChange === null || onChange === void 0 ? void 0 : onChange(nextOptions);
266
231
  };
267
232
  var handleBulkAddOption = function handleBulkAddOption(optionsString) {
268
233
  setShouldFocus(true);
269
- var position = filterDeletedRecords(options).length;
234
+ var position = options.length;
270
235
  var newOptions = optionsString.trim().split(/\r?\n|,/).filter(isNotEmpty).map(function (option, index) {
271
236
  return {
272
237
  id: "draft-".concat(randomId()),
@@ -275,23 +240,23 @@ var useOptionFields = function useOptionFields(_ref) {
275
240
  position: position + index
276
241
  };
277
242
  });
278
- onChange([].concat(_toConsumableArray(options), _toConsumableArray(newOptions)));
243
+ var nextOptions = [].concat(_toConsumableArray(options), _toConsumableArray(newOptions));
244
+ setValue(nextOptions);
245
+ onChange === null || onChange === void 0 ? void 0 : onChange(nextOptions);
279
246
  };
280
- var handleKeyDown = function handleKeyDown() {
281
- return function (event) {
282
- if (event.key !== "Enter") return;
283
- var form = event.target.form;
284
- var inputs = Array.from(form.querySelectorAll("input"));
285
- var currentInput = inputs.indexOf(event.target);
286
- if (currentInput >= inputs.length - 1) {
287
- handleAddOption();
288
- } else {
289
- var _inputs;
290
- (_inputs = inputs[currentInput + 1]) === null || _inputs === void 0 ? void 0 : _inputs.focus();
291
- event.preventDefault();
292
- }
293
- event.stopPropagation();
294
- };
247
+ var handleKeyDown = function handleKeyDown(event) {
248
+ if (event.key !== "Enter") return;
249
+ var form = event.target.form;
250
+ var inputs = Array.from(form.querySelectorAll("input"));
251
+ var currentInput = inputs.indexOf(event.target);
252
+ if (currentInput >= inputs.length - 1) {
253
+ handleAddOption();
254
+ } else {
255
+ var _inputs;
256
+ (_inputs = inputs[currentInput + 1]) === null || _inputs === void 0 ? void 0 : _inputs.focus();
257
+ event.preventDefault();
258
+ }
259
+ event.stopPropagation();
295
260
  };
296
261
  return {
297
262
  shouldFocus: shouldFocus,
@@ -304,6 +269,89 @@ var useOptionFields = function useOptionFields(_ref) {
304
269
  };
305
270
  };
306
271
 
272
+ function ownKeys$3(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
273
+ function _objectSpread$3(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$3(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$3(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
274
+ var DragHandle = function DragHandle(props) {
275
+ return /*#__PURE__*/jsx("div", _objectSpread$3(_objectSpread$3({
276
+ className: "neeto-ui-text-gray-500 flex h-8 flex-shrink-0 cursor-grab items-center",
277
+ "data-cy": "drag-handle",
278
+ "data-testid": "drag-handle"
279
+ }, props), {}, {
280
+ children: /*#__PURE__*/jsx(Drag, {
281
+ size: 16,
282
+ strokeWidth: 2
283
+ })
284
+ }));
285
+ };
286
+
287
+ function ownKeys$2(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
288
+ function _objectSpread$2(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$2(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$2(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
289
+ var BlockOption = withT(function (_ref) {
290
+ var t = _ref.t,
291
+ innerRef = _ref.innerRef,
292
+ _ref$hideDeleteOption = _ref.hideDeleteOption,
293
+ hideDeleteOption = _ref$hideDeleteOption === void 0 ? false : _ref$hideDeleteOption,
294
+ draggableProps = _ref.draggableProps,
295
+ dragHandleProps = _ref.dragHandleProps,
296
+ label = _ref.label,
297
+ onEdit = _ref.onEdit,
298
+ handleDeleteClick = _ref.handleDeleteClick,
299
+ id = _ref.id,
300
+ isDraggable = _ref.isDraggable,
301
+ optionIndex = _ref.optionIndex,
302
+ arrayHelpers = _ref.arrayHelpers;
303
+ return /*#__PURE__*/jsx("div", _objectSpread$2(_objectSpread$2(_objectSpread$2({
304
+ ref: innerRef
305
+ }, draggableProps), dragHandleProps), {}, {
306
+ children: /*#__PURE__*/jsxs("div", {
307
+ className: "neeto-ui-border-gray-400 hover:neeto-ui-bg-gray-200 neeto-ui-rounded-sm shadow-xs group relative mb-2 flex h-10 cursor-pointer items-center gap-2 border p-3 transition-colors duration-300 ease-in-out",
308
+ "data-cy": "address-field-block",
309
+ children: [isDraggable && /*#__PURE__*/jsx(DragHandle, _objectSpread$2(_objectSpread$2({}, dragHandleProps), {}, {
310
+ "data-testid": "drag-handle-".concat(optionIndex)
311
+ })), /*#__PURE__*/jsx(Typography, {
312
+ className: "flex-grow leading-4",
313
+ "data-cy": "address-text-field",
314
+ "data-testid": "address-text-field",
315
+ style: "body2",
316
+ children: label
317
+ }), /*#__PURE__*/jsxs("div", {
318
+ className: "flex items-center justify-end gap-2 opacity-0 group-hover:opacity-100",
319
+ children: [/*#__PURE__*/jsx(Button, {
320
+ className: "neeto-ui-rounded hover:neeto-ui-bg-gray-400 p-1",
321
+ "data-cy": "address-field-edit-button",
322
+ "data-testid": "address-field-edit-button",
323
+ icon: Edit,
324
+ size: "small",
325
+ style: "text",
326
+ tooltipProps: {
327
+ content: t("neetoMolecules.common.actions.edit"),
328
+ position: "top",
329
+ touch: ["hold", 500]
330
+ },
331
+ onClick: function onClick() {
332
+ return onEdit(id);
333
+ }
334
+ }), !hideDeleteOption && /*#__PURE__*/jsx(Button, {
335
+ className: "neeto-ui-rounded hover:neeto-ui-bg-gray-400 p-1",
336
+ "data-cy": "address-field-delete-button",
337
+ "data-testid": "delete-option-button-".concat(optionIndex),
338
+ icon: Delete,
339
+ size: "small",
340
+ style: "text",
341
+ tooltipProps: {
342
+ content: t("neetoMolecules.common.actions.delete"),
343
+ position: "top",
344
+ touch: ["hold", 500]
345
+ },
346
+ onClick: function onClick() {
347
+ return handleDeleteClick(id, arrayHelpers);
348
+ }
349
+ })]
350
+ })]
351
+ })
352
+ }));
353
+ });
354
+
307
355
  function ownKeys$1(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
308
356
  function _objectSpread$1(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$1(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$1(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
309
357
  var InputOption = function InputOption(_ref) {
@@ -317,22 +365,31 @@ var InputOption = function InputOption(_ref) {
317
365
  onKeyDown = _ref.onKeyDown,
318
366
  onDelete = _ref.onDelete,
319
367
  _onChange = _ref.onChange,
320
- label = _ref.label,
321
- index = _ref.index,
368
+ optionIndex = _ref.optionIndex,
322
369
  disabled = _ref.disabled,
323
370
  isTextArea = _ref.isTextArea,
324
- id = _ref.id;
371
+ id = _ref.id,
372
+ isDraggable = _ref.isDraggable,
373
+ name = _ref.name,
374
+ placeholder = _ref.placeholder,
375
+ _onBlur = _ref.onBlur,
376
+ arrayHelpers = _ref.arrayHelpers;
325
377
  var _useTranslation = useTranslation(),
326
378
  t = _useTranslation.t;
327
- var Component = isTextArea ? Textarea : Input;
379
+ var Component = isTextArea ? Textarea$1 : Input;
380
+ var _useField = useField(name),
381
+ _useField2 = _slicedToArray(_useField, 1),
382
+ field = _useField2[0];
328
383
  return /*#__PURE__*/jsx("div", _objectSpread$1(_objectSpread$1({
329
384
  ref: innerRef
330
385
  }, _objectSpread$1(_objectSpread$1({}, draggableProps), dragHandleProps)), {}, {
331
386
  "data-cy": "form-block-options",
332
387
  children: /*#__PURE__*/jsxs("div", {
333
388
  className: "nf-input-options group relative mb-2 flex items-start gap-1",
334
- children: [/*#__PURE__*/jsx(DragHandle, _objectSpread$1({}, dragHandleProps)), isPictureChoice && /*#__PURE__*/jsx(Fragment, {
335
- children: index === 0 ? /*#__PURE__*/jsx("img", {
389
+ children: [isDraggable && /*#__PURE__*/jsx(DragHandle, _objectSpread$1(_objectSpread$1({}, dragHandleProps), {}, {
390
+ "data-testid": "drag-handle-".concat(optionIndex)
391
+ })), isPictureChoice && /*#__PURE__*/jsx(Fragment, {
392
+ children: optionIndex === 0 ? /*#__PURE__*/jsx("img", {
336
393
  className: "neeto-ui-rounded-sm h-8 w-8 cursor-pointer overflow-hidden object-cover",
337
394
  "data-testid": "picture-choice-option",
338
395
  src: "https://i.pravatar.cc/300"
@@ -344,16 +401,17 @@ var InputOption = function InputOption(_ref) {
344
401
  })
345
402
  }), /*#__PURE__*/jsx(Component, {
346
403
  disabled: disabled,
404
+ name: name,
405
+ onKeyDown: onKeyDown,
406
+ placeholder: placeholder,
347
407
  autoFocus: shouldFocus,
348
- "data-cy": "option-input-".concat(index),
349
- "data-testid": isTextArea ? "textarea-option" : "input-option",
350
- placeholder: t("neetoMolecules.optionFields.optionName"),
351
- value: label,
408
+ "data-cy": "option-input-".concat(optionIndex),
409
+ "data-testid": "".concat(isTextArea ? "textarea-option" : "input-option", "-").concat(optionIndex),
352
410
  suffix: !hideDeleteOption && /*#__PURE__*/jsx(Button, {
353
411
  disabled: disabled,
354
412
  className: "p-0.5",
355
- "data-cy": "delete-option-button-".concat(index),
356
- "data-testid": "delete-option-button",
413
+ "data-cy": "delete-option-button-".concat(optionIndex),
414
+ "data-testid": "delete-option-button-".concat(optionIndex),
357
415
  icon: Delete,
358
416
  size: "small",
359
417
  style: "text",
@@ -363,39 +421,97 @@ var InputOption = function InputOption(_ref) {
363
421
  touch: ["hold", 500]
364
422
  },
365
423
  onClick: function onClick() {
366
- return onDelete({
367
- index: index,
368
- id: id
369
- });
424
+ return onDelete(id, arrayHelpers);
370
425
  }
371
426
  }),
372
- onBlur: function onBlur(_ref2) {
427
+ onChange: function onChange(_ref2) {
373
428
  var target = _ref2.target;
374
- return _onChange(id, target.value, true);
375
- },
376
- onChange: function onChange(_ref3) {
377
- var target = _ref3.target;
378
429
  return _onChange(id, target.value);
379
430
  },
380
- onFocus: function onFocus(_ref4) {
381
- var target = _ref4.target;
431
+ onFocus: function onFocus(_ref3) {
432
+ var target = _ref3.target;
382
433
  return target.select();
383
434
  },
384
- onKeyDown: onKeyDown(index)
435
+ onBlur: function onBlur(event) {
436
+ _onChange(id, event.target.value, true);
437
+ _onBlur === null || _onBlur === void 0 ? void 0 : _onBlur(id, event.target.value);
438
+ for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
439
+ args[_key - 1] = arguments[_key];
440
+ }
441
+ field.onBlur.apply(field, [event].concat(args));
442
+ }
385
443
  })]
386
444
  })
387
445
  }));
388
446
  };
389
447
 
448
+ var OptionField = function OptionField(_ref) {
449
+ var option = _ref.option,
450
+ editable = _ref.editable,
451
+ dragHandleProps = _ref.dragHandleProps,
452
+ draggableProps = _ref.draggableProps,
453
+ handleDeleteClick = _ref.handleDeleteClick,
454
+ hideDeleteOption = _ref.hideDeleteOption,
455
+ innerRef = _ref.innerRef,
456
+ onEdit = _ref.onEdit,
457
+ draggable = _ref.draggable,
458
+ disabled = _ref.disabled,
459
+ isPictureChoice = _ref.isPictureChoice,
460
+ isTextArea = _ref.isTextArea,
461
+ shouldFocus = _ref.shouldFocus,
462
+ isEditable = _ref.isEditable,
463
+ itemLabel = _ref.itemLabel,
464
+ handleChange = _ref.handleChange,
465
+ handleKeyDown = _ref.handleKeyDown,
466
+ name = _ref.name,
467
+ optionIndex = _ref.optionIndex,
468
+ onBlur = _ref.onBlur,
469
+ arrayHelpers = _ref.arrayHelpers;
470
+ if (!editable) {
471
+ return /*#__PURE__*/jsx(BlockOption, {
472
+ arrayHelpers: arrayHelpers,
473
+ dragHandleProps: dragHandleProps,
474
+ draggableProps: draggableProps,
475
+ handleDeleteClick: handleDeleteClick,
476
+ hideDeleteOption: hideDeleteOption,
477
+ innerRef: innerRef,
478
+ onEdit: onEdit,
479
+ optionIndex: optionIndex,
480
+ id: option.id,
481
+ isDraggable: draggable && !disabled,
482
+ label: option.label
483
+ });
484
+ }
485
+ return /*#__PURE__*/jsx(InputOption, {
486
+ arrayHelpers: arrayHelpers,
487
+ dragHandleProps: dragHandleProps,
488
+ draggableProps: draggableProps,
489
+ hideDeleteOption: hideDeleteOption,
490
+ innerRef: innerRef,
491
+ isPictureChoice: isPictureChoice,
492
+ isTextArea: isTextArea,
493
+ name: name,
494
+ onBlur: onBlur,
495
+ optionIndex: optionIndex,
496
+ shouldFocus: shouldFocus,
497
+ disabled: !isEditable || disabled,
498
+ id: option.id,
499
+ isDraggable: draggable && !disabled,
500
+ placeholder: generatePlaceholder(itemLabel, optionIndex),
501
+ onChange: handleChange,
502
+ onDelete: handleDeleteClick,
503
+ onKeyDown: handleKeyDown
504
+ });
505
+ };
506
+
390
507
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
391
508
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
392
509
  var OptionFields = function OptionFields(_ref) {
393
- var options = _ref.options,
510
+ var name = _ref.name,
394
511
  error = _ref.error,
395
- onChange = _ref.onChange,
396
- name = _ref.name,
397
512
  onDelete = _ref.onDelete,
398
- itemLabel = _ref.itemLabel,
513
+ onChange = _ref.onChange,
514
+ onBlur = _ref.onBlur,
399
515
  _ref$onEdit = _ref.onEdit,
400
516
  onEdit = _ref$onEdit === void 0 ? noop : _ref$onEdit,
401
517
  _ref$buttonProps = _ref.buttonProps,
@@ -406,12 +522,26 @@ var OptionFields = function OptionFields(_ref) {
406
522
  draggable = _ref$draggable === void 0 ? false : _ref$draggable,
407
523
  _ref$editable = _ref.editable,
408
524
  editable = _ref$editable === void 0 ? true : _ref$editable,
525
+ _ref$disabled = _ref.disabled,
526
+ disabled = _ref$disabled === void 0 ? false : _ref$disabled,
527
+ _ref$isAddOptionEnabl = _ref.isAddOptionEnabled,
528
+ isAddOptionEnabled = _ref$isAddOptionEnabl === void 0 ? true : _ref$isAddOptionEnabl,
529
+ _ref$isDeleteOptionEn = _ref.isDeleteOptionEnabled,
530
+ isDeleteOptionEnabled = _ref$isDeleteOptionEn === void 0 ? true : _ref$isDeleteOptionEn,
409
531
  _ref$isPictureChoice = _ref.isPictureChoice,
410
532
  isPictureChoice = _ref$isPictureChoice === void 0 ? false : _ref$isPictureChoice,
411
533
  _ref$isTextArea = _ref.isTextArea,
412
534
  isTextArea = _ref$isTextArea === void 0 ? false : _ref$isTextArea,
413
535
  _ref$shouldDestroy = _ref.shouldDestroy,
414
536
  shouldDestroy = _ref$shouldDestroy === void 0 ? false : _ref$shouldDestroy,
537
+ _ref$destroyFlagName = _ref.destroyFlagName,
538
+ destroyFlagName = _ref$destroyFlagName === void 0 ? "deleted" : _ref$destroyFlagName,
539
+ _ref$shouldResetEmpty = _ref.shouldResetEmptyOptionOnBlur,
540
+ shouldResetEmptyOptionOnBlur = _ref$shouldResetEmpty === void 0 ? true : _ref$shouldResetEmpty,
541
+ _ref$isNewItemsPrefil = _ref.isNewItemsPrefilled,
542
+ isNewItemsPrefilled = _ref$isNewItemsPrefil === void 0 ? true : _ref$isNewItemsPrefil,
543
+ _ref$itemLabel = _ref.itemLabel,
544
+ itemLabel = _ref$itemLabel === void 0 ? DEFAULT_OPTION_LABEL : _ref$itemLabel,
415
545
  _ref$bulkActionProps = _ref.bulkActionProps,
416
546
  bulkActionProps = _ref$bulkActionProps === void 0 ? {
417
547
  enabled: false,
@@ -423,11 +553,21 @@ var OptionFields = function OptionFields(_ref) {
423
553
  _useState2 = _slicedToArray(_useState, 2),
424
554
  isBulkOptionModalOpen = _useState2[0],
425
555
  setIsBulkOptionModalOpen = _useState2[1];
556
+ var _useField = useField(name),
557
+ _useField2 = _slicedToArray(_useField, 2),
558
+ field = _useField2[0],
559
+ meta = _useField2[1];
560
+ var fieldError = meta.touched ? meta.error : null;
561
+ var options = field.value;
426
562
  var _useOptionFields = useOptionFields({
427
563
  options: options,
428
564
  onChange: onChange,
429
565
  shouldDestroy: shouldDestroy,
430
- itemLabel: itemLabel
566
+ itemLabel: itemLabel,
567
+ shouldResetEmptyOptionOnBlur: shouldResetEmptyOptionOnBlur,
568
+ destroyFlagName: destroyFlagName,
569
+ name: name,
570
+ isNewItemsPrefilled: isNewItemsPrefilled
431
571
  }),
432
572
  shouldFocus = _useOptionFields.shouldFocus,
433
573
  handleAddOption = _useOptionFields.handleAddOption,
@@ -440,20 +580,49 @@ var OptionFields = function OptionFields(_ref) {
440
580
  // Temporarily disables the modifications to options when changed are getting saved to server
441
581
  // This condition only needed for server id generated types
442
582
  var isEditable = true;
443
- if (shouldDestroy) {
444
- isEditable = options.every(prop("id") || error) && !options.some(prop("_destroy"));
445
- }
446
- var handleDeleteClick = function handleDeleteClick(_ref2) {
447
- var index = _ref2.index,
448
- id = _ref2.id;
449
- var deleteFunction = onDelete !== null && onDelete !== void 0 ? onDelete : handleDelete;
450
- deleteFunction(index, id);
583
+ if (shouldDestroy) isEditable = options.every(prop("id") || error);
584
+ var orderedOptions = getOrderedOptions(options, destroyFlagName);
585
+ var filteredOptions = orderedOptions.filter(function (_ref2) {
586
+ var deleted = _ref2[destroyFlagName];
587
+ return !deleted;
588
+ });
589
+ var hideDeleteOption = filteredOptions.length <= minOptions || disabled || !isDeleteOptionEnabled;
590
+ var optionFieldProps = {
591
+ disabled: disabled,
592
+ draggable: draggable,
593
+ editable: editable,
594
+ handleChange: handleChange,
595
+ handleDeleteClick: onDelete !== null && onDelete !== void 0 ? onDelete : handleDelete,
596
+ handleKeyDown: handleKeyDown,
597
+ hideDeleteOption: hideDeleteOption,
598
+ isEditable: isEditable,
599
+ isPictureChoice: isPictureChoice,
600
+ isTextArea: isTextArea,
601
+ itemLabel: itemLabel,
602
+ onEdit: onEdit,
603
+ onBlur: onBlur,
604
+ shouldFocus: shouldFocus
451
605
  };
452
- var orderedVisibleOptions = getOrderedVisibleOptions(options);
453
- var hideDeleteOption = orderedVisibleOptions.length <= minOptions;
454
- var getUniqueOptionId = function getUniqueOptionId(option) {
455
- var _option$id, _option$position;
456
- return option.altId || ((_option$id = option.id) === null || _option$id === void 0 ? void 0 : _option$id.toString()) || ((_option$position = option.position) === null || _option$position === void 0 ? void 0 : _option$position.toString()) || slugify(option.label);
606
+
607
+ // To fix the element positioned incorrectly while dragging when in a pane or modal.
608
+ // https://github.com/hello-pangea/dnd/issues/560#issuecomment-1679201455
609
+ var renderDragClone = function renderDragClone(provided, _, rubric) {
610
+ var draggingIndex = rubric["source"].index;
611
+ var option = filteredOptions[draggingIndex];
612
+ var formikIndex = findIndexById(option.id, options);
613
+ return /*#__PURE__*/jsx(OptionField, _objectSpread(_objectSpread({}, _objectSpread(_objectSpread({}, optionFieldProps), {}, {
614
+ option: option
615
+ })), {}, {
616
+ dragHandleProps: provided.dragHandleProps,
617
+ innerRef: null,
618
+ name: "".concat(name, ".").concat(formikIndex, ".label"),
619
+ optionIndex: draggingIndex,
620
+ draggableProps: _objectSpread(_objectSpread({}, provided.draggableProps), {}, {
621
+ style: _objectSpread(_objectSpread({}, provided.draggableProps.style), {}, {
622
+ zIndex: 999999
623
+ })
624
+ })
625
+ }));
457
626
  };
458
627
  return /*#__PURE__*/jsxs("form", {
459
628
  onSubmit: function onSubmit(e) {
@@ -464,6 +633,7 @@ var OptionFields = function OptionFields(_ref) {
464
633
  children: [/*#__PURE__*/jsx(Droppable, {
465
634
  droppableId: "neeto-molecules-option-fields-".concat(name),
466
635
  isDropDisabled: !draggable,
636
+ renderClone: renderDragClone,
467
637
  children: function children(_ref3) {
468
638
  var innerRef = _ref3.innerRef,
469
639
  droppableProps = _ref3.droppableProps,
@@ -471,56 +641,44 @@ var OptionFields = function OptionFields(_ref) {
471
641
  return /*#__PURE__*/jsxs("div", _objectSpread(_objectSpread({
472
642
  ref: innerRef
473
643
  }, droppableProps), {}, {
474
- children: [orderedVisibleOptions.map(function (option, index) {
475
- return /*#__PURE__*/createElement(Draggable, {
476
- index: index,
477
- dataKey: getUniqueOptionId(option),
478
- draggableId: getUniqueOptionId(option),
479
- isDragDisabled: !draggable,
480
- key: getUniqueOptionId(option)
481
- }, function (_ref4) {
482
- var innerRef = _ref4.innerRef,
483
- draggableProps = _ref4.draggableProps,
484
- dragHandleProps = _ref4.dragHandleProps;
485
- return editable ? /*#__PURE__*/jsx(InputOption, {
486
- dragHandleProps: dragHandleProps,
487
- draggableProps: draggableProps,
488
- hideDeleteOption: hideDeleteOption,
489
- index: index,
490
- innerRef: innerRef,
491
- isPictureChoice: isPictureChoice,
492
- isTextArea: isTextArea,
493
- name: name,
494
- shouldFocus: shouldFocus,
495
- disabled: !isEditable,
496
- id: option.id,
497
- label: option.label,
498
- onChange: handleChange,
499
- onDelete: handleDeleteClick,
500
- onKeyDown: handleKeyDown
501
- }) : /*#__PURE__*/jsx(BlockOption, {
502
- dragHandleProps: dragHandleProps,
503
- draggableProps: draggableProps,
504
- handleDeleteClick: handleDeleteClick,
505
- hideDeleteOption: hideDeleteOption,
506
- index: index,
507
- innerRef: innerRef,
508
- onEdit: onEdit,
509
- id: option.id,
510
- label: option.label
644
+ children: [/*#__PURE__*/jsx(FieldArray, {
645
+ name: name,
646
+ children: function children(arrayHelpers) {
647
+ var options = resolveFormikValue(name, arrayHelpers.form.values);
648
+ return options === null || options === void 0 ? void 0 : options.map(function (option, index) {
649
+ if (option !== null && option !== void 0 && option[destroyFlagName]) return null;
650
+ var optionIndex = findIndexById(option.id, filteredOptions);
651
+ return /*#__PURE__*/jsx(Draggable, {
652
+ dataKey: getUniqueOptionId(option),
653
+ draggableId: getUniqueOptionId(option),
654
+ index: optionIndex,
655
+ isDragDisabled: !draggable,
656
+ children: function children(_ref4) {
657
+ var innerRef = _ref4.innerRef,
658
+ draggableProps = _ref4.draggableProps,
659
+ dragHandleProps = _ref4.dragHandleProps;
660
+ return /*#__PURE__*/jsx(OptionField, _objectSpread(_objectSpread({}, _objectSpread(_objectSpread({}, optionFieldProps), {}, {
661
+ arrayHelpers: arrayHelpers,
662
+ dragHandleProps: dragHandleProps,
663
+ draggableProps: draggableProps,
664
+ innerRef: innerRef,
665
+ option: option,
666
+ optionIndex: optionIndex
667
+ })), {}, {
668
+ name: "".concat(name, ".").concat(index, ".label")
669
+ }));
670
+ }
671
+ }, getUniqueOptionId(option));
511
672
  });
512
- });
513
- }), error && /*#__PURE__*/jsx(Typography, {
514
- className: "neeto-ui-input__error",
515
- "data-testid": "validation-error-container",
516
- style: "body3",
517
- children: error
518
- }), placeholder]
673
+ }
674
+ }), placeholder, /*#__PURE__*/jsx(FormError, {
675
+ error: error !== null && error !== void 0 ? error : fieldError
676
+ })]
519
677
  }));
520
678
  }
521
679
  }), /*#__PURE__*/jsxs("div", {
522
680
  className: "mt-4 flex items-center justify-between",
523
- children: [/*#__PURE__*/jsx(Button, _objectSpread({
681
+ children: [isAddOptionEnabled && /*#__PURE__*/jsx(Button, _objectSpread({
524
682
  "data-cy": "add-option-link",
525
683
  "data-testid": "add-option-link",
526
684
  disabled: !isEditable,