@contentful/field-editor-tags 1.4.0 → 1.4.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.
@@ -9,11 +9,12 @@ Object.defineProperty(exports, "TagsEditor", {
9
9
  }
10
10
  });
11
11
  const _react = _interop_require_wildcard(require("react"));
12
- const _reactsortablehoc = require("react-sortable-hoc");
13
12
  const _f36components = require("@contentful/f36-components");
14
- const _f36icons = require("@contentful/f36-icons");
15
13
  const _f36tokens = _interop_require_default(require("@contentful/f36-tokens"));
16
- const _arraymove = _interop_require_default(require("array-move"));
14
+ const _core = require("@dnd-kit/core");
15
+ const _modifiers = require("@dnd-kit/modifiers");
16
+ const _sortable = require("@dnd-kit/sortable");
17
+ const _utilities = require("@dnd-kit/utilities");
17
18
  const _emotion = require("emotion");
18
19
  const _noop = _interop_require_default(require("lodash/noop"));
19
20
  const _TagsEditorConstraints = require("./TagsEditorConstraints");
@@ -62,11 +63,6 @@ function _interop_require_wildcard(obj, nodeInterop) {
62
63
  return newObj;
63
64
  }
64
65
  const styles = {
65
- dropContainer: (0, _emotion.css)({
66
- whiteSpace: 'nowrap',
67
- display: 'flex',
68
- flexWrap: 'wrap'
69
- }),
70
66
  input: (0, _emotion.css)({
71
67
  marginTop: _f36tokens.default.spacingS,
72
68
  marginBottom: _f36tokens.default.spacingM
@@ -81,59 +77,69 @@ const styles = {
81
77
  cursor: 'not-allowed !important',
82
78
  pointerEvents: 'none'
83
79
  }
84
- }),
85
- handle: (0, _emotion.css)({
86
- lineHeight: '1.5rem',
87
- padding: '0.375rem 0.625rem',
88
- paddingRight: 0,
89
- cursor: 'grab',
90
- userSelect: 'none',
91
- svg: {
92
- fill: _f36tokens.default.gray500,
93
- verticalAlign: 'middle'
94
- }
95
80
  })
96
81
  };
97
- const SortablePillHandle = (0, _reactsortablehoc.SortableHandle)((props)=>_react.default.createElement("div", {
98
- className: (0, _emotion.cx)(styles.handle, {
99
- [styles.pillDisabled]: props.isDisabled
100
- })
101
- }, _react.default.createElement(_f36icons.DragIcon, {
102
- variant: "muted"
103
- })));
104
- const SortablePill = (0, _reactsortablehoc.SortableElement)((props)=>_react.default.createElement(_f36components.Pill, {
82
+ const SortablePill = ({ id , label , index , disabled , onRemove })=>{
83
+ const { listeners , setNodeRef , setActivatorNodeRef , transform , transition } = (0, _sortable.useSortable)({
84
+ id
85
+ });
86
+ const style = {
87
+ transform: transform ? _utilities.CSS.Transform.toString({
88
+ ...transform,
89
+ scaleX: 1,
90
+ scaleY: 1
91
+ }) : '',
92
+ transition
93
+ };
94
+ return _react.default.createElement(_f36components.Pill, {
95
+ ref: setNodeRef,
105
96
  testId: "tag-editor-pill",
106
97
  className: (0, _emotion.cx)(styles.pill, {
107
- [styles.pillDisabled]: props.isSortablePillDisabled
98
+ [styles.pillDisabled]: disabled
108
99
  }),
109
- label: props.label,
100
+ style: style,
101
+ label: label,
110
102
  onClose: ()=>{
111
- if (!props.isSortablePillDisabled) {
112
- props.onRemove(props.index);
103
+ if (!disabled) {
104
+ onRemove(index);
113
105
  }
114
106
  },
115
107
  onDrag: _noop.default,
116
- dragHandleComponent: _react.default.createElement(SortablePillHandle, {
117
- isDisabled: props.isSortablePillDisabled
108
+ dragHandleComponent: _react.default.createElement(_f36components.DragHandle, {
109
+ ref: setActivatorNodeRef,
110
+ variant: "transparent",
111
+ label: "",
112
+ ...listeners
118
113
  })
119
- }));
120
- const SortableList = (0, _reactsortablehoc.SortableContainer)((props)=>_react.default.createElement("div", {
121
- className: styles.dropContainer
122
- }, props.children));
114
+ });
115
+ };
123
116
  function TagsEditor(props) {
124
117
  const [pendingValue, setPendingValue] = (0, _react.useState)('');
125
- const { isDisabled , items , constraints , constraintsType , hasError } = props;
118
+ const { isDisabled , items , constraints , constraintsType , hasError , onUpdate } = props;
119
+ const itemsMap = _react.default.useMemo(()=>items.map((item, index)=>({
120
+ id: item + index,
121
+ value: item
122
+ })), [
123
+ items
124
+ ]);
126
125
  const removeItem = (0, _react.useCallback)((index)=>{
127
- const newItems = props.items.filter((_, filterIndex)=>index !== filterIndex);
128
- props.onUpdate(newItems);
126
+ const newItems = items.filter((_, filterIndex)=>index !== filterIndex);
127
+ onUpdate(newItems);
129
128
  }, [
130
- props
129
+ items,
130
+ onUpdate
131
131
  ]);
132
- const swapItems = (0, _react.useCallback)(({ oldIndex , newIndex })=>{
133
- const newItems = (0, _arraymove.default)(props.items, oldIndex, newIndex);
134
- props.onUpdate(newItems);
132
+ const swapItems = (0, _react.useCallback)(({ active , over })=>{
133
+ if (active.id !== over.id) {
134
+ const oldIndex = itemsMap.findIndex(({ id })=>id === active.id);
135
+ const newIndex = itemsMap.findIndex(({ id })=>id === over.id);
136
+ const newItems = (0, _sortable.arrayMove)(items, oldIndex, newIndex);
137
+ onUpdate(newItems);
138
+ }
135
139
  }, [
136
- props
140
+ items,
141
+ itemsMap,
142
+ onUpdate
137
143
  ]);
138
144
  return _react.default.createElement("div", {
139
145
  "data-test-id": "tag-editor-container"
@@ -147,8 +153,8 @@ function TagsEditor(props) {
147
153
  placeholder: "Type the value and hit enter",
148
154
  onKeyDown: (e)=>{
149
155
  if (pendingValue && e.keyCode === 13) {
150
- props.onUpdate([
151
- ...props.items,
156
+ onUpdate([
157
+ ...items,
152
158
  pendingValue
153
159
  ]);
154
160
  setPendingValue('');
@@ -157,28 +163,21 @@ function TagsEditor(props) {
157
163
  onChange: (e)=>{
158
164
  setPendingValue(e.target.value);
159
165
  }
160
- }), _react.default.createElement(SortableList, {
161
- useDragHandle: true,
162
- axis: "xy",
163
- distance: 10,
164
- onSortEnd: ({ oldIndex , newIndex })=>{
165
- swapItems({
166
- oldIndex,
167
- newIndex
168
- });
169
- }
170
- }, items.map((item, index)=>{
171
- return _react.default.createElement(SortablePill, {
172
- label: item,
166
+ }), _react.default.createElement(_core.DndContext, {
167
+ onDragEnd: swapItems,
168
+ modifiers: [
169
+ _modifiers.restrictToParentElement
170
+ ]
171
+ }, _react.default.createElement(_sortable.SortableContext, {
172
+ items: itemsMap
173
+ }, itemsMap.map((item, index)=>_react.default.createElement(SortablePill, {
174
+ key: item.id,
175
+ id: item.id,
176
+ label: item.value,
173
177
  index: index,
174
- key: item + index,
175
178
  disabled: isDisabled,
176
- isSortablePillDisabled: isDisabled,
177
- onRemove: ()=>{
178
- removeItem(index);
179
- }
180
- });
181
- })), constraints && constraintsType && _react.default.createElement(_TagsEditorConstraints.TagsEditorConstraints, {
179
+ onRemove: ()=>removeItem(index)
180
+ })))), constraints && constraintsType && _react.default.createElement(_TagsEditorConstraints.TagsEditorConstraints, {
182
181
  constraints: constraints,
183
182
  constraintsType: constraintsType
184
183
  }));
@@ -1,18 +1,14 @@
1
1
  import React, { useCallback, useState } from 'react';
2
- import { SortableContainer, SortableElement, SortableHandle } from 'react-sortable-hoc';
3
- import { Pill, TextInput } from '@contentful/f36-components';
4
- import { DragIcon } from '@contentful/f36-icons';
2
+ import { DragHandle, Pill, TextInput } from '@contentful/f36-components';
5
3
  import tokens from '@contentful/f36-tokens';
6
- import arrayMove from 'array-move';
4
+ import { DndContext } from '@dnd-kit/core';
5
+ import { restrictToParentElement } from '@dnd-kit/modifiers';
6
+ import { arrayMove, SortableContext, useSortable } from '@dnd-kit/sortable';
7
+ import { CSS } from '@dnd-kit/utilities';
7
8
  import { css, cx } from 'emotion';
8
9
  import noop from 'lodash/noop';
9
10
  import { TagsEditorConstraints } from './TagsEditorConstraints';
10
11
  const styles = {
11
- dropContainer: css({
12
- whiteSpace: 'nowrap',
13
- display: 'flex',
14
- flexWrap: 'wrap'
15
- }),
16
12
  input: css({
17
13
  marginTop: tokens.spacingS,
18
14
  marginBottom: tokens.spacingM
@@ -27,59 +23,69 @@ const styles = {
27
23
  cursor: 'not-allowed !important',
28
24
  pointerEvents: 'none'
29
25
  }
30
- }),
31
- handle: css({
32
- lineHeight: '1.5rem',
33
- padding: '0.375rem 0.625rem',
34
- paddingRight: 0,
35
- cursor: 'grab',
36
- userSelect: 'none',
37
- svg: {
38
- fill: tokens.gray500,
39
- verticalAlign: 'middle'
40
- }
41
26
  })
42
27
  };
43
- const SortablePillHandle = SortableHandle((props)=>React.createElement("div", {
44
- className: cx(styles.handle, {
45
- [styles.pillDisabled]: props.isDisabled
46
- })
47
- }, React.createElement(DragIcon, {
48
- variant: "muted"
49
- })));
50
- const SortablePill = SortableElement((props)=>React.createElement(Pill, {
28
+ const SortablePill = ({ id , label , index , disabled , onRemove })=>{
29
+ const { listeners , setNodeRef , setActivatorNodeRef , transform , transition } = useSortable({
30
+ id
31
+ });
32
+ const style = {
33
+ transform: transform ? CSS.Transform.toString({
34
+ ...transform,
35
+ scaleX: 1,
36
+ scaleY: 1
37
+ }) : '',
38
+ transition
39
+ };
40
+ return React.createElement(Pill, {
41
+ ref: setNodeRef,
51
42
  testId: "tag-editor-pill",
52
43
  className: cx(styles.pill, {
53
- [styles.pillDisabled]: props.isSortablePillDisabled
44
+ [styles.pillDisabled]: disabled
54
45
  }),
55
- label: props.label,
46
+ style: style,
47
+ label: label,
56
48
  onClose: ()=>{
57
- if (!props.isSortablePillDisabled) {
58
- props.onRemove(props.index);
49
+ if (!disabled) {
50
+ onRemove(index);
59
51
  }
60
52
  },
61
53
  onDrag: noop,
62
- dragHandleComponent: React.createElement(SortablePillHandle, {
63
- isDisabled: props.isSortablePillDisabled
54
+ dragHandleComponent: React.createElement(DragHandle, {
55
+ ref: setActivatorNodeRef,
56
+ variant: "transparent",
57
+ label: "",
58
+ ...listeners
64
59
  })
65
- }));
66
- const SortableList = SortableContainer((props)=>React.createElement("div", {
67
- className: styles.dropContainer
68
- }, props.children));
60
+ });
61
+ };
69
62
  export function TagsEditor(props) {
70
63
  const [pendingValue, setPendingValue] = useState('');
71
- const { isDisabled , items , constraints , constraintsType , hasError } = props;
64
+ const { isDisabled , items , constraints , constraintsType , hasError , onUpdate } = props;
65
+ const itemsMap = React.useMemo(()=>items.map((item, index)=>({
66
+ id: item + index,
67
+ value: item
68
+ })), [
69
+ items
70
+ ]);
72
71
  const removeItem = useCallback((index)=>{
73
- const newItems = props.items.filter((_, filterIndex)=>index !== filterIndex);
74
- props.onUpdate(newItems);
72
+ const newItems = items.filter((_, filterIndex)=>index !== filterIndex);
73
+ onUpdate(newItems);
75
74
  }, [
76
- props
75
+ items,
76
+ onUpdate
77
77
  ]);
78
- const swapItems = useCallback(({ oldIndex , newIndex })=>{
79
- const newItems = arrayMove(props.items, oldIndex, newIndex);
80
- props.onUpdate(newItems);
78
+ const swapItems = useCallback(({ active , over })=>{
79
+ if (active.id !== over.id) {
80
+ const oldIndex = itemsMap.findIndex(({ id })=>id === active.id);
81
+ const newIndex = itemsMap.findIndex(({ id })=>id === over.id);
82
+ const newItems = arrayMove(items, oldIndex, newIndex);
83
+ onUpdate(newItems);
84
+ }
81
85
  }, [
82
- props
86
+ items,
87
+ itemsMap,
88
+ onUpdate
83
89
  ]);
84
90
  return React.createElement("div", {
85
91
  "data-test-id": "tag-editor-container"
@@ -93,8 +99,8 @@ export function TagsEditor(props) {
93
99
  placeholder: "Type the value and hit enter",
94
100
  onKeyDown: (e)=>{
95
101
  if (pendingValue && e.keyCode === 13) {
96
- props.onUpdate([
97
- ...props.items,
102
+ onUpdate([
103
+ ...items,
98
104
  pendingValue
99
105
  ]);
100
106
  setPendingValue('');
@@ -103,28 +109,21 @@ export function TagsEditor(props) {
103
109
  onChange: (e)=>{
104
110
  setPendingValue(e.target.value);
105
111
  }
106
- }), React.createElement(SortableList, {
107
- useDragHandle: true,
108
- axis: "xy",
109
- distance: 10,
110
- onSortEnd: ({ oldIndex , newIndex })=>{
111
- swapItems({
112
- oldIndex,
113
- newIndex
114
- });
115
- }
116
- }, items.map((item, index)=>{
117
- return React.createElement(SortablePill, {
118
- label: item,
112
+ }), React.createElement(DndContext, {
113
+ onDragEnd: swapItems,
114
+ modifiers: [
115
+ restrictToParentElement
116
+ ]
117
+ }, React.createElement(SortableContext, {
118
+ items: itemsMap
119
+ }, itemsMap.map((item, index)=>React.createElement(SortablePill, {
120
+ key: item.id,
121
+ id: item.id,
122
+ label: item.value,
119
123
  index: index,
120
- key: item + index,
121
124
  disabled: isDisabled,
122
- isSortablePillDisabled: isDisabled,
123
- onRemove: ()=>{
124
- removeItem(index);
125
- }
126
- });
127
- })), constraints && constraintsType && React.createElement(TagsEditorConstraints, {
125
+ onRemove: ()=>removeItem(index)
126
+ })))), constraints && constraintsType && React.createElement(TagsEditorConstraints, {
128
127
  constraints: constraints,
129
128
  constraintsType: constraintsType
130
129
  }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentful/field-editor-tags",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
4
4
  "main": "dist/cjs/index.js",
5
5
  "module": "dist/esm/index.js",
6
6
  "types": "dist/types/index.d.ts",
@@ -38,17 +38,18 @@
38
38
  "@contentful/f36-components": "^4.0.27",
39
39
  "@contentful/f36-icons": "^4.1.0",
40
40
  "@contentful/f36-tokens": "^4.0.0",
41
- "@contentful/field-editor-shared": "^1.4.0",
42
- "array-move": "^3.0.0",
41
+ "@contentful/field-editor-shared": "^1.4.1",
42
+ "@dnd-kit/core": "^6.0.8",
43
+ "@dnd-kit/modifiers": "^6.0.1",
44
+ "@dnd-kit/sortable": "^7.0.2",
43
45
  "emotion": "^10.0.0",
44
- "lodash": "^4.17.15",
45
- "react-sortable-hoc": "^2.0.0"
46
+ "lodash": "^4.17.15"
46
47
  },
47
48
  "devDependencies": {
48
- "@contentful/field-editor-test-utils": "^1.4.1"
49
+ "@contentful/field-editor-test-utils": "^1.4.2"
49
50
  },
50
51
  "peerDependencies": {
51
52
  "react": ">=16.8.0"
52
53
  },
53
- "gitHead": "2db95226af0417c42aae8d1be85796fc8774e283"
54
+ "gitHead": "0fc8da4aa8ad8603c3adf4bd77b26ba46de11e33"
54
55
  }