@contentful/field-editor-shared 4.5.1 → 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,88 +8,86 @@ 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
|
-
function _define_property(obj, key, value) {
|
|
15
|
-
if (key in obj) {
|
|
16
|
-
Object.defineProperty(obj, key, {
|
|
17
|
-
value: value,
|
|
18
|
-
enumerable: true,
|
|
19
|
-
configurable: true,
|
|
20
|
-
writable: true
|
|
21
|
-
});
|
|
22
|
-
} else {
|
|
23
|
-
obj[key] = value;
|
|
24
|
-
}
|
|
25
|
-
return obj;
|
|
26
|
-
}
|
|
27
14
|
function _interop_require_default(obj) {
|
|
28
15
|
return obj && obj.__esModule ? obj : {
|
|
29
16
|
default: obj
|
|
30
17
|
};
|
|
31
18
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
|
38
|
-
})(nodeInterop);
|
|
39
|
-
}
|
|
40
|
-
function _interop_require_wildcard(obj, nodeInterop) {
|
|
41
|
-
if (!nodeInterop && obj && obj.__esModule) {
|
|
42
|
-
return obj;
|
|
43
|
-
}
|
|
44
|
-
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
|
19
|
+
const defaultIsEmptyValue = (value)=>value === null || value === '';
|
|
20
|
+
const defaultIsEqualValues = (value1, value2)=>(0, _fastdeepequal.default)(value1, value2);
|
|
21
|
+
function FieldConnector(props) {
|
|
22
|
+
const [state, setState] = (0, _react.useState)(()=>{
|
|
23
|
+
const initialValue = props.field.getValue();
|
|
45
24
|
return {
|
|
46
|
-
|
|
25
|
+
isLocalValueChange: false,
|
|
26
|
+
externalReset: 0,
|
|
27
|
+
value: initialValue,
|
|
28
|
+
lastRemoteValue: initialValue,
|
|
29
|
+
disabled: props.isInitiallyDisabled || props.field.getIsDisabled(),
|
|
30
|
+
errors: []
|
|
47
31
|
};
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
for(var key in obj){
|
|
58
|
-
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
59
|
-
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
|
60
|
-
if (desc && (desc.get || desc.set)) {
|
|
61
|
-
Object.defineProperty(newObj, key, desc);
|
|
32
|
+
});
|
|
33
|
+
const propsRef = (0, _react.useRef)(props);
|
|
34
|
+
propsRef.current = props;
|
|
35
|
+
const subscribedFieldRef = (0, _react.useRef)(props.field);
|
|
36
|
+
const getDebounceDuration = ()=>'throttle' in propsRef.current ? propsRef.current.throttle : propsRef.current.debounce ?? 300;
|
|
37
|
+
const triggerSetValueCallbacks = (0, _react.useCallback)((value)=>{
|
|
38
|
+
return new Promise((resolve, reject)=>{
|
|
39
|
+
if ((propsRef.current.isEmptyValue ?? defaultIsEmptyValue)(value ?? null)) {
|
|
40
|
+
propsRef.current.field.removeValue().then(resolve).catch(reject);
|
|
62
41
|
} else {
|
|
63
|
-
|
|
42
|
+
propsRef.current.field.setValue(value).then(resolve).catch(reject);
|
|
64
43
|
}
|
|
44
|
+
});
|
|
45
|
+
}, []);
|
|
46
|
+
const debouncedTriggerSetValueCallbacks = (0, _react.useMemo)(()=>(0, _debounce.default)(triggerSetValueCallbacks, getDebounceDuration()), [
|
|
47
|
+
triggerSetValueCallbacks
|
|
48
|
+
]);
|
|
49
|
+
const setValue = (0, _react.useCallback)(async (value)=>{
|
|
50
|
+
if ((propsRef.current.isEmptyValue ?? defaultIsEmptyValue)(value ?? null)) {
|
|
51
|
+
setState((currentState)=>({
|
|
52
|
+
...currentState,
|
|
53
|
+
value: undefined
|
|
54
|
+
}));
|
|
55
|
+
} else {
|
|
56
|
+
setState((currentState)=>({
|
|
57
|
+
...currentState,
|
|
58
|
+
value
|
|
59
|
+
}));
|
|
65
60
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
61
|
+
if (getDebounceDuration() === 0) {
|
|
62
|
+
await triggerSetValueCallbacks(value);
|
|
63
|
+
} else {
|
|
64
|
+
await debouncedTriggerSetValueCallbacks(value);
|
|
65
|
+
}
|
|
66
|
+
}, [
|
|
67
|
+
debouncedTriggerSetValueCallbacks,
|
|
68
|
+
triggerSetValueCallbacks
|
|
69
|
+
]);
|
|
70
|
+
(0, _react.useLayoutEffect)(()=>{
|
|
71
|
+
const field = subscribedFieldRef.current;
|
|
72
|
+
const unsubscribeErrors = field.onSchemaErrorsChanged((errors)=>{
|
|
73
|
+
setState((currentState)=>({
|
|
74
|
+
...currentState,
|
|
75
|
+
errors: errors || []
|
|
76
|
+
}));
|
|
81
77
|
});
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
78
|
+
const unsubscribeDisabled = field.onIsDisabledChanged((disabled)=>{
|
|
79
|
+
setState((currentState)=>({
|
|
80
|
+
...currentState,
|
|
81
|
+
disabled
|
|
82
|
+
}));
|
|
86
83
|
});
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
const isLocalValueChange =
|
|
84
|
+
const unsubscribeValue = field.onValueChanged((value)=>{
|
|
85
|
+
setState((currentState)=>{
|
|
86
|
+
const isLocalValueChange = (propsRef.current.isEqualValues ?? defaultIsEqualValues)(value, currentState.value);
|
|
90
87
|
const lastRemoteValue = isLocalValueChange ? currentState.lastRemoteValue : value;
|
|
91
88
|
const externalReset = currentState.externalReset + (isLocalValueChange ? 0 : 1);
|
|
92
89
|
return {
|
|
90
|
+
...currentState,
|
|
93
91
|
value,
|
|
94
92
|
lastRemoteValue,
|
|
95
93
|
isLocalValueChange,
|
|
@@ -97,70 +95,21 @@ class FieldConnector extends (_React_Component = _react.Component) {
|
|
|
97
95
|
};
|
|
98
96
|
});
|
|
99
97
|
});
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
this.unsubscribeErrors();
|
|
104
|
-
}
|
|
105
|
-
if (typeof this.unsubscribeDisabled === 'function') {
|
|
106
|
-
this.unsubscribeDisabled();
|
|
107
|
-
}
|
|
108
|
-
if (typeof this.unsubscribeValue === 'function') {
|
|
109
|
-
this.unsubscribeValue();
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
render() {
|
|
113
|
-
return this.props.children({
|
|
114
|
-
...this.state,
|
|
115
|
-
setValue: this.setValue,
|
|
116
|
-
disabled: this.props.isDisabled || this.state.disabled
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
constructor(props){
|
|
120
|
-
super(props), _define_property(this, "unsubscribeErrors", null), _define_property(this, "unsubscribeDisabled", null), _define_property(this, "unsubscribeValue", null), _define_property(this, "getDebounceDuration", ()=>'debounce' in this.props ? this.props.debounce : this.props.throttle), _define_property(this, "setValue", async (value)=>{
|
|
121
|
-
if (this.props.isEmptyValue(value ?? null)) {
|
|
122
|
-
this.setState({
|
|
123
|
-
value: undefined
|
|
124
|
-
});
|
|
125
|
-
} else {
|
|
126
|
-
this.setState({
|
|
127
|
-
value
|
|
128
|
-
});
|
|
98
|
+
return ()=>{
|
|
99
|
+
if (typeof unsubscribeErrors === 'function') {
|
|
100
|
+
unsubscribeErrors();
|
|
129
101
|
}
|
|
130
|
-
if (
|
|
131
|
-
|
|
132
|
-
}
|
|
133
|
-
|
|
102
|
+
if (typeof unsubscribeDisabled === 'function') {
|
|
103
|
+
unsubscribeDisabled();
|
|
104
|
+
}
|
|
105
|
+
if (typeof unsubscribeValue === 'function') {
|
|
106
|
+
unsubscribeValue();
|
|
134
107
|
}
|
|
135
|
-
}), _define_property(this, "triggerSetValueCallbacks", (value)=>{
|
|
136
|
-
return new Promise((resolve, reject)=>{
|
|
137
|
-
if (this.props.isEmptyValue(value ?? null)) {
|
|
138
|
-
this.props.field.removeValue().then(resolve).catch(reject);
|
|
139
|
-
} else {
|
|
140
|
-
this.props.field.setValue(value).then(resolve).catch(reject);
|
|
141
|
-
}
|
|
142
|
-
});
|
|
143
|
-
}), _define_property(this, "debouncedTriggerSetValueCallbacks", (0, _debounce.default)(this.triggerSetValueCallbacks, this.getDebounceDuration()));
|
|
144
|
-
const initialValue = props.field.getValue();
|
|
145
|
-
this.state = {
|
|
146
|
-
isLocalValueChange: false,
|
|
147
|
-
externalReset: 0,
|
|
148
|
-
value: initialValue,
|
|
149
|
-
lastRemoteValue: initialValue,
|
|
150
|
-
disabled: props.isInitiallyDisabled || props.field.getIsDisabled(),
|
|
151
|
-
errors: []
|
|
152
108
|
};
|
|
153
|
-
}
|
|
109
|
+
}, []);
|
|
110
|
+
return props.children ? props.children({
|
|
111
|
+
...state,
|
|
112
|
+
setValue,
|
|
113
|
+
disabled: props.isDisabled || state.disabled
|
|
114
|
+
}) : null;
|
|
154
115
|
}
|
|
155
|
-
_define_property(FieldConnector, "defaultProps", {
|
|
156
|
-
children: ()=>{
|
|
157
|
-
return null;
|
|
158
|
-
},
|
|
159
|
-
isEmptyValue: (value)=>{
|
|
160
|
-
return value === null || value === '';
|
|
161
|
-
},
|
|
162
|
-
isEqualValues: (value1, value2)=>{
|
|
163
|
-
return (0, _fastdeepequal.default)(value1, value2);
|
|
164
|
-
},
|
|
165
|
-
debounce: 300
|
|
166
|
-
});
|
|
@@ -106,3 +106,66 @@ function getChild(children) {
|
|
|
106
106
|
disabled: true
|
|
107
107
|
}));
|
|
108
108
|
});
|
|
109
|
+
(0, _vitest.it)('handles subscriptions that do not return unsubscribe callbacks', ()=>{
|
|
110
|
+
const [field] = (0, _fieldeditortestutils.createFakeFieldAPI)((field)=>({
|
|
111
|
+
...field,
|
|
112
|
+
onSchemaErrorsChanged: _vitest.vi.fn(),
|
|
113
|
+
onIsDisabledChanged: _vitest.vi.fn(),
|
|
114
|
+
onValueChanged: _vitest.vi.fn()
|
|
115
|
+
}));
|
|
116
|
+
const { unmount } = (0, _react1.render)(/*#__PURE__*/ _react.createElement(_FieldConnector.FieldConnector, {
|
|
117
|
+
field: field,
|
|
118
|
+
isInitiallyDisabled: false,
|
|
119
|
+
debounce: 0
|
|
120
|
+
}));
|
|
121
|
+
(0, _vitest.expect)(()=>unmount()).not.toThrow();
|
|
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,39 +1,78 @@
|
|
|
1
|
-
|
|
2
|
-
if (key in obj) {
|
|
3
|
-
Object.defineProperty(obj, key, {
|
|
4
|
-
value: value,
|
|
5
|
-
enumerable: true,
|
|
6
|
-
configurable: true,
|
|
7
|
-
writable: true
|
|
8
|
-
});
|
|
9
|
-
} else {
|
|
10
|
-
obj[key] = value;
|
|
11
|
-
}
|
|
12
|
-
return obj;
|
|
13
|
-
}
|
|
14
|
-
import * as React from 'react';
|
|
1
|
+
import { useCallback, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
|
15
2
|
import deepEqual from 'fast-deep-equal';
|
|
16
3
|
import debounce from 'lodash/debounce';
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
4
|
+
const defaultIsEmptyValue = (value)=>value === null || value === '';
|
|
5
|
+
const defaultIsEqualValues = (value1, value2)=>deepEqual(value1, value2);
|
|
6
|
+
export function FieldConnector(props) {
|
|
7
|
+
const [state, setState] = useState(()=>{
|
|
8
|
+
const initialValue = props.field.getValue();
|
|
9
|
+
return {
|
|
10
|
+
isLocalValueChange: false,
|
|
11
|
+
externalReset: 0,
|
|
12
|
+
value: initialValue,
|
|
13
|
+
lastRemoteValue: initialValue,
|
|
14
|
+
disabled: props.isInitiallyDisabled || props.field.getIsDisabled(),
|
|
15
|
+
errors: []
|
|
16
|
+
};
|
|
17
|
+
});
|
|
18
|
+
const propsRef = useRef(props);
|
|
19
|
+
propsRef.current = props;
|
|
20
|
+
const subscribedFieldRef = useRef(props.field);
|
|
21
|
+
const getDebounceDuration = ()=>'throttle' in propsRef.current ? propsRef.current.throttle : propsRef.current.debounce ?? 300;
|
|
22
|
+
const triggerSetValueCallbacks = useCallback((value)=>{
|
|
23
|
+
return new Promise((resolve, reject)=>{
|
|
24
|
+
if ((propsRef.current.isEmptyValue ?? defaultIsEmptyValue)(value ?? null)) {
|
|
25
|
+
propsRef.current.field.removeValue().then(resolve).catch(reject);
|
|
26
|
+
} else {
|
|
27
|
+
propsRef.current.field.setValue(value).then(resolve).catch(reject);
|
|
28
|
+
}
|
|
25
29
|
});
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
+
}, []);
|
|
31
|
+
const debouncedTriggerSetValueCallbacks = useMemo(()=>debounce(triggerSetValueCallbacks, getDebounceDuration()), [
|
|
32
|
+
triggerSetValueCallbacks
|
|
33
|
+
]);
|
|
34
|
+
const setValue = useCallback(async (value)=>{
|
|
35
|
+
if ((propsRef.current.isEmptyValue ?? defaultIsEmptyValue)(value ?? null)) {
|
|
36
|
+
setState((currentState)=>({
|
|
37
|
+
...currentState,
|
|
38
|
+
value: undefined
|
|
39
|
+
}));
|
|
40
|
+
} else {
|
|
41
|
+
setState((currentState)=>({
|
|
42
|
+
...currentState,
|
|
43
|
+
value
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
if (getDebounceDuration() === 0) {
|
|
47
|
+
await triggerSetValueCallbacks(value);
|
|
48
|
+
} else {
|
|
49
|
+
await debouncedTriggerSetValueCallbacks(value);
|
|
50
|
+
}
|
|
51
|
+
}, [
|
|
52
|
+
debouncedTriggerSetValueCallbacks,
|
|
53
|
+
triggerSetValueCallbacks
|
|
54
|
+
]);
|
|
55
|
+
useLayoutEffect(()=>{
|
|
56
|
+
const field = subscribedFieldRef.current;
|
|
57
|
+
const unsubscribeErrors = field.onSchemaErrorsChanged((errors)=>{
|
|
58
|
+
setState((currentState)=>({
|
|
59
|
+
...currentState,
|
|
60
|
+
errors: errors || []
|
|
61
|
+
}));
|
|
30
62
|
});
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
63
|
+
const unsubscribeDisabled = field.onIsDisabledChanged((disabled)=>{
|
|
64
|
+
setState((currentState)=>({
|
|
65
|
+
...currentState,
|
|
66
|
+
disabled
|
|
67
|
+
}));
|
|
68
|
+
});
|
|
69
|
+
const unsubscribeValue = field.onValueChanged((value)=>{
|
|
70
|
+
setState((currentState)=>{
|
|
71
|
+
const isLocalValueChange = (propsRef.current.isEqualValues ?? defaultIsEqualValues)(value, currentState.value);
|
|
34
72
|
const lastRemoteValue = isLocalValueChange ? currentState.lastRemoteValue : value;
|
|
35
73
|
const externalReset = currentState.externalReset + (isLocalValueChange ? 0 : 1);
|
|
36
74
|
return {
|
|
75
|
+
...currentState,
|
|
37
76
|
value,
|
|
38
77
|
lastRemoteValue,
|
|
39
78
|
isLocalValueChange,
|
|
@@ -41,70 +80,21 @@ export class FieldConnector extends (_React_Component = React.Component) {
|
|
|
41
80
|
};
|
|
42
81
|
});
|
|
43
82
|
});
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
this.unsubscribeErrors();
|
|
48
|
-
}
|
|
49
|
-
if (typeof this.unsubscribeDisabled === 'function') {
|
|
50
|
-
this.unsubscribeDisabled();
|
|
51
|
-
}
|
|
52
|
-
if (typeof this.unsubscribeValue === 'function') {
|
|
53
|
-
this.unsubscribeValue();
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
render() {
|
|
57
|
-
return this.props.children({
|
|
58
|
-
...this.state,
|
|
59
|
-
setValue: this.setValue,
|
|
60
|
-
disabled: this.props.isDisabled || this.state.disabled
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
constructor(props){
|
|
64
|
-
super(props), _define_property(this, "unsubscribeErrors", null), _define_property(this, "unsubscribeDisabled", null), _define_property(this, "unsubscribeValue", null), _define_property(this, "getDebounceDuration", ()=>'debounce' in this.props ? this.props.debounce : this.props.throttle), _define_property(this, "setValue", async (value)=>{
|
|
65
|
-
if (this.props.isEmptyValue(value ?? null)) {
|
|
66
|
-
this.setState({
|
|
67
|
-
value: undefined
|
|
68
|
-
});
|
|
69
|
-
} else {
|
|
70
|
-
this.setState({
|
|
71
|
-
value
|
|
72
|
-
});
|
|
83
|
+
return ()=>{
|
|
84
|
+
if (typeof unsubscribeErrors === 'function') {
|
|
85
|
+
unsubscribeErrors();
|
|
73
86
|
}
|
|
74
|
-
if (
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
|
|
87
|
+
if (typeof unsubscribeDisabled === 'function') {
|
|
88
|
+
unsubscribeDisabled();
|
|
89
|
+
}
|
|
90
|
+
if (typeof unsubscribeValue === 'function') {
|
|
91
|
+
unsubscribeValue();
|
|
78
92
|
}
|
|
79
|
-
}), _define_property(this, "triggerSetValueCallbacks", (value)=>{
|
|
80
|
-
return new Promise((resolve, reject)=>{
|
|
81
|
-
if (this.props.isEmptyValue(value ?? null)) {
|
|
82
|
-
this.props.field.removeValue().then(resolve).catch(reject);
|
|
83
|
-
} else {
|
|
84
|
-
this.props.field.setValue(value).then(resolve).catch(reject);
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
}), _define_property(this, "debouncedTriggerSetValueCallbacks", debounce(this.triggerSetValueCallbacks, this.getDebounceDuration()));
|
|
88
|
-
const initialValue = props.field.getValue();
|
|
89
|
-
this.state = {
|
|
90
|
-
isLocalValueChange: false,
|
|
91
|
-
externalReset: 0,
|
|
92
|
-
value: initialValue,
|
|
93
|
-
lastRemoteValue: initialValue,
|
|
94
|
-
disabled: props.isInitiallyDisabled || props.field.getIsDisabled(),
|
|
95
|
-
errors: []
|
|
96
93
|
};
|
|
97
|
-
}
|
|
94
|
+
}, []);
|
|
95
|
+
return props.children ? props.children({
|
|
96
|
+
...state,
|
|
97
|
+
setValue,
|
|
98
|
+
disabled: props.isDisabled || state.disabled
|
|
99
|
+
}) : null;
|
|
98
100
|
}
|
|
99
|
-
_define_property(FieldConnector, "defaultProps", {
|
|
100
|
-
children: ()=>{
|
|
101
|
-
return null;
|
|
102
|
-
},
|
|
103
|
-
isEmptyValue: (value)=>{
|
|
104
|
-
return value === null || value === '';
|
|
105
|
-
},
|
|
106
|
-
isEqualValues: (value1, value2)=>{
|
|
107
|
-
return deepEqual(value1, value2);
|
|
108
|
-
},
|
|
109
|
-
debounce: 300
|
|
110
|
-
});
|
|
@@ -56,3 +56,66 @@ it('takes initial disable state from sdk.field', ()=>{
|
|
|
56
56
|
disabled: true
|
|
57
57
|
}));
|
|
58
58
|
});
|
|
59
|
+
it('handles subscriptions that do not return unsubscribe callbacks', ()=>{
|
|
60
|
+
const [field] = createFakeFieldAPI((field)=>({
|
|
61
|
+
...field,
|
|
62
|
+
onSchemaErrorsChanged: vi.fn(),
|
|
63
|
+
onIsDisabledChanged: vi.fn(),
|
|
64
|
+
onValueChanged: vi.fn()
|
|
65
|
+
}));
|
|
66
|
+
const { unmount } = render(/*#__PURE__*/ React.createElement(FieldConnector, {
|
|
67
|
+
field: field,
|
|
68
|
+
isInitiallyDisabled: false,
|
|
69
|
+
debounce: 0
|
|
70
|
+
}));
|
|
71
|
+
expect(()=>unmount()).not.toThrow();
|
|
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,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
import * as React from 'react';
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
3
2
|
import { FieldAPI, ValidationError } from '@contentful/app-sdk';
|
|
4
3
|
type Nullable = null | undefined;
|
|
5
4
|
export interface FieldConnectorChildProps<ValueType> {
|
|
@@ -11,44 +10,18 @@ export interface FieldConnectorChildProps<ValueType> {
|
|
|
11
10
|
errors: ValidationError[];
|
|
12
11
|
setValue: (value: ValueType | Nullable) => Promise<unknown>;
|
|
13
12
|
}
|
|
14
|
-
interface FieldConnectorState<ValueType> {
|
|
15
|
-
isLocalValueChange: boolean;
|
|
16
|
-
externalReset: number;
|
|
17
|
-
lastRemoteValue: ValueType | Nullable;
|
|
18
|
-
value: ValueType | Nullable;
|
|
19
|
-
disabled: boolean;
|
|
20
|
-
errors: ValidationError[];
|
|
21
|
-
}
|
|
22
13
|
type FieldConnectorProps<ValueType> = {
|
|
23
14
|
field: FieldAPI;
|
|
24
15
|
isInitiallyDisabled: boolean;
|
|
25
16
|
isDisabled?: boolean;
|
|
26
|
-
children
|
|
27
|
-
isEmptyValue
|
|
17
|
+
children?: (state: FieldConnectorChildProps<ValueType>) => ReactNode;
|
|
18
|
+
isEmptyValue?: (value: ValueType | null) => boolean;
|
|
28
19
|
isEqualValues?: (value1: ValueType | Nullable, value2: ValueType | Nullable) => boolean;
|
|
29
20
|
} & ({
|
|
30
|
-
debounce
|
|
21
|
+
debounce?: number;
|
|
31
22
|
} | {
|
|
32
23
|
/** @deprecated: Please use `debounce` instead */
|
|
33
24
|
throttle: number;
|
|
34
25
|
});
|
|
35
|
-
export declare
|
|
36
|
-
static defaultProps: {
|
|
37
|
-
children: () => null;
|
|
38
|
-
isEmptyValue: (value: any | Nullable) => boolean;
|
|
39
|
-
isEqualValues: (value1: any | Nullable, value2: any | Nullable) => boolean;
|
|
40
|
-
debounce: number;
|
|
41
|
-
};
|
|
42
|
-
constructor(props: FieldConnectorProps<ValueType>);
|
|
43
|
-
unsubscribeErrors: Function | null;
|
|
44
|
-
unsubscribeDisabled: Function | null;
|
|
45
|
-
unsubscribeValue: Function | null;
|
|
46
|
-
getDebounceDuration: () => number;
|
|
47
|
-
setValue: (value: ValueType | Nullable) => Promise<void>;
|
|
48
|
-
triggerSetValueCallbacks: (value: ValueType | Nullable) => Promise<unknown>;
|
|
49
|
-
debouncedTriggerSetValueCallbacks: import("lodash").DebouncedFunc<(value: ValueType | Nullable) => Promise<unknown>>;
|
|
50
|
-
componentDidMount(): void;
|
|
51
|
-
componentWillUnmount(): void;
|
|
52
|
-
render(): React.ReactNode;
|
|
53
|
-
}
|
|
26
|
+
export declare function FieldConnector<ValueType>(props: FieldConnectorProps<ValueType>): ReactNode;
|
|
54
27
|
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contentful/field-editor-shared",
|
|
3
|
-
"version": "4.5.
|
|
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
|
}
|