@jsonforms/core 3.2.1 → 3.3.0-alpha.1

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.
@@ -71,6 +71,13 @@ export declare const uiTypeIs: (expected: string) => Tester;
71
71
  * @param {any} optionValue the expected value of the option
72
72
  */
73
73
  export declare const optionIs: (optionName: string, optionValue: any) => Tester;
74
+ /**
75
+ * Checks whether the given UI schema has an option with the given
76
+ * name. If no options property is set, returns false.
77
+ *
78
+ * @param {string} optionName the name of the option to check
79
+ */
80
+ export declare const hasOption: (optionName: string) => Tester;
74
81
  /**
75
82
  * Only applicable for Controls.
76
83
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsonforms/core",
3
- "version": "3.2.1",
3
+ "version": "3.3.0-alpha.1",
4
4
  "description": "Core module of JSON Forms",
5
5
  "repository": "https://github.com/eclipsesource/jsonforms",
6
6
  "bugs": "https://github.com/eclipsesource/jsonforms/issues",
@@ -40,13 +40,12 @@
40
40
  "files": [
41
41
  "test/**/*"
42
42
  ],
43
- "compileEnhancements": false,
44
43
  "extensions": [
45
44
  "ts"
46
45
  ],
47
46
  "require": [
48
47
  "./test-config/ts-node.config.js",
49
- "source-map-support/register"
48
+ "source-map-support/register.js"
50
49
  ]
51
50
  },
52
51
  "nyc": {
@@ -66,9 +65,9 @@
66
65
  "@types/redux-mock-store": "^1.0.1",
67
66
  "@typescript-eslint/eslint-plugin": "^5.54.1",
68
67
  "@typescript-eslint/parser": "^5.54.1",
69
- "ava": "~2.4.0",
68
+ "ava": "^6.1.2",
70
69
  "document-register-element": "^1.14.3",
71
- "eslint": "^7.32.0",
70
+ "eslint": "^8.56.0",
72
71
  "eslint-config-prettier": "^8.7.0",
73
72
  "eslint-plugin-import": "^2.27.5",
74
73
  "eslint-plugin-prettier": "^4.2.1",
@@ -83,7 +82,7 @@
83
82
  "rollup-plugin-cleanup": "^3.2.1",
84
83
  "rollup-plugin-typescript2": "^0.34.1",
85
84
  "rollup-plugin-visualizer": "^5.4.1",
86
- "source-map-support": "0.5.16",
85
+ "source-map-support": "^0.5.21",
87
86
  "ts-node": "^10.4.0",
88
87
  "tslib": "^2.5.0",
89
88
  "typedoc": "~0.25.3",
@@ -56,6 +56,57 @@ export const UPDATE_I18N = 'jsonforms/UPDATE_I18N' as const;
56
56
  export const ADD_DEFAULT_DATA = 'jsonforms/ADD_DEFAULT_DATA' as const;
57
57
  export const REMOVE_DEFAULT_DATA = 'jsonforms/REMOVE_DEFAULT_DATA' as const;
58
58
 
59
+ export type UpdateArrayContext =
60
+ | { type: 'ADD'; values: any[] }
61
+ | { type: 'REMOVE'; indices: number[] }
62
+ | { type: 'MOVE'; moves: { from: number; to: number }[] };
63
+
64
+ export const isUpdateArrayContext = (
65
+ context: object
66
+ ): context is UpdateArrayContext => {
67
+ if (!('type' in context)) {
68
+ return false;
69
+ }
70
+ if (typeof context.type !== 'string') {
71
+ return false;
72
+ }
73
+ switch (context.type) {
74
+ case 'ADD': {
75
+ return (
76
+ 'values' in context &&
77
+ Array.isArray(context.values) &&
78
+ context.values.length > 0
79
+ );
80
+ }
81
+ case 'REMOVE': {
82
+ return (
83
+ 'indices' in context &&
84
+ Array.isArray(context.indices) &&
85
+ context.indices.length > 0 &&
86
+ context.indices.every((i) => typeof i === 'number')
87
+ );
88
+ }
89
+ case 'MOVE': {
90
+ return (
91
+ 'moves' in context &&
92
+ Array.isArray(context.moves) &&
93
+ context.moves.length > 0 &&
94
+ context.moves.every(
95
+ (m) =>
96
+ typeof m === 'object' &&
97
+ m !== null &&
98
+ 'from' in m &&
99
+ 'to' in m &&
100
+ typeof m.from === 'number' &&
101
+ typeof m.to === 'number'
102
+ )
103
+ );
104
+ }
105
+ default:
106
+ return false;
107
+ }
108
+ };
109
+
59
110
  export type CoreActions =
60
111
  | InitAction
61
112
  | UpdateCoreAction
@@ -70,6 +121,7 @@ export interface UpdateAction {
70
121
  type: 'jsonforms/UPDATE';
71
122
  path: string;
72
123
  updater(existingData?: any): any;
124
+ context?: object;
73
125
  }
74
126
 
75
127
  export interface UpdateErrorsAction {
@@ -165,11 +217,13 @@ export const setAjv = (ajv: AJV) => ({
165
217
 
166
218
  export const update = (
167
219
  path: string,
168
- updater: (existingData: any) => any
220
+ updater: (existingData: any) => any,
221
+ context?: object
169
222
  ): UpdateAction => ({
170
223
  type: UPDATE_DATA,
171
224
  path,
172
225
  updater,
226
+ context,
173
227
  });
174
228
 
175
229
  export const updateErrors = (errors: ErrorObject[]): UpdateErrorsAction => ({
@@ -31,6 +31,7 @@ import isArray from 'lodash/isArray';
31
31
  import reduce from 'lodash/reduce';
32
32
  import toPairs from 'lodash/toPairs';
33
33
  import includes from 'lodash/includes';
34
+ import isUndefined from 'lodash/isUndefined';
34
35
  import type {
35
36
  Categorization,
36
37
  ControlElement,
@@ -215,6 +216,23 @@ export const optionIs =
215
216
  return !isEmpty(options) && options[optionName] === optionValue;
216
217
  };
217
218
 
219
+ /**
220
+ * Checks whether the given UI schema has an option with the given
221
+ * name. If no options property is set, returns false.
222
+ *
223
+ * @param {string} optionName the name of the option to check
224
+ */
225
+ export const hasOption =
226
+ (optionName: string): Tester =>
227
+ (uischema: UISchemaElement): boolean => {
228
+ if (isEmpty(uischema)) {
229
+ return false;
230
+ }
231
+
232
+ const options = uischema.options;
233
+ return !isEmpty(options) && !isUndefined(options[optionName]);
234
+ };
235
+
218
236
  /**
219
237
  * Only applicable for Controls.
220
238
  *
@@ -59,7 +59,7 @@ import { moveDown, moveUp } from './array';
59
59
  import type { AnyAction, Dispatch } from './type';
60
60
  import { Resolve, convertDateToString, hasType } from './util';
61
61
  import { composePaths, composeWithUi } from './path';
62
- import { CoreActions, update } from '../actions';
62
+ import { CoreActions, update, UpdateArrayContext } from '../actions';
63
63
  import type { ErrorObject } from 'ajv';
64
64
  import type { JsonFormsState } from '../store';
65
65
  import {
@@ -823,41 +823,63 @@ export const mapDispatchToArrayControlProps = (
823
823
  ): DispatchPropsOfArrayControl => ({
824
824
  addItem: (path: string, value: any) => () => {
825
825
  dispatch(
826
- update(path, (array) => {
827
- if (array === undefined || array === null) {
828
- return [value];
829
- }
830
-
831
- array.push(value);
832
- return array;
833
- })
826
+ update(
827
+ path,
828
+ (array) => {
829
+ if (array === undefined || array === null) {
830
+ return [value];
831
+ }
832
+
833
+ array.push(value);
834
+ return array;
835
+ },
836
+ { type: 'ADD', values: [value] } as UpdateArrayContext
837
+ )
834
838
  );
835
839
  },
836
840
  removeItems: (path: string, toDelete: number[]) => () => {
837
841
  dispatch(
838
- update(path, (array) => {
839
- toDelete
840
- .sort((a, b) => a - b)
841
- .reverse()
842
- .forEach((s) => array.splice(s, 1));
843
- return array;
844
- })
842
+ update(
843
+ path,
844
+ (array) => {
845
+ toDelete
846
+ .sort((a, b) => a - b)
847
+ .reverse()
848
+ .forEach((s) => array.splice(s, 1));
849
+ return array;
850
+ },
851
+ { type: 'REMOVE', indices: toDelete } as UpdateArrayContext
852
+ )
845
853
  );
846
854
  },
847
855
  moveUp: (path, toMove: number) => () => {
848
856
  dispatch(
849
- update(path, (array) => {
850
- moveUp(array, toMove);
851
- return array;
852
- })
857
+ update(
858
+ path,
859
+ (array) => {
860
+ moveUp(array, toMove);
861
+ return array;
862
+ },
863
+ {
864
+ type: 'MOVE',
865
+ moves: [{ from: toMove, to: toMove - 1 }],
866
+ } as UpdateArrayContext
867
+ )
853
868
  );
854
869
  },
855
870
  moveDown: (path, toMove: number) => () => {
856
871
  dispatch(
857
- update(path, (array) => {
858
- moveDown(array, toMove);
859
- return array;
860
- })
872
+ update(
873
+ path,
874
+ (array) => {
875
+ moveDown(array, toMove);
876
+ return array;
877
+ },
878
+ {
879
+ type: 'MOVE',
880
+ moves: [{ from: toMove, to: toMove + 1 }],
881
+ } as UpdateArrayContext
882
+ )
861
883
  );
862
884
  },
863
885
  });