@contentful/field-editor-shared 4.5.2-canary.29 → 4.5.2-canary.30
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.
|
@@ -8,7 +8,7 @@ Object.defineProperty(exports, "FieldConnector", {
|
|
|
8
8
|
return FieldConnector;
|
|
9
9
|
}
|
|
10
10
|
});
|
|
11
|
-
const _react =
|
|
11
|
+
const _react = require("react");
|
|
12
12
|
const _fastdeepequal = /*#__PURE__*/ _interop_require_default(require("fast-deep-equal"));
|
|
13
13
|
const _debounce = /*#__PURE__*/ _interop_require_default(require("lodash/debounce"));
|
|
14
14
|
function _interop_require_default(obj) {
|
|
@@ -16,51 +16,10 @@ function _interop_require_default(obj) {
|
|
|
16
16
|
default: obj
|
|
17
17
|
};
|
|
18
18
|
}
|
|
19
|
-
function _getRequireWildcardCache(nodeInterop) {
|
|
20
|
-
if (typeof WeakMap !== "function") return null;
|
|
21
|
-
var cacheBabelInterop = new WeakMap();
|
|
22
|
-
var cacheNodeInterop = new WeakMap();
|
|
23
|
-
return (_getRequireWildcardCache = function(nodeInterop) {
|
|
24
|
-
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
|
25
|
-
})(nodeInterop);
|
|
26
|
-
}
|
|
27
|
-
function _interop_require_wildcard(obj, nodeInterop) {
|
|
28
|
-
if (!nodeInterop && obj && obj.__esModule) {
|
|
29
|
-
return obj;
|
|
30
|
-
}
|
|
31
|
-
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
|
32
|
-
return {
|
|
33
|
-
default: obj
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
var cache = _getRequireWildcardCache(nodeInterop);
|
|
37
|
-
if (cache && cache.has(obj)) {
|
|
38
|
-
return cache.get(obj);
|
|
39
|
-
}
|
|
40
|
-
var newObj = {
|
|
41
|
-
__proto__: null
|
|
42
|
-
};
|
|
43
|
-
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
44
|
-
for(var key in obj){
|
|
45
|
-
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
46
|
-
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
|
47
|
-
if (desc && (desc.get || desc.set)) {
|
|
48
|
-
Object.defineProperty(newObj, key, desc);
|
|
49
|
-
} else {
|
|
50
|
-
newObj[key] = obj[key];
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
newObj.default = obj;
|
|
55
|
-
if (cache) {
|
|
56
|
-
cache.set(obj, newObj);
|
|
57
|
-
}
|
|
58
|
-
return newObj;
|
|
59
|
-
}
|
|
60
19
|
const defaultIsEmptyValue = (value)=>value === null || value === '';
|
|
61
20
|
const defaultIsEqualValues = (value1, value2)=>(0, _fastdeepequal.default)(value1, value2);
|
|
62
21
|
function FieldConnector(props) {
|
|
63
|
-
const [state, setState] = _react.useState(()=>{
|
|
22
|
+
const [state, setState] = (0, _react.useState)(()=>{
|
|
64
23
|
const initialValue = props.field.getValue();
|
|
65
24
|
return {
|
|
66
25
|
isLocalValueChange: false,
|
|
@@ -71,11 +30,11 @@ function FieldConnector(props) {
|
|
|
71
30
|
errors: []
|
|
72
31
|
};
|
|
73
32
|
});
|
|
74
|
-
const propsRef = _react.useRef(props);
|
|
33
|
+
const propsRef = (0, _react.useRef)(props);
|
|
75
34
|
propsRef.current = props;
|
|
76
|
-
const
|
|
35
|
+
const subscribedFieldRef = (0, _react.useRef)(props.field);
|
|
77
36
|
const getDebounceDuration = ()=>'throttle' in propsRef.current ? propsRef.current.throttle : propsRef.current.debounce ?? 300;
|
|
78
|
-
const triggerSetValueCallbacks = _react.useCallback((value)=>{
|
|
37
|
+
const triggerSetValueCallbacks = (0, _react.useCallback)((value)=>{
|
|
79
38
|
return new Promise((resolve, reject)=>{
|
|
80
39
|
if ((propsRef.current.isEmptyValue ?? defaultIsEmptyValue)(value ?? null)) {
|
|
81
40
|
propsRef.current.field.removeValue().then(resolve).catch(reject);
|
|
@@ -84,10 +43,10 @@ function FieldConnector(props) {
|
|
|
84
43
|
}
|
|
85
44
|
});
|
|
86
45
|
}, []);
|
|
87
|
-
const debouncedTriggerSetValueCallbacks = _react.useMemo(()=>(0, _debounce.default)(triggerSetValueCallbacks, getDebounceDuration()), [
|
|
46
|
+
const debouncedTriggerSetValueCallbacks = (0, _react.useMemo)(()=>(0, _debounce.default)(triggerSetValueCallbacks, getDebounceDuration()), [
|
|
88
47
|
triggerSetValueCallbacks
|
|
89
48
|
]);
|
|
90
|
-
const setValue = _react.useCallback(async (value)=>{
|
|
49
|
+
const setValue = (0, _react.useCallback)(async (value)=>{
|
|
91
50
|
if ((propsRef.current.isEmptyValue ?? defaultIsEmptyValue)(value ?? null)) {
|
|
92
51
|
setState((currentState)=>({
|
|
93
52
|
...currentState,
|
|
@@ -108,7 +67,8 @@ function FieldConnector(props) {
|
|
|
108
67
|
debouncedTriggerSetValueCallbacks,
|
|
109
68
|
triggerSetValueCallbacks
|
|
110
69
|
]);
|
|
111
|
-
_react.
|
|
70
|
+
(0, _react.useLayoutEffect)(()=>{
|
|
71
|
+
const field = subscribedFieldRef.current;
|
|
112
72
|
const unsubscribeErrors = field.onSchemaErrorsChanged((errors)=>{
|
|
113
73
|
setState((currentState)=>({
|
|
114
74
|
...currentState,
|
|
@@ -145,15 +105,11 @@ function FieldConnector(props) {
|
|
|
145
105
|
if (typeof unsubscribeValue === 'function') {
|
|
146
106
|
unsubscribeValue();
|
|
147
107
|
}
|
|
148
|
-
debouncedTriggerSetValueCallbacks.cancel();
|
|
149
108
|
};
|
|
150
|
-
}, [
|
|
151
|
-
|
|
152
|
-
field
|
|
153
|
-
]);
|
|
154
|
-
return props.children?.({
|
|
109
|
+
}, []);
|
|
110
|
+
return props.children ? props.children({
|
|
155
111
|
...state,
|
|
156
112
|
setValue,
|
|
157
113
|
disabled: props.isDisabled || state.disabled
|
|
158
|
-
})
|
|
114
|
+
}) : null;
|
|
159
115
|
}
|
|
@@ -120,3 +120,52 @@ function getChild(children) {
|
|
|
120
120
|
}));
|
|
121
121
|
(0, _vitest.expect)(()=>unmount()).not.toThrow();
|
|
122
122
|
});
|
|
123
|
+
(0, _vitest.it)('keeps pending debounced value updates after unmounting', async ()=>{
|
|
124
|
+
_vitest.vi.useFakeTimers();
|
|
125
|
+
const setValue = _vitest.vi.fn().mockResolvedValue(undefined);
|
|
126
|
+
const [field] = (0, _fieldeditortestutils.createFakeFieldAPI)((field)=>({
|
|
127
|
+
...field,
|
|
128
|
+
setValue
|
|
129
|
+
}));
|
|
130
|
+
const children = _vitest.vi.fn().mockReturnValue(null);
|
|
131
|
+
try {
|
|
132
|
+
const { unmount } = (0, _react1.render)(/*#__PURE__*/ _react.createElement(_FieldConnector.FieldConnector, {
|
|
133
|
+
field: field,
|
|
134
|
+
isInitiallyDisabled: false,
|
|
135
|
+
debounce: 300
|
|
136
|
+
}, children));
|
|
137
|
+
await (0, _react1.act)(async ()=>{
|
|
138
|
+
await getChild(children).setValue('new value');
|
|
139
|
+
});
|
|
140
|
+
unmount();
|
|
141
|
+
await _vitest.vi.advanceTimersByTimeAsync(300);
|
|
142
|
+
(0, _vitest.expect)(setValue).toHaveBeenCalledWith('new value');
|
|
143
|
+
} finally{
|
|
144
|
+
_vitest.vi.useRealTimers();
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
(0, _vitest.it)('uses the deprecated throttle value as the debounce duration', async ()=>{
|
|
148
|
+
_vitest.vi.useFakeTimers();
|
|
149
|
+
const setValue = _vitest.vi.fn().mockResolvedValue(undefined);
|
|
150
|
+
const [field] = (0, _fieldeditortestutils.createFakeFieldAPI)((field)=>({
|
|
151
|
+
...field,
|
|
152
|
+
setValue
|
|
153
|
+
}));
|
|
154
|
+
const children = _vitest.vi.fn().mockReturnValue(null);
|
|
155
|
+
try {
|
|
156
|
+
(0, _react1.render)(/*#__PURE__*/ _react.createElement(_FieldConnector.FieldConnector, {
|
|
157
|
+
field: field,
|
|
158
|
+
isInitiallyDisabled: false,
|
|
159
|
+
throttle: 100
|
|
160
|
+
}, children));
|
|
161
|
+
await (0, _react1.act)(async ()=>{
|
|
162
|
+
await getChild(children).setValue('new value');
|
|
163
|
+
});
|
|
164
|
+
await _vitest.vi.advanceTimersByTimeAsync(99);
|
|
165
|
+
(0, _vitest.expect)(setValue).not.toHaveBeenCalled();
|
|
166
|
+
await _vitest.vi.advanceTimersByTimeAsync(1);
|
|
167
|
+
(0, _vitest.expect)(setValue).toHaveBeenCalledWith('new value');
|
|
168
|
+
} finally{
|
|
169
|
+
_vitest.vi.useRealTimers();
|
|
170
|
+
}
|
|
171
|
+
});
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { useCallback, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
|
2
2
|
import deepEqual from 'fast-deep-equal';
|
|
3
3
|
import debounce from 'lodash/debounce';
|
|
4
4
|
const defaultIsEmptyValue = (value)=>value === null || value === '';
|
|
5
5
|
const defaultIsEqualValues = (value1, value2)=>deepEqual(value1, value2);
|
|
6
6
|
export function FieldConnector(props) {
|
|
7
|
-
const [state, setState] =
|
|
7
|
+
const [state, setState] = useState(()=>{
|
|
8
8
|
const initialValue = props.field.getValue();
|
|
9
9
|
return {
|
|
10
10
|
isLocalValueChange: false,
|
|
@@ -15,11 +15,11 @@ export function FieldConnector(props) {
|
|
|
15
15
|
errors: []
|
|
16
16
|
};
|
|
17
17
|
});
|
|
18
|
-
const propsRef =
|
|
18
|
+
const propsRef = useRef(props);
|
|
19
19
|
propsRef.current = props;
|
|
20
|
-
const
|
|
20
|
+
const subscribedFieldRef = useRef(props.field);
|
|
21
21
|
const getDebounceDuration = ()=>'throttle' in propsRef.current ? propsRef.current.throttle : propsRef.current.debounce ?? 300;
|
|
22
|
-
const triggerSetValueCallbacks =
|
|
22
|
+
const triggerSetValueCallbacks = useCallback((value)=>{
|
|
23
23
|
return new Promise((resolve, reject)=>{
|
|
24
24
|
if ((propsRef.current.isEmptyValue ?? defaultIsEmptyValue)(value ?? null)) {
|
|
25
25
|
propsRef.current.field.removeValue().then(resolve).catch(reject);
|
|
@@ -28,10 +28,10 @@ export function FieldConnector(props) {
|
|
|
28
28
|
}
|
|
29
29
|
});
|
|
30
30
|
}, []);
|
|
31
|
-
const debouncedTriggerSetValueCallbacks =
|
|
31
|
+
const debouncedTriggerSetValueCallbacks = useMemo(()=>debounce(triggerSetValueCallbacks, getDebounceDuration()), [
|
|
32
32
|
triggerSetValueCallbacks
|
|
33
33
|
]);
|
|
34
|
-
const setValue =
|
|
34
|
+
const setValue = useCallback(async (value)=>{
|
|
35
35
|
if ((propsRef.current.isEmptyValue ?? defaultIsEmptyValue)(value ?? null)) {
|
|
36
36
|
setState((currentState)=>({
|
|
37
37
|
...currentState,
|
|
@@ -52,7 +52,8 @@ export function FieldConnector(props) {
|
|
|
52
52
|
debouncedTriggerSetValueCallbacks,
|
|
53
53
|
triggerSetValueCallbacks
|
|
54
54
|
]);
|
|
55
|
-
|
|
55
|
+
useLayoutEffect(()=>{
|
|
56
|
+
const field = subscribedFieldRef.current;
|
|
56
57
|
const unsubscribeErrors = field.onSchemaErrorsChanged((errors)=>{
|
|
57
58
|
setState((currentState)=>({
|
|
58
59
|
...currentState,
|
|
@@ -89,15 +90,11 @@ export function FieldConnector(props) {
|
|
|
89
90
|
if (typeof unsubscribeValue === 'function') {
|
|
90
91
|
unsubscribeValue();
|
|
91
92
|
}
|
|
92
|
-
debouncedTriggerSetValueCallbacks.cancel();
|
|
93
93
|
};
|
|
94
|
-
}, [
|
|
95
|
-
|
|
96
|
-
field
|
|
97
|
-
]);
|
|
98
|
-
return props.children?.({
|
|
94
|
+
}, []);
|
|
95
|
+
return props.children ? props.children({
|
|
99
96
|
...state,
|
|
100
97
|
setValue,
|
|
101
98
|
disabled: props.isDisabled || state.disabled
|
|
102
|
-
})
|
|
99
|
+
}) : null;
|
|
103
100
|
}
|
|
@@ -70,3 +70,52 @@ it('handles subscriptions that do not return unsubscribe callbacks', ()=>{
|
|
|
70
70
|
}));
|
|
71
71
|
expect(()=>unmount()).not.toThrow();
|
|
72
72
|
});
|
|
73
|
+
it('keeps pending debounced value updates after unmounting', async ()=>{
|
|
74
|
+
vi.useFakeTimers();
|
|
75
|
+
const setValue = vi.fn().mockResolvedValue(undefined);
|
|
76
|
+
const [field] = createFakeFieldAPI((field)=>({
|
|
77
|
+
...field,
|
|
78
|
+
setValue
|
|
79
|
+
}));
|
|
80
|
+
const children = vi.fn().mockReturnValue(null);
|
|
81
|
+
try {
|
|
82
|
+
const { unmount } = render(/*#__PURE__*/ React.createElement(FieldConnector, {
|
|
83
|
+
field: field,
|
|
84
|
+
isInitiallyDisabled: false,
|
|
85
|
+
debounce: 300
|
|
86
|
+
}, children));
|
|
87
|
+
await act(async ()=>{
|
|
88
|
+
await getChild(children).setValue('new value');
|
|
89
|
+
});
|
|
90
|
+
unmount();
|
|
91
|
+
await vi.advanceTimersByTimeAsync(300);
|
|
92
|
+
expect(setValue).toHaveBeenCalledWith('new value');
|
|
93
|
+
} finally{
|
|
94
|
+
vi.useRealTimers();
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
it('uses the deprecated throttle value as the debounce duration', async ()=>{
|
|
98
|
+
vi.useFakeTimers();
|
|
99
|
+
const setValue = vi.fn().mockResolvedValue(undefined);
|
|
100
|
+
const [field] = createFakeFieldAPI((field)=>({
|
|
101
|
+
...field,
|
|
102
|
+
setValue
|
|
103
|
+
}));
|
|
104
|
+
const children = vi.fn().mockReturnValue(null);
|
|
105
|
+
try {
|
|
106
|
+
render(/*#__PURE__*/ React.createElement(FieldConnector, {
|
|
107
|
+
field: field,
|
|
108
|
+
isInitiallyDisabled: false,
|
|
109
|
+
throttle: 100
|
|
110
|
+
}, children));
|
|
111
|
+
await act(async ()=>{
|
|
112
|
+
await getChild(children).setValue('new value');
|
|
113
|
+
});
|
|
114
|
+
await vi.advanceTimersByTimeAsync(99);
|
|
115
|
+
expect(setValue).not.toHaveBeenCalled();
|
|
116
|
+
await vi.advanceTimersByTimeAsync(1);
|
|
117
|
+
expect(setValue).toHaveBeenCalledWith('new value');
|
|
118
|
+
} finally{
|
|
119
|
+
vi.useRealTimers();
|
|
120
|
+
}
|
|
121
|
+
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
2
|
import { FieldAPI, ValidationError } from '@contentful/app-sdk';
|
|
3
3
|
type Nullable = null | undefined;
|
|
4
4
|
export interface FieldConnectorChildProps<ValueType> {
|
|
@@ -14,7 +14,7 @@ type FieldConnectorProps<ValueType> = {
|
|
|
14
14
|
field: FieldAPI;
|
|
15
15
|
isInitiallyDisabled: boolean;
|
|
16
16
|
isDisabled?: boolean;
|
|
17
|
-
children?: (state: FieldConnectorChildProps<ValueType>) =>
|
|
17
|
+
children?: (state: FieldConnectorChildProps<ValueType>) => ReactNode;
|
|
18
18
|
isEmptyValue?: (value: ValueType | null) => boolean;
|
|
19
19
|
isEqualValues?: (value1: ValueType | Nullable, value2: ValueType | Nullable) => boolean;
|
|
20
20
|
} & ({
|
|
@@ -23,5 +23,5 @@ type FieldConnectorProps<ValueType> = {
|
|
|
23
23
|
/** @deprecated: Please use `debounce` instead */
|
|
24
24
|
throttle: number;
|
|
25
25
|
});
|
|
26
|
-
export declare function FieldConnector<ValueType>(props: FieldConnectorProps<ValueType>):
|
|
26
|
+
export declare function FieldConnector<ValueType>(props: FieldConnectorProps<ValueType>): ReactNode;
|
|
27
27
|
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contentful/field-editor-shared",
|
|
3
|
-
"version": "4.5.2-canary.
|
|
3
|
+
"version": "4.5.2-canary.30+69033935",
|
|
4
4
|
"main": "dist/cjs/index.js",
|
|
5
5
|
"module": "dist/esm/index.js",
|
|
6
6
|
"types": "dist/types/index.d.ts",
|
|
@@ -73,5 +73,5 @@
|
|
|
73
73
|
"publishConfig": {
|
|
74
74
|
"registry": "https://npm.pkg.github.com/"
|
|
75
75
|
},
|
|
76
|
-
"gitHead": "
|
|
76
|
+
"gitHead": "69033935571ab64c941e30158340242c5a449c64"
|
|
77
77
|
}
|