@contentful/field-editor-shared 4.5.1 → 4.5.2-canary.sha-cf70e54f.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.
|
@@ -11,19 +11,6 @@ Object.defineProperty(exports, "FieldConnector", {
|
|
|
11
11
|
const _react = /*#__PURE__*/ _interop_require_wildcard(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
|
|
@@ -70,26 +57,78 @@ function _interop_require_wildcard(obj, nodeInterop) {
|
|
|
70
57
|
}
|
|
71
58
|
return newObj;
|
|
72
59
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
60
|
+
const defaultIsEmptyValue = (value)=>value === null || value === '';
|
|
61
|
+
const defaultIsEqualValues = (value1, value2)=>(0, _fastdeepequal.default)(value1, value2);
|
|
62
|
+
function FieldConnector(props) {
|
|
63
|
+
const [state, setState] = _react.useState(()=>{
|
|
64
|
+
const initialValue = props.field.getValue();
|
|
65
|
+
return {
|
|
66
|
+
isLocalValueChange: false,
|
|
67
|
+
externalReset: 0,
|
|
68
|
+
value: initialValue,
|
|
69
|
+
lastRemoteValue: initialValue,
|
|
70
|
+
disabled: props.isInitiallyDisabled || props.field.getIsDisabled(),
|
|
71
|
+
errors: []
|
|
72
|
+
};
|
|
73
|
+
});
|
|
74
|
+
const propsRef = _react.useRef(props);
|
|
75
|
+
propsRef.current = props;
|
|
76
|
+
const subscribedFieldRef = _react.useRef(props.field);
|
|
77
|
+
const getDebounceDuration = ()=>'debounce' in propsRef.current ? propsRef.current.debounce ?? 300 : 300;
|
|
78
|
+
const triggerSetValueCallbacks = _react.useCallback((value)=>{
|
|
79
|
+
return new Promise((resolve, reject)=>{
|
|
80
|
+
if ((propsRef.current.isEmptyValue ?? defaultIsEmptyValue)(value ?? null)) {
|
|
81
|
+
propsRef.current.field.removeValue().then(resolve).catch(reject);
|
|
82
|
+
} else {
|
|
83
|
+
propsRef.current.field.setValue(value).then(resolve).catch(reject);
|
|
84
|
+
}
|
|
81
85
|
});
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
+
}, []);
|
|
87
|
+
const debouncedTriggerSetValueCallbacks = _react.useMemo(()=>(0, _debounce.default)(triggerSetValueCallbacks, getDebounceDuration()), [
|
|
88
|
+
triggerSetValueCallbacks
|
|
89
|
+
]);
|
|
90
|
+
const setValue = _react.useCallback(async (value)=>{
|
|
91
|
+
if ((propsRef.current.isEmptyValue ?? defaultIsEmptyValue)(value ?? null)) {
|
|
92
|
+
setState((currentState)=>({
|
|
93
|
+
...currentState,
|
|
94
|
+
value: undefined
|
|
95
|
+
}));
|
|
96
|
+
} else {
|
|
97
|
+
setState((currentState)=>({
|
|
98
|
+
...currentState,
|
|
99
|
+
value
|
|
100
|
+
}));
|
|
101
|
+
}
|
|
102
|
+
if (getDebounceDuration() === 0) {
|
|
103
|
+
await triggerSetValueCallbacks(value);
|
|
104
|
+
} else {
|
|
105
|
+
await debouncedTriggerSetValueCallbacks(value);
|
|
106
|
+
}
|
|
107
|
+
}, [
|
|
108
|
+
debouncedTriggerSetValueCallbacks,
|
|
109
|
+
triggerSetValueCallbacks
|
|
110
|
+
]);
|
|
111
|
+
_react.useLayoutEffect(()=>{
|
|
112
|
+
const field = subscribedFieldRef.current;
|
|
113
|
+
const unsubscribeErrors = field.onSchemaErrorsChanged((errors)=>{
|
|
114
|
+
setState((currentState)=>({
|
|
115
|
+
...currentState,
|
|
116
|
+
errors: errors || []
|
|
117
|
+
}));
|
|
86
118
|
});
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
119
|
+
const unsubscribeDisabled = field.onIsDisabledChanged((disabled)=>{
|
|
120
|
+
setState((currentState)=>({
|
|
121
|
+
...currentState,
|
|
122
|
+
disabled
|
|
123
|
+
}));
|
|
124
|
+
});
|
|
125
|
+
const unsubscribeValue = field.onValueChanged((value)=>{
|
|
126
|
+
setState((currentState)=>{
|
|
127
|
+
const isLocalValueChange = (propsRef.current.isEqualValues ?? defaultIsEqualValues)(value, currentState.value);
|
|
90
128
|
const lastRemoteValue = isLocalValueChange ? currentState.lastRemoteValue : value;
|
|
91
129
|
const externalReset = currentState.externalReset + (isLocalValueChange ? 0 : 1);
|
|
92
130
|
return {
|
|
131
|
+
...currentState,
|
|
93
132
|
value,
|
|
94
133
|
lastRemoteValue,
|
|
95
134
|
isLocalValueChange,
|
|
@@ -97,70 +136,21 @@ class FieldConnector extends (_React_Component = _react.Component) {
|
|
|
97
136
|
};
|
|
98
137
|
});
|
|
99
138
|
});
|
|
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
|
-
});
|
|
139
|
+
return ()=>{
|
|
140
|
+
if (typeof unsubscribeErrors === 'function') {
|
|
141
|
+
unsubscribeErrors();
|
|
129
142
|
}
|
|
130
|
-
if (
|
|
131
|
-
|
|
132
|
-
}
|
|
133
|
-
|
|
143
|
+
if (typeof unsubscribeDisabled === 'function') {
|
|
144
|
+
unsubscribeDisabled();
|
|
145
|
+
}
|
|
146
|
+
if (typeof unsubscribeValue === 'function') {
|
|
147
|
+
unsubscribeValue();
|
|
134
148
|
}
|
|
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
149
|
};
|
|
153
|
-
}
|
|
150
|
+
}, []);
|
|
151
|
+
return props.children ? props.children({
|
|
152
|
+
...state,
|
|
153
|
+
setValue,
|
|
154
|
+
disabled: props.isDisabled || state.disabled
|
|
155
|
+
}) : null;
|
|
154
156
|
}
|
|
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,41 @@ 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
|
+
});
|
|
@@ -1,39 +1,78 @@
|
|
|
1
|
-
function _define_property(obj, key, value) {
|
|
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
1
|
import * as React 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] = React.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 = React.useRef(props);
|
|
19
|
+
propsRef.current = props;
|
|
20
|
+
const subscribedFieldRef = React.useRef(props.field);
|
|
21
|
+
const getDebounceDuration = ()=>'debounce' in propsRef.current ? propsRef.current.debounce ?? 300 : 300;
|
|
22
|
+
const triggerSetValueCallbacks = React.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 = React.useMemo(()=>debounce(triggerSetValueCallbacks, getDebounceDuration()), [
|
|
32
|
+
triggerSetValueCallbacks
|
|
33
|
+
]);
|
|
34
|
+
const setValue = React.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
|
+
React.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,41 @@ 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
|
+
});
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/// <reference types="lodash" />
|
|
2
1
|
import * as React from 'react';
|
|
3
2
|
import { FieldAPI, ValidationError } from '@contentful/app-sdk';
|
|
4
3
|
type Nullable = null | undefined;
|
|
@@ -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>) => React.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>): React.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.sha-cf70e54f.30+cf70e54f",
|
|
4
4
|
"main": "dist/cjs/index.js",
|
|
5
5
|
"module": "dist/esm/index.js",
|
|
6
6
|
"types": "dist/types/index.d.ts",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@contentful/app-sdk": "^4.42.0",
|
|
46
|
-
"@contentful/field-editor-test-utils": "^3.1.
|
|
46
|
+
"@contentful/field-editor-test-utils": "^3.1.3-canary.sha-cf70e54f.34+cf70e54f",
|
|
47
47
|
"@lingui/core": "5.3.0",
|
|
48
48
|
"@tanstack/react-query": "^4.3.9",
|
|
49
49
|
"@testing-library/react": "16.3.0"
|
|
@@ -73,5 +73,5 @@
|
|
|
73
73
|
"publishConfig": {
|
|
74
74
|
"registry": "https://npm.pkg.github.com/"
|
|
75
75
|
},
|
|
76
|
-
"gitHead": "
|
|
76
|
+
"gitHead": "cf70e54f33a6ecad25723afee834f3fe75bef531"
|
|
77
77
|
}
|