@abgov/jsonforms-components 2.40.0 → 2.41.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.
package/index.esm.js CHANGED
@@ -5,7 +5,7 @@ import { GoAFormItem, GoAInput, GoATextArea, GoACallout, GoAInputDate, GoAInputD
5
5
  import styled from 'styled-components';
6
6
  import axios from 'axios';
7
7
  import get$1 from 'lodash/get';
8
- import { rankWith, isStringControl, and, optionIs, uiTypeIs, isDateControl, isNumberControl, isIntegerControl, isDateTimeControl, isTimeControl, isControl, isEnumControl, isBooleanControl, schemaMatches, getAjv, isVisible, isEnabled, deriveLabelForUISchemaElement, schemaTypeIs, formatIs, createDefaultValue, or, isObjectArrayControl, isPrimitiveArrayControl, Paths, toDataPath, hasType, isCategorization, isLayout } from '@jsonforms/core';
8
+ import { rankWith, isStringControl, and, optionIs, uiTypeIs, isDateControl, isNumberControl, isIntegerControl, isDateTimeControl, isTimeControl, isControl as isControl$1, isEnumControl, isBooleanControl, schemaMatches, getAjv, isVisible, isEnabled, deriveLabelForUISchemaElement, schemaTypeIs, formatIs, createDefaultValue, or, isObjectArrayControl, isPrimitiveArrayControl, Paths, toDataPath, Resolve, hasType, isCategorization, isLayout } from '@jsonforms/core';
9
9
  import { withJsonFormsControlProps, withJsonFormsEnumProps, withTranslateProps, JsonFormsDispatch, useJsonForms, withJsonFormsLayoutProps, withJsonFormsArrayLayoutProps, withJsonFormsAllOfProps, withJsonFormsCellProps } from '@jsonforms/react';
10
10
  import * as _$c from 'lodash';
11
11
  import ___default, { isEqual, isEmpty as isEmpty$1, isObject as isObject$f } from 'lodash';
@@ -14,7 +14,7 @@ import isEmpty from 'lodash/isEmpty';
14
14
  import range from 'lodash/range';
15
15
  import pluralize from 'pluralize';
16
16
  import { evaluateSync, compileSync } from '@mdx-js/mdx';
17
- import { Parser } from 'expr-eval';
17
+ import { Parser } from 'expr-eval-fork';
18
18
  import Ajv from 'ajv';
19
19
  import addErrors from 'ajv-errors';
20
20
  import addFormats from 'ajv-formats';
@@ -6426,7 +6426,7 @@ const enumControl = props => {
6426
6426
  };
6427
6427
  const GoAEnumControlTester = rankWith(4, (uischema, schema, context) => {
6428
6428
  var _a;
6429
- return (schema === null || schema === void 0 ? void 0 : schema.type) === 'object' && isControl(uischema) && ((_a = uischema === null || uischema === void 0 ? void 0 : uischema.options) === null || _a === void 0 ? void 0 : _a.format) === 'enum' || isEnumControl(uischema, schema, context);
6429
+ return (schema === null || schema === void 0 ? void 0 : schema.type) === 'object' && isControl$1(uischema) && ((_a = uischema === null || uischema === void 0 ? void 0 : uischema.options) === null || _a === void 0 ? void 0 : _a.format) === 'enum' || isEnumControl(uischema, schema, context);
6430
6430
  });
6431
6431
  const GoAEnumControl = withJsonFormsEnumProps(withTranslateProps(enumControl), true);
6432
6432
 
@@ -6601,8 +6601,32 @@ const CheckBoxGroupDiv = styled.div(_t$8 || (_t$8 = _$8`
6601
6601
  }
6602
6602
  `));
6603
6603
 
6604
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
6605
+ const getEnumOptions = schema => {
6606
+ var _a, _b;
6607
+ if (((_a = schema === null || schema === void 0 ? void 0 : schema.items) === null || _a === void 0 ? void 0 : _a.oneOf) && Array.isArray(schema.items.oneOf)) {
6608
+ return schema.items.oneOf;
6609
+ }
6610
+ if ((schema === null || schema === void 0 ? void 0 : schema.oneOf) && Array.isArray(schema.oneOf)) {
6611
+ return schema.oneOf;
6612
+ }
6613
+ const newSchema = schema;
6614
+ return (schema === null || schema === void 0 ? void 0 : schema.enum) || ((_b = newSchema === null || newSchema === void 0 ? void 0 : newSchema.items) === null || _b === void 0 ? void 0 : _b.enum) || [];
6615
+ };
6616
+ const getOptionValue = option => {
6617
+ return typeof option === 'string' ? option : option.const;
6618
+ };
6619
+ const getOptionLabel = option => {
6620
+ if (typeof option === 'string') {
6621
+ return option;
6622
+ }
6623
+ return option.title || option.const;
6624
+ };
6625
+ const getOptionDescription = option => {
6626
+ return typeof option === 'string' ? undefined : option.description;
6627
+ };
6604
6628
  const CheckboxGroup = props => {
6605
- var _a, _b, _c;
6629
+ var _a, _b;
6606
6630
  const {
6607
6631
  data,
6608
6632
  id,
@@ -6615,20 +6639,23 @@ const CheckboxGroup = props => {
6615
6639
  label,
6616
6640
  enabled
6617
6641
  } = props;
6618
- const newSchema = schema;
6619
- const enumData = (schema === null || schema === void 0 ? void 0 : schema.enum) || ((_a = newSchema === null || newSchema === void 0 ? void 0 : newSchema.items) === null || _a === void 0 ? void 0 : _a.enum) || [];
6642
+ const enumOptions = getEnumOptions(schema);
6620
6643
  const appliedUiSchemaOptions = merge({}, config, props.uischema.options, options);
6621
6644
  return jsx(Checkboxes, {
6622
- orientation: ((_b = uischema.options) === null || _b === void 0 ? void 0 : _b.orientation) ? (_c = uischema.options) === null || _c === void 0 ? void 0 : _c.orientation : 'vertical',
6645
+ orientation: ((_a = uischema.options) === null || _a === void 0 ? void 0 : _a.orientation) ? (_b = uischema.options) === null || _b === void 0 ? void 0 : _b.orientation : 'vertical',
6623
6646
  testId: `${label || id}-jsonforms-checkboxes`,
6624
- children: enumData.map(enumValue => {
6647
+ children: enumOptions.map(enumOption => {
6648
+ const enumValue = getOptionValue(enumOption);
6649
+ const enumLabel = getOptionLabel(enumOption);
6650
+ const enumDescription = getOptionDescription(enumOption);
6625
6651
  return jsx(GoACheckbox, Object.assign({
6626
6652
  name: enumValue,
6627
6653
  disabled: !enabled,
6628
6654
  checked: data ? data.includes(enumValue) : false,
6629
6655
  value: `${enumValue}`
6630
6656
  }, appliedUiSchemaOptions, {
6631
- text: enumValue,
6657
+ text: enumLabel,
6658
+ description: enumDescription,
6632
6659
  testId: `${enumValue}-checkbox`,
6633
6660
  onChange: (name, value) => {
6634
6661
  let newData = Array.isArray(data) ? [...data] : [];
@@ -6639,7 +6666,7 @@ const CheckboxGroup = props => {
6639
6666
  }
6640
6667
  handleChange(path, newData.length === 0 ? undefined : newData);
6641
6668
  }
6642
- }));
6669
+ }), enumValue);
6643
6670
  })
6644
6671
  });
6645
6672
  };
@@ -6975,7 +7002,7 @@ const GoAEmailInput = props => {
6975
7002
  });
6976
7003
  };
6977
7004
  const GoAEmailControl = props => jsx(GoAEmailInput, Object.assign({}, props));
6978
- const GoAEmailControlTester = rankWith(4, and(isControl, schemaMatches(schema => schema.format === 'email')));
7005
+ const GoAEmailControlTester = rankWith(4, and(isControl$1, schemaMatches(schema => schema.format === 'email')));
6979
7006
  const GoAInputEmailControl = withJsonFormsControlProps(GoAEmailControl);
6980
7007
 
6981
7008
  let _$7 = t => t,
@@ -11067,6 +11094,25 @@ const getItemsTitle = schema => {
11067
11094
  }
11068
11095
  return undefined;
11069
11096
  };
11097
+ function isControl(uischema) {
11098
+ return uischema.type === 'Control';
11099
+ }
11100
+ function hasElements$1(uischema) {
11101
+ return Array.isArray(uischema.elements);
11102
+ }
11103
+ function hasDetail(uischema) {
11104
+ var _a;
11105
+ return uischema.type === 'ListWithDetail' && typeof ((_a = uischema.options) === null || _a === void 0 ? void 0 : _a.detail) === 'object';
11106
+ }
11107
+ function normalizeLabel(label) {
11108
+ if (typeof label === 'string') {
11109
+ return label;
11110
+ }
11111
+ if (typeof label === 'object' && label !== null) {
11112
+ return label.text;
11113
+ }
11114
+ return;
11115
+ }
11070
11116
  // eslint-disable-next-line
11071
11117
  const extractScopesFromUISchema = uischema => {
11072
11118
  var _a, _b, _c, _d, _e, _f;
@@ -11362,6 +11408,84 @@ function orderRowData(rowData, detailUiSchema) {
11362
11408
  }
11363
11409
  return ordered;
11364
11410
  }
11411
+ function getEffectiveInstancePath(error) {
11412
+ var _a;
11413
+ if (error.keyword === 'required' && ((_a = error.params) === null || _a === void 0 ? void 0 : _a.missingProperty)) {
11414
+ return `${error.instancePath}/${error.params.missingProperty}`;
11415
+ }
11416
+ return error.instancePath;
11417
+ }
11418
+ function prettify(prop) {
11419
+ return prop.replace(/([A-Z])/g, ' $1').replace(/[_-]/g, ' ').replace(/^./, c => c.toUpperCase());
11420
+ }
11421
+ function resolveLabel(instancePath, schema, uischema) {
11422
+ const prop = instancePath.split('/').filter(Boolean).pop();
11423
+ const scope = `#/properties/${prop}`;
11424
+ if (uischema) {
11425
+ const uiLabel = findControlLabel(uischema, scope);
11426
+ if (uiLabel) {
11427
+ return uiLabel;
11428
+ }
11429
+ }
11430
+ const schemaPath = ajvPathToSchemaPath(instancePath);
11431
+ const resolvedSchema = Resolve.schema(schema, schemaPath, schema);
11432
+ if (resolvedSchema === null || resolvedSchema === void 0 ? void 0 : resolvedSchema.title) {
11433
+ return resolvedSchema.title;
11434
+ }
11435
+ return prettify(prop || 'Unknown field');
11436
+ }
11437
+ function ajvPathToSchemaPath(instancePath) {
11438
+ const parts = instancePath.split('/').filter(Boolean);
11439
+ const result = [];
11440
+ for (const part of parts) {
11441
+ if (!isNaN(Number(part))) {
11442
+ // array index → items
11443
+ result.push('items');
11444
+ } else {
11445
+ result.push('properties', part);
11446
+ }
11447
+ }
11448
+ return result.join('.');
11449
+ }
11450
+ function findControlLabel(uischema, scope) {
11451
+ if (isControl(uischema) && uischema.scope === scope) {
11452
+ return normalizeLabel(uischema.label);
11453
+ }
11454
+ if (hasDetail(uischema)) {
11455
+ const found = findControlLabel(uischema.options.detail, scope);
11456
+ if (found) return found;
11457
+ }
11458
+ if (hasElements$1(uischema)) {
11459
+ for (const el of uischema.elements) {
11460
+ const found = findControlLabel(el, scope);
11461
+ if (found) return found;
11462
+ }
11463
+ }
11464
+ return;
11465
+ }
11466
+ function humanizeAjvError(error, schema, uischema) {
11467
+ var _a;
11468
+ const path = getEffectiveInstancePath(error);
11469
+ const label = resolveLabel(path, schema, uischema);
11470
+ switch (error.keyword) {
11471
+ case 'required':
11472
+ return `${label} is required`;
11473
+ case 'minLength':
11474
+ return `${label} must be at least ${error.params.limit} characters`;
11475
+ case 'maxLength':
11476
+ return `${label} must be at most ${error.params.limit} characters`;
11477
+ case 'format':
11478
+ return `${label} must be a valid ${error.params.format}`;
11479
+ case 'minimum':
11480
+ return `${label} must be ≥ ${error.params.limit}`;
11481
+ case 'maximum':
11482
+ return `${label} must be ≤ ${error.params.limit}`;
11483
+ case 'type':
11484
+ return `${label} must be a ${error.params.type}`;
11485
+ default:
11486
+ return `${label} ${(_a = error.message) !== null && _a !== void 0 ? _a : ''}`.trim();
11487
+ }
11488
+ }
11365
11489
  const MainTab = ({
11366
11490
  childPath,
11367
11491
  rowIndex,
@@ -11384,10 +11508,10 @@ const MainTab = ({
11384
11508
  const rowErrors = (_b = core === null || core === void 0 ? void 0 : core.errors) === null || _b === void 0 ? void 0 : _b.filter(e => {
11385
11509
  const base = `/${childPath.replace(/\./g, '/')}`;
11386
11510
  return e.instancePath === base || e.instancePath.startsWith(base + '/');
11387
- }).map(e => {
11388
- var _a, _b;
11389
- return ((_b = (_a = e === null || e === void 0 ? void 0 : e.message) === null || _a === void 0 ? void 0 : _a.trim) === null || _b === void 0 ? void 0 : _b.call(_a)) || '';
11390
- }).filter(msg => msg.length > 0).map((msg, index, arr) => `${msg}${index < arr.length - 1 ? ', ' : ''}`);
11511
+ }).filter(e => {
11512
+ var _a;
11513
+ return (((_a = e === null || e === void 0 ? void 0 : e.message) === null || _a === void 0 ? void 0 : _a.length) || 0) > 0;
11514
+ }).map(e => humanizeAjvError(e, core.schema, core.uischema)).map((msg, index, arr) => `${msg}${index < arr.length - 1 ? ', ' : ''}`);
11391
11515
  return jsx("div", {
11392
11516
  "data-testid": `object-array-main-item-${rowIndex}`,
11393
11517
  children: (rowErrors === null || rowErrors === void 0 ? void 0 : rowErrors.length) ? jsx(GoAFormItem, {
@@ -13901,11 +14025,11 @@ const getUISchemaErrors = (uiSchema, schema) => {
13901
14025
  if (isEmptyObject(uiSchema)) {
13902
14026
  return '';
13903
14027
  }
13904
- if (isControl(uiSchema) && (schema === null || schema === void 0 ? void 0 : schema.type) === 'object' && ((_a = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema.options) === null || _a === void 0 ? void 0 : _a.format) === 'enum' && ((_b = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema.options) === null || _b === void 0 ? void 0 : _b.register) === undefined) {
14028
+ if (isControl$1(uiSchema) && (schema === null || schema === void 0 ? void 0 : schema.type) === 'object' && ((_a = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema.options) === null || _a === void 0 ? void 0 : _a.format) === 'enum' && ((_b = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema.options) === null || _b === void 0 ? void 0 : _b.register) === undefined) {
13905
14029
  return errMissingRegister;
13906
14030
  }
13907
14031
  // Check control elements
13908
- if (isControl(uiSchema) && hasType(uiSchema, 'Control')) {
14032
+ if (isControl$1(uiSchema) && hasType(uiSchema, 'Control')) {
13909
14033
  if (!isScopedPrefixed(uiSchema.scope)) {
13910
14034
  return errMalformedScope(uiSchema.scope);
13911
14035
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abgov/jsonforms-components",
3
- "version": "2.40.0",
3
+ "version": "2.41.1",
4
4
  "license": "Apache-2.0",
5
5
  "description": "Government of Alberta - React renderers for JSON Forms based on the design system.",
6
6
  "repository": "https://github.com/GovAlta/adsp-monorepo",
@@ -13,7 +13,7 @@
13
13
  "ajv-errors": "^3.0.0",
14
14
  "ajv-formats": "^3.0.1",
15
15
  "@mdx-js/mdx": "^3.1.0",
16
- "expr-eval": "^2.0.2",
16
+ "expr-eval-fork": "^3.0.1",
17
17
  "pluralize": "^8.0.0"
18
18
  },
19
19
  "dependencies": {
@@ -1,5 +1,6 @@
1
1
  import React from 'react';
2
- import { ArrayLayoutProps, ControlElement, JsonSchema, JsonFormsRendererRegistryEntry, JsonFormsCellRendererRegistryEntry, ArrayTranslations, Layout } from '@jsonforms/core';
2
+ import type { ErrorObject } from 'ajv';
3
+ import { ArrayLayoutProps, ControlElement, JsonSchema, JsonFormsRendererRegistryEntry, JsonFormsCellRendererRegistryEntry, ArrayTranslations, UISchemaElement, Layout } from '@jsonforms/core';
3
4
  import { WithDeleteDialogSupport } from './DeleteDialog';
4
5
  export type ObjectArrayControlProps = ArrayLayoutProps & WithDeleteDialogSupport;
5
6
  export declare const extractScopesFromUISchema: (uischema: any) => string[];
@@ -33,6 +34,8 @@ interface NonEmptyRowProps {
33
34
  uischema: ControlElement;
34
35
  listTitle?: string;
35
36
  }
37
+ export declare function findControlLabel(uischema: UISchemaElement, scope: string): string | undefined;
38
+ export declare function humanizeAjvError(error: ErrorObject, schema: JsonSchema, uischema?: UISchemaElement): string;
36
39
  export declare const NonEmptyList: React.MemoExoticComponent<({ childPath, schema, enabled, cells, uischema, }: NonEmptyRowProps & WithDeleteDialogSupport) => import("react/jsx-runtime").JSX.Element>;
37
40
  interface ListWithDetailControlProps extends ObjectArrayControlProps {
38
41
  currentTab: number;