@sphereon/ui-components.ssi-react 0.4.1-next.145 → 0.4.1-next.146
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.
|
@@ -4,19 +4,19 @@ import { JsonForms } from '@jsonforms/react';
|
|
|
4
4
|
import { materialCells } from '@jsonforms/material-renderers';
|
|
5
5
|
import { jsonFormsMaterialRenderers } from '../../../renders/jsonFormsRenders';
|
|
6
6
|
import Ajv from 'ajv';
|
|
7
|
-
import addFormats from
|
|
7
|
+
import addFormats from 'ajv-formats';
|
|
8
8
|
import { formatDate } from '../../../helpers';
|
|
9
9
|
const defaultAjv = new Ajv({ useDefaults: true });
|
|
10
10
|
addFormats(defaultAjv);
|
|
11
11
|
defaultAjv.addKeyword({
|
|
12
|
-
keyword: '
|
|
12
|
+
keyword: 'dateFormat',
|
|
13
13
|
modifying: true,
|
|
14
|
-
validate: (
|
|
14
|
+
validate: (dateFormat, data, parentSchema, dataCxt) => {
|
|
15
15
|
if (typeof data === 'string') {
|
|
16
16
|
if (!dataCxt?.parentData || dataCxt.parentDataProperty === undefined) {
|
|
17
17
|
return true;
|
|
18
18
|
}
|
|
19
|
-
const formatted = formatDate(data,
|
|
19
|
+
const formatted = formatDate(data, dateFormat);
|
|
20
20
|
if (formatted && formatted !== 'Invalid date') {
|
|
21
21
|
dataCxt.parentData[dataCxt.parentDataProperty] = formatted;
|
|
22
22
|
}
|
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
export type DateFormat = 'date-time' | 'date' | 'time' | undefined;
|
|
2
|
-
export declare const formatDate: (dateString?: string, format?: DateFormat) => string;
|
|
3
|
-
export declare const formatDateToISO: (dateString?: string, format?: DateFormat) => string;
|
|
1
|
+
export type DateFormat = 'date-time' | 'date' | 'time' | 'iso-date-time' | 'iso-date' | 'iso-time' | undefined;
|
|
2
|
+
export declare const formatDate: (dateString?: string, format?: DateFormat | string) => string;
|
|
@@ -1,45 +1,86 @@
|
|
|
1
|
+
const KNOWN_FORMATS = new Set(['date-time', 'date', 'time', 'iso-date-time', 'iso-date', 'iso-time']);
|
|
2
|
+
const customFormatTokens = {
|
|
3
|
+
yyyy: (date) => date.getFullYear().toString().padStart(4, '0'),
|
|
4
|
+
yy: (date) => date.getFullYear().toString().slice(-2).padStart(2, '0'),
|
|
5
|
+
MM: (date) => (date.getMonth() + 1).toString().padStart(2, '0'),
|
|
6
|
+
M: (date) => (date.getMonth() + 1).toString(),
|
|
7
|
+
dd: (date) => date.getDate().toString().padStart(2, '0'),
|
|
8
|
+
d: (date) => date.getDate().toString(),
|
|
9
|
+
HH: (date) => date.getHours().toString().padStart(2, '0'),
|
|
10
|
+
H: (date) => date.getHours().toString(),
|
|
11
|
+
mm: (date) => date.getMinutes().toString().padStart(2, '0'),
|
|
12
|
+
m: (date) => date.getMinutes().toString(),
|
|
13
|
+
ss: (date) => date.getSeconds().toString().padStart(2, '0'),
|
|
14
|
+
s: (date) => date.getSeconds().toString(),
|
|
15
|
+
};
|
|
16
|
+
const orderedTokens = ['yyyy', 'yy', 'MM', 'dd', 'HH', 'mm', 'ss', 'M', 'd', 'H', 'm', 's'];
|
|
17
|
+
const applyCustomFormat = (date, format) => {
|
|
18
|
+
let result = '';
|
|
19
|
+
let i = 0;
|
|
20
|
+
while (i < format.length) {
|
|
21
|
+
const char = format[i];
|
|
22
|
+
if (char === '[') {
|
|
23
|
+
const closeIdx = format.indexOf(']', i + 1);
|
|
24
|
+
if (closeIdx !== -1) {
|
|
25
|
+
result += format.slice(i + 1, closeIdx);
|
|
26
|
+
i = closeIdx + 1;
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const token = orderedTokens.find((t) => format.startsWith(t, i));
|
|
31
|
+
if (token) {
|
|
32
|
+
result += customFormatTokens[token](date);
|
|
33
|
+
i += token.length;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
result += char;
|
|
37
|
+
i += 1;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return result;
|
|
41
|
+
};
|
|
1
42
|
export const formatDate = (dateString, format = 'date-time') => {
|
|
2
|
-
const userDateTimeOpts = Intl.DateTimeFormat().resolvedOptions();
|
|
3
43
|
if (!dateString) {
|
|
4
44
|
return '';
|
|
5
45
|
}
|
|
6
|
-
|
|
46
|
+
let dateToParse = dateString;
|
|
47
|
+
if (/^\d{4}-\d{2}-\d{2}$/.test(dateString)) {
|
|
48
|
+
dateToParse += 'T00:00:00';
|
|
49
|
+
}
|
|
50
|
+
const date = dateString === 'now' ? new Date() : new Date(dateToParse);
|
|
7
51
|
if (isNaN(date.getTime())) {
|
|
8
52
|
console.error('Invalid date:', dateString);
|
|
9
53
|
return 'Invalid date';
|
|
10
54
|
}
|
|
55
|
+
const effectiveFormat = format ?? 'date-time';
|
|
56
|
+
if (effectiveFormat === 'iso-date-time')
|
|
57
|
+
return applyCustomFormat(date, 'yyyy-MM-ddTHH:mm:ss');
|
|
58
|
+
if (effectiveFormat === 'iso-date')
|
|
59
|
+
return applyCustomFormat(date, 'yyyy-MM-dd');
|
|
60
|
+
if (effectiveFormat === 'iso-time')
|
|
61
|
+
return applyCustomFormat(date, 'HH:mm:ss');
|
|
62
|
+
if (!KNOWN_FORMATS.has(effectiveFormat)) {
|
|
63
|
+
return applyCustomFormat(date, effectiveFormat);
|
|
64
|
+
}
|
|
65
|
+
const userDateTimeOpts = Intl.DateTimeFormat().resolvedOptions();
|
|
11
66
|
const formatOptions = {
|
|
12
67
|
timeZone: userDateTimeOpts.timeZone,
|
|
13
68
|
};
|
|
14
|
-
if (
|
|
69
|
+
if (effectiveFormat === 'date-time') {
|
|
15
70
|
formatOptions.dateStyle = 'short';
|
|
16
71
|
formatOptions.timeStyle = 'short';
|
|
17
72
|
}
|
|
18
|
-
else if (
|
|
73
|
+
else if (effectiveFormat === 'date') {
|
|
19
74
|
formatOptions.dateStyle = 'short';
|
|
20
75
|
}
|
|
21
|
-
else if (
|
|
76
|
+
else if (effectiveFormat === 'time') {
|
|
22
77
|
formatOptions.timeStyle = 'short';
|
|
23
78
|
}
|
|
24
|
-
|
|
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);
|
|
79
|
+
try {
|
|
80
|
+
return new Intl.DateTimeFormat(userDateTimeOpts.locale, formatOptions).format(date);
|
|
41
81
|
}
|
|
42
|
-
|
|
43
|
-
|
|
82
|
+
catch (error) {
|
|
83
|
+
console.error('Intl formatting error', error);
|
|
84
|
+
return date.toString();
|
|
44
85
|
}
|
|
45
86
|
};
|
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.4.1-next.
|
|
4
|
+
"version": "0.4.1-next.146+7771557",
|
|
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>",
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"@mui/x-date-pickers": "^8.18.0",
|
|
51
51
|
"@sphereon/ssi-sdk.data-store": "0.34.1-feature.SSISDK.45.94",
|
|
52
52
|
"@sphereon/ssi-types": "0.34.1-feature.SSISDK.45.94",
|
|
53
|
-
"@sphereon/ui-components.core": "0.4.1-next.
|
|
53
|
+
"@sphereon/ui-components.core": "0.4.1-next.146+7771557",
|
|
54
54
|
"@tanstack/react-table": "^8.9.3",
|
|
55
55
|
"ajv": "^8.17.1",
|
|
56
56
|
"ajv-formats": "^3.0.1",
|
|
@@ -72,5 +72,5 @@
|
|
|
72
72
|
"peerDependencies": {
|
|
73
73
|
"react": ">= 18"
|
|
74
74
|
},
|
|
75
|
-
"gitHead": "
|
|
75
|
+
"gitHead": "7771557388b35167616e95a1614f1d7a7cf280f1"
|
|
76
76
|
}
|