@contentful/field-editor-shared 4.5.2-canary.sha-cf70e54f.30 → 4.5.2

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 = /*#__PURE__*/ _interop_require_wildcard(require("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 subscribedFieldRef = _react.useRef(props.field);
77
- const getDebounceDuration = ()=>'debounce' in propsRef.current ? propsRef.current.debounce ?? 300 : 300;
78
- const triggerSetValueCallbacks = _react.useCallback((value)=>{
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)=>{
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,7 @@ function FieldConnector(props) {
108
67
  debouncedTriggerSetValueCallbacks,
109
68
  triggerSetValueCallbacks
110
69
  ]);
111
- _react.useLayoutEffect(()=>{
70
+ (0, _react.useLayoutEffect)(()=>{
112
71
  const field = subscribedFieldRef.current;
113
72
  const unsubscribeErrors = field.onSchemaErrorsChanged((errors)=>{
114
73
  setState((currentState)=>({
@@ -144,3 +144,28 @@ function getChild(children) {
144
144
  _vitest.vi.useRealTimers();
145
145
  }
146
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 * as React from 'react';
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] = React.useState(()=>{
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 = React.useRef(props);
18
+ const propsRef = useRef(props);
19
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)=>{
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
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 = React.useMemo(()=>debounce(triggerSetValueCallbacks, getDebounceDuration()), [
31
+ const debouncedTriggerSetValueCallbacks = useMemo(()=>debounce(triggerSetValueCallbacks, getDebounceDuration()), [
32
32
  triggerSetValueCallbacks
33
33
  ]);
34
- const setValue = React.useCallback(async (value)=>{
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,7 @@ export function FieldConnector(props) {
52
52
  debouncedTriggerSetValueCallbacks,
53
53
  triggerSetValueCallbacks
54
54
  ]);
55
- React.useLayoutEffect(()=>{
55
+ useLayoutEffect(()=>{
56
56
  const field = subscribedFieldRef.current;
57
57
  const unsubscribeErrors = field.onSchemaErrorsChanged((errors)=>{
58
58
  setState((currentState)=>({
@@ -94,3 +94,28 @@ it('keeps pending debounced value updates after unmounting', async ()=>{
94
94
  vi.useRealTimers();
95
95
  }
96
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 * as React from 'react';
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>) => React.ReactNode;
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>): React.ReactNode;
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.sha-cf70e54f.30+cf70e54f",
3
+ "version": "4.5.2",
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.3-canary.sha-cf70e54f.34+cf70e54f",
46
+ "@contentful/field-editor-test-utils": "^3.1.2",
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": "cf70e54f33a6ecad25723afee834f3fe75bef531"
76
+ "gitHead": "dabc9af0a14a4fe336716b35471e203a976dd14b"
77
77
  }