@dxos/react-ui-form 0.7.5-main.9d2a38b → 0.7.5-main.e9bb01b
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/dist/lib/browser/index.mjs +703 -462
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +730 -504
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +703 -462
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/components/FieldEditor/FieldEditor.d.ts.map +1 -1
- package/dist/types/src/components/Form/ArrayField.d.ts +16 -0
- package/dist/types/src/components/Form/ArrayField.d.ts.map +1 -0
- package/dist/types/src/components/Form/Defaults.d.ts +5 -6
- package/dist/types/src/components/Form/Defaults.d.ts.map +1 -1
- package/dist/types/src/components/Form/Form.d.ts +11 -20
- package/dist/types/src/components/Form/Form.d.ts.map +1 -1
- package/dist/types/src/components/Form/Form.stories.d.ts +2 -1
- package/dist/types/src/components/Form/Form.stories.d.ts.map +1 -1
- package/dist/types/src/components/Form/FormActions.d.ts +7 -0
- package/dist/types/src/components/Form/FormActions.d.ts.map +1 -0
- package/dist/types/src/components/Form/FormContent.d.ts +26 -0
- package/dist/types/src/components/Form/FormContent.d.ts.map +1 -0
- package/dist/types/src/components/Form/FormContext.d.ts +20 -0
- package/dist/types/src/components/Form/FormContext.d.ts.map +1 -0
- package/dist/types/src/components/Form/Input.d.ts +5 -6
- package/dist/types/src/components/Form/Input.d.ts.map +1 -1
- package/dist/types/src/components/Form/custom/GeoPointInput.d.ts +1 -2
- package/dist/types/src/components/Form/custom/GeoPointInput.d.ts.map +1 -1
- package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts +4 -0
- package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts.map +1 -0
- package/dist/types/src/components/Form/custom/SelectOptionsInput.stories.d.ts +7 -0
- package/dist/types/src/components/Form/custom/SelectOptionsInput.stories.d.ts.map +1 -0
- package/dist/types/src/components/Form/custom/TupleInput.d.ts +1 -2
- package/dist/types/src/components/Form/custom/TupleInput.d.ts.map +1 -1
- package/dist/types/src/components/Form/custom/index.d.ts +1 -0
- package/dist/types/src/components/Form/custom/index.d.ts.map +1 -1
- package/dist/types/src/components/Form/factory.d.ts +2 -2
- package/dist/types/src/components/Form/factory.d.ts.map +1 -1
- package/dist/types/src/components/ViewEditor/ViewEditor.d.ts.map +1 -1
- package/dist/types/src/components/deprecated/FormInput.d.ts +5 -0
- package/dist/types/src/components/deprecated/FormInput.d.ts.map +1 -1
- package/dist/types/src/hooks/useForm.d.ts +9 -10
- package/dist/types/src/hooks/useForm.d.ts.map +1 -1
- package/dist/types/src/translations.d.ts +6 -0
- package/dist/types/src/translations.d.ts.map +1 -1
- package/package.json +25 -24
- package/src/components/FieldEditor/FieldEditor.stories.tsx +1 -1
- package/src/components/FieldEditor/FieldEditor.tsx +7 -6
- package/src/components/Form/ArrayField.tsx +111 -0
- package/src/components/Form/Defaults.tsx +18 -33
- package/src/components/Form/Form.stories.tsx +15 -5
- package/src/components/Form/Form.tsx +38 -358
- package/src/components/Form/FormActions.tsx +41 -0
- package/src/components/Form/FormContent.tsx +173 -0
- package/src/components/Form/FormContext.tsx +48 -0
- package/src/components/Form/Input.tsx +5 -6
- package/src/components/Form/custom/GeoPointInput.tsx +6 -15
- package/src/components/Form/custom/SelectOptionsInput.stories.tsx +58 -0
- package/src/components/Form/custom/SelectOptionsInput.tsx +192 -0
- package/src/components/Form/custom/TupleInput.tsx +5 -9
- package/src/components/Form/custom/index.ts +1 -0
- package/src/components/Form/factory.tsx +2 -5
- package/src/components/ViewEditor/ViewEditor.tsx +5 -2
- package/src/components/deprecated/FormInput.tsx +19 -3
- package/src/hooks/useForm.ts +54 -47
- package/src/translations.ts +8 -0
|
@@ -2,29 +2,28 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
import { capitalize } from 'effect/String';
|
|
7
|
-
import React, { useEffect, useMemo } from 'react';
|
|
5
|
+
import React, { type ReactElement, useMemo } from 'react';
|
|
8
6
|
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import { log } from '@dxos/log';
|
|
12
|
-
import { IconButton, type ThemedClassName, useTranslation } from '@dxos/react-ui';
|
|
7
|
+
import { type BaseObject, type S, type PropertyKey } from '@dxos/echo-schema';
|
|
8
|
+
import { type ThemedClassName } from '@dxos/react-ui';
|
|
13
9
|
import { mx } from '@dxos/react-ui-theme';
|
|
14
|
-
import {
|
|
15
|
-
import { isNotFalsy } from '@dxos/util';
|
|
10
|
+
import { type SchemaProperty } from '@dxos/schema';
|
|
16
11
|
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
import { type
|
|
21
|
-
import {
|
|
12
|
+
import { FormActions } from './FormActions';
|
|
13
|
+
import { FormFields } from './FormContent';
|
|
14
|
+
import { FormProvider } from './FormContext';
|
|
15
|
+
import { type InputProps, type InputComponent } from './Input';
|
|
16
|
+
import { type FormOptions } from '../../hooks';
|
|
22
17
|
|
|
23
|
-
|
|
18
|
+
export type PropsFilter<T extends BaseObject> = (props: SchemaProperty<T>[]) => SchemaProperty<T>[];
|
|
24
19
|
|
|
25
|
-
|
|
20
|
+
export type ComponentLookup = (args: {
|
|
21
|
+
prop: string;
|
|
22
|
+
schema: S.Schema<any>;
|
|
23
|
+
inputProps: InputProps;
|
|
24
|
+
}) => ReactElement | undefined;
|
|
26
25
|
|
|
27
|
-
export type
|
|
26
|
+
export type CustomInputMap = Partial<Record<string, InputComponent>>;
|
|
28
27
|
|
|
29
28
|
export type FormProps<T extends BaseObject> = ThemedClassName<
|
|
30
29
|
{
|
|
@@ -42,24 +41,15 @@ export type FormProps<T extends BaseObject> = ThemedClassName<
|
|
|
42
41
|
autoSave?: boolean;
|
|
43
42
|
testId?: string;
|
|
44
43
|
onCancel?: () => void;
|
|
45
|
-
|
|
46
|
-
lookupComponent?: (args: {
|
|
47
|
-
prop: string;
|
|
48
|
-
schema: S.Schema<any>;
|
|
49
|
-
inputProps: InputProps<T>;
|
|
50
|
-
}) => React.ReactElement | undefined;
|
|
44
|
+
lookupComponent?: ComponentLookup;
|
|
51
45
|
/**
|
|
52
46
|
* Map of custom renderers for specific properties.
|
|
53
|
-
*
|
|
47
|
+
* Prefer lookupComponent for plugin specific input surfaces.
|
|
54
48
|
*/
|
|
55
|
-
Custom?:
|
|
49
|
+
Custom?: CustomInputMap;
|
|
56
50
|
} & Pick<FormOptions<T>, 'schema' | 'onValuesChanged' | 'onValidate' | 'onSave'>
|
|
57
51
|
>;
|
|
58
52
|
|
|
59
|
-
/**
|
|
60
|
-
* General purpose form component that displays field controls based on the given schema.
|
|
61
|
-
*/
|
|
62
|
-
// TODO(burdon): Area to show general validation errors.
|
|
63
53
|
export const Form = <T extends BaseObject>({
|
|
64
54
|
classNames,
|
|
65
55
|
schema,
|
|
@@ -78,338 +68,28 @@ export const Form = <T extends BaseObject>({
|
|
|
78
68
|
Custom,
|
|
79
69
|
}: FormProps<T>) => {
|
|
80
70
|
const onValid = useMemo(() => (autoSave ? onSave : undefined), [autoSave, onSave]);
|
|
81
|
-
const { canSave, values, errors, handleSave, ...inputProps } = useForm<T>({
|
|
82
|
-
schema,
|
|
83
|
-
initialValues,
|
|
84
|
-
onValuesChanged,
|
|
85
|
-
onValidate,
|
|
86
|
-
onValid,
|
|
87
|
-
onSave,
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
// Filter and sort props.
|
|
91
|
-
// TODO(burdon): Move into useForm?
|
|
92
|
-
const properties = useMemo(() => {
|
|
93
|
-
const props = getSchemaProperties<T>(schema.ast, values);
|
|
94
|
-
const filtered = filter ? filter(props) : props;
|
|
95
|
-
const findIndex = (props: PropertyKey<T>[], prop: PropertyKey<T>) => {
|
|
96
|
-
const idx = props.findIndex((p) => p === prop);
|
|
97
|
-
return idx === -1 ? Infinity : idx;
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
return sort ? filtered.sort(({ name: a }, { name: b }) => findIndex(sort, a) - findIndex(sort, b)) : filtered;
|
|
101
|
-
}, [schema, filter, values]);
|
|
102
|
-
|
|
103
|
-
// TODO(burdon): Highlight in UX.
|
|
104
|
-
useEffect(() => {
|
|
105
|
-
if (errors && Object.keys(errors).length) {
|
|
106
|
-
log.warn('validation', { errors });
|
|
107
|
-
}
|
|
108
|
-
}, [errors]);
|
|
109
71
|
|
|
110
72
|
return (
|
|
111
|
-
<
|
|
112
|
-
{
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
placeholder,
|
|
129
|
-
...inputProps,
|
|
130
|
-
},
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
if (FoundComponent) {
|
|
134
|
-
return (
|
|
135
|
-
<div key={name} role='none' className={padding}>
|
|
136
|
-
{FoundComponent}
|
|
137
|
-
</div>
|
|
138
|
-
);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// Get generic input.
|
|
142
|
-
// TODO(ZaymonFC): We might need to switch this to using globs since we're now indexing into arrays.
|
|
143
|
-
let InputComponent = Custom?.[key.join('.')];
|
|
144
|
-
if (!InputComponent) {
|
|
145
|
-
// Select.
|
|
146
|
-
if (options) {
|
|
147
|
-
return (
|
|
148
|
-
<div key={name} role='none' className={padding}>
|
|
149
|
-
<SelectInput
|
|
150
|
-
type={type}
|
|
151
|
-
format={format}
|
|
152
|
-
property={name}
|
|
153
|
-
disabled={readonly}
|
|
154
|
-
label={label}
|
|
155
|
-
options={options.map((option) => ({ value: option, label: String(option) }))}
|
|
156
|
-
placeholder={placeholder}
|
|
157
|
-
{...inputProps}
|
|
158
|
-
/>
|
|
159
|
-
</div>
|
|
160
|
-
);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
InputComponent = getInputComponent<T>(type, format);
|
|
164
|
-
|
|
165
|
-
// TODO(ZaymonFC): Array handling is inelegant. We need to push this complexity down into SDK/Schema.
|
|
166
|
-
if (array) {
|
|
167
|
-
return (
|
|
168
|
-
<ArrayField
|
|
169
|
-
key={name}
|
|
170
|
-
name={name}
|
|
171
|
-
type={type}
|
|
172
|
-
array={true}
|
|
173
|
-
label={label}
|
|
174
|
-
ast={ast}
|
|
175
|
-
values={values}
|
|
176
|
-
format={format}
|
|
177
|
-
readonly={readonly}
|
|
178
|
-
placeholder={placeholder}
|
|
179
|
-
inputProps={inputProps}
|
|
180
|
-
Custom={Custom}
|
|
181
|
-
/>
|
|
182
|
-
);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
if (!InputComponent) {
|
|
187
|
-
// Recursively render form.
|
|
188
|
-
if (type === 'object') {
|
|
189
|
-
const baseNode = findNode(ast, isDiscriminatedUnion);
|
|
190
|
-
const typeLiteral = baseNode
|
|
191
|
-
? getDiscriminatedType(baseNode, values[name] as any)
|
|
192
|
-
: findNode(ast, AST.isTypeLiteral);
|
|
193
|
-
|
|
194
|
-
if (typeLiteral) {
|
|
195
|
-
const schema = S.make(typeLiteral);
|
|
196
|
-
return (
|
|
197
|
-
<div key={name} role='none'>
|
|
198
|
-
<div className={padding}>{label}</div>
|
|
199
|
-
<Form<any>
|
|
200
|
-
schema={schema}
|
|
201
|
-
path={key}
|
|
202
|
-
values={values[name] ?? {}}
|
|
203
|
-
onValuesChanged={(childValues) => {
|
|
204
|
-
inputProps.onValueChange(name, 'object', childValues);
|
|
205
|
-
}}
|
|
206
|
-
Custom={Custom as any}
|
|
207
|
-
/>
|
|
208
|
-
</div>
|
|
209
|
-
);
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
log('no renderer for property', { name, type });
|
|
214
|
-
return null;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
return (
|
|
218
|
-
<div key={name} role='none' className={padding}>
|
|
219
|
-
<InputComponent
|
|
220
|
-
type={type}
|
|
221
|
-
format={format}
|
|
222
|
-
property={name}
|
|
223
|
-
disabled={readonly}
|
|
224
|
-
label={label}
|
|
225
|
-
placeholder={placeholder}
|
|
226
|
-
{...inputProps}
|
|
227
|
-
/>
|
|
228
|
-
</div>
|
|
229
|
-
);
|
|
230
|
-
})
|
|
231
|
-
.filter(isNotFalsy)}
|
|
232
|
-
{(onCancel || onSave) && !autoSave && (
|
|
233
|
-
<FormActions onCancel={onCancel} onSubmit={handleSave} canSubmit={canSave} readonly={readonly} />
|
|
234
|
-
)}
|
|
235
|
-
</div>
|
|
236
|
-
);
|
|
237
|
-
};
|
|
238
|
-
|
|
239
|
-
type ArrayFieldProps<T extends BaseObject> = {
|
|
240
|
-
name: PropertyKey<T>;
|
|
241
|
-
type: SimpleType;
|
|
242
|
-
array: true;
|
|
243
|
-
label: string;
|
|
244
|
-
ast: AST.AST;
|
|
245
|
-
values: Record<string, any>;
|
|
246
|
-
format?: FormatEnum;
|
|
247
|
-
readonly?: boolean;
|
|
248
|
-
placeholder?: string;
|
|
249
|
-
inputProps: Pick<FormHandler<T>, 'getStatus' | 'getValue' | 'onValueChange' | 'onBlur'>;
|
|
250
|
-
Custom?: Partial<Record<string, InputComponent<T>>>;
|
|
251
|
-
};
|
|
252
|
-
|
|
253
|
-
const ArrayField = <T extends BaseObject>({
|
|
254
|
-
name,
|
|
255
|
-
type,
|
|
256
|
-
label,
|
|
257
|
-
ast,
|
|
258
|
-
values,
|
|
259
|
-
format,
|
|
260
|
-
readonly,
|
|
261
|
-
placeholder,
|
|
262
|
-
inputProps,
|
|
263
|
-
Custom,
|
|
264
|
-
}: ArrayFieldProps<T>) => {
|
|
265
|
-
const { t } = useTranslation(translationKey);
|
|
266
|
-
const arrayValues = (values[name] ?? []) as any[];
|
|
267
|
-
const key = [name];
|
|
268
|
-
|
|
269
|
-
// TODO(ZaymonFC): Should this unwrapping happen at a lower level?
|
|
270
|
-
const tupleType = findNode(ast, AST.isTupleType);
|
|
271
|
-
const elementType = (tupleType as AST.TupleType | undefined)?.rest[0]?.type;
|
|
272
|
-
|
|
273
|
-
if (type === 'object' && elementType) {
|
|
274
|
-
const baseNode = findNode(elementType, isDiscriminatedUnion);
|
|
275
|
-
const typeLiteral = baseNode
|
|
276
|
-
? getDiscriminatedType(baseNode, values[name] as any)
|
|
277
|
-
: findNode(elementType, AST.isTypeLiteral);
|
|
278
|
-
|
|
279
|
-
if (typeLiteral) {
|
|
280
|
-
const schema = S.make(typeLiteral);
|
|
281
|
-
|
|
282
|
-
return (
|
|
283
|
-
<div key={name} role='none' className={mx(padding)}>
|
|
284
|
-
<InputHeader>{label}</InputHeader>
|
|
285
|
-
<div role='none' className='flex flex-col gap-1'>
|
|
286
|
-
{arrayValues.map((value, index) => {
|
|
287
|
-
return (
|
|
288
|
-
<div key={index} role='none' className='flex items-center gap-1'>
|
|
289
|
-
<div role='none' className='flex-1'>
|
|
290
|
-
<Form<any>
|
|
291
|
-
schema={schema}
|
|
292
|
-
path={[...key, `[${index}]`]}
|
|
293
|
-
values={value}
|
|
294
|
-
onValuesChanged={(childValues) => {
|
|
295
|
-
const newValues = [...arrayValues];
|
|
296
|
-
newValues[index] = childValues;
|
|
297
|
-
inputProps.onValueChange(name, 'object', newValues);
|
|
298
|
-
}}
|
|
299
|
-
Custom={Custom as any}
|
|
300
|
-
/>
|
|
301
|
-
</div>
|
|
302
|
-
<IconButton
|
|
303
|
-
icon='ph--trash--regular'
|
|
304
|
-
iconOnly
|
|
305
|
-
label={t('button remove')}
|
|
306
|
-
onClick={() => {
|
|
307
|
-
const newValues = arrayValues.filter((_, i) => i !== index);
|
|
308
|
-
inputProps.onValueChange(name, type, newValues);
|
|
309
|
-
}}
|
|
310
|
-
/>
|
|
311
|
-
</div>
|
|
312
|
-
);
|
|
313
|
-
})}
|
|
314
|
-
</div>
|
|
315
|
-
<div role='none' className='flex justify-between items-center plb-1'>
|
|
316
|
-
<IconButton
|
|
317
|
-
icon='ph--plus--regular'
|
|
318
|
-
iconOnly
|
|
319
|
-
label={t('button add')}
|
|
320
|
-
onClick={() => {
|
|
321
|
-
const newValues = [...arrayValues, {}];
|
|
322
|
-
inputProps.onValueChange(name, type, newValues);
|
|
323
|
-
}}
|
|
324
|
-
/>
|
|
325
|
-
</div>
|
|
326
|
-
</div>
|
|
327
|
-
);
|
|
328
|
-
}
|
|
329
|
-
return (
|
|
330
|
-
<div key={name} role='none'>
|
|
331
|
-
Nested form not supported in arrays, yet.
|
|
332
|
-
</div>
|
|
333
|
-
);
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
const InputComponent = getInputComponent<T>(type, format);
|
|
337
|
-
if (!InputComponent) {
|
|
338
|
-
return null;
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
return (
|
|
342
|
-
<div role='none' key='meta-input' className={mx(padding)}>
|
|
343
|
-
<InputHeader>{label}</InputHeader>
|
|
344
|
-
<div role='none' className='flex flex-col gap-1'>
|
|
345
|
-
{arrayValues.map((_, index) => (
|
|
346
|
-
<div key={index} role='none' className='flex items-start gap-1'>
|
|
347
|
-
<div role='none' className='flex-1'>
|
|
348
|
-
<InputComponent
|
|
349
|
-
type={type}
|
|
350
|
-
format={format}
|
|
351
|
-
label={label}
|
|
352
|
-
inputOnly
|
|
353
|
-
property={`${name}[${index}]` as PropertyKey<T>}
|
|
354
|
-
disabled={readonly}
|
|
355
|
-
placeholder={placeholder}
|
|
356
|
-
{...inputProps}
|
|
357
|
-
/>
|
|
358
|
-
</div>
|
|
359
|
-
<IconButton
|
|
360
|
-
icon='ph--trash--regular'
|
|
361
|
-
iconOnly
|
|
362
|
-
label={t('button remove')}
|
|
363
|
-
onClick={() => {
|
|
364
|
-
const newValues = arrayValues.filter((_, i) => i !== index);
|
|
365
|
-
inputProps.onValueChange(name, type, newValues);
|
|
366
|
-
}}
|
|
367
|
-
/>
|
|
368
|
-
</div>
|
|
369
|
-
))}
|
|
370
|
-
</div>
|
|
371
|
-
<div role='none' className='flex justify-between items-center plb-1'>
|
|
372
|
-
<IconButton
|
|
373
|
-
icon='ph--plus--regular'
|
|
374
|
-
iconOnly
|
|
375
|
-
label={t('button add')}
|
|
376
|
-
onClick={() => {
|
|
377
|
-
const newValues = [...arrayValues, SimpleType.getDefaultValue(type)];
|
|
378
|
-
inputProps.onValueChange(name, type, newValues);
|
|
379
|
-
}}
|
|
73
|
+
<FormProvider
|
|
74
|
+
schema={schema}
|
|
75
|
+
initialValues={initialValues}
|
|
76
|
+
onValuesChanged={onValuesChanged}
|
|
77
|
+
onValidate={onValidate}
|
|
78
|
+
onValid={onValid}
|
|
79
|
+
onSave={onSave}
|
|
80
|
+
>
|
|
81
|
+
<div role='none' className={mx('p-2', classNames)} data-testid={testId}>
|
|
82
|
+
<FormFields
|
|
83
|
+
schema={schema}
|
|
84
|
+
path={path}
|
|
85
|
+
readonly={readonly}
|
|
86
|
+
filter={filter}
|
|
87
|
+
sort={sort}
|
|
88
|
+
lookupComponent={lookupComponent}
|
|
89
|
+
Custom={Custom}
|
|
380
90
|
/>
|
|
91
|
+
{(onCancel || onSave) && !autoSave && <FormActions onCancel={onCancel} readonly={readonly} />}
|
|
381
92
|
</div>
|
|
382
|
-
</
|
|
383
|
-
);
|
|
384
|
-
};
|
|
385
|
-
|
|
386
|
-
type FormActionsProps = {
|
|
387
|
-
readonly?: boolean;
|
|
388
|
-
canSubmit: boolean;
|
|
389
|
-
onCancel?: () => void;
|
|
390
|
-
onSubmit: () => void;
|
|
391
|
-
};
|
|
392
|
-
|
|
393
|
-
export const FormActions = ({ onCancel, onSubmit, canSubmit, readonly }: FormActionsProps) => {
|
|
394
|
-
const { t } = useTranslation(translationKey);
|
|
395
|
-
|
|
396
|
-
return (
|
|
397
|
-
<div role='none' className='flex justify-center'>
|
|
398
|
-
<div role='none' className={mx(onCancel && !readonly && 'grid grid-cols-2 gap-2')}>
|
|
399
|
-
{onCancel && !readonly && (
|
|
400
|
-
<IconButton data-testid='cancel-button' icon='ph--x--regular' label={t('button cancel')} onClick={onCancel} />
|
|
401
|
-
)}
|
|
402
|
-
{onSubmit && (
|
|
403
|
-
<IconButton
|
|
404
|
-
type='submit'
|
|
405
|
-
data-testid='save-button'
|
|
406
|
-
disabled={!canSubmit}
|
|
407
|
-
icon='ph--check--regular'
|
|
408
|
-
label={t('button save')}
|
|
409
|
-
onClick={onSubmit}
|
|
410
|
-
/>
|
|
411
|
-
)}
|
|
412
|
-
</div>
|
|
413
|
-
</div>
|
|
93
|
+
</FormProvider>
|
|
414
94
|
);
|
|
415
95
|
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import React from 'react';
|
|
6
|
+
|
|
7
|
+
import { IconButton, useTranslation } from '@dxos/react-ui';
|
|
8
|
+
import { mx } from '@dxos/react-ui-theme';
|
|
9
|
+
|
|
10
|
+
import { useFormContext } from './FormContext';
|
|
11
|
+
import { translationKey } from '../../translations';
|
|
12
|
+
|
|
13
|
+
export type FormActionsProps = {
|
|
14
|
+
readonly?: boolean;
|
|
15
|
+
onCancel?: () => void;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const FormActions = ({ onCancel, readonly }: FormActionsProps) => {
|
|
19
|
+
const { t } = useTranslation(translationKey);
|
|
20
|
+
const { canSave, handleSave } = useFormContext();
|
|
21
|
+
|
|
22
|
+
return (
|
|
23
|
+
<div role='none' className='flex justify-center'>
|
|
24
|
+
<div role='none' className={mx(onCancel && !readonly && 'grid grid-cols-2 gap-2')}>
|
|
25
|
+
{onCancel && !readonly && (
|
|
26
|
+
<IconButton data-testid='cancel-button' icon='ph--x--regular' label={t('button cancel')} onClick={onCancel} />
|
|
27
|
+
)}
|
|
28
|
+
{handleSave && (
|
|
29
|
+
<IconButton
|
|
30
|
+
type='submit'
|
|
31
|
+
data-testid='save-button'
|
|
32
|
+
disabled={!canSave}
|
|
33
|
+
icon='ph--check--regular'
|
|
34
|
+
label={t('button save')}
|
|
35
|
+
onClick={handleSave}
|
|
36
|
+
/>
|
|
37
|
+
)}
|
|
38
|
+
</div>
|
|
39
|
+
</div>
|
|
40
|
+
);
|
|
41
|
+
};
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { pipe } from 'effect';
|
|
6
|
+
import { capitalize } from 'effect/String';
|
|
7
|
+
import React, { useMemo } from 'react';
|
|
8
|
+
|
|
9
|
+
import { AST, S } from '@dxos/echo-schema';
|
|
10
|
+
import { createJsonPath, findNode, getDiscriminatedType, isDiscriminatedUnion } from '@dxos/effect';
|
|
11
|
+
import { mx } from '@dxos/react-ui-theme';
|
|
12
|
+
import { getSchemaProperties, type SchemaProperty } from '@dxos/schema';
|
|
13
|
+
import { isNotFalsy } from '@dxos/util';
|
|
14
|
+
|
|
15
|
+
import { ArrayField } from './ArrayField';
|
|
16
|
+
import { SelectInput } from './Defaults';
|
|
17
|
+
import { type ComponentLookup } from './Form';
|
|
18
|
+
import { useInputProps, useFormValues } from './FormContext';
|
|
19
|
+
import { type InputComponent } from './Input';
|
|
20
|
+
import { getInputComponent } from './factory';
|
|
21
|
+
|
|
22
|
+
export type FormContentProps = {
|
|
23
|
+
schema: S.Schema.All;
|
|
24
|
+
path?: (string | number)[];
|
|
25
|
+
filter?: (props: SchemaProperty<any>[]) => SchemaProperty<any>[];
|
|
26
|
+
sort?: string[];
|
|
27
|
+
readonly?: boolean;
|
|
28
|
+
lookupComponent?: ComponentLookup;
|
|
29
|
+
Custom?: Partial<Record<string, InputComponent>>;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type FormFieldProps = {
|
|
33
|
+
property: SchemaProperty<any>;
|
|
34
|
+
path?: (string | number)[];
|
|
35
|
+
readonly?: boolean;
|
|
36
|
+
/** Used to indicate if input should be presented inline (e.g. for array items). */
|
|
37
|
+
inline?: boolean;
|
|
38
|
+
lookupComponent?: ComponentLookup;
|
|
39
|
+
Custom?: Partial<Record<string, InputComponent>>;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export const FormField = ({ property, path, readonly, inline, lookupComponent, Custom }: FormFieldProps) => {
|
|
43
|
+
const inputProps = useInputProps(path);
|
|
44
|
+
const { ast, name, type, format, title, description, options, examples, array } = property;
|
|
45
|
+
|
|
46
|
+
const label = useMemo(() => title ?? pipe(name, capitalize), [title, name]);
|
|
47
|
+
const placeholder = useMemo(
|
|
48
|
+
() => (examples?.length ? `Example: "${examples[0]}"` : description),
|
|
49
|
+
[examples, description],
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
const FoundComponent = lookupComponent?.({
|
|
53
|
+
prop: name,
|
|
54
|
+
schema: S.make(ast),
|
|
55
|
+
inputProps: {
|
|
56
|
+
type,
|
|
57
|
+
format,
|
|
58
|
+
label,
|
|
59
|
+
disabled: readonly,
|
|
60
|
+
placeholder,
|
|
61
|
+
...inputProps,
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
if (FoundComponent) {
|
|
66
|
+
return <div role='none'>{FoundComponent}</div>;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const jsonPath = createJsonPath(path ?? []);
|
|
70
|
+
const CustomComponent = Custom?.[jsonPath];
|
|
71
|
+
if (CustomComponent) {
|
|
72
|
+
return (
|
|
73
|
+
// TODO(ZaymonFC): We should try to remove this div wrapper.
|
|
74
|
+
<div role='none'>
|
|
75
|
+
<CustomComponent
|
|
76
|
+
type={type}
|
|
77
|
+
format={format}
|
|
78
|
+
label={label}
|
|
79
|
+
inputOnly={inline}
|
|
80
|
+
placeholder={placeholder}
|
|
81
|
+
disabled={readonly}
|
|
82
|
+
{...inputProps}
|
|
83
|
+
/>
|
|
84
|
+
</div>
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (array) {
|
|
89
|
+
return <ArrayField property={property} path={path} inputProps={inputProps} readonly={readonly} Custom={Custom} />;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const InputComponent = getInputComponent(type, format);
|
|
93
|
+
if (InputComponent) {
|
|
94
|
+
return (
|
|
95
|
+
<div role='none'>
|
|
96
|
+
<InputComponent
|
|
97
|
+
type={type}
|
|
98
|
+
format={format}
|
|
99
|
+
label={label}
|
|
100
|
+
inputOnly={inline}
|
|
101
|
+
placeholder={placeholder}
|
|
102
|
+
disabled={readonly}
|
|
103
|
+
{...inputProps}
|
|
104
|
+
/>
|
|
105
|
+
</div>
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (options) {
|
|
110
|
+
return (
|
|
111
|
+
<div role='none'>
|
|
112
|
+
<SelectInput
|
|
113
|
+
type={type}
|
|
114
|
+
format={format}
|
|
115
|
+
disabled={readonly}
|
|
116
|
+
inputOnly={inline}
|
|
117
|
+
label={label}
|
|
118
|
+
options={options.map((option) => ({ value: option, label: String(option) }))}
|
|
119
|
+
placeholder={placeholder}
|
|
120
|
+
{...inputProps}
|
|
121
|
+
/>
|
|
122
|
+
</div>
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (type === 'object') {
|
|
127
|
+
const baseNode = findNode(ast, isDiscriminatedUnion);
|
|
128
|
+
const typeLiteral = baseNode
|
|
129
|
+
? getDiscriminatedType(baseNode, inputProps.getValue() as any)
|
|
130
|
+
: findNode(ast, AST.isTypeLiteral);
|
|
131
|
+
|
|
132
|
+
if (typeLiteral) {
|
|
133
|
+
return (
|
|
134
|
+
<div role='none'>
|
|
135
|
+
{!inline && <div>{label}</div>}
|
|
136
|
+
<FormFields schema={S.make(typeLiteral)} path={path} readonly={readonly} Custom={Custom} />
|
|
137
|
+
</div>
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return null;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
export const FormFields = ({ schema, path, filter, sort, readonly, lookupComponent, Custom }: FormContentProps) => {
|
|
146
|
+
const values = useFormValues(path);
|
|
147
|
+
|
|
148
|
+
const properties = useMemo(() => {
|
|
149
|
+
const props = getSchemaProperties(schema.ast, values);
|
|
150
|
+
const filtered = filter ? filter(props) : props;
|
|
151
|
+
|
|
152
|
+
return sort ? filtered.sort((a, b) => sort.indexOf(a.name) - sort.indexOf(b.name)) : filtered;
|
|
153
|
+
}, [schema, values, filter, sort]);
|
|
154
|
+
|
|
155
|
+
return (
|
|
156
|
+
<div role='form' className={mx('flex flex-col w-full gap-2 py-2')}>
|
|
157
|
+
{properties
|
|
158
|
+
.map((property) => {
|
|
159
|
+
return (
|
|
160
|
+
<FormField
|
|
161
|
+
key={property.name}
|
|
162
|
+
property={property}
|
|
163
|
+
path={[...(path ?? []), property.name]}
|
|
164
|
+
readonly={readonly}
|
|
165
|
+
lookupComponent={lookupComponent}
|
|
166
|
+
Custom={Custom}
|
|
167
|
+
/>
|
|
168
|
+
);
|
|
169
|
+
})
|
|
170
|
+
.filter(isNotFalsy)}
|
|
171
|
+
</div>
|
|
172
|
+
);
|
|
173
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import React, { createContext, useContext, type FocusEvent, type PropsWithChildren } from 'react';
|
|
6
|
+
|
|
7
|
+
import { raise } from '@dxos/debug';
|
|
8
|
+
import { type BaseObject, getValue } from '@dxos/echo-schema';
|
|
9
|
+
import { createJsonPath, type SimpleType } from '@dxos/effect';
|
|
10
|
+
|
|
11
|
+
import { type FormHandler, type FormOptions, useForm } from '../../hooks';
|
|
12
|
+
|
|
13
|
+
type FormContextValue<T extends BaseObject> = FormHandler<T>;
|
|
14
|
+
|
|
15
|
+
const FormContext = createContext<FormContextValue<any> | undefined>(undefined);
|
|
16
|
+
|
|
17
|
+
export const useFormContext = <T extends BaseObject>(): FormContextValue<T> => {
|
|
18
|
+
return useContext(FormContext) ?? raise(new Error('Missing FormContext'));
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export const useFormValues = (path: (string | number)[] = []): any => {
|
|
22
|
+
const { values: formValues } = useFormContext();
|
|
23
|
+
const jsonPath = createJsonPath(path);
|
|
24
|
+
return getValue(formValues, jsonPath) as BaseObject;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type FormInputStateProps = {
|
|
28
|
+
getStatus: () => { status?: 'error'; error?: string };
|
|
29
|
+
getValue: <V>() => V | undefined;
|
|
30
|
+
onValueChange: (type: SimpleType, value: any) => void;
|
|
31
|
+
onBlur: (event: FocusEvent<HTMLElement>) => void;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export const useInputProps = (path: (string | number)[] = []): FormInputStateProps => {
|
|
35
|
+
const { getStatus, getValue: getFormValue, onValueChange, onTouched } = useFormContext();
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
getStatus: () => getStatus(path),
|
|
39
|
+
getValue: () => getFormValue(path),
|
|
40
|
+
onValueChange: (type: SimpleType, value: any) => onValueChange(path, type, value),
|
|
41
|
+
onBlur: () => onTouched(path),
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export const FormProvider = ({ children, ...formOptions }: PropsWithChildren<FormOptions<any>>) => {
|
|
46
|
+
const formHandler = useForm(formOptions);
|
|
47
|
+
return <FormContext.Provider value={formHandler}>{children}</FormContext.Provider>;
|
|
48
|
+
};
|