@contentful/field-editor-shared 4.5.1 → 4.5.2-canary.29

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,77 @@ function _interop_require_wildcard(obj, nodeInterop) {
70
57
  }
71
58
  return newObj;
72
59
  }
73
- var _React_Component;
74
- class FieldConnector extends (_React_Component = _react.Component) {
75
- componentDidMount() {
76
- const { field } = this.props;
77
- this.unsubscribeErrors = field.onSchemaErrorsChanged((errors)=>{
78
- this.setState({
79
- errors: errors || []
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 field = props.field;
77
+ const getDebounceDuration = ()=>'throttle' in propsRef.current ? propsRef.current.throttle : propsRef.current.debounce ?? 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
- this.unsubscribeDisabled = field.onIsDisabledChanged((disabled)=>{
83
- this.setState({
84
- disabled
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.useEffect(()=>{
112
+ const unsubscribeErrors = field.onSchemaErrorsChanged((errors)=>{
113
+ setState((currentState)=>({
114
+ ...currentState,
115
+ errors: errors || []
116
+ }));
86
117
  });
87
- this.unsubscribeValue = field.onValueChanged((value)=>{
88
- this.setState((currentState)=>{
89
- const isLocalValueChange = this.props.isEqualValues(value, currentState.value);
118
+ const unsubscribeDisabled = field.onIsDisabledChanged((disabled)=>{
119
+ setState((currentState)=>({
120
+ ...currentState,
121
+ disabled
122
+ }));
123
+ });
124
+ const unsubscribeValue = field.onValueChanged((value)=>{
125
+ setState((currentState)=>{
126
+ const isLocalValueChange = (propsRef.current.isEqualValues ?? defaultIsEqualValues)(value, currentState.value);
90
127
  const lastRemoteValue = isLocalValueChange ? currentState.lastRemoteValue : value;
91
128
  const externalReset = currentState.externalReset + (isLocalValueChange ? 0 : 1);
92
129
  return {
130
+ ...currentState,
93
131
  value,
94
132
  lastRemoteValue,
95
133
  isLocalValueChange,
@@ -97,70 +135,25 @@ class FieldConnector extends (_React_Component = _react.Component) {
97
135
  };
98
136
  });
99
137
  });
100
- }
101
- componentWillUnmount() {
102
- if (typeof this.unsubscribeErrors === 'function') {
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
- });
138
+ return ()=>{
139
+ if (typeof unsubscribeErrors === 'function') {
140
+ unsubscribeErrors();
129
141
  }
130
- if (this.getDebounceDuration() === 0) {
131
- await this.triggerSetValueCallbacks(value);
132
- } else {
133
- await this.debouncedTriggerSetValueCallbacks(value);
142
+ if (typeof unsubscribeDisabled === 'function') {
143
+ unsubscribeDisabled();
134
144
  }
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: []
145
+ if (typeof unsubscribeValue === 'function') {
146
+ unsubscribeValue();
147
+ }
148
+ debouncedTriggerSetValueCallbacks.cancel();
152
149
  };
153
- }
150
+ }, [
151
+ debouncedTriggerSetValueCallbacks,
152
+ field
153
+ ]);
154
+ return props.children?.({
155
+ ...state,
156
+ setValue,
157
+ disabled: props.isDisabled || state.disabled
158
+ }) ?? null;
154
159
  }
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,17 @@ 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
+ });
@@ -1,39 +1,77 @@
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
- var _React_Component;
18
- export class FieldConnector extends (_React_Component = React.Component) {
19
- componentDidMount() {
20
- const { field } = this.props;
21
- this.unsubscribeErrors = field.onSchemaErrorsChanged((errors)=>{
22
- this.setState({
23
- errors: errors || []
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 field = props.field;
21
+ const getDebounceDuration = ()=>'throttle' in propsRef.current ? propsRef.current.throttle : propsRef.current.debounce ?? 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
- this.unsubscribeDisabled = field.onIsDisabledChanged((disabled)=>{
27
- this.setState({
28
- disabled
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.useEffect(()=>{
56
+ const unsubscribeErrors = field.onSchemaErrorsChanged((errors)=>{
57
+ setState((currentState)=>({
58
+ ...currentState,
59
+ errors: errors || []
60
+ }));
30
61
  });
31
- this.unsubscribeValue = field.onValueChanged((value)=>{
32
- this.setState((currentState)=>{
33
- const isLocalValueChange = this.props.isEqualValues(value, currentState.value);
62
+ const unsubscribeDisabled = field.onIsDisabledChanged((disabled)=>{
63
+ setState((currentState)=>({
64
+ ...currentState,
65
+ disabled
66
+ }));
67
+ });
68
+ const unsubscribeValue = field.onValueChanged((value)=>{
69
+ setState((currentState)=>{
70
+ const isLocalValueChange = (propsRef.current.isEqualValues ?? defaultIsEqualValues)(value, currentState.value);
34
71
  const lastRemoteValue = isLocalValueChange ? currentState.lastRemoteValue : value;
35
72
  const externalReset = currentState.externalReset + (isLocalValueChange ? 0 : 1);
36
73
  return {
74
+ ...currentState,
37
75
  value,
38
76
  lastRemoteValue,
39
77
  isLocalValueChange,
@@ -41,70 +79,25 @@ export class FieldConnector extends (_React_Component = React.Component) {
41
79
  };
42
80
  });
43
81
  });
44
- }
45
- componentWillUnmount() {
46
- if (typeof this.unsubscribeErrors === 'function') {
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
- });
82
+ return ()=>{
83
+ if (typeof unsubscribeErrors === 'function') {
84
+ unsubscribeErrors();
73
85
  }
74
- if (this.getDebounceDuration() === 0) {
75
- await this.triggerSetValueCallbacks(value);
76
- } else {
77
- await this.debouncedTriggerSetValueCallbacks(value);
86
+ if (typeof unsubscribeDisabled === 'function') {
87
+ unsubscribeDisabled();
78
88
  }
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: []
89
+ if (typeof unsubscribeValue === 'function') {
90
+ unsubscribeValue();
91
+ }
92
+ debouncedTriggerSetValueCallbacks.cancel();
96
93
  };
97
- }
94
+ }, [
95
+ debouncedTriggerSetValueCallbacks,
96
+ field
97
+ ]);
98
+ return props.children?.({
99
+ ...state,
100
+ setValue,
101
+ disabled: props.isDisabled || state.disabled
102
+ }) ?? null;
98
103
  }
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,17 @@ 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
+ });
@@ -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: (state: FieldConnectorChildProps<ValueType>) => React.ReactNode;
27
- isEmptyValue: (value: ValueType | null) => boolean;
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: number;
21
+ debounce?: number;
31
22
  } | {
32
23
  /** @deprecated: Please use `debounce` instead */
33
24
  throttle: number;
34
25
  });
35
- export declare class FieldConnector<ValueType> extends React.Component<FieldConnectorProps<ValueType>, FieldConnectorState<ValueType>> {
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>): string | number | boolean | React.ReactElement<any, string | React.JSXElementConstructor<any>> | Iterable<React.ReactNode> | null;
54
27
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentful/field-editor-shared",
3
- "version": "4.5.1",
3
+ "version": "4.5.2-canary.29+3ee64435",
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": "78ed76ca4a3207924294971766a40bb46e196203"
76
+ "gitHead": "3ee64435a6b1f0cb073174e7de406dc8cb4a74ad"
77
77
  }