@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.
Files changed (154) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +157 -2
  3. package/lib/additional/LabelRenderer.d.ts +4 -0
  4. package/lib/additional/index.d.ts +1 -0
  5. package/lib/cells/BooleanCell.d.ts +3 -0
  6. package/lib/cells/Cell.d.ts +3 -0
  7. package/lib/cells/CompositeCell.d.ts +9 -0
  8. package/lib/cells/CompositeDetailDialog.d.ts +3 -0
  9. package/lib/cells/EnumCell.d.ts +3 -0
  10. package/lib/cells/NumberCell.d.ts +3 -0
  11. package/lib/cells/NumberFormatCell.d.ts +4 -0
  12. package/lib/cells/TextCell.d.ts +3 -0
  13. package/lib/cells/asCell.d.ts +12 -0
  14. package/lib/cells/index.d.ts +7 -0
  15. package/lib/complex/AdditionalProperties.d.ts +4 -0
  16. package/lib/complex/AnyOfRenderer.d.ts +7 -0
  17. package/lib/complex/ArrayChoicesControl.d.ts +6 -0
  18. package/lib/complex/ArrayControlRenderer.d.ts +4 -0
  19. package/lib/complex/ArrayPanelToggle.d.ts +6 -0
  20. package/lib/complex/CollectionPager.d.ts +5 -0
  21. package/lib/complex/CombinatorSwitchDialog.d.ts +8 -0
  22. package/lib/complex/DeleteDialog.d.ts +6 -0
  23. package/lib/complex/DetailDialogContent.d.ts +7 -0
  24. package/lib/complex/ErrorIndicator.d.ts +7 -0
  25. package/lib/complex/MixedRenderer.d.ts +6 -0
  26. package/lib/complex/ObjectRenderer.d.ts +3 -0
  27. package/lib/complex/OneOfRenderer.d.ts +5 -0
  28. package/lib/complex/RecursiveTreeRenderer.d.ts +4 -0
  29. package/lib/complex/RowDetailFrame.d.ts +16 -0
  30. package/lib/complex/ScalarCompositionRenderer.d.ts +4 -0
  31. package/lib/complex/TupleAdditionalItems.d.ts +4 -0
  32. package/lib/complex/TupleControlRenderer.d.ts +6 -0
  33. package/lib/complex/TupleField.d.ts +4 -0
  34. package/lib/complex/array/ArrayItemPanels.d.ts +18 -0
  35. package/lib/complex/array/ArrayItemViewProps.d.ts +11 -0
  36. package/lib/complex/array/ArrayListDetail.d.ts +9 -0
  37. package/lib/complex/array/ArrayTable.d.ts +21 -0
  38. package/lib/complex/index.d.ts +10 -0
  39. package/lib/complex/mixed/JsonTypeIcon.d.ts +5 -0
  40. package/lib/complex/mixed/MixedWidgets.d.ts +22 -0
  41. package/lib/complex/mixed/useConfirmation.d.ts +6 -0
  42. package/lib/components/ClearValueButton.d.ts +12 -0
  43. package/lib/components/ScrollRegion.d.ts +4 -0
  44. package/lib/components/TimePicker.d.ts +13 -0
  45. package/lib/components/index.d.ts +2 -0
  46. package/lib/controls/AnyOfStringOrEnumControl.d.ts +4 -0
  47. package/lib/controls/BooleanControl.d.ts +4 -0
  48. package/lib/controls/BooleanToggleControl.d.ts +4 -0
  49. package/lib/controls/ChoiceCards.d.ts +1 -0
  50. package/lib/controls/DateControl.d.ts +4 -0
  51. package/lib/controls/DateTimeControl.d.ts +4 -0
  52. package/lib/controls/EnumControl.d.ts +5 -0
  53. package/lib/controls/InputControl.d.ts +30 -0
  54. package/lib/controls/IntegerControl.d.ts +4 -0
  55. package/lib/controls/NumberControl.d.ts +4 -0
  56. package/lib/controls/OneOfEnumControl.d.ts +3 -0
  57. package/lib/controls/PasswordControl.d.ts +9 -0
  58. package/lib/controls/RadioGroupControl.d.ts +4 -0
  59. package/lib/controls/SliderControl.d.ts +4 -0
  60. package/lib/controls/TextControl.d.ts +4 -0
  61. package/lib/controls/TimeControl.d.ts +4 -0
  62. package/lib/controls/index.d.ts +15 -0
  63. package/lib/index.d.ts +6 -0
  64. package/lib/jsonforms-react-shadcn.cjs.js +3 -0
  65. package/lib/jsonforms-react-shadcn.cjs.js.map +1 -0
  66. package/lib/jsonforms-react-shadcn.esm.js +4040 -0
  67. package/lib/jsonforms-react-shadcn.esm.js.map +1 -0
  68. package/lib/layouts/CategorizationLayout.d.ts +7 -0
  69. package/lib/layouts/GroupLayout.d.ts +4 -0
  70. package/lib/layouts/HorizontalLayout.d.ts +4 -0
  71. package/lib/layouts/Layout.d.ts +5 -0
  72. package/lib/layouts/ValidationIndicator.d.ts +4 -0
  73. package/lib/layouts/VerticalLayout.d.ts +4 -0
  74. package/lib/layouts/index.d.ts +4 -0
  75. package/lib/renderers.d.ts +8 -0
  76. package/lib/util/dateTime.d.ts +12 -0
  77. package/lib/util/groupState.d.ts +1 -0
  78. package/lib/util/useTemporalControl.d.ts +28 -0
  79. package/package.json +86 -3
  80. package/src/additional/LabelRenderer.tsx +8 -0
  81. package/src/additional/index.ts +1 -0
  82. package/src/cells/BooleanCell.tsx +26 -0
  83. package/src/cells/Cell.tsx +101 -0
  84. package/src/cells/CompositeCell.tsx +275 -0
  85. package/src/cells/CompositeDetailDialog.tsx +79 -0
  86. package/src/cells/EnumCell.tsx +74 -0
  87. package/src/cells/NumberCell.tsx +51 -0
  88. package/src/cells/NumberFormatCell.tsx +47 -0
  89. package/src/cells/TextCell.tsx +40 -0
  90. package/src/cells/asCell.tsx +79 -0
  91. package/src/cells/index.ts +7 -0
  92. package/src/complex/AdditionalProperties.tsx +453 -0
  93. package/src/complex/AnyOfRenderer.tsx +341 -0
  94. package/src/complex/ArrayChoicesControl.tsx +303 -0
  95. package/src/complex/ArrayControlRenderer.tsx +400 -0
  96. package/src/complex/ArrayPanelToggle.tsx +33 -0
  97. package/src/complex/CollectionPager.tsx +76 -0
  98. package/src/complex/CombinatorSwitchDialog.tsx +43 -0
  99. package/src/complex/DeleteDialog.tsx +41 -0
  100. package/src/complex/DetailDialogContent.tsx +84 -0
  101. package/src/complex/ErrorIndicator.tsx +136 -0
  102. package/src/complex/MixedRenderer.tsx +262 -0
  103. package/src/complex/ObjectRenderer.tsx +43 -0
  104. package/src/complex/OneOfRenderer.tsx +14 -0
  105. package/src/complex/RecursiveTreeRenderer.tsx +20 -0
  106. package/src/complex/RowDetailFrame.tsx +158 -0
  107. package/src/complex/ScalarCompositionRenderer.tsx +14 -0
  108. package/src/complex/TupleAdditionalItems.tsx +94 -0
  109. package/src/complex/TupleControlRenderer.tsx +54 -0
  110. package/src/complex/TupleField.tsx +41 -0
  111. package/src/complex/array/ArrayItemPanels.tsx +217 -0
  112. package/src/complex/array/ArrayItemViewProps.ts +15 -0
  113. package/src/complex/array/ArrayListDetail.tsx +165 -0
  114. package/src/complex/array/ArrayTable.tsx +535 -0
  115. package/src/complex/index.ts +16 -0
  116. package/src/complex/mixed/JsonTypeIcon.tsx +41 -0
  117. package/src/complex/mixed/MixedWidgets.tsx +326 -0
  118. package/src/complex/mixed/useConfirmation.tsx +34 -0
  119. package/src/components/ClearValueButton.tsx +56 -0
  120. package/src/components/ScrollRegion.tsx +27 -0
  121. package/src/components/TimePicker.tsx +250 -0
  122. package/src/components/index.ts +2 -0
  123. package/src/controls/AnyOfStringOrEnumControl.tsx +44 -0
  124. package/src/controls/BooleanControl.tsx +67 -0
  125. package/src/controls/BooleanToggleControl.tsx +58 -0
  126. package/src/controls/ChoiceCards.tsx +9 -0
  127. package/src/controls/DateControl.tsx +240 -0
  128. package/src/controls/DateTimeControl.tsx +187 -0
  129. package/src/controls/EnumControl.tsx +159 -0
  130. package/src/controls/InputControl.tsx +258 -0
  131. package/src/controls/IntegerControl.tsx +14 -0
  132. package/src/controls/NumberControl.tsx +14 -0
  133. package/src/controls/OneOfEnumControl.tsx +13 -0
  134. package/src/controls/PasswordControl.tsx +216 -0
  135. package/src/controls/RadioGroupControl.tsx +72 -0
  136. package/src/controls/SliderControl.tsx +77 -0
  137. package/src/controls/TextControl.tsx +50 -0
  138. package/src/controls/TimeControl.tsx +135 -0
  139. package/src/controls/index.ts +19 -0
  140. package/src/index.tsx +10 -0
  141. package/src/layouts/CategorizationLayout.tsx +248 -0
  142. package/src/layouts/GroupLayout.tsx +210 -0
  143. package/src/layouts/HorizontalLayout.tsx +12 -0
  144. package/src/layouts/Layout.tsx +62 -0
  145. package/src/layouts/ValidationIndicator.tsx +40 -0
  146. package/src/layouts/VerticalLayout.tsx +12 -0
  147. package/src/layouts/index.ts +4 -0
  148. package/src/renderers.ts +225 -0
  149. package/src/styles.css +482 -0
  150. package/src/tailwind-theme.cjs +42 -0
  151. package/src/theme.css +43 -0
  152. package/src/util/dateTime.ts +80 -0
  153. package/src/util/groupState.ts +5 -0
  154. package/src/util/useTemporalControl.ts +160 -0
@@ -0,0 +1,159 @@
1
+ import isEqual from 'lodash/isEqual';
2
+ import {
3
+ ControlProps,
4
+ isEnumControl,
5
+ OwnPropsOfEnum,
6
+ RankedTester,
7
+ rankWith,
8
+ } from '@jsonforms/core';
9
+ import { TranslateProps } from '@jsonforms/react';
10
+ import React from 'react';
11
+ import { useI18n } from '@chobantonov/jsonforms-react-renderer-common/locale/translate';
12
+ import { Input } from '@jsonforms-react-shadcn-ui/input';
13
+ import {
14
+ ClearValueButton,
15
+ hasClearableValue,
16
+ } from '../components/ClearValueButton';
17
+ import { InputShell, makeId } from './InputControl';
18
+ import {
19
+ Select,
20
+ SelectContent,
21
+ SelectItem,
22
+ SelectTrigger,
23
+ SelectValue,
24
+ } from '@jsonforms-react-shadcn-ui/select';
25
+
26
+ function enumPresentation(
27
+ options: ControlProps['uischema']['options'],
28
+ config: ControlProps['config'],
29
+ ) {
30
+ const local = options ?? {};
31
+ const global = config ?? {};
32
+ return {
33
+ searchable: (local.autocomplete ?? global.autocomplete) === true,
34
+ clearable: local.clearable ?? global.clearable ?? true,
35
+ placeholder: local.placeholder ?? global.placeholder,
36
+ };
37
+ }
38
+
39
+ export const ShadcnEnumControl = ({
40
+ schema,
41
+ data,
42
+ config,
43
+ description,
44
+ enabled,
45
+ errors,
46
+ handleChange,
47
+ label,
48
+ options,
49
+ path,
50
+ required,
51
+ readonly,
52
+ uischema,
53
+ visible,
54
+ }: ControlProps & OwnPropsOfEnum & TranslateProps) => {
55
+ const t = useI18n();
56
+ const [query, setQuery] = React.useState('');
57
+ const { searchable, clearable, placeholder } = enumPresentation(
58
+ uischema.options,
59
+ config,
60
+ );
61
+ const choices = (options ?? [])
62
+ .map((option, index) => ({ option, index }))
63
+ .filter(
64
+ ({ option }) =>
65
+ !searchable ||
66
+ option.label.toLocaleLowerCase().includes(query.toLocaleLowerCase()),
67
+ );
68
+ if (!visible) return null;
69
+ const id = makeId(path, label);
70
+ const showClear =
71
+ clearable && enabled && !readonly && hasClearableValue(data);
72
+
73
+ return (
74
+ <InputShell
75
+ path={path}
76
+ schema={schema}
77
+ config={config}
78
+ uischema={uischema}
79
+ id={id}
80
+ label={label}
81
+ required={required}
82
+ description={description}
83
+ errors={errors}
84
+ >
85
+ <div className='group relative w-full'>
86
+ <Select
87
+ onOpenChange={() => setQuery('')}
88
+ disabled={!enabled || readonly}
89
+ value={(() => {
90
+ const index = (options ?? []).findIndex((option) =>
91
+ isEqual(option.value, data),
92
+ );
93
+ return index < 0 ? '' : `option-${index}`;
94
+ })()}
95
+ onValueChange={(value) => {
96
+ const option = (options ?? [])[
97
+ Number(value.replace('option-', ''))
98
+ ];
99
+ if (option && enabled && !readonly)
100
+ handleChange(path, option.value);
101
+ }}
102
+ >
103
+ <SelectTrigger
104
+ id={id}
105
+ className='shadcn-jsonforms-input shadcn-jsonforms-select w-full min-w-0 pr-3'
106
+ >
107
+ <SelectValue
108
+ className={showClear ? 'mr-10 min-w-0' : 'min-w-0'}
109
+ placeholder={placeholder ?? t('enum.select')}
110
+ />
111
+ </SelectTrigger>
112
+ <SelectContent className='shadcn-jsonforms-select-content'>
113
+ {searchable && (
114
+ <Input
115
+ type='search'
116
+ role='searchbox'
117
+ aria-label={t('enum.search')}
118
+ value={query}
119
+ disabled={!enabled || readonly}
120
+ onChange={(event) => setQuery(event.currentTarget.value)}
121
+ onKeyDown={(event) => {
122
+ if (
123
+ !['ArrowUp', 'ArrowDown', 'Escape', 'Tab'].includes(
124
+ event.key,
125
+ )
126
+ ) {
127
+ event.stopPropagation();
128
+ }
129
+ }}
130
+ />
131
+ )}
132
+ {searchable && choices.length === 0 && (
133
+ <div role='status'>{t('enum.noMatches')}</div>
134
+ )}
135
+ {choices.map(({ option, index }) => (
136
+ <SelectItem
137
+ className='shadcn-jsonforms-select-item'
138
+ key={index}
139
+ value={`option-${index}`}
140
+ >
141
+ {option.label}
142
+ </SelectItem>
143
+ ))}
144
+ </SelectContent>
145
+ </Select>
146
+ <ClearValueButton
147
+ clearable={clearable}
148
+ trailingControlSpace={36}
149
+ data={data}
150
+ enabled={enabled}
151
+ readonly={readonly}
152
+ onClear={() => handleChange(path, undefined)}
153
+ />
154
+ </div>
155
+ </InputShell>
156
+ );
157
+ };
158
+
159
+ export const enumControlTester: RankedTester = rankWith(2, isEnumControl);
@@ -0,0 +1,258 @@
1
+ import { useMixedScalar } from '@chobantonov/jsonforms-react-renderer-common/mixed/mixedScalar';
2
+ import { usePreTouchErrors } from '@chobantonov/jsonforms-react-renderer-common/validation/preTouchErrors';
3
+ import { useJsonForms } from '@jsonforms/react';
4
+ import {
5
+ getControlHelp,
6
+ ControlHelpProps,
7
+ } from '@chobantonov/jsonforms-react-renderer-common/controls/controlHelp';
8
+ import { ControlProps } from '@jsonforms/core';
9
+ import React, { useContext } from 'react';
10
+ import { InputAdornments } from '@chobantonov/jsonforms-react-renderer-common/controls/inputAdornments';
11
+ import { ShadcnCellMode } from '../cells/asCell';
12
+ import { ClearValueButton } from '../components/ClearValueButton';
13
+ import { Input } from '@jsonforms-react-shadcn-ui/input';
14
+
15
+ export const toStringValue = (value: unknown) =>
16
+ value === undefined ? '' : String(value);
17
+
18
+ export const makeId = (path: string, label?: string) =>
19
+ `shadcn-jsonforms-${path || label || 'root'}`.replace(/[^a-zA-Z0-9_-]/g, '-');
20
+
21
+ export const InputShell = ({
22
+ id,
23
+ label,
24
+ required,
25
+ description,
26
+ errors: rawErrors,
27
+ path = '',
28
+ schema,
29
+ children,
30
+ config,
31
+ uischema,
32
+ }: React.PropsWithChildren<
33
+ ControlHelpProps & {
34
+ config?: ControlHelpProps['config'] & { hideRequiredAsterisk?: boolean };
35
+ uischema?: ControlHelpProps['uischema'] & {
36
+ label?: ControlProps['uischema']['label'];
37
+ options?: { hideRequiredAsterisk?: boolean };
38
+ };
39
+ id: string;
40
+ path?: string;
41
+ schema?: ControlProps['schema'];
42
+ label?: string;
43
+ required?: boolean;
44
+ description?: string;
45
+ errors?: string;
46
+ }
47
+ >) => {
48
+ const ctx = useJsonForms();
49
+ const { errors, focused, onFocus, onBlur } = usePreTouchErrors({
50
+ errors: rawErrors,
51
+ path,
52
+ schema,
53
+ config: config ?? ctx.config,
54
+ uischema: uischema as ControlProps['uischema'],
55
+ });
56
+ const help = getControlHelp(
57
+ { description, errors, config: config ?? ctx.config, uischema },
58
+ focused,
59
+ );
60
+ const cell = useContext(ShadcnCellMode);
61
+ const adornmentOptions = uischema?.options as
62
+ | Record<string, unknown>
63
+ | undefined;
64
+ const adorned = (
65
+ <InputAdornments schema={schema} options={adornmentOptions}>
66
+ {children}
67
+ </InputAdornments>
68
+ );
69
+ if (cell)
70
+ return (
71
+ <div
72
+ className='shadcn-jsonforms-cell'
73
+ title={errors || undefined}
74
+ onFocusCapture={onFocus}
75
+ onBlurCapture={onBlur}
76
+ >
77
+ {adorned}
78
+ </div>
79
+ );
80
+ return (
81
+ <div
82
+ className='shadcn-jsonforms-field'
83
+ onFocusCapture={onFocus}
84
+ onBlurCapture={(event) => {
85
+ if (!event.currentTarget.contains(event.relatedTarget as Node))
86
+ onBlur();
87
+ }}
88
+ >
89
+ {label && uischema?.label !== false ? (
90
+ <label className='shadcn-jsonforms-label' htmlFor={id}>
91
+ {label}
92
+ {required &&
93
+ !(
94
+ uischema?.options?.hideRequiredAsterisk ??
95
+ (config ?? ctx.config)?.hideRequiredAsterisk
96
+ ) ? (
97
+ <span aria-hidden='true'> *</span>
98
+ ) : null}
99
+ </label>
100
+ ) : null}
101
+ {adorned}
102
+ {help && !errors ? (
103
+ <div className='shadcn-jsonforms-description'>{help}</div>
104
+ ) : null}
105
+ {errors ? (
106
+ <div
107
+ id={`${id}-errors`}
108
+ role='alert'
109
+ className='shadcn-jsonforms-error'
110
+ >
111
+ {errors}
112
+ </div>
113
+ ) : null}
114
+ </div>
115
+ );
116
+ };
117
+
118
+ export const ShadcnInputControl = ({
119
+ data,
120
+ schema,
121
+ config,
122
+ description,
123
+ enabled,
124
+ errors,
125
+ handleChange: originalHandleChange,
126
+ label,
127
+ path,
128
+ required,
129
+ readonly,
130
+ uischema,
131
+ visible,
132
+ type = 'text',
133
+ suggestions,
134
+ }: ControlProps & { type?: string; suggestions?: string[] }) => {
135
+ const keepEmptyString = useMixedScalar(path);
136
+ const handleChange = (target: string, value: any) =>
137
+ originalHandleChange(
138
+ target,
139
+ keepEmptyString && value === undefined ? '' : value,
140
+ );
141
+ if (!visible) return null;
142
+ const id = makeId(path, label);
143
+ const inputType = uischema.options?.format ?? type;
144
+
145
+ return (
146
+ <InputShell
147
+ path={path}
148
+ schema={schema}
149
+ config={config}
150
+ uischema={uischema}
151
+ id={id}
152
+ label={label}
153
+ required={required}
154
+ description={description}
155
+ errors={errors}
156
+ >
157
+ <div className='group relative w-full'>
158
+ <Input
159
+ id={id}
160
+ className='shadcn-jsonforms-input pr-10'
161
+ type={inputType}
162
+ maxLength={
163
+ (uischema.options?.restrict ?? config?.restrict)
164
+ ? schema.maxLength
165
+ : undefined
166
+ }
167
+ disabled={!enabled || readonly}
168
+ aria-invalid={Boolean(errors)}
169
+ aria-describedby={errors ? `${id}-errors` : undefined}
170
+ list={suggestions ? `${id}-suggestions` : undefined}
171
+ value={toStringValue(data)}
172
+ onChange={(event) =>
173
+ handleChange(path, event.currentTarget.value || undefined)
174
+ }
175
+ />
176
+ {suggestions && (
177
+ <datalist id={`${id}-suggestions`}>
178
+ {suggestions.map((value) => (
179
+ <option key={value} value={value} />
180
+ ))}
181
+ </datalist>
182
+ )}
183
+ <ClearValueButton
184
+ clearable={uischema.options?.clearable ?? config?.clearable ?? true}
185
+ data={data}
186
+ enabled={enabled}
187
+ readonly={readonly}
188
+ onClear={() => handleChange(path, undefined)}
189
+ />
190
+ </div>
191
+ </InputShell>
192
+ );
193
+ };
194
+
195
+ export const ShadcnNumberControl = (
196
+ props: ControlProps & { integer?: boolean },
197
+ ) => {
198
+ const {
199
+ config,
200
+ data,
201
+ enabled,
202
+ handleChange: originalHandleChange,
203
+ path,
204
+ readonly,
205
+ uischema,
206
+ visible,
207
+ } = props;
208
+ const keepSelectedType = useMixedScalar(path);
209
+ const handleChange = (target: string, value: any) =>
210
+ originalHandleChange(
211
+ target,
212
+ keepSelectedType && value === undefined ? 0 : value,
213
+ );
214
+ if (!visible) return null;
215
+ const id = makeId(path, props.label);
216
+
217
+ return (
218
+ <InputShell
219
+ {...props}
220
+ id={id}
221
+ label={props.label}
222
+ required={props.required}
223
+ description={props.description}
224
+ errors={props.errors}
225
+ >
226
+ <div className='group relative w-full'>
227
+ <Input
228
+ id={id}
229
+ className='shadcn-jsonforms-input'
230
+ style={{ paddingInlineEnd: 64 }}
231
+ type='number'
232
+ step={props.integer ? 1 : 'any'}
233
+ disabled={!enabled || readonly}
234
+ value={toStringValue(data)}
235
+ onChange={(event) => {
236
+ const value = event.currentTarget.value;
237
+ handleChange(
238
+ path,
239
+ value === ''
240
+ ? undefined
241
+ : props.integer
242
+ ? Math.trunc(Number(value))
243
+ : Number(value),
244
+ );
245
+ }}
246
+ />
247
+ <ClearValueButton
248
+ trailingControlSpace={24}
249
+ clearable={uischema.options?.clearable ?? config?.clearable ?? true}
250
+ data={data}
251
+ enabled={enabled}
252
+ readonly={readonly}
253
+ onClear={() => handleChange(path, undefined)}
254
+ />
255
+ </div>
256
+ </InputShell>
257
+ );
258
+ };
@@ -0,0 +1,14 @@
1
+ import {
2
+ ControlProps,
3
+ isIntegerControl,
4
+ RankedTester,
5
+ rankWith,
6
+ } from '@jsonforms/core';
7
+ import React from 'react';
8
+ import { ShadcnNumberControl } from './InputControl';
9
+
10
+ export const ShadcnIntegerControl = (props: ControlProps) => (
11
+ <ShadcnNumberControl {...props} integer />
12
+ );
13
+
14
+ export const integerControlTester: RankedTester = rankWith(2, isIntegerControl);
@@ -0,0 +1,14 @@
1
+ import {
2
+ ControlProps,
3
+ isNumberControl,
4
+ RankedTester,
5
+ rankWith,
6
+ } from '@jsonforms/core';
7
+ import React from 'react';
8
+ import { ShadcnNumberControl } from './InputControl';
9
+
10
+ export const ShadcnNumberInputControl = (props: ControlProps) => (
11
+ <ShadcnNumberControl {...props} />
12
+ );
13
+
14
+ export const numberControlTester: RankedTester = rankWith(1, isNumberControl);
@@ -0,0 +1,13 @@
1
+ import React from 'react';
2
+ import { isOneOfEnumControl, rankWith } from '@jsonforms/core';
3
+ import {
4
+ withJsonFormsOneOfEnumProps,
5
+ withTranslateProps,
6
+ } from '@jsonforms/react';
7
+ import { ShadcnEnumControl } from './EnumControl';
8
+
9
+ export const oneOfEnumControlTester = rankWith(5, isOneOfEnumControl);
10
+ export const ShadcnOneOfEnumControl = withJsonFormsOneOfEnumProps(
11
+ withTranslateProps(React.memo(ShadcnEnumControl)),
12
+ false,
13
+ );
@@ -0,0 +1,216 @@
1
+ import {
2
+ and,
3
+ ControlProps,
4
+ formatIs,
5
+ isStringControl,
6
+ optionIs,
7
+ or,
8
+ rankWith,
9
+ schemaMatches,
10
+ } from '@jsonforms/core';
11
+ import React, { useRef, useState } from 'react';
12
+ import { Eye, EyeOff } from 'lucide-react';
13
+ import { Input } from '@jsonforms-react-shadcn-ui/input';
14
+ import { Button } from '@jsonforms-react-shadcn-ui/button';
15
+ import {
16
+ Tooltip,
17
+ TooltipContent,
18
+ TooltipProvider,
19
+ TooltipTrigger,
20
+ } from '@jsonforms-react-shadcn-ui/tooltip';
21
+ import { useI18n } from '@chobantonov/jsonforms-react-renderer-common/locale/translate';
22
+ import { InputShell, makeId } from './InputControl';
23
+ import { ClearValueButton } from '../components/ClearValueButton';
24
+
25
+ const isPassword = and(
26
+ isStringControl,
27
+ or(formatIs('password'), optionIs('format', 'password')),
28
+ );
29
+ export const passwordControlTester = rankWith(4, isPassword);
30
+ export const passwordOtpControlTester = rankWith(
31
+ 5,
32
+ and(
33
+ isPassword,
34
+ optionIs('variant', 'otp'),
35
+ schemaMatches(
36
+ (schema) =>
37
+ typeof schema.minLength === 'number' &&
38
+ typeof schema.maxLength === 'number' &&
39
+ schema.maxLength > 0,
40
+ ),
41
+ ),
42
+ );
43
+
44
+ /** Shared masking and actions; segmented edits commit partial values immediately. */
45
+ export const ShadcnPasswordControl = (
46
+ props: ControlProps & { segmented?: boolean },
47
+ ) => {
48
+ const [revealed, setRevealed] = useState(false);
49
+ const inputs = useRef<Array<HTMLInputElement | null>>([]);
50
+ const t = useI18n();
51
+ if (!props.visible) return null;
52
+ const {
53
+ data,
54
+ path,
55
+ label,
56
+ schema,
57
+ enabled,
58
+ readonly,
59
+ errors,
60
+ handleChange,
61
+ uischema,
62
+ config,
63
+ } = props;
64
+ const id = makeId(path, label);
65
+ const disabled = !enabled || readonly;
66
+ const value = typeof data === 'string' ? data : '';
67
+ const length = props.segmented ? schema.maxLength! : 1;
68
+ const revealLabel = t(revealed ? 'password.hide' : 'password.show');
69
+ const commit = (next: string) => handleChange(path, next || undefined);
70
+ const focus = (index: number) => {
71
+ const input = inputs.current[Math.max(0, Math.min(length - 1, index))];
72
+ input?.focus();
73
+ input?.select();
74
+ };
75
+ const replace = (index: number, text: string) => {
76
+ const start = Math.min(index, value.length);
77
+ const next = (
78
+ value.slice(0, start) +
79
+ text +
80
+ value.slice(start + text.length)
81
+ ).slice(0, length);
82
+ commit(next);
83
+ focus(start + text.length);
84
+ };
85
+ return (
86
+ <InputShell {...props} id={id}>
87
+ <div
88
+ className={
89
+ props.segmented
90
+ ? 'group relative flex min-w-0 items-center gap-2'
91
+ : 'group relative min-w-0 w-full'
92
+ }
93
+ >
94
+ <div
95
+ role={props.segmented ? 'group' : undefined}
96
+ aria-label={props.segmented ? label : undefined}
97
+ className={`flex min-w-0 gap-1 overflow-x-auto${
98
+ props.segmented ? '' : ' w-full'
99
+ }`}
100
+ >
101
+ {Array.from({ length }, (_, index) => (
102
+ <Input
103
+ key={index}
104
+ ref={(element) => {
105
+ inputs.current[index] = element;
106
+ }}
107
+ id={index === 0 ? id : `${id}-${index}`}
108
+ className={
109
+ props.segmented
110
+ ? 'w-10 shrink-0 px-1 text-center'
111
+ : 'shadcn-jsonforms-input pr-20'
112
+ }
113
+ type={revealed ? 'text' : 'password'}
114
+ disabled={disabled}
115
+ aria-label={
116
+ props.segmented
117
+ ? `${label || ''} ${index + 1} / ${length}`
118
+ : undefined
119
+ }
120
+ aria-invalid={Boolean(errors)}
121
+ aria-describedby={errors ? `${id}-errors` : undefined}
122
+ autoComplete={
123
+ props.segmented ? 'one-time-code' : 'current-password'
124
+ }
125
+ value={props.segmented ? (value[index] ?? '') : value}
126
+ onFocus={(event) => {
127
+ if (props.segmented) event.currentTarget.select();
128
+ }}
129
+ onChange={(event) => {
130
+ const text = event.currentTarget.value;
131
+ if (!props.segmented) commit(text);
132
+ else if (text) replace(index, text);
133
+ else commit(value.slice(0, index) + value.slice(index + 1));
134
+ }}
135
+ onPaste={
136
+ props.segmented
137
+ ? (event) => {
138
+ event.preventDefault();
139
+ replace(index, event.clipboardData.getData('text'));
140
+ }
141
+ : undefined
142
+ }
143
+ onKeyDown={
144
+ props.segmented
145
+ ? (event) => {
146
+ if (
147
+ event.key === 'ArrowLeft' ||
148
+ event.key === 'ArrowRight'
149
+ ) {
150
+ event.preventDefault();
151
+ focus(index + (event.key === 'ArrowLeft' ? -1 : 1));
152
+ } else if (
153
+ event.key === 'Backspace' &&
154
+ !value[index] &&
155
+ index > 0
156
+ ) {
157
+ event.preventDefault();
158
+ commit(value.slice(0, index - 1) + value.slice(index));
159
+ focus(index - 1);
160
+ }
161
+ }
162
+ : undefined
163
+ }
164
+ />
165
+ ))}
166
+ </div>
167
+ <TooltipProvider>
168
+ <Tooltip>
169
+ <TooltipTrigger asChild>
170
+ <Button
171
+ type='button'
172
+ variant='ghost'
173
+ size='icon'
174
+ className={
175
+ props.segmented
176
+ ? 'h-7 w-7 shrink-0'
177
+ : 'absolute inset-y-0 end-1 my-auto h-7 w-7'
178
+ }
179
+ disabled={disabled}
180
+ aria-label={revealLabel}
181
+ onMouseDown={(event) => event.preventDefault()}
182
+ onClick={() => setRevealed((v) => !v)}
183
+ >
184
+ {revealed ? (
185
+ <Eye className='h-4 w-4' />
186
+ ) : (
187
+ <EyeOff className='h-4 w-4' />
188
+ )}
189
+ </Button>
190
+ </TooltipTrigger>
191
+ <TooltipContent>{revealLabel}</TooltipContent>
192
+ </Tooltip>
193
+ </TooltipProvider>
194
+ <div
195
+ className={
196
+ props.segmented
197
+ ? 'relative h-10 w-10 shrink-0'
198
+ : 'absolute inset-y-0 end-11 my-auto h-9 w-9'
199
+ }
200
+ >
201
+ <ClearValueButton
202
+ adornmentAware={false}
203
+ clearable={uischema.options?.clearable ?? config?.clearable ?? true}
204
+ data={data}
205
+ enabled={enabled}
206
+ readonly={readonly}
207
+ onClear={() => commit('')}
208
+ />
209
+ </div>
210
+ </div>
211
+ </InputShell>
212
+ );
213
+ };
214
+ export const ShadcnPasswordOtpControl = (props: ControlProps) => (
215
+ <ShadcnPasswordControl {...props} segmented />
216
+ );