@sphereon/ui-components.ssi-react 0.3.1-unstable.20 → 0.3.1-unstable.22
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.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CSSProperties,
|
|
1
|
+
import { CSSProperties, ReactElement } from 'react';
|
|
2
2
|
import { JsonFormsCellRendererRegistryEntry, JsonFormsRendererRegistryEntry, JsonSchema, Middleware, UISchemaElement, ValidationMode } from '@jsonforms/core';
|
|
3
3
|
import { JSONFormState } from '../../../types';
|
|
4
4
|
import Ajv from 'ajv';
|
|
@@ -16,5 +16,5 @@ type Props<DataType = Record<any, any>> = {
|
|
|
16
16
|
readonly?: boolean;
|
|
17
17
|
config?: any;
|
|
18
18
|
};
|
|
19
|
-
declare const FormView:
|
|
19
|
+
declare const FormView: (props: Props) => ReactElement;
|
|
20
20
|
export default FormView;
|
|
@@ -1,12 +1,57 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useState } from 'react';
|
|
2
3
|
import { JsonForms } from '@jsonforms/react';
|
|
3
4
|
import { materialCells } from '@jsonforms/material-renderers';
|
|
4
5
|
import { jsonFormsMaterialRenderers } from '../../../renders/jsonFormsRenders';
|
|
6
|
+
import { formatDateToISO } from '../../../helpers/date/DateHelper';
|
|
7
|
+
const initializeDefaultValues = (schema, currentData = {}) => {
|
|
8
|
+
const result = { ...currentData };
|
|
9
|
+
if (!schema.properties) {
|
|
10
|
+
return result;
|
|
11
|
+
}
|
|
12
|
+
Object.entries(schema.properties).forEach(([key, property]) => {
|
|
13
|
+
if (typeof property === 'object') {
|
|
14
|
+
if (property.const && (!(key in result) || !result[key])) {
|
|
15
|
+
result[key] = property.const;
|
|
16
|
+
}
|
|
17
|
+
if (property.default && (!(key in result) || !result[key])) {
|
|
18
|
+
if (property.format?.startsWith('date') || property.format?.startsWith('time')) {
|
|
19
|
+
result[key] = formatDateToISO(property.default, property.format);
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
result[key] = property.default;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (property.properties) {
|
|
26
|
+
if (!result[key]) {
|
|
27
|
+
result[key] = {};
|
|
28
|
+
}
|
|
29
|
+
result[key] = initializeDefaultValues(property, result[key]);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
return result;
|
|
34
|
+
};
|
|
5
35
|
const FormView = (props) => {
|
|
6
36
|
const { data, schema, uiSchema, validationMode = 'ValidateAndShow', renderers = jsonFormsMaterialRenderers, cells = materialCells, style, middleware, ajv, onFormStateChange, readonly = false, config, } = props;
|
|
37
|
+
const [formData, setFormData] = useState(data ?? {});
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
const initializedData = initializeDefaultValues(schema, formData);
|
|
40
|
+
setFormData(initializedData);
|
|
41
|
+
}, [schema]);
|
|
7
42
|
const onFormStateChanged = (state) => {
|
|
8
|
-
|
|
43
|
+
const updatedData = initializeDefaultValues(schema, state.data);
|
|
44
|
+
if (JSON.stringify(updatedData) !== JSON.stringify(state.data)) {
|
|
45
|
+
setFormData(updatedData);
|
|
46
|
+
void onFormStateChange?.({
|
|
47
|
+
...state,
|
|
48
|
+
data: updatedData,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
void onFormStateChange?.(state);
|
|
53
|
+
}
|
|
9
54
|
};
|
|
10
|
-
return (_jsx("div", { style: style, children: _jsx(JsonForms, { schema: schema, uischema: uiSchema, data:
|
|
55
|
+
return (_jsx("div", { style: style, children: _jsx(JsonForms, { schema: schema, uischema: uiSchema, data: formData, renderers: renderers, cells: cells, onChange: onFormStateChanged, validationMode: validationMode, middleware: middleware, ajv: ajv, readonly: readonly, config: config }) }));
|
|
11
56
|
};
|
|
12
57
|
export default FormView;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export const formatDate = (dateString, format = 'date-time') => {
|
|
2
|
+
const userDateTimeOpts = Intl.DateTimeFormat().resolvedOptions();
|
|
3
|
+
if (!dateString) {
|
|
4
|
+
return '';
|
|
5
|
+
}
|
|
6
|
+
const date = dateString === 'now' ? new Date() : new Date(dateString);
|
|
7
|
+
if (isNaN(date.getTime())) {
|
|
8
|
+
console.error('Invalid date:', dateString);
|
|
9
|
+
return 'Invalid date';
|
|
10
|
+
}
|
|
11
|
+
const formatOptions = {
|
|
12
|
+
timeZone: userDateTimeOpts.timeZone
|
|
13
|
+
};
|
|
14
|
+
if (format === 'date-time') {
|
|
15
|
+
formatOptions.dateStyle = 'short';
|
|
16
|
+
formatOptions.timeStyle = 'short';
|
|
17
|
+
}
|
|
18
|
+
else if (format === 'date') {
|
|
19
|
+
formatOptions.dateStyle = 'short';
|
|
20
|
+
}
|
|
21
|
+
else if (format === 'time') {
|
|
22
|
+
formatOptions.timeStyle = 'short';
|
|
23
|
+
}
|
|
24
|
+
return new Intl.DateTimeFormat(userDateTimeOpts.locale, formatOptions).format(date);
|
|
25
|
+
};
|
|
26
|
+
export const formatDateToISO = (dateString, format = 'date-time') => {
|
|
27
|
+
if (!dateString) {
|
|
28
|
+
return '';
|
|
29
|
+
}
|
|
30
|
+
const date = dateString === 'now' ? new Date() : new Date(dateString);
|
|
31
|
+
if (isNaN(date.getTime())) {
|
|
32
|
+
console.error('Invalid date:', dateString);
|
|
33
|
+
return 'Invalid date';
|
|
34
|
+
}
|
|
35
|
+
const isoString = date.toISOString();
|
|
36
|
+
if (format === 'date-time') {
|
|
37
|
+
return isoString.slice(0, 19).replace('T', ' ');
|
|
38
|
+
}
|
|
39
|
+
else if (format === 'date') {
|
|
40
|
+
return isoString.slice(0, 10);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
return isoString.slice(11, 19);
|
|
44
|
+
}
|
|
45
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sphereon/ui-components.ssi-react",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.3.1-unstable.
|
|
4
|
+
"version": "0.3.1-unstable.22+589aa0e",
|
|
5
5
|
"description": "SSI UI components for React",
|
|
6
6
|
"repository": "git@github.com:Sphereon-Opensource/UI-Components.git",
|
|
7
7
|
"author": "Sphereon <dev@sphereon.com>",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"@mui/system": "^5.15.12",
|
|
42
42
|
"@mui/x-date-pickers": "^6.19.5",
|
|
43
43
|
"@sphereon/ssi-sdk.data-store": "0.29.1-next.47",
|
|
44
|
-
"@sphereon/ui-components.core": "0.3.1-unstable.
|
|
44
|
+
"@sphereon/ui-components.core": "0.3.1-unstable.22+589aa0e",
|
|
45
45
|
"@tanstack/react-table": "^8.9.3",
|
|
46
46
|
"react-json-tree": "^0.18.0",
|
|
47
47
|
"react-loader-spinner": "5.4.5",
|
|
@@ -59,5 +59,5 @@
|
|
|
59
59
|
"peerDependencies": {
|
|
60
60
|
"react": ">= 18"
|
|
61
61
|
},
|
|
62
|
-
"gitHead": "
|
|
62
|
+
"gitHead": "589aa0e67cae7fef01bb063df03d46f02bc6c723"
|
|
63
63
|
}
|