@dxos/react-ui-form 0.7.4 → 0.7.5-labs.071a3e2

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