@chobantonov/jsonforms-react-shadcn-renderers 0.0.0-stage → 0.1.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.
- package/LICENSE +21 -0
- package/README.md +157 -2
- package/lib/additional/LabelRenderer.d.ts +4 -0
- package/lib/additional/index.d.ts +1 -0
- package/lib/cells/BooleanCell.d.ts +3 -0
- package/lib/cells/Cell.d.ts +3 -0
- package/lib/cells/CompositeCell.d.ts +9 -0
- package/lib/cells/CompositeDetailDialog.d.ts +3 -0
- package/lib/cells/EnumCell.d.ts +3 -0
- package/lib/cells/NumberCell.d.ts +3 -0
- package/lib/cells/NumberFormatCell.d.ts +4 -0
- package/lib/cells/TextCell.d.ts +3 -0
- package/lib/cells/asCell.d.ts +12 -0
- package/lib/cells/index.d.ts +7 -0
- package/lib/complex/AdditionalProperties.d.ts +4 -0
- package/lib/complex/AnyOfRenderer.d.ts +7 -0
- package/lib/complex/ArrayChoicesControl.d.ts +6 -0
- package/lib/complex/ArrayControlRenderer.d.ts +4 -0
- package/lib/complex/ArrayPanelToggle.d.ts +6 -0
- package/lib/complex/CollectionPager.d.ts +5 -0
- package/lib/complex/CombinatorSwitchDialog.d.ts +8 -0
- package/lib/complex/DeleteDialog.d.ts +6 -0
- package/lib/complex/DetailDialogContent.d.ts +7 -0
- package/lib/complex/ErrorIndicator.d.ts +7 -0
- package/lib/complex/MixedRenderer.d.ts +6 -0
- package/lib/complex/ObjectRenderer.d.ts +3 -0
- package/lib/complex/OneOfRenderer.d.ts +5 -0
- package/lib/complex/RecursiveTreeRenderer.d.ts +4 -0
- package/lib/complex/RowDetailFrame.d.ts +16 -0
- package/lib/complex/ScalarCompositionRenderer.d.ts +4 -0
- package/lib/complex/TupleAdditionalItems.d.ts +4 -0
- package/lib/complex/TupleControlRenderer.d.ts +6 -0
- package/lib/complex/TupleField.d.ts +4 -0
- package/lib/complex/array/ArrayItemPanels.d.ts +18 -0
- package/lib/complex/array/ArrayItemViewProps.d.ts +11 -0
- package/lib/complex/array/ArrayListDetail.d.ts +9 -0
- package/lib/complex/array/ArrayTable.d.ts +21 -0
- package/lib/complex/index.d.ts +10 -0
- package/lib/complex/mixed/JsonTypeIcon.d.ts +5 -0
- package/lib/complex/mixed/MixedWidgets.d.ts +22 -0
- package/lib/complex/mixed/useConfirmation.d.ts +6 -0
- package/lib/components/ClearValueButton.d.ts +12 -0
- package/lib/components/ScrollRegion.d.ts +4 -0
- package/lib/components/TimePicker.d.ts +13 -0
- package/lib/components/index.d.ts +2 -0
- package/lib/controls/AnyOfStringOrEnumControl.d.ts +4 -0
- package/lib/controls/BooleanControl.d.ts +4 -0
- package/lib/controls/BooleanToggleControl.d.ts +4 -0
- package/lib/controls/ChoiceCards.d.ts +1 -0
- package/lib/controls/DateControl.d.ts +4 -0
- package/lib/controls/DateTimeControl.d.ts +4 -0
- package/lib/controls/EnumControl.d.ts +5 -0
- package/lib/controls/InputControl.d.ts +30 -0
- package/lib/controls/IntegerControl.d.ts +4 -0
- package/lib/controls/NumberControl.d.ts +4 -0
- package/lib/controls/OneOfEnumControl.d.ts +3 -0
- package/lib/controls/PasswordControl.d.ts +9 -0
- package/lib/controls/RadioGroupControl.d.ts +4 -0
- package/lib/controls/SliderControl.d.ts +4 -0
- package/lib/controls/TextControl.d.ts +4 -0
- package/lib/controls/TimeControl.d.ts +4 -0
- package/lib/controls/index.d.ts +15 -0
- package/lib/index.d.ts +6 -0
- package/lib/jsonforms-react-shadcn.cjs.js +3 -0
- package/lib/jsonforms-react-shadcn.cjs.js.map +1 -0
- package/lib/jsonforms-react-shadcn.esm.js +4040 -0
- package/lib/jsonforms-react-shadcn.esm.js.map +1 -0
- package/lib/layouts/CategorizationLayout.d.ts +7 -0
- package/lib/layouts/GroupLayout.d.ts +4 -0
- package/lib/layouts/HorizontalLayout.d.ts +4 -0
- package/lib/layouts/Layout.d.ts +5 -0
- package/lib/layouts/ValidationIndicator.d.ts +4 -0
- package/lib/layouts/VerticalLayout.d.ts +4 -0
- package/lib/layouts/index.d.ts +4 -0
- package/lib/renderers.d.ts +8 -0
- package/lib/util/dateTime.d.ts +12 -0
- package/lib/util/groupState.d.ts +1 -0
- package/lib/util/useTemporalControl.d.ts +28 -0
- package/package.json +86 -3
- package/src/additional/LabelRenderer.tsx +8 -0
- package/src/additional/index.ts +1 -0
- package/src/cells/BooleanCell.tsx +26 -0
- package/src/cells/Cell.tsx +101 -0
- package/src/cells/CompositeCell.tsx +275 -0
- package/src/cells/CompositeDetailDialog.tsx +79 -0
- package/src/cells/EnumCell.tsx +74 -0
- package/src/cells/NumberCell.tsx +51 -0
- package/src/cells/NumberFormatCell.tsx +47 -0
- package/src/cells/TextCell.tsx +40 -0
- package/src/cells/asCell.tsx +79 -0
- package/src/cells/index.ts +7 -0
- package/src/complex/AdditionalProperties.tsx +453 -0
- package/src/complex/AnyOfRenderer.tsx +341 -0
- package/src/complex/ArrayChoicesControl.tsx +303 -0
- package/src/complex/ArrayControlRenderer.tsx +400 -0
- package/src/complex/ArrayPanelToggle.tsx +33 -0
- package/src/complex/CollectionPager.tsx +76 -0
- package/src/complex/CombinatorSwitchDialog.tsx +43 -0
- package/src/complex/DeleteDialog.tsx +41 -0
- package/src/complex/DetailDialogContent.tsx +84 -0
- package/src/complex/ErrorIndicator.tsx +136 -0
- package/src/complex/MixedRenderer.tsx +262 -0
- package/src/complex/ObjectRenderer.tsx +43 -0
- package/src/complex/OneOfRenderer.tsx +14 -0
- package/src/complex/RecursiveTreeRenderer.tsx +20 -0
- package/src/complex/RowDetailFrame.tsx +158 -0
- package/src/complex/ScalarCompositionRenderer.tsx +14 -0
- package/src/complex/TupleAdditionalItems.tsx +94 -0
- package/src/complex/TupleControlRenderer.tsx +54 -0
- package/src/complex/TupleField.tsx +41 -0
- package/src/complex/array/ArrayItemPanels.tsx +217 -0
- package/src/complex/array/ArrayItemViewProps.ts +15 -0
- package/src/complex/array/ArrayListDetail.tsx +165 -0
- package/src/complex/array/ArrayTable.tsx +535 -0
- package/src/complex/index.ts +16 -0
- package/src/complex/mixed/JsonTypeIcon.tsx +41 -0
- package/src/complex/mixed/MixedWidgets.tsx +326 -0
- package/src/complex/mixed/useConfirmation.tsx +34 -0
- package/src/components/ClearValueButton.tsx +56 -0
- package/src/components/ScrollRegion.tsx +27 -0
- package/src/components/TimePicker.tsx +250 -0
- package/src/components/index.ts +2 -0
- package/src/controls/AnyOfStringOrEnumControl.tsx +44 -0
- package/src/controls/BooleanControl.tsx +67 -0
- package/src/controls/BooleanToggleControl.tsx +58 -0
- package/src/controls/ChoiceCards.tsx +9 -0
- package/src/controls/DateControl.tsx +240 -0
- package/src/controls/DateTimeControl.tsx +187 -0
- package/src/controls/EnumControl.tsx +159 -0
- package/src/controls/InputControl.tsx +258 -0
- package/src/controls/IntegerControl.tsx +14 -0
- package/src/controls/NumberControl.tsx +14 -0
- package/src/controls/OneOfEnumControl.tsx +13 -0
- package/src/controls/PasswordControl.tsx +216 -0
- package/src/controls/RadioGroupControl.tsx +72 -0
- package/src/controls/SliderControl.tsx +77 -0
- package/src/controls/TextControl.tsx +50 -0
- package/src/controls/TimeControl.tsx +135 -0
- package/src/controls/index.ts +19 -0
- package/src/index.tsx +10 -0
- package/src/layouts/CategorizationLayout.tsx +248 -0
- package/src/layouts/GroupLayout.tsx +210 -0
- package/src/layouts/HorizontalLayout.tsx +12 -0
- package/src/layouts/Layout.tsx +62 -0
- package/src/layouts/ValidationIndicator.tsx +40 -0
- package/src/layouts/VerticalLayout.tsx +12 -0
- package/src/layouts/index.ts +4 -0
- package/src/renderers.ts +225 -0
- package/src/styles.css +482 -0
- package/src/tailwind-theme.cjs +42 -0
- package/src/theme.css +43 -0
- package/src/util/dateTime.ts +80 -0
- package/src/util/groupState.ts +5 -0
- package/src/util/useTemporalControl.ts +160 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import {
|
|
3
|
+
ControlProps,
|
|
4
|
+
JsonSchema,
|
|
5
|
+
and,
|
|
6
|
+
rankWith,
|
|
7
|
+
schemaMatches,
|
|
8
|
+
uiTypeIs,
|
|
9
|
+
} from '@jsonforms/core';
|
|
10
|
+
import { ShadcnInputControl } from './InputControl';
|
|
11
|
+
|
|
12
|
+
export const anyOfStringOrEnumControlTester = rankWith(
|
|
13
|
+
5,
|
|
14
|
+
and(
|
|
15
|
+
uiTypeIs('Control'),
|
|
16
|
+
schemaMatches((schema) => {
|
|
17
|
+
const branches = schema.anyOf as JsonSchema[] | undefined;
|
|
18
|
+
return (
|
|
19
|
+
Array.isArray(branches) &&
|
|
20
|
+
branches.every(
|
|
21
|
+
(branch) =>
|
|
22
|
+
(!branch.type || branch.type === 'string') &&
|
|
23
|
+
(!branch.enum ||
|
|
24
|
+
branch.enum.every((value) => typeof value === 'string')),
|
|
25
|
+
) &&
|
|
26
|
+
branches.some((branch) => branch.type === 'string' && !branch.enum) &&
|
|
27
|
+
branches.some((branch) => Array.isArray(branch.enum))
|
|
28
|
+
);
|
|
29
|
+
}),
|
|
30
|
+
),
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
export const ShadcnAnyOfStringOrEnumControl = (props: ControlProps) => (
|
|
34
|
+
<ShadcnInputControl
|
|
35
|
+
{...props}
|
|
36
|
+
suggestions={Array.from(
|
|
37
|
+
new Set(
|
|
38
|
+
(props.schema.anyOf ?? [])
|
|
39
|
+
.flatMap((branch) => branch.enum ?? [])
|
|
40
|
+
.filter((value): value is string => typeof value === 'string'),
|
|
41
|
+
),
|
|
42
|
+
)}
|
|
43
|
+
/>
|
|
44
|
+
);
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { useI18n } from '@chobantonov/jsonforms-react-renderer-common/locale/translate';
|
|
2
|
+
import {
|
|
3
|
+
ControlProps,
|
|
4
|
+
isBooleanControl,
|
|
5
|
+
RankedTester,
|
|
6
|
+
rankWith,
|
|
7
|
+
} from '@jsonforms/core';
|
|
8
|
+
import React from 'react';
|
|
9
|
+
import { InputShell, makeId } from './InputControl';
|
|
10
|
+
import { Checkbox } from '@jsonforms-react-shadcn-ui/checkbox';
|
|
11
|
+
|
|
12
|
+
function BooleanValueLabel({ value }: { value: unknown }) {
|
|
13
|
+
const t = useI18n();
|
|
14
|
+
return (
|
|
15
|
+
<span>
|
|
16
|
+
{typeof value === 'boolean'
|
|
17
|
+
? t(value ? 'boolean.yes' : 'boolean.no')
|
|
18
|
+
: ''}
|
|
19
|
+
</span>
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const ShadcnBooleanControl = ({
|
|
24
|
+
config,
|
|
25
|
+
uischema,
|
|
26
|
+
schema,
|
|
27
|
+
data,
|
|
28
|
+
description,
|
|
29
|
+
enabled,
|
|
30
|
+
errors,
|
|
31
|
+
handleChange,
|
|
32
|
+
label,
|
|
33
|
+
path,
|
|
34
|
+
required,
|
|
35
|
+
readonly,
|
|
36
|
+
visible,
|
|
37
|
+
}: ControlProps) => {
|
|
38
|
+
if (!visible) return null;
|
|
39
|
+
const id = makeId(path, label);
|
|
40
|
+
|
|
41
|
+
return (
|
|
42
|
+
<InputShell
|
|
43
|
+
path={path}
|
|
44
|
+
schema={schema}
|
|
45
|
+
config={config}
|
|
46
|
+
uischema={uischema}
|
|
47
|
+
id={id}
|
|
48
|
+
label={label}
|
|
49
|
+
required={required}
|
|
50
|
+
description={description}
|
|
51
|
+
errors={errors}
|
|
52
|
+
>
|
|
53
|
+
<label className='shadcn-jsonforms-checkbox'>
|
|
54
|
+
<Checkbox
|
|
55
|
+
className='shadcn-jsonforms-checkbox-control'
|
|
56
|
+
id={id}
|
|
57
|
+
disabled={!enabled || readonly}
|
|
58
|
+
checked={typeof data === 'boolean' ? data : 'indeterminate'}
|
|
59
|
+
onCheckedChange={(checked) => handleChange(path, checked === true)}
|
|
60
|
+
/>
|
|
61
|
+
<BooleanValueLabel value={data} />
|
|
62
|
+
</label>
|
|
63
|
+
</InputShell>
|
|
64
|
+
);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export const booleanControlTester: RankedTester = rankWith(2, isBooleanControl);
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { useI18n } from '@chobantonov/jsonforms-react-renderer-common/locale/translate';
|
|
3
|
+
import {
|
|
4
|
+
ControlProps,
|
|
5
|
+
and,
|
|
6
|
+
isBooleanControl,
|
|
7
|
+
optionIs,
|
|
8
|
+
rankWith,
|
|
9
|
+
} from '@jsonforms/core';
|
|
10
|
+
import { Switch } from '@jsonforms-react-shadcn-ui/switch';
|
|
11
|
+
import { InputShell, makeId } from './InputControl';
|
|
12
|
+
|
|
13
|
+
export const ShadcnBooleanToggleControl = (props: ControlProps) => {
|
|
14
|
+
const t = useI18n();
|
|
15
|
+
if (!props.visible) return null;
|
|
16
|
+
const id = makeId(props.path, props.label);
|
|
17
|
+
const committed = typeof props.data === 'boolean';
|
|
18
|
+
const stateId = `${id}-state`;
|
|
19
|
+
return (
|
|
20
|
+
<InputShell
|
|
21
|
+
{...props}
|
|
22
|
+
id={id}
|
|
23
|
+
label={props.label}
|
|
24
|
+
required={props.required}
|
|
25
|
+
description={props.description}
|
|
26
|
+
errors={props.errors}
|
|
27
|
+
>
|
|
28
|
+
<Switch
|
|
29
|
+
id={id}
|
|
30
|
+
checked={props.data === true}
|
|
31
|
+
disabled={!props.enabled || props.readonly}
|
|
32
|
+
aria-invalid={Boolean(props.errors)}
|
|
33
|
+
aria-describedby={
|
|
34
|
+
[
|
|
35
|
+
props.errors ? `${id}-errors` : undefined,
|
|
36
|
+
committed ? undefined : stateId,
|
|
37
|
+
]
|
|
38
|
+
.filter(Boolean)
|
|
39
|
+
.join(' ') || undefined
|
|
40
|
+
}
|
|
41
|
+
onCheckedChange={(checked) => props.handleChange(props.path, checked)}
|
|
42
|
+
/>
|
|
43
|
+
{!committed && (
|
|
44
|
+
<span id={stateId} className='sr-only'>
|
|
45
|
+
{t(
|
|
46
|
+
props.data === undefined || props.data === null
|
|
47
|
+
? 'boolean.notSet'
|
|
48
|
+
: 'boolean.invalid',
|
|
49
|
+
)}
|
|
50
|
+
</span>
|
|
51
|
+
)}
|
|
52
|
+
</InputShell>
|
|
53
|
+
);
|
|
54
|
+
};
|
|
55
|
+
export const booleanToggleControlTester = rankWith(
|
|
56
|
+
3,
|
|
57
|
+
and(isBooleanControl, optionIs('toggle', true)),
|
|
58
|
+
);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { createChoiceCards } from '@chobantonov/jsonforms-react-renderer-common/composition/choiceCards';
|
|
2
|
+
import { useConfirmation } from '../complex/mixed/useConfirmation';
|
|
3
|
+
export const ChoiceCards = createChoiceCards(useConfirmation, () => ({
|
|
4
|
+
border: 'hsl(var(--border, 214.3 31.8% 91.4%))',
|
|
5
|
+
selected: 'hsl(var(--primary, 222.2 47.4% 11.2%))',
|
|
6
|
+
focus: 'hsl(var(--ring, 222.2 84% 4.9%))',
|
|
7
|
+
background: 'hsl(var(--card, 0 0% 100%))',
|
|
8
|
+
error: 'hsl(var(--destructive, 0 84.2% 60.2%))',
|
|
9
|
+
}));
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { resolveIntlLocale } from '@chobantonov/jsonforms-react-renderer-common/locale/intl';
|
|
2
|
+
import { useI18n } from '@chobantonov/jsonforms-react-renderer-common/locale/translate';
|
|
3
|
+
import { useTemporalControl } from '../util/useTemporalControl';
|
|
4
|
+
import {
|
|
5
|
+
ControlProps,
|
|
6
|
+
isDateControl,
|
|
7
|
+
RankedTester,
|
|
8
|
+
rankWith,
|
|
9
|
+
} from '@jsonforms/core';
|
|
10
|
+
import { CalendarIcon, X } from 'lucide-react';
|
|
11
|
+
import React from 'react';
|
|
12
|
+
import { format } from 'date-fns/format';
|
|
13
|
+
import { parse } from 'date-fns/parse';
|
|
14
|
+
import { isValid } from 'date-fns/isValid';
|
|
15
|
+
import { useJsonForms } from '@jsonforms/react';
|
|
16
|
+
import {
|
|
17
|
+
Select,
|
|
18
|
+
SelectTrigger,
|
|
19
|
+
SelectValue,
|
|
20
|
+
SelectContent,
|
|
21
|
+
SelectItem,
|
|
22
|
+
} from '@jsonforms-react-shadcn-ui/select';
|
|
23
|
+
import { Button } from '@jsonforms-react-shadcn-ui/button';
|
|
24
|
+
import { Calendar } from '@jsonforms-react-shadcn-ui/calendar';
|
|
25
|
+
import {
|
|
26
|
+
Popover,
|
|
27
|
+
PopoverContent,
|
|
28
|
+
PopoverTrigger,
|
|
29
|
+
} from '@jsonforms-react-shadcn-ui/popover';
|
|
30
|
+
import { formatDisplayDate } from '../util/dateTime';
|
|
31
|
+
import { InputShell, makeId } from './InputControl';
|
|
32
|
+
|
|
33
|
+
export const ShadcnDateControl = (props: ControlProps) => {
|
|
34
|
+
const text = useI18n();
|
|
35
|
+
const {
|
|
36
|
+
data,
|
|
37
|
+
description,
|
|
38
|
+
enabled,
|
|
39
|
+
errors,
|
|
40
|
+
handleChange,
|
|
41
|
+
label,
|
|
42
|
+
path,
|
|
43
|
+
required,
|
|
44
|
+
uischema,
|
|
45
|
+
visible,
|
|
46
|
+
} = props;
|
|
47
|
+
const temporal = useTemporalControl(props, 'date');
|
|
48
|
+
const ctx = useJsonForms();
|
|
49
|
+
const options = { ...props.config, ...uischema.options };
|
|
50
|
+
const datePattern = (value: string) =>
|
|
51
|
+
value.replace(/\[([^\]]*)\]|YYYY|YY|DD|D/g, (token, literal) =>
|
|
52
|
+
literal !== undefined
|
|
53
|
+
? "'" + literal + "'"
|
|
54
|
+
: { YYYY: 'yyyy', YY: 'yy', DD: 'dd', D: 'd' }[token]!,
|
|
55
|
+
);
|
|
56
|
+
const saveFormat =
|
|
57
|
+
typeof options.dateSaveFormat === 'string'
|
|
58
|
+
? datePattern(options.dateSaveFormat)
|
|
59
|
+
: 'yyyy-MM-dd';
|
|
60
|
+
const selectedDate =
|
|
61
|
+
typeof data === 'string'
|
|
62
|
+
? parse(data, saveFormat, new Date(2000, 0, 1))
|
|
63
|
+
: undefined;
|
|
64
|
+
const selected =
|
|
65
|
+
selectedDate && isValid(selectedDate) ? selectedDate : undefined;
|
|
66
|
+
const monthYearOnly =
|
|
67
|
+
Array.isArray(options.views) &&
|
|
68
|
+
options.views.includes('month') &&
|
|
69
|
+
options.views.includes('year') &&
|
|
70
|
+
!options.views.includes('day') &&
|
|
71
|
+
!options.views.includes('date');
|
|
72
|
+
const [initialDate] = React.useState(() => new Date());
|
|
73
|
+
const [draft, setDraft] = React.useState(selected ?? initialDate);
|
|
74
|
+
const [open, setOpen] = React.useState(false);
|
|
75
|
+
const [calendarYear] = React.useState(() => new Date().getFullYear());
|
|
76
|
+
|
|
77
|
+
if (!visible) return null;
|
|
78
|
+
|
|
79
|
+
const id = makeId(path, label);
|
|
80
|
+
|
|
81
|
+
const disabledDates = temporal.disabledDate;
|
|
82
|
+
const { min, max } = temporal;
|
|
83
|
+
const { placeholder } = temporal;
|
|
84
|
+
|
|
85
|
+
return (
|
|
86
|
+
<InputShell
|
|
87
|
+
{...props}
|
|
88
|
+
id={id}
|
|
89
|
+
label={label}
|
|
90
|
+
required={required}
|
|
91
|
+
description={description}
|
|
92
|
+
errors={errors}
|
|
93
|
+
>
|
|
94
|
+
<div className='group relative w-full'>
|
|
95
|
+
<Popover
|
|
96
|
+
open={open}
|
|
97
|
+
onOpenChange={(value) => {
|
|
98
|
+
if (value) setDraft(selected ?? new Date());
|
|
99
|
+
setOpen(value);
|
|
100
|
+
}}
|
|
101
|
+
>
|
|
102
|
+
<PopoverTrigger asChild>
|
|
103
|
+
<Button
|
|
104
|
+
id={id}
|
|
105
|
+
type='button'
|
|
106
|
+
variant='outline'
|
|
107
|
+
disabled={!enabled || props.readonly}
|
|
108
|
+
autoFocus={uischema.options?.focus === true}
|
|
109
|
+
aria-invalid={!!errors}
|
|
110
|
+
className='w-full justify-start pr-10 text-left font-normal data-[empty=true]:text-muted-foreground'
|
|
111
|
+
data-empty={!selected}
|
|
112
|
+
>
|
|
113
|
+
<CalendarIcon className='h-4 w-4' />
|
|
114
|
+
{selected
|
|
115
|
+
? typeof options.dateFormat === 'string'
|
|
116
|
+
? format(selected, datePattern(options.dateFormat))
|
|
117
|
+
: formatDisplayDate(selected, ctx.i18n?.locale)
|
|
118
|
+
: placeholder}
|
|
119
|
+
</Button>
|
|
120
|
+
</PopoverTrigger>
|
|
121
|
+
<PopoverContent align='start' className='w-auto p-0'>
|
|
122
|
+
{monthYearOnly ? (
|
|
123
|
+
<div className='space-y-4 p-3'>
|
|
124
|
+
<div className='flex gap-2'>
|
|
125
|
+
<Select
|
|
126
|
+
value={String(draft.getMonth())}
|
|
127
|
+
onValueChange={(value) =>
|
|
128
|
+
setDraft(new Date(draft.getFullYear(), Number(value), 1))
|
|
129
|
+
}
|
|
130
|
+
>
|
|
131
|
+
<SelectTrigger
|
|
132
|
+
aria-label={text('temporal.month')}
|
|
133
|
+
className='w-36'
|
|
134
|
+
>
|
|
135
|
+
<SelectValue />
|
|
136
|
+
</SelectTrigger>
|
|
137
|
+
<SelectContent>
|
|
138
|
+
{Array.from({ length: 12 }, (_, month) => (
|
|
139
|
+
<SelectItem key={month} value={String(month)}>
|
|
140
|
+
{new Intl.DateTimeFormat(
|
|
141
|
+
resolveIntlLocale(ctx.i18n?.locale),
|
|
142
|
+
{
|
|
143
|
+
month: 'long',
|
|
144
|
+
},
|
|
145
|
+
).format(new Date(2000, month, 1))}
|
|
146
|
+
</SelectItem>
|
|
147
|
+
))}
|
|
148
|
+
</SelectContent>
|
|
149
|
+
</Select>
|
|
150
|
+
<Select
|
|
151
|
+
value={String(draft.getFullYear())}
|
|
152
|
+
onValueChange={(value) =>
|
|
153
|
+
setDraft(new Date(Number(value), draft.getMonth(), 1))
|
|
154
|
+
}
|
|
155
|
+
>
|
|
156
|
+
<SelectTrigger
|
|
157
|
+
aria-label={text('temporal.year')}
|
|
158
|
+
className='w-24'
|
|
159
|
+
>
|
|
160
|
+
<SelectValue />
|
|
161
|
+
</SelectTrigger>
|
|
162
|
+
<SelectContent>
|
|
163
|
+
{Array.from(
|
|
164
|
+
{
|
|
165
|
+
length:
|
|
166
|
+
(max?.getFullYear() ?? calendarYear + 100) -
|
|
167
|
+
(min?.getFullYear() ?? calendarYear - 100) +
|
|
168
|
+
1,
|
|
169
|
+
},
|
|
170
|
+
(_, index) =>
|
|
171
|
+
(min?.getFullYear() ?? calendarYear - 100) + index,
|
|
172
|
+
).map((year) => (
|
|
173
|
+
<SelectItem key={year} value={String(year)}>
|
|
174
|
+
{year}
|
|
175
|
+
</SelectItem>
|
|
176
|
+
))}
|
|
177
|
+
</SelectContent>
|
|
178
|
+
</Select>
|
|
179
|
+
</div>
|
|
180
|
+
<div className='flex justify-end gap-2'>
|
|
181
|
+
<Button
|
|
182
|
+
variant='outline'
|
|
183
|
+
type='button'
|
|
184
|
+
onClick={() => setOpen(false)}
|
|
185
|
+
>
|
|
186
|
+
{text('control.cancel')}
|
|
187
|
+
</Button>
|
|
188
|
+
<Button
|
|
189
|
+
type='button'
|
|
190
|
+
disabled={!!((min && draft < min) || (max && draft > max))}
|
|
191
|
+
onClick={() => {
|
|
192
|
+
temporal.commit(draft);
|
|
193
|
+
setOpen(false);
|
|
194
|
+
}}
|
|
195
|
+
>
|
|
196
|
+
{text('control.apply')}
|
|
197
|
+
</Button>
|
|
198
|
+
</div>
|
|
199
|
+
</div>
|
|
200
|
+
) : (
|
|
201
|
+
<Calendar
|
|
202
|
+
mode='single'
|
|
203
|
+
selected={selected}
|
|
204
|
+
defaultMonth={selected}
|
|
205
|
+
onSelect={(date) => {
|
|
206
|
+
if (date) temporal.commit(date);
|
|
207
|
+
else if (enabled && !props.readonly)
|
|
208
|
+
handleChange(path, undefined);
|
|
209
|
+
setOpen(false);
|
|
210
|
+
}}
|
|
211
|
+
disabled={disabledDates}
|
|
212
|
+
captionLayout='dropdown'
|
|
213
|
+
startMonth={min ?? new Date(calendarYear - 100, 0)}
|
|
214
|
+
endMonth={max ?? new Date(calendarYear + 20, 11)}
|
|
215
|
+
/>
|
|
216
|
+
)}
|
|
217
|
+
</PopoverContent>
|
|
218
|
+
</Popover>
|
|
219
|
+
{selected &&
|
|
220
|
+
enabled &&
|
|
221
|
+
!props.readonly &&
|
|
222
|
+
(uischema.options?.clearable ?? props.config?.clearable ?? true) ? (
|
|
223
|
+
<Button
|
|
224
|
+
type='button'
|
|
225
|
+
variant='ghost'
|
|
226
|
+
size='icon'
|
|
227
|
+
className='absolute end-1 top-1 h-8 w-8 opacity-0 transition-opacity group-hover:opacity-100 group-focus-within:opacity-100'
|
|
228
|
+
aria-label={text('temporal.clearDate')}
|
|
229
|
+
title={text('temporal.clearDate')}
|
|
230
|
+
onClick={() => handleChange(path, undefined)}
|
|
231
|
+
>
|
|
232
|
+
<X className='h-4 w-4' />
|
|
233
|
+
</Button>
|
|
234
|
+
) : null}
|
|
235
|
+
</div>
|
|
236
|
+
</InputShell>
|
|
237
|
+
);
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
export const dateControlTester: RankedTester = rankWith(4, isDateControl);
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { useJsonForms } from '@jsonforms/react';
|
|
2
|
+
import { useI18n } from '@chobantonov/jsonforms-react-renderer-common/locale/translate';
|
|
3
|
+
import { useTemporalControl } from '../util/useTemporalControl';
|
|
4
|
+
import {
|
|
5
|
+
ControlProps,
|
|
6
|
+
isDateTimeControl,
|
|
7
|
+
RankedTester,
|
|
8
|
+
rankWith,
|
|
9
|
+
} from '@jsonforms/core';
|
|
10
|
+
import { CalendarClock, X } from 'lucide-react';
|
|
11
|
+
import React from 'react';
|
|
12
|
+
import { TimePicker } from '../components/TimePicker';
|
|
13
|
+
import { Button } from '@jsonforms-react-shadcn-ui/button';
|
|
14
|
+
import { Calendar } from '@jsonforms-react-shadcn-ui/calendar';
|
|
15
|
+
import {
|
|
16
|
+
Popover,
|
|
17
|
+
PopoverContent,
|
|
18
|
+
PopoverTrigger,
|
|
19
|
+
} from '@jsonforms-react-shadcn-ui/popover';
|
|
20
|
+
import {
|
|
21
|
+
TimeValue,
|
|
22
|
+
formatDisplayDateTime,
|
|
23
|
+
formatTimeValue,
|
|
24
|
+
parseTimeValue,
|
|
25
|
+
} from '../util/dateTime';
|
|
26
|
+
import { InputShell, makeId } from './InputControl';
|
|
27
|
+
|
|
28
|
+
export const ShadcnDateTimeControl = (props: ControlProps) => {
|
|
29
|
+
const text = useI18n();
|
|
30
|
+
const locale = useJsonForms().i18n?.locale;
|
|
31
|
+
const {
|
|
32
|
+
data,
|
|
33
|
+
description,
|
|
34
|
+
enabled,
|
|
35
|
+
errors,
|
|
36
|
+
handleChange,
|
|
37
|
+
label,
|
|
38
|
+
path,
|
|
39
|
+
required,
|
|
40
|
+
uischema,
|
|
41
|
+
visible,
|
|
42
|
+
} = props;
|
|
43
|
+
const temporal = useTemporalControl(props, 'dateTime');
|
|
44
|
+
const [open, setOpen] = React.useState(false);
|
|
45
|
+
const [calendarYear] = React.useState(() => new Date().getFullYear());
|
|
46
|
+
const [draftDate, setDraftDate] = React.useState<Date>();
|
|
47
|
+
const [draftTime, setDraftTime] = React.useState<TimeValue>();
|
|
48
|
+
|
|
49
|
+
if (!visible) return null;
|
|
50
|
+
|
|
51
|
+
const id = makeId(path, label);
|
|
52
|
+
const selectedDate = temporal.selected?.toDate();
|
|
53
|
+
const { selectedTime, ampm, useSeconds, placeholder } = temporal;
|
|
54
|
+
|
|
55
|
+
const updateValue = (date: Date | undefined, time: TimeValue | undefined) => {
|
|
56
|
+
if (date && time) {
|
|
57
|
+
const value = new Date(date);
|
|
58
|
+
value.setHours(time.hours, time.minutes, time.seconds, 0);
|
|
59
|
+
temporal.commit(value);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
return (
|
|
64
|
+
<InputShell
|
|
65
|
+
{...props}
|
|
66
|
+
id={id}
|
|
67
|
+
label={label}
|
|
68
|
+
required={required}
|
|
69
|
+
description={description}
|
|
70
|
+
errors={errors}
|
|
71
|
+
>
|
|
72
|
+
<div className='group relative w-full'>
|
|
73
|
+
<Popover
|
|
74
|
+
open={open}
|
|
75
|
+
onOpenChange={(nextOpen) => {
|
|
76
|
+
setOpen(nextOpen);
|
|
77
|
+
if (nextOpen) {
|
|
78
|
+
setDraftDate(selectedDate);
|
|
79
|
+
setDraftTime(selectedTime);
|
|
80
|
+
}
|
|
81
|
+
}}
|
|
82
|
+
>
|
|
83
|
+
<PopoverTrigger asChild>
|
|
84
|
+
<Button
|
|
85
|
+
id={id}
|
|
86
|
+
type='button'
|
|
87
|
+
variant='outline'
|
|
88
|
+
disabled={!enabled || props.readonly}
|
|
89
|
+
autoFocus={uischema.options?.focus === true}
|
|
90
|
+
aria-invalid={!!errors}
|
|
91
|
+
className='w-full justify-start pr-10 text-left font-normal data-[empty=true]:text-muted-foreground'
|
|
92
|
+
data-empty={!selectedDate || !selectedTime}
|
|
93
|
+
>
|
|
94
|
+
<CalendarClock className='h-4 w-4' />
|
|
95
|
+
{selectedDate && selectedTime
|
|
96
|
+
? typeof temporal.options.dateTimeFormat === 'string'
|
|
97
|
+
? temporal.displayText
|
|
98
|
+
: formatDisplayDateTime(
|
|
99
|
+
selectedDate,
|
|
100
|
+
selectedTime,
|
|
101
|
+
ampm,
|
|
102
|
+
locale,
|
|
103
|
+
)
|
|
104
|
+
: typeof data === 'string' && data
|
|
105
|
+
? data
|
|
106
|
+
: placeholder}
|
|
107
|
+
</Button>
|
|
108
|
+
</PopoverTrigger>
|
|
109
|
+
<PopoverContent align='start' className='w-auto p-2'>
|
|
110
|
+
<Calendar
|
|
111
|
+
mode='single'
|
|
112
|
+
disabled={temporal.disabledDate}
|
|
113
|
+
selected={draftDate}
|
|
114
|
+
defaultMonth={draftDate ?? selectedDate}
|
|
115
|
+
captionLayout='dropdown'
|
|
116
|
+
startMonth={new Date(calendarYear - 100, 0)}
|
|
117
|
+
endMonth={new Date(calendarYear + 20, 11)}
|
|
118
|
+
onSelect={(date) => {
|
|
119
|
+
const nextTime =
|
|
120
|
+
draftTime ??
|
|
121
|
+
selectedTime ??
|
|
122
|
+
({ hours: 0, minutes: 0, seconds: 0 } as TimeValue);
|
|
123
|
+
setDraftDate(date);
|
|
124
|
+
setDraftTime(nextTime);
|
|
125
|
+
updateValue(date, nextTime);
|
|
126
|
+
}}
|
|
127
|
+
/>
|
|
128
|
+
<div className='border-t p-2 pt-3'>
|
|
129
|
+
<TimePicker
|
|
130
|
+
value={
|
|
131
|
+
draftTime
|
|
132
|
+
? formatTimeValue(draftTime, true)
|
|
133
|
+
: typeof data === 'string'
|
|
134
|
+
? data
|
|
135
|
+
: undefined
|
|
136
|
+
}
|
|
137
|
+
useSeconds={useSeconds}
|
|
138
|
+
showHours={temporal.showHours}
|
|
139
|
+
showMinutes={temporal.showMinutes}
|
|
140
|
+
ampm={ampm}
|
|
141
|
+
disabled={!enabled || props.readonly}
|
|
142
|
+
isAllowed={(time) => {
|
|
143
|
+
const date = new Date(
|
|
144
|
+
draftDate ?? selectedDate ?? new Date(),
|
|
145
|
+
);
|
|
146
|
+
date.setHours(time.hours, time.minutes, time.seconds, 0);
|
|
147
|
+
return temporal.allowed(date);
|
|
148
|
+
}}
|
|
149
|
+
onChange={(value) => {
|
|
150
|
+
const nextTime = parseTimeValue(value);
|
|
151
|
+
setDraftTime(nextTime);
|
|
152
|
+
updateValue(draftDate ?? selectedDate, nextTime);
|
|
153
|
+
}}
|
|
154
|
+
/>
|
|
155
|
+
</div>
|
|
156
|
+
<div className='flex justify-end p-2 pt-0'>
|
|
157
|
+
<Button type='button' size='sm' onClick={() => setOpen(false)}>
|
|
158
|
+
{text('control.done')}
|
|
159
|
+
</Button>
|
|
160
|
+
</div>
|
|
161
|
+
</PopoverContent>
|
|
162
|
+
</Popover>
|
|
163
|
+
{selectedDate &&
|
|
164
|
+
enabled &&
|
|
165
|
+
!props.readonly &&
|
|
166
|
+
(uischema.options?.clearable ?? props.config?.clearable ?? true) ? (
|
|
167
|
+
<Button
|
|
168
|
+
type='button'
|
|
169
|
+
variant='ghost'
|
|
170
|
+
size='icon'
|
|
171
|
+
className='absolute end-1 top-1 h-8 w-8 opacity-0 transition-opacity group-hover:opacity-100 group-focus-within:opacity-100'
|
|
172
|
+
aria-label={text('temporal.clearDateTime')}
|
|
173
|
+
title={text('temporal.clearDateTime')}
|
|
174
|
+
onClick={() => handleChange(path, undefined)}
|
|
175
|
+
>
|
|
176
|
+
<X className='h-4 w-4' />
|
|
177
|
+
</Button>
|
|
178
|
+
) : null}
|
|
179
|
+
</div>
|
|
180
|
+
</InputShell>
|
|
181
|
+
);
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
export const dateTimeControlTester: RankedTester = rankWith(
|
|
185
|
+
4,
|
|
186
|
+
isDateTimeControl,
|
|
187
|
+
);
|