@dxos/react-ui-form 0.7.5-feature-compute.4d9d99a → 0.7.5-labs.a279d8c

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 (47) hide show
  1. package/dist/lib/browser/index.mjs +416 -409
  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 +461 -469
  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 +416 -409
  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/Form/ArrayField.d.ts +16 -0
  11. package/dist/types/src/components/Form/ArrayField.d.ts.map +1 -0
  12. package/dist/types/src/components/Form/Defaults.d.ts +5 -6
  13. package/dist/types/src/components/Form/Defaults.d.ts.map +1 -1
  14. package/dist/types/src/components/Form/Form.d.ts +10 -20
  15. package/dist/types/src/components/Form/Form.d.ts.map +1 -1
  16. package/dist/types/src/components/Form/Form.stories.d.ts +2 -1
  17. package/dist/types/src/components/Form/Form.stories.d.ts.map +1 -1
  18. package/dist/types/src/components/Form/FormActions.d.ts +7 -0
  19. package/dist/types/src/components/Form/FormActions.d.ts.map +1 -0
  20. package/dist/types/src/components/Form/FormContent.d.ts +26 -0
  21. package/dist/types/src/components/Form/FormContent.d.ts.map +1 -0
  22. package/dist/types/src/components/Form/FormContext.d.ts +20 -0
  23. package/dist/types/src/components/Form/FormContext.d.ts.map +1 -0
  24. package/dist/types/src/components/Form/Input.d.ts +5 -6
  25. package/dist/types/src/components/Form/Input.d.ts.map +1 -1
  26. package/dist/types/src/components/Form/custom/GeoPointInput.d.ts +1 -2
  27. package/dist/types/src/components/Form/custom/GeoPointInput.d.ts.map +1 -1
  28. package/dist/types/src/components/Form/custom/TupleInput.d.ts +1 -2
  29. package/dist/types/src/components/Form/custom/TupleInput.d.ts.map +1 -1
  30. package/dist/types/src/components/Form/factory.d.ts +2 -2
  31. package/dist/types/src/components/Form/factory.d.ts.map +1 -1
  32. package/dist/types/src/hooks/useForm.d.ts +9 -10
  33. package/dist/types/src/hooks/useForm.d.ts.map +1 -1
  34. package/package.json +23 -23
  35. package/src/components/FieldEditor/FieldEditor.tsx +4 -4
  36. package/src/components/Form/ArrayField.tsx +111 -0
  37. package/src/components/Form/Defaults.tsx +18 -33
  38. package/src/components/Form/Form.stories.tsx +15 -5
  39. package/src/components/Form/Form.tsx +37 -359
  40. package/src/components/Form/FormActions.tsx +41 -0
  41. package/src/components/Form/FormContent.tsx +155 -0
  42. package/src/components/Form/FormContext.tsx +48 -0
  43. package/src/components/Form/Input.tsx +5 -6
  44. package/src/components/Form/custom/GeoPointInput.tsx +6 -15
  45. package/src/components/Form/custom/TupleInput.tsx +5 -9
  46. package/src/components/Form/factory.tsx +2 -5
  47. package/src/hooks/useForm.ts +54 -46
@@ -0,0 +1,111 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { pipe } from 'effect';
6
+ import { capitalize } from 'effect/String';
7
+ import React, { useCallback, useMemo } from 'react';
8
+
9
+ import { AST } from '@dxos/echo-schema';
10
+ import { findNode, getDiscriminatedType, isDiscriminatedUnion, SimpleType } from '@dxos/effect';
11
+ import { invariant } from '@dxos/invariant';
12
+ import { IconButton, useTranslation } from '@dxos/react-ui';
13
+ import { mx } from '@dxos/react-ui-theme';
14
+ import { getSchemaProperties, type SchemaProperty } from '@dxos/schema';
15
+
16
+ import { type ComponentLookup } from './Form';
17
+ import { FormField } from './FormContent';
18
+ import { useFormValues, type FormInputStateProps } from './FormContext';
19
+ import { InputHeader, type InputComponent } from './Input';
20
+ import { translationKey } from '../../translations';
21
+
22
+ const padding = 'px-2';
23
+
24
+ type ArrayFieldProps = {
25
+ property: SchemaProperty<any>;
26
+ readonly?: boolean;
27
+ inputProps: FormInputStateProps;
28
+ path?: (string | number)[];
29
+ Custom?: Partial<Record<string, InputComponent>>;
30
+ lookupComponent?: ComponentLookup;
31
+ };
32
+
33
+ export const ArrayField = ({ property, readonly, path, inputProps, Custom, lookupComponent }: ArrayFieldProps) => {
34
+ const { t } = useTranslation(translationKey);
35
+ const { ast, name, type, title } = property;
36
+ const values = useFormValues(path ?? []) as any[];
37
+ invariant(Array.isArray(values));
38
+ const label = title ?? pipe(name, capitalize);
39
+
40
+ const tupleType = findNode(ast, AST.isTupleType);
41
+ const elementType = (tupleType as AST.TupleType | undefined)?.rest[0]?.type;
42
+
43
+ const getDefaultObjectValue = (typeNode: AST.AST): any => {
44
+ const baseNode = findNode(typeNode, isDiscriminatedUnion);
45
+ const typeLiteral = baseNode ? getDiscriminatedType(baseNode, {}) : findNode(typeNode, AST.isTypeLiteral);
46
+ if (!typeLiteral) {
47
+ return {};
48
+ }
49
+
50
+ return Object.fromEntries(getSchemaProperties(typeLiteral, {}).map((prop) => [prop.name, prop.defaultValue]));
51
+ };
52
+
53
+ const newValue = useMemo(() => {
54
+ if (type === 'object' && elementType) {
55
+ return getDefaultObjectValue(elementType);
56
+ } else {
57
+ return SimpleType.getDefaultValue(type);
58
+ }
59
+ }, [type, elementType]);
60
+
61
+ const handleAdd = useCallback(() => {
62
+ inputProps.onValueChange(type, [...values, newValue]);
63
+ }, [type, elementType, inputProps, newValue, values]);
64
+
65
+ const handleRemove = useCallback(
66
+ (index: number) => {
67
+ const newValues = values.filter((_, i) => i !== index);
68
+ inputProps.onValueChange(type, newValues);
69
+ },
70
+ [type, inputProps, values],
71
+ );
72
+
73
+ if (!elementType) {
74
+ return null;
75
+ }
76
+
77
+ return (
78
+ <div role='none' className={mx(padding)}>
79
+ <InputHeader>{label}</InputHeader>
80
+ <div role='none' className='flex flex-col gap-1'>
81
+ {values.map((_value, index) => (
82
+ <div key={index} role='none' className='flex items-center gap-1'>
83
+ <div role='none' className='flex-1'>
84
+ <FormField
85
+ property={{
86
+ ...property,
87
+ array: false, // NOTE(ZaymonFC): This breaks arrays of arrays but ¯\_(ツ)_/¯. Ping me if you need that.
88
+ ast: elementType,
89
+ }}
90
+ path={[...(path ?? []), index]}
91
+ readonly={readonly}
92
+ inline
93
+ Custom={Custom}
94
+ lookupComponent={lookupComponent}
95
+ />
96
+ </div>
97
+ <IconButton
98
+ icon='ph--trash--regular'
99
+ iconOnly
100
+ label={t('button remove')}
101
+ onClick={() => handleRemove(index)}
102
+ />
103
+ </div>
104
+ ))}
105
+ </div>
106
+ <div role='none' className='flex justify-between items-center plb-1'>
107
+ <IconButton icon='ph--plus--regular' iconOnly label={t('button add')} onClick={handleAdd} />
108
+ </div>
109
+ </div>
110
+ );
111
+ };
@@ -4,13 +4,11 @@
4
4
 
5
5
  import React from 'react';
6
6
 
7
- import { type BaseObject } from '@dxos/echo-schema';
8
7
  import { Input, Select } from '@dxos/react-ui';
9
8
 
10
9
  import { type InputProps, InputHeader } from './Input';
11
10
 
12
- export const TextInput = <T extends BaseObject>({
13
- property,
11
+ export const TextInput = ({
14
12
  type,
15
13
  label,
16
14
  inputOnly,
@@ -20,8 +18,8 @@ export const TextInput = <T extends BaseObject>({
20
18
  getValue,
21
19
  onValueChange,
22
20
  onBlur,
23
- }: InputProps<T>) => {
24
- const { status, error } = getStatus?.(property);
21
+ }: InputProps) => {
22
+ const { status, error } = getStatus();
25
23
 
26
24
  return (
27
25
  <Input.Root validationValence={status}>
@@ -33,8 +31,8 @@ export const TextInput = <T extends BaseObject>({
33
31
  <Input.TextInput
34
32
  disabled={disabled}
35
33
  placeholder={placeholder}
36
- value={getValue(property) ?? ''}
37
- onChange={(event) => onValueChange(property, type, event.target.value)}
34
+ value={getValue() ?? ''}
35
+ onChange={(event) => onValueChange(type, event.target.value)}
38
36
  onBlur={onBlur}
39
37
  />
40
38
  {inputOnly && <Input.Validation>{error}</Input.Validation>}
@@ -42,8 +40,7 @@ export const TextInput = <T extends BaseObject>({
42
40
  );
43
41
  };
44
42
 
45
- export const NumberInput = <T extends BaseObject>({
46
- property,
43
+ export const NumberInput = ({
47
44
  type,
48
45
  label,
49
46
  inputOnly,
@@ -53,8 +50,8 @@ export const NumberInput = <T extends BaseObject>({
53
50
  getValue,
54
51
  onValueChange,
55
52
  onBlur,
56
- }: InputProps<T>) => {
57
- const { status, error } = getStatus?.(property);
53
+ }: InputProps) => {
54
+ const { status, error } = getStatus();
58
55
 
59
56
  return (
60
57
  <Input.Root validationValence={status}>
@@ -67,8 +64,8 @@ export const NumberInput = <T extends BaseObject>({
67
64
  type='number'
68
65
  disabled={disabled}
69
66
  placeholder={placeholder}
70
- value={getValue(property) ?? 0}
71
- onChange={(event) => onValueChange(property, type, event.target.value)}
67
+ value={getValue() ?? 0}
68
+ onChange={(event) => onValueChange(type, event.target.value)}
72
69
  onBlur={onBlur}
73
70
  />
74
71
  {inputOnly && <Input.DescriptionAndValidation>{error}</Input.DescriptionAndValidation>}
@@ -76,16 +73,8 @@ export const NumberInput = <T extends BaseObject>({
76
73
  );
77
74
  };
78
75
 
79
- export const BooleanInput = <T extends BaseObject>({
80
- property,
81
- type,
82
- label,
83
- inputOnly,
84
- getStatus,
85
- getValue,
86
- onValueChange,
87
- }: InputProps<T>) => {
88
- const { status, error } = getStatus?.(property);
76
+ export const BooleanInput = ({ type, label, inputOnly, getStatus, getValue, onValueChange }: InputProps) => {
77
+ const { status, error } = getStatus();
89
78
 
90
79
  // TODO(burdon): Runtime error:
91
80
  // Warning: Unknown event handler property `onCheckedChange`. It will be ignored.
@@ -96,21 +85,17 @@ export const BooleanInput = <T extends BaseObject>({
96
85
  <Input.Label>{label}</Input.Label>
97
86
  </InputHeader>
98
87
  )}
99
- <Input.Switch
100
- checked={getValue<boolean>(property)}
101
- onCheckedChange={(value) => onValueChange(property, type, value)}
102
- />
88
+ <Input.Switch checked={getValue()} onCheckedChange={(value) => onValueChange(type, value)} />
103
89
  {inputOnly && <Input.DescriptionAndValidation>{error}</Input.DescriptionAndValidation>}
104
90
  </Input.Root>
105
91
  );
106
92
  };
107
93
 
108
- export type SelectInputOptions<T extends BaseObject> = InputProps<T> & {
94
+ export type SelectInputOptions = InputProps & {
109
95
  options?: Array<{ value: string | number; label?: string }>;
110
96
  };
111
97
 
112
- export const SelectInput = <T extends BaseObject>({
113
- property,
98
+ export const SelectInput = ({
114
99
  type,
115
100
  label,
116
101
  inputOnly,
@@ -120,8 +105,8 @@ export const SelectInput = <T extends BaseObject>({
120
105
  getStatus,
121
106
  getValue,
122
107
  onValueChange,
123
- }: SelectInputOptions<T>) => {
124
- const { status, error } = getStatus?.(property);
108
+ }: SelectInputOptions) => {
109
+ const { status, error } = getStatus();
125
110
 
126
111
  return (
127
112
  <Input.Root validationValence={status}>
@@ -130,7 +115,7 @@ export const SelectInput = <T extends BaseObject>({
130
115
  <Input.Label>{label}</Input.Label>
131
116
  </InputHeader>
132
117
  )}
133
- <Select.Root value={getValue(property)} onValueChange={(value) => onValueChange(property, type, value)}>
118
+ <Select.Root value={getValue()} onValueChange={(value) => onValueChange(type, value)}>
134
119
  {/* TODO(burdon): Placeholder not working? */}
135
120
  <Select.TriggerButton classNames='is-full' disabled={disabled} placeholder={placeholder} />
136
121
  <Select.Portal>
@@ -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,7 +78,6 @@ 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,
@@ -89,6 +88,17 @@ export const Org: Story<Testing.OrgSchemaType> = {
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,