@jsonforms/core 3.3.0-alpha.0 → 3.3.0-beta.0

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.
@@ -303,6 +303,7 @@ const isScopable = (obj) => !!obj && typeof obj === 'object';
303
303
  const isScoped = (obj) => isScopable(obj) && typeof obj.scope === 'string';
304
304
  const isLabelable = (obj) => !!obj && typeof obj === 'object';
305
305
  const isLabeled = (obj) => isLabelable(obj) && ['string', 'boolean'].includes(typeof obj.label);
306
+ const isControlElement = (uiSchema) => uiSchema.type === 'Control';
306
307
 
307
308
  const move = (array, index, delta) => {
308
309
  const newIndex = index + delta;
@@ -970,7 +971,7 @@ const isObjectControl = and(uiTypeIs('Control'), schemaTypeIs('object'));
970
971
  const isAllOfControl = and(uiTypeIs('Control'), schemaMatches((schema) => Object.prototype.hasOwnProperty.call(schema, 'allOf')));
971
972
  const isAnyOfControl = and(uiTypeIs('Control'), schemaMatches((schema) => Object.prototype.hasOwnProperty.call(schema, 'anyOf')));
972
973
  const isOneOfControl = and(uiTypeIs('Control'), schemaMatches((schema) => Object.prototype.hasOwnProperty.call(schema, 'oneOf')));
973
- const isEnumControl = and(uiTypeIs('Control'), or(schemaMatches((schema) => Object.prototype.hasOwnProperty.call(schema, 'enum')), schemaMatches((schema) => Object.prototype.hasOwnProperty.call(schema, 'const'))));
974
+ const isEnumControl = and(uiTypeIs('Control'), schemaMatches((schema) => isEnumSchema(schema)));
974
975
  const isOneOfEnumControl = and(uiTypeIs('Control'), schemaMatches((schema) => isOneOfEnumSchema(schema)));
975
976
  const isIntegerControl = and(uiTypeIs('Control'), schemaTypeIs('integer'));
976
977
  const isNumberControl = and(uiTypeIs('Control'), schemaTypeIs('number'));
@@ -1518,6 +1519,73 @@ const Runtime = {
1518
1519
  },
1519
1520
  };
1520
1521
 
1522
+ const getFirstPrimitiveProp = (schema) => {
1523
+ if (schema.properties) {
1524
+ return find(Object.keys(schema.properties), (propName) => {
1525
+ const prop = schema.properties[propName];
1526
+ return (prop.type === 'string' ||
1527
+ prop.type === 'number' ||
1528
+ prop.type === 'integer');
1529
+ });
1530
+ }
1531
+ return undefined;
1532
+ };
1533
+ const isOneOfEnumSchema = (schema) => !!schema &&
1534
+ Object.prototype.hasOwnProperty.call(schema, 'oneOf') &&
1535
+ schema.oneOf &&
1536
+ schema.oneOf.every((s) => s.const !== undefined);
1537
+ const isEnumSchema = (schema) => !!schema &&
1538
+ typeof schema === 'object' &&
1539
+ (Object.prototype.hasOwnProperty.call(schema, 'enum') ||
1540
+ Object.prototype.hasOwnProperty.call(schema, 'const'));
1541
+
1542
+ const setReadonlyPropertyValue = (value) => (child) => {
1543
+ if (!child.options) {
1544
+ child.options = {};
1545
+ }
1546
+ child.options.readonly = value;
1547
+ };
1548
+ const setReadonly = (uischema) => {
1549
+ iterateSchema(uischema, setReadonlyPropertyValue(true));
1550
+ };
1551
+ const unsetReadonly = (uischema) => {
1552
+ iterateSchema(uischema, setReadonlyPropertyValue(false));
1553
+ };
1554
+ const iterateSchema = (uischema, toApply) => {
1555
+ if (isEmpty(uischema)) {
1556
+ return;
1557
+ }
1558
+ if (isLayout(uischema)) {
1559
+ uischema.elements.forEach((child) => iterateSchema(child, toApply));
1560
+ return;
1561
+ }
1562
+ toApply(uischema);
1563
+ };
1564
+ const getPropPath = (path) => {
1565
+ return `/properties/${path
1566
+ .split('.')
1567
+ .map((p) => encode(p))
1568
+ .join('/properties/')}`;
1569
+ };
1570
+ const findUiControl = (uiSchema, path) => {
1571
+ if (isControlElement(uiSchema)) {
1572
+ if (isScoped(uiSchema) && uiSchema.scope.endsWith(getPropPath(path))) {
1573
+ return uiSchema;
1574
+ }
1575
+ else if (uiSchema.options?.detail) {
1576
+ return findUiControl(uiSchema.options.detail, path);
1577
+ }
1578
+ }
1579
+ if (isLayout(uiSchema)) {
1580
+ for (const elem of uiSchema.elements) {
1581
+ const result = findUiControl(elem, path);
1582
+ if (result !== undefined)
1583
+ return result;
1584
+ }
1585
+ }
1586
+ return undefined;
1587
+ };
1588
+
1521
1589
  const deriveLabel = (controlElement, schemaElement) => {
1522
1590
  if (schemaElement && typeof schemaElement.title === 'string') {
1523
1591
  return schemaElement.title;
@@ -1553,6 +1621,32 @@ const labelDescription = (text, show) => ({
1553
1621
  text: text,
1554
1622
  show: show,
1555
1623
  });
1624
+ const computeChildLabel = (data, childPath, childLabelProp, schema, rootSchema, translateFct, uiSchema) => {
1625
+ const childData = Resolve.data(data, childPath);
1626
+ if (!childLabelProp) {
1627
+ childLabelProp = getFirstPrimitiveProp(schema);
1628
+ }
1629
+ if (!childLabelProp) {
1630
+ return '';
1631
+ }
1632
+ const currentValue = get(childData, childLabelProp);
1633
+ if (currentValue === undefined) {
1634
+ return '';
1635
+ }
1636
+ const childSchema = Resolve.schema(schema, '#' + getPropPath(childLabelProp), rootSchema);
1637
+ let enumOption = undefined;
1638
+ if (isEnumSchema(childSchema)) {
1639
+ enumOption = enumToEnumOptionMapper(currentValue, translateFct, getI18nKeyPrefix(childSchema, findUiControl(uiSchema, childLabelProp), childPath + '.' + childLabelProp));
1640
+ }
1641
+ else if (isOneOfEnumSchema(childSchema)) {
1642
+ const oneOfArray = childSchema.oneOf;
1643
+ const oneOfSchema = oneOfArray.find((e) => isEqual(e.const, currentValue));
1644
+ if (oneOfSchema) {
1645
+ enumOption = oneOfToEnumOptionMapper(oneOfSchema, translateFct, getI18nKeyPrefix(oneOfSchema, undefined, childPath + '.' + childLabelProp));
1646
+ }
1647
+ }
1648
+ return enumOption ? enumOption.label : currentValue;
1649
+ };
1556
1650
 
1557
1651
  const isRequired = (schema, schemaPath, rootSchema) => {
1558
1652
  const pathSegments = schemaPath.split('/');
@@ -1748,18 +1842,9 @@ const mapStateToMultiEnumControlProps = (state, ownProps) => {
1748
1842
  };
1749
1843
  };
1750
1844
  const mapStateToMasterListItemProps = (state, ownProps) => {
1751
- const { schema, path, index } = ownProps;
1752
- const firstPrimitiveProp = schema.properties
1753
- ? find(Object.keys(schema.properties), (propName) => {
1754
- const prop = schema.properties[propName];
1755
- return (prop.type === 'string' ||
1756
- prop.type === 'number' ||
1757
- prop.type === 'integer');
1758
- })
1759
- : undefined;
1845
+ const { schema, path, uischema, childLabelProp, index } = ownProps;
1760
1846
  const childPath = compose(path, `${index}`);
1761
- const childData = Resolve.data(getData(state), childPath);
1762
- const childLabel = firstPrimitiveProp ? childData[firstPrimitiveProp] : '';
1847
+ const childLabel = computeChildLabel(getData(state), childPath, childLabelProp, schema, getSchema(state), state.jsonforms.i18n.translate, uischema);
1763
1848
  return {
1764
1849
  ...ownProps,
1765
1850
  childLabel,
@@ -1797,7 +1882,7 @@ const mapDispatchToArrayControlProps = (dispatch) => ({
1797
1882
  }
1798
1883
  array.push(value);
1799
1884
  return array;
1800
- }));
1885
+ }, { type: 'ADD', values: [value] }));
1801
1886
  },
1802
1887
  removeItems: (path, toDelete) => () => {
1803
1888
  dispatch(update(path, (array) => {
@@ -1806,18 +1891,24 @@ const mapDispatchToArrayControlProps = (dispatch) => ({
1806
1891
  .reverse()
1807
1892
  .forEach((s) => array.splice(s, 1));
1808
1893
  return array;
1809
- }));
1894
+ }, { type: 'REMOVE', indices: toDelete }));
1810
1895
  },
1811
1896
  moveUp: (path, toMove) => () => {
1812
1897
  dispatch(update(path, (array) => {
1813
1898
  moveUp(array, toMove);
1814
1899
  return array;
1900
+ }, {
1901
+ type: 'MOVE',
1902
+ moves: [{ from: toMove, to: toMove - 1 }],
1815
1903
  }));
1816
1904
  },
1817
1905
  moveDown: (path, toMove) => () => {
1818
1906
  dispatch(update(path, (array) => {
1819
1907
  moveDown(array, toMove);
1820
1908
  return array;
1909
+ }, {
1910
+ type: 'MOVE',
1911
+ moves: [{ from: toMove, to: toMove + 1 }],
1821
1912
  }));
1822
1913
  },
1823
1914
  });
@@ -2102,45 +2193,6 @@ const createId = (proposedId) => {
2102
2193
  const removeId = (id) => usedIds.delete(id);
2103
2194
  const clearAllIds = () => usedIds.clear();
2104
2195
 
2105
- const getFirstPrimitiveProp = (schema) => {
2106
- if (schema.properties) {
2107
- return find(Object.keys(schema.properties), (propName) => {
2108
- const prop = schema.properties[propName];
2109
- return (prop.type === 'string' ||
2110
- prop.type === 'number' ||
2111
- prop.type === 'integer');
2112
- });
2113
- }
2114
- return undefined;
2115
- };
2116
- const isOneOfEnumSchema = (schema) => !!schema &&
2117
- Object.prototype.hasOwnProperty.call(schema, 'oneOf') &&
2118
- schema.oneOf &&
2119
- schema.oneOf.every((s) => s.const !== undefined);
2120
-
2121
- const setReadonlyPropertyValue = (value) => (child) => {
2122
- if (!child.options) {
2123
- child.options = {};
2124
- }
2125
- child.options.readonly = value;
2126
- };
2127
- const setReadonly = (uischema) => {
2128
- iterateSchema(uischema, setReadonlyPropertyValue(true));
2129
- };
2130
- const unsetReadonly = (uischema) => {
2131
- iterateSchema(uischema, setReadonlyPropertyValue(false));
2132
- };
2133
- const iterateSchema = (uischema, toApply) => {
2134
- if (isEmpty(uischema)) {
2135
- return;
2136
- }
2137
- if (isLayout(uischema)) {
2138
- uischema.elements.forEach((child) => iterateSchema(child, toApply));
2139
- return;
2140
- }
2141
- toApply(uischema);
2142
- };
2143
-
2144
2196
  const createAjv = (options) => {
2145
2197
  const ajv = new Ajv({
2146
2198
  allErrors: true,
@@ -2276,6 +2328,40 @@ const SET_TRANSLATOR = 'jsonforms/SET_TRANSLATOR';
2276
2328
  const UPDATE_I18N = 'jsonforms/UPDATE_I18N';
2277
2329
  const ADD_DEFAULT_DATA = 'jsonforms/ADD_DEFAULT_DATA';
2278
2330
  const REMOVE_DEFAULT_DATA = 'jsonforms/REMOVE_DEFAULT_DATA';
2331
+ const isUpdateArrayContext = (context) => {
2332
+ if (!('type' in context)) {
2333
+ return false;
2334
+ }
2335
+ if (typeof context.type !== 'string') {
2336
+ return false;
2337
+ }
2338
+ switch (context.type) {
2339
+ case 'ADD': {
2340
+ return ('values' in context &&
2341
+ Array.isArray(context.values) &&
2342
+ context.values.length > 0);
2343
+ }
2344
+ case 'REMOVE': {
2345
+ return ('indices' in context &&
2346
+ Array.isArray(context.indices) &&
2347
+ context.indices.length > 0 &&
2348
+ context.indices.every((i) => typeof i === 'number'));
2349
+ }
2350
+ case 'MOVE': {
2351
+ return ('moves' in context &&
2352
+ Array.isArray(context.moves) &&
2353
+ context.moves.length > 0 &&
2354
+ context.moves.every((m) => typeof m === 'object' &&
2355
+ m !== null &&
2356
+ 'from' in m &&
2357
+ 'to' in m &&
2358
+ typeof m.from === 'number' &&
2359
+ typeof m.to === 'number'));
2360
+ }
2361
+ default:
2362
+ return false;
2363
+ }
2364
+ };
2279
2365
  const init = (data, schema = generateJsonSchema(data), uischema, options) => ({
2280
2366
  type: INIT,
2281
2367
  data,
@@ -2303,10 +2389,11 @@ const setAjv = (ajv) => ({
2303
2389
  type: SET_AJV,
2304
2390
  ajv,
2305
2391
  });
2306
- const update = (path, updater) => ({
2392
+ const update = (path, updater, context) => ({
2307
2393
  type: UPDATE_DATA,
2308
2394
  path,
2309
2395
  updater,
2396
+ context,
2310
2397
  });
2311
2398
  const updateErrors = (errors) => ({
2312
2399
  type: UPDATE_ERRORS,
@@ -2400,6 +2487,7 @@ var index = /*#__PURE__*/Object.freeze({
2400
2487
  UPDATE_I18N: UPDATE_I18N,
2401
2488
  ADD_DEFAULT_DATA: ADD_DEFAULT_DATA,
2402
2489
  REMOVE_DEFAULT_DATA: REMOVE_DEFAULT_DATA,
2490
+ isUpdateArrayContext: isUpdateArrayContext,
2403
2491
  init: init,
2404
2492
  updateCore: updateCore,
2405
2493
  registerDefaultData: registerDefaultData,
@@ -2427,5 +2515,5 @@ const Helpers = {
2427
2515
  convertToValidClassName,
2428
2516
  };
2429
2517
 
2430
- export { ADD_CELL, ADD_DEFAULT_DATA, ADD_RENDERER, ADD_UI_SCHEMA, index as Actions, ArrayTranslationEnum, CombinatorTranslationEnum, Draft4, Generate, Helpers, INIT, NOT_APPLICABLE, Paths, REMOVE_CELL, REMOVE_DEFAULT_DATA, REMOVE_RENDERER, REMOVE_UI_SCHEMA, Resolve, RuleEffect, Runtime, SET_AJV, SET_CONFIG, SET_LOCALE, SET_SCHEMA, SET_TRANSLATOR, SET_UISCHEMA, SET_VALIDATION_MODE, index$1 as Test, UPDATE_CORE, UPDATE_DATA, UPDATE_ERRORS, UPDATE_I18N, VALIDATE, addI18nKeyToPrefix, and, arrayDefaultTranslations, categorizationHasCategory, cellReducer, clearAllIds, combinatorDefaultTranslations, compose, compose as composePaths, composeWithUi, computeLabel, configReducer, controlDefaultProps, convertDateToString, convertToValidClassName, coreReducer, createAjv, createCleanLabel, createCombinatorRenderInfos, createControlElement, createDefaultValue, createId, createLabelDescriptionFrom, decode, defaultDataReducer, defaultDateFormat, defaultDateTimeFormat, defaultErrorTranslator, defaultJsonFormsI18nState, defaultMapDispatchToControlProps, defaultMapStateToEnumCellProps, defaultMiddleware, defaultTimeFormat, defaultTranslator, deriveLabelForUISchemaElement, deriveTypes, encode, enumToEnumOptionMapper, errorAt, errorsAt, evalEnablement, evalVisibility, extractAjv, extractData, extractDefaultData, extractDefaults, extractSchema, extractUiSchema, fetchErrorTranslator, fetchLocale, fetchTranslator, findAllRefs, findMatchingUISchema, findUISchema, formatErrorMessage, formatIs, generateDefaultUISchema, generateJsonSchema, getAjv, getArrayTranslations, getCells, getCombinatorTranslations, getCombinedErrorMessage, getConfig, getControlPath, getData, getDefaultData, getErrorAt, getErrorTranslator, getFirstPrimitiveProp, getI18nKey, getI18nKeyPrefix, getI18nKeyPrefixBySchema, getLocale, getRenderers, getSchema, getSubErrorsAt, getTranslator, getUISchemas, getUiSchema, hasCategory, hasEnableRule, hasOption, hasShowRule, hasType, i18nReducer, init, isAllOfControl, isAnyOfControl, isArrayObjectControl, isBooleanControl, isCategorization, isCategory, isControl, isDateControl, isDateTimeControl, isDescriptionHidden, isEnabled, isEnumControl, isGroup, isInherentlyEnabled, isIntegerControl, isInternationalized, isLabelable, isLabeled, isLayout, isMultiLineControl, isNumberControl, isNumberFormatControl, isObjectArray, isObjectArrayControl, isObjectArrayWithNesting, isObjectControl, isOneOfControl, isOneOfEnumControl, isOneOfEnumSchema, isPrimitiveArrayControl, isRangeControl, isScopable, isScoped, isStringControl, isTimeControl, isVisible, iterateSchema, jsonFormsReducerConfig, layoutDefaultProps, mapDispatchToArrayControlProps, mapDispatchToCellProps, mapDispatchToControlProps, mapDispatchToMultiEnumProps, mapStateToAllOfProps, mapStateToAnyOfProps, mapStateToArrayControlProps, mapStateToArrayLayoutProps, mapStateToCellProps, mapStateToCombinatorRendererProps, mapStateToControlProps, mapStateToControlWithDetailProps, mapStateToDispatchCellProps, mapStateToEnumControlProps, mapStateToJsonFormsRendererProps, mapStateToLabelProps, mapStateToLayoutProps, mapStateToMasterListItemProps, mapStateToMultiEnumControlProps, mapStateToOneOfEnumCellProps, mapStateToOneOfEnumControlProps, mapStateToOneOfProps, moveDown, moveUp, not, oneOfToEnumOptionMapper, optionIs, or, rankWith, registerCell, registerDefaultData, registerRenderer, registerUISchema, removeId, rendererReducer, resolveData, resolveSchema, schemaMatches, schemaSubPathMatches, schemaTypeIs, scopeEndIs, scopeEndsWith, setAjv, setConfig, setLocale, setReadonly, setSchema, setTranslator, setUISchema, setValidationMode, showAsRequired, subErrorsAt, toDataPath, toDataPathSegments, transformPathToI18nPrefix, uiTypeIs, uischemaRegistryReducer, unregisterCell, unregisterDefaultData, unregisterRenderer, unregisterUISchema, unsetReadonly, update, updateCore, updateErrors, updateI18n, validate, withIncreasedRank };
2518
+ export { ADD_CELL, ADD_DEFAULT_DATA, ADD_RENDERER, ADD_UI_SCHEMA, index as Actions, ArrayTranslationEnum, CombinatorTranslationEnum, Draft4, Generate, Helpers, INIT, NOT_APPLICABLE, Paths, REMOVE_CELL, REMOVE_DEFAULT_DATA, REMOVE_RENDERER, REMOVE_UI_SCHEMA, Resolve, RuleEffect, Runtime, SET_AJV, SET_CONFIG, SET_LOCALE, SET_SCHEMA, SET_TRANSLATOR, SET_UISCHEMA, SET_VALIDATION_MODE, index$1 as Test, UPDATE_CORE, UPDATE_DATA, UPDATE_ERRORS, UPDATE_I18N, VALIDATE, addI18nKeyToPrefix, and, arrayDefaultTranslations, categorizationHasCategory, cellReducer, clearAllIds, combinatorDefaultTranslations, compose, compose as composePaths, composeWithUi, computeChildLabel, computeLabel, configReducer, controlDefaultProps, convertDateToString, convertToValidClassName, coreReducer, createAjv, createCleanLabel, createCombinatorRenderInfos, createControlElement, createDefaultValue, createId, createLabelDescriptionFrom, decode, defaultDataReducer, defaultDateFormat, defaultDateTimeFormat, defaultErrorTranslator, defaultJsonFormsI18nState, defaultMapDispatchToControlProps, defaultMapStateToEnumCellProps, defaultMiddleware, defaultTimeFormat, defaultTranslator, deriveLabelForUISchemaElement, deriveTypes, encode, enumToEnumOptionMapper, errorAt, errorsAt, evalEnablement, evalVisibility, extractAjv, extractData, extractDefaultData, extractDefaults, extractSchema, extractUiSchema, fetchErrorTranslator, fetchLocale, fetchTranslator, findAllRefs, findMatchingUISchema, findUISchema, findUiControl, formatErrorMessage, formatIs, generateDefaultUISchema, generateJsonSchema, getAjv, getArrayTranslations, getCells, getCombinatorTranslations, getCombinedErrorMessage, getConfig, getControlPath, getData, getDefaultData, getErrorAt, getErrorTranslator, getFirstPrimitiveProp, getI18nKey, getI18nKeyPrefix, getI18nKeyPrefixBySchema, getLocale, getPropPath, getRenderers, getSchema, getSubErrorsAt, getTranslator, getUISchemas, getUiSchema, hasCategory, hasEnableRule, hasOption, hasShowRule, hasType, i18nReducer, init, isAllOfControl, isAnyOfControl, isArrayObjectControl, isBooleanControl, isCategorization, isCategory, isControl, isControlElement, isDateControl, isDateTimeControl, isDescriptionHidden, isEnabled, isEnumControl, isEnumSchema, isGroup, isInherentlyEnabled, isIntegerControl, isInternationalized, isLabelable, isLabeled, isLayout, isMultiLineControl, isNumberControl, isNumberFormatControl, isObjectArray, isObjectArrayControl, isObjectArrayWithNesting, isObjectControl, isOneOfControl, isOneOfEnumControl, isOneOfEnumSchema, isPrimitiveArrayControl, isRangeControl, isScopable, isScoped, isStringControl, isTimeControl, isUpdateArrayContext, isVisible, iterateSchema, jsonFormsReducerConfig, layoutDefaultProps, mapDispatchToArrayControlProps, mapDispatchToCellProps, mapDispatchToControlProps, mapDispatchToMultiEnumProps, mapStateToAllOfProps, mapStateToAnyOfProps, mapStateToArrayControlProps, mapStateToArrayLayoutProps, mapStateToCellProps, mapStateToCombinatorRendererProps, mapStateToControlProps, mapStateToControlWithDetailProps, mapStateToDispatchCellProps, mapStateToEnumControlProps, mapStateToJsonFormsRendererProps, mapStateToLabelProps, mapStateToLayoutProps, mapStateToMasterListItemProps, mapStateToMultiEnumControlProps, mapStateToOneOfEnumCellProps, mapStateToOneOfEnumControlProps, mapStateToOneOfProps, moveDown, moveUp, not, oneOfToEnumOptionMapper, optionIs, or, rankWith, registerCell, registerDefaultData, registerRenderer, registerUISchema, removeId, rendererReducer, resolveData, resolveSchema, schemaMatches, schemaSubPathMatches, schemaTypeIs, scopeEndIs, scopeEndsWith, setAjv, setConfig, setLocale, setReadonly, setSchema, setTranslator, setUISchema, setValidationMode, showAsRequired, subErrorsAt, toDataPath, toDataPathSegments, transformPathToI18nPrefix, uiTypeIs, uischemaRegistryReducer, unregisterCell, unregisterDefaultData, unregisterRenderer, unregisterUISchema, unsetReadonly, update, updateCore, updateErrors, updateI18n, validate, withIncreasedRank };
2431
2519
  //# sourceMappingURL=jsonforms-core.esm.js.map