@contentful/field-editor-shared 4.5.2-canary.29 → 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.
@@ -73,8 +73,8 @@ function FieldConnector(props) {
73
73
  });
74
74
  const propsRef = _react.useRef(props);
75
75
  propsRef.current = props;
76
- const field = props.field;
77
- const getDebounceDuration = ()=>'throttle' in propsRef.current ? propsRef.current.throttle : propsRef.current.debounce ?? 300;
76
+ const subscribedFieldRef = _react.useRef(props.field);
77
+ const getDebounceDuration = ()=>'debounce' in propsRef.current ? propsRef.current.debounce ?? 300 : 300;
78
78
  const triggerSetValueCallbacks = _react.useCallback((value)=>{
79
79
  return new Promise((resolve, reject)=>{
80
80
  if ((propsRef.current.isEmptyValue ?? defaultIsEmptyValue)(value ?? null)) {
@@ -108,7 +108,8 @@ function FieldConnector(props) {
108
108
  debouncedTriggerSetValueCallbacks,
109
109
  triggerSetValueCallbacks
110
110
  ]);
111
- _react.useEffect(()=>{
111
+ _react.useLayoutEffect(()=>{
112
+ const field = subscribedFieldRef.current;
112
113
  const unsubscribeErrors = field.onSchemaErrorsChanged((errors)=>{
113
114
  setState((currentState)=>({
114
115
  ...currentState,
@@ -145,15 +146,11 @@ function FieldConnector(props) {
145
146
  if (typeof unsubscribeValue === 'function') {
146
147
  unsubscribeValue();
147
148
  }
148
- debouncedTriggerSetValueCallbacks.cancel();
149
149
  };
150
- }, [
151
- debouncedTriggerSetValueCallbacks,
152
- field
153
- ]);
154
- return props.children?.({
150
+ }, []);
151
+ return props.children ? props.children({
155
152
  ...state,
156
153
  setValue,
157
154
  disabled: props.isDisabled || state.disabled
158
- }) ?? null;
155
+ }) : null;
159
156
  }
@@ -120,3 +120,27 @@ 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
+ });
@@ -17,8 +17,8 @@ export function FieldConnector(props) {
17
17
  });
18
18
  const propsRef = React.useRef(props);
19
19
  propsRef.current = props;
20
- const field = props.field;
21
- const getDebounceDuration = ()=>'throttle' in propsRef.current ? propsRef.current.throttle : propsRef.current.debounce ?? 300;
20
+ const subscribedFieldRef = React.useRef(props.field);
21
+ const getDebounceDuration = ()=>'debounce' in propsRef.current ? propsRef.current.debounce ?? 300 : 300;
22
22
  const triggerSetValueCallbacks = React.useCallback((value)=>{
23
23
  return new Promise((resolve, reject)=>{
24
24
  if ((propsRef.current.isEmptyValue ?? defaultIsEmptyValue)(value ?? null)) {
@@ -52,7 +52,8 @@ export function FieldConnector(props) {
52
52
  debouncedTriggerSetValueCallbacks,
53
53
  triggerSetValueCallbacks
54
54
  ]);
55
- React.useEffect(()=>{
55
+ React.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
- debouncedTriggerSetValueCallbacks,
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
- }) ?? null;
99
+ }) : null;
103
100
  }
@@ -70,3 +70,27 @@ 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
+ });
@@ -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>): string | number | boolean | React.ReactElement<any, string | React.JSXElementConstructor<any>> | Iterable<React.ReactNode> | null;
26
+ export declare function FieldConnector<ValueType>(props: FieldConnectorProps<ValueType>): React.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.29+3ee64435",
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.2",
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": "3ee64435a6b1f0cb073174e7de406dc8cb4a74ad"
76
+ "gitHead": "cf70e54f33a6ecad25723afee834f3fe75bef531"
77
77
  }