@contentful/field-editor-list 1.5.5 → 1.5.6

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.
@@ -62,6 +62,19 @@ function _interop_require_wildcard(obj, nodeInterop) {
62
62
  function isEmptyListValue(value) {
63
63
  return value === null || value.length === 0;
64
64
  }
65
+ const useExternalChanges = (cb, externalValue)=>{
66
+ const lastExternalValue = _react.useRef(externalValue);
67
+ _react.useEffect(()=>{
68
+ if ((0, _isEqual.default)(lastExternalValue.current, externalValue)) {
69
+ return;
70
+ }
71
+ lastExternalValue.current = externalValue;
72
+ cb((externalValue ?? []).join(', '));
73
+ }, [
74
+ cb,
75
+ externalValue
76
+ ]);
77
+ };
65
78
  function ListEditor(props) {
66
79
  const { field, locales, id } = props;
67
80
  const direction = locales.direction[field.locale] || 'ltr';
@@ -86,6 +99,7 @@ function ListEditorInternal({ setValue, value, errors, disabled, direction, isRe
86
99
  const valueAsString = valueAsArray.join(', ');
87
100
  setValueState(changed ? valueAsString : e.target.value);
88
101
  };
102
+ useExternalChanges(setValueState, value);
89
103
  return _react.createElement(_f36components.TextInput, {
90
104
  id: id,
91
105
  testId: "list-editor-input",
@@ -148,4 +148,28 @@ describe('ListEditor', ()=>{
148
148
  ]);
149
149
  expectInputValue(renderResult, 'test1,');
150
150
  });
151
+ it('listens to external changes', async ()=>{
152
+ const [field] = (0, _fieldeditortestutils.createFakeFieldAPI)((field)=>{
153
+ jest.spyOn(field, 'setValue');
154
+ return {
155
+ ...field,
156
+ validations: []
157
+ };
158
+ }, [
159
+ 'test1'
160
+ ]);
161
+ const renderResult = (0, _react1.render)(_react.createElement(_ListEditor.ListEditor, {
162
+ field: field,
163
+ locales: (0, _fieldeditortestutils.createFakeLocalesAPI)(),
164
+ isInitiallyDisabled: false
165
+ }));
166
+ expectInputValue(renderResult, 'test1');
167
+ field.setValue([
168
+ 'test1',
169
+ 'test2'
170
+ ]);
171
+ await (0, _react1.waitFor)(()=>{
172
+ expectInputValue(renderResult, 'test1, test2');
173
+ });
174
+ });
151
175
  });
@@ -6,6 +6,19 @@ import * as styles from './styles';
6
6
  function isEmptyListValue(value) {
7
7
  return value === null || value.length === 0;
8
8
  }
9
+ const useExternalChanges = (cb, externalValue)=>{
10
+ const lastExternalValue = React.useRef(externalValue);
11
+ React.useEffect(()=>{
12
+ if (isEqual(lastExternalValue.current, externalValue)) {
13
+ return;
14
+ }
15
+ lastExternalValue.current = externalValue;
16
+ cb((externalValue ?? []).join(', '));
17
+ }, [
18
+ cb,
19
+ externalValue
20
+ ]);
21
+ };
9
22
  export function ListEditor(props) {
10
23
  const { field, locales, id } = props;
11
24
  const direction = locales.direction[field.locale] || 'ltr';
@@ -30,6 +43,7 @@ function ListEditorInternal({ setValue, value, errors, disabled, direction, isRe
30
43
  const valueAsString = valueAsArray.join(', ');
31
44
  setValueState(changed ? valueAsString : e.target.value);
32
45
  };
46
+ useExternalChanges(setValueState, value);
33
47
  return React.createElement(TextInput, {
34
48
  id: id,
35
49
  testId: "list-editor-input",
@@ -1,7 +1,7 @@
1
1
  import * as React from 'react';
2
2
  import { createFakeFieldAPI, createFakeLocalesAPI } from '@contentful/field-editor-test-utils';
3
3
  import '@testing-library/jest-dom/extend-expect';
4
- import { cleanup, configure, fireEvent, render } from '@testing-library/react';
4
+ import { cleanup, configure, fireEvent, render, waitFor } from '@testing-library/react';
5
5
  import { ListEditor } from './ListEditor';
6
6
  configure({
7
7
  testIdAttribute: 'data-test-id'
@@ -103,4 +103,28 @@ describe('ListEditor', ()=>{
103
103
  ]);
104
104
  expectInputValue(renderResult, 'test1,');
105
105
  });
106
+ it('listens to external changes', async ()=>{
107
+ const [field] = createFakeFieldAPI((field)=>{
108
+ jest.spyOn(field, 'setValue');
109
+ return {
110
+ ...field,
111
+ validations: []
112
+ };
113
+ }, [
114
+ 'test1'
115
+ ]);
116
+ const renderResult = render(React.createElement(ListEditor, {
117
+ field: field,
118
+ locales: createFakeLocalesAPI(),
119
+ isInitiallyDisabled: false
120
+ }));
121
+ expectInputValue(renderResult, 'test1');
122
+ field.setValue([
123
+ 'test1',
124
+ 'test2'
125
+ ]);
126
+ await waitFor(()=>{
127
+ expectInputValue(renderResult, 'test1, test2');
128
+ });
129
+ });
106
130
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentful/field-editor-list",
3
- "version": "1.5.5",
3
+ "version": "1.5.6",
4
4
  "main": "dist/cjs/index.js",
5
5
  "module": "dist/esm/index.js",
6
6
  "types": "dist/types/index.d.ts",
@@ -50,5 +50,5 @@
50
50
  "publishConfig": {
51
51
  "registry": "https://npm.pkg.github.com/"
52
52
  },
53
- "gitHead": "62868063532796d312b7002cac13f27ab3557110"
53
+ "gitHead": "b8392c70e8a794498df27ffe3d4b0c3131ddf172"
54
54
  }