@dxos/react-ui-form 0.8.2-main.fbd8ed0 → 0.8.2-staging.4d6ad0f

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 (53) hide show
  1. package/dist/lib/browser/index.mjs +1440 -1244
  2. package/dist/lib/browser/index.mjs.map +3 -3
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node/index.cjs +1426 -1230
  5. package/dist/lib/node/index.cjs.map +3 -3
  6. package/dist/lib/node/meta.json +1 -1
  7. package/dist/lib/node-esm/index.mjs +1440 -1244
  8. package/dist/lib/node-esm/index.mjs.map +3 -3
  9. package/dist/lib/node-esm/meta.json +1 -1
  10. package/dist/types/src/components/ControlSection/ControlSection.d.ts +1 -1
  11. package/dist/types/src/components/ControlSection/ControlSection.d.ts.map +1 -1
  12. package/dist/types/src/components/FieldEditor/FieldEditor.d.ts.map +1 -1
  13. package/dist/types/src/components/FieldEditor/FieldEditor.test.d.ts +2 -0
  14. package/dist/types/src/components/FieldEditor/FieldEditor.test.d.ts.map +1 -0
  15. package/dist/types/src/components/Form/ArrayField.d.ts.map +1 -1
  16. package/dist/types/src/components/Form/Defaults.d.ts +4 -4
  17. package/dist/types/src/components/Form/Defaults.d.ts.map +1 -1
  18. package/dist/types/src/components/Form/Form.d.ts.map +1 -1
  19. package/dist/types/src/components/Form/Form.stories.d.ts +1 -2
  20. package/dist/types/src/components/Form/Form.stories.d.ts.map +1 -1
  21. package/dist/types/src/components/Form/FormActions.d.ts +1 -1
  22. package/dist/types/src/components/Form/FormActions.d.ts.map +1 -1
  23. package/dist/types/src/components/Form/FormContent.d.ts.map +1 -1
  24. package/dist/types/src/components/Form/FormContext.d.ts.map +1 -1
  25. package/dist/types/src/components/Form/RefField.d.ts.map +1 -1
  26. package/dist/types/src/components/ViewEditor/ViewEditor.d.ts.map +1 -1
  27. package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts +5 -3
  28. package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts.map +1 -1
  29. package/dist/types/src/components/testing.d.ts.map +1 -1
  30. package/dist/types/src/hooks/useRefQueryLookupHandler.d.ts.map +1 -1
  31. package/dist/types/src/translations.d.ts +5 -0
  32. package/dist/types/src/translations.d.ts.map +1 -1
  33. package/dist/types/src/vitest-setup.d.ts +2 -0
  34. package/dist/types/src/vitest-setup.d.ts.map +1 -0
  35. package/package.json +27 -25
  36. package/src/components/ControlSection/ControlSection.tsx +2 -2
  37. package/src/components/FieldEditor/FieldEditor.test.tsx +49 -0
  38. package/src/components/FieldEditor/FieldEditor.tsx +3 -2
  39. package/src/components/Form/ArrayField.tsx +51 -34
  40. package/src/components/Form/Defaults.tsx +74 -48
  41. package/src/components/Form/Form.stories.tsx +77 -101
  42. package/src/components/Form/Form.tsx +1 -2
  43. package/src/components/Form/FormActions.tsx +15 -18
  44. package/src/components/Form/FormContent.tsx +24 -29
  45. package/src/components/Form/FormContext.tsx +11 -7
  46. package/src/components/Form/Input.tsx +1 -1
  47. package/src/components/Form/RefField.tsx +30 -10
  48. package/src/components/ViewEditor/ViewEditor.stories.tsx +14 -5
  49. package/src/components/ViewEditor/ViewEditor.tsx +51 -24
  50. package/src/components/testing.tsx +5 -9
  51. package/src/hooks/useRefQueryLookupHandler.ts +1 -3
  52. package/src/translations.ts +9 -2
  53. package/src/vitest-setup.ts +11 -0
@@ -0,0 +1,49 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { composeStories } from '@storybook/react';
6
+ import { screen, cleanup, fireEvent, act } from '@testing-library/react';
7
+ import { afterEach, describe, expect, test } from 'vitest';
8
+
9
+ import * as stories from './FieldEditor.stories';
10
+
11
+ const { Default } = composeStories(stories);
12
+
13
+ describe('FieldEditor', () => {
14
+ afterEach(() => {
15
+ cleanup();
16
+ });
17
+
18
+ test('update field type', async () => {
19
+ await Default.run();
20
+
21
+ expect(screen.getByText('Type format')).toBeInTheDocument();
22
+ expect(screen.getByText('String')).toBeInTheDocument();
23
+
24
+ {
25
+ const debug = JSON.parse(screen.getByTestId('debug').textContent!);
26
+ const name = debug.props.projection._schema.properties.name;
27
+ expect(name.type).toBe('string');
28
+ expect(name.description).toBe('Full name.');
29
+ }
30
+
31
+ fireEvent.click(screen.getByRole('combobox'));
32
+ const options = screen.getAllByRole('option');
33
+ const option = options.find((option) => option.textContent === 'Number');
34
+ fireEvent.click(option!);
35
+ expect(screen.getByText('Number')).toBeInTheDocument();
36
+
37
+ await act(async () => {
38
+ fireEvent.click(screen.getByTestId('save-button'));
39
+ });
40
+
41
+ {
42
+ const debug = JSON.parse(screen.getByTestId('debug').textContent!);
43
+ const name = debug.props.projection._schema.properties.name;
44
+ expect(name.type).toBe('number');
45
+ expect(name.format).toBe('number');
46
+ expect(name.description).toBe('Full name.');
47
+ }
48
+ });
49
+ });
@@ -79,6 +79,7 @@ export const FieldEditor = ({ view, projection, field, registry, onSave, onCance
79
79
  }
80
80
  return { fieldSchema };
81
81
  });
82
+
82
83
  setReferenceSchema((prev) => {
83
84
  if (_props.referenceSchema !== prev?.typename) {
84
85
  const newSchema = schemas.find((schema) => schema.typename === _props.referenceSchema);
@@ -129,7 +130,7 @@ export const FieldEditor = ({ view, projection, field, registry, onSave, onCance
129
130
  onSave();
130
131
  }, [onSave]);
131
132
 
132
- const Custom: Partial<Record<string, InputComponent>> = useMemo(
133
+ const custom: Partial<Record<string, InputComponent>> = useMemo(
133
134
  () => ({
134
135
  ['format' satisfies keyof PropertyType]: (props) => (
135
136
  <SelectInput
@@ -187,7 +188,7 @@ export const FieldEditor = ({ view, projection, field, registry, onSave, onCance
187
188
  onValidate={handleValidate}
188
189
  onSave={handleSave}
189
190
  onCancel={handleCancel}
190
- Custom={Custom}
191
+ Custom={custom}
191
192
  />
192
193
  );
193
194
  };
@@ -4,12 +4,11 @@
4
4
 
5
5
  import { SchemaAST, pipe } from 'effect';
6
6
  import { capitalize } from 'effect/String';
7
- import React, { useCallback } from 'react';
7
+ import React, { Fragment, useCallback } from 'react';
8
8
 
9
9
  import { findNode, getDiscriminatedType, isDiscriminatedUnion, SimpleType } from '@dxos/effect';
10
10
  import { invariant } from '@dxos/invariant';
11
11
  import { IconButton, useTranslation } from '@dxos/react-ui';
12
- import { mx } from '@dxos/react-ui-theme';
13
12
  import { getSchemaProperties, type SchemaProperty } from '@dxos/schema';
14
13
 
15
14
  import { type ComponentLookup } from './Form';
@@ -19,8 +18,6 @@ import { InputHeader, type InputComponent } from './Input';
19
18
  import { translationKey } from '../../translations';
20
19
  import { findArrayElementType } from '../../util';
21
20
 
22
- const padding = 'px-2';
23
-
24
21
  type ArrayFieldProps = {
25
22
  property: SchemaProperty<any>;
26
23
  readonly?: boolean;
@@ -70,38 +67,58 @@ export const ArrayField = ({ property, readonly, path, inputProps, Custom, looku
70
67
  return null;
71
68
  }
72
69
 
73
- return (
74
- <div role='none' className={mx(padding)}>
70
+ return readonly && values.length < 1 ? null : (
71
+ <>
75
72
  <InputHeader>{label}</InputHeader>
76
- <div role='none' className='flex flex-col gap-1'>
77
- {values.map((_value, index) => (
78
- <div key={index} role='none' className='flex items-center gap-1'>
79
- <div role='none' className='flex-1'>
80
- <FormField
81
- property={{
82
- ...property,
83
- array: false, // NOTE(ZaymonFC): This breaks arrays of arrays but ¯\_(ツ)_/¯. Ping me if you need that.
84
- ast: elementType,
85
- }}
86
- path={[...(path ?? []), index]}
87
- readonly={readonly}
88
- inline
89
- Custom={Custom}
90
- lookupComponent={lookupComponent}
91
- />
92
- </div>
93
- <IconButton
94
- icon='ph--trash--regular'
95
- iconOnly
96
- label={t('button remove')}
97
- onClick={() => handleRemove(index)}
73
+ <div
74
+ role='none'
75
+ className={
76
+ readonly
77
+ ? 'flex flex-wrap gap-1 mlb-1'
78
+ : values.length > 0
79
+ ? 'grid gap-1 grid-cols-[1fr_min-content] mlb-1'
80
+ : 'hidden'
81
+ }
82
+ >
83
+ {values.map((_value, index) => {
84
+ const field = (
85
+ <FormField
86
+ property={{
87
+ ...property,
88
+ array: false, // NOTE(ZaymonFC): This breaks arrays of arrays but ¯\_(ツ)_/¯. Ping me if you need that.
89
+ ast: elementType,
90
+ }}
91
+ path={[...(path ?? []), index]}
92
+ readonly={readonly}
93
+ inline
94
+ Custom={Custom}
95
+ lookupComponent={lookupComponent}
98
96
  />
99
- </div>
100
- ))}
101
- </div>
102
- <div role='none' className='flex justify-between items-center plb-1'>
103
- <IconButton icon='ph--plus--regular' iconOnly label={t('button add')} onClick={handleAdd} />
97
+ );
98
+ return readonly ? (
99
+ field
100
+ ) : (
101
+ <Fragment key={index}>
102
+ <div role='none'>{field}</div>
103
+ <IconButton
104
+ icon='ph--trash--regular'
105
+ iconOnly
106
+ label={t('button remove')}
107
+ onClick={() => handleRemove(index)}
108
+ classNames='self-center'
109
+ />
110
+ </Fragment>
111
+ );
112
+ })}
104
113
  </div>
105
- </div>
114
+ {!readonly && (
115
+ <IconButton
116
+ classNames='is-full mlb-1 flex'
117
+ icon='ph--plus--regular'
118
+ label={t('add field')}
119
+ onClick={handleAdd}
120
+ />
121
+ )}
122
+ </>
106
123
  );
107
124
  };
@@ -4,9 +4,10 @@
4
4
 
5
5
  import React, { useCallback, useEffect, useRef } from 'react';
6
6
 
7
- import { Input, Select } from '@dxos/react-ui';
7
+ import { Input, Select, useTranslation } from '@dxos/react-ui';
8
8
 
9
9
  import { type InputProps, InputHeader } from './Input';
10
+ import { translationKey } from '../../translations';
10
11
 
11
12
  export const TextInput = ({
12
13
  type,
@@ -21,20 +22,26 @@ export const TextInput = ({
21
22
  }: InputProps) => {
22
23
  const { status, error } = getStatus();
23
24
 
24
- return (
25
+ return disabled && !getValue() ? null : disabled && inputOnly ? (
26
+ <p>{getValue() ?? ''}</p>
27
+ ) : (
25
28
  <Input.Root validationValence={status}>
26
29
  {!inputOnly && (
27
30
  <InputHeader error={error}>
28
31
  <Input.Label>{label}</Input.Label>
29
32
  </InputHeader>
30
33
  )}
31
- <Input.TextInput
32
- disabled={disabled}
33
- placeholder={placeholder}
34
- value={getValue() ?? ''}
35
- onChange={(event) => onValueChange(type, event.target.value)}
36
- onBlur={onBlur}
37
- />
34
+ {disabled ? (
35
+ <p>{getValue() ?? ''}</p>
36
+ ) : (
37
+ <Input.TextInput
38
+ disabled={disabled}
39
+ placeholder={placeholder}
40
+ value={getValue() ?? ''}
41
+ onChange={(event) => onValueChange(type, event.target.value)}
42
+ onBlur={onBlur}
43
+ />
44
+ )}
38
45
  {inputOnly && <Input.Validation>{error}</Input.Validation>}
39
46
  </Input.Root>
40
47
  );
@@ -53,29 +60,36 @@ export const NumberInput = ({
53
60
  }: InputProps) => {
54
61
  const { status, error } = getStatus();
55
62
 
56
- return (
63
+ return disabled && !getValue() ? null : disabled && inputOnly ? (
64
+ <p>{getValue() ?? ''}</p>
65
+ ) : (
57
66
  <Input.Root validationValence={status}>
58
67
  {!inputOnly && (
59
68
  <InputHeader error={error}>
60
69
  <Input.Label>{label}</Input.Label>
61
70
  </InputHeader>
62
71
  )}
63
- <Input.TextInput
64
- type='number'
65
- disabled={disabled}
66
- placeholder={placeholder}
67
- value={getValue() ?? 0}
68
- onChange={(event) => onValueChange(type, event.target.value)}
69
- onBlur={onBlur}
70
- />
72
+ {disabled ? (
73
+ <p>{getValue() ?? ''}</p>
74
+ ) : (
75
+ <Input.TextInput
76
+ type='number'
77
+ disabled={disabled}
78
+ placeholder={placeholder}
79
+ value={getValue() ?? 0}
80
+ onChange={(event) => onValueChange(type, event.target.value)}
81
+ onBlur={onBlur}
82
+ />
83
+ )}
71
84
  {inputOnly && <Input.DescriptionAndValidation>{error}</Input.DescriptionAndValidation>}
72
85
  </Input.Root>
73
86
  );
74
87
  };
75
88
 
76
- export const BooleanInput = ({ type, label, inputOnly, getStatus, getValue, onValueChange }: InputProps) => {
89
+ export const BooleanInput = ({ type, label, inputOnly, getStatus, getValue, onValueChange, disabled }: InputProps) => {
77
90
  const { status, error } = getStatus();
78
91
  const checked = Boolean(getValue());
92
+ const { t } = useTranslation(translationKey);
79
93
 
80
94
  return (
81
95
  <Input.Root validationValence={status}>
@@ -84,7 +98,11 @@ export const BooleanInput = ({ type, label, inputOnly, getStatus, getValue, onVa
84
98
  <Input.Label>{label}</Input.Label>
85
99
  </InputHeader>
86
100
  )}
87
- <Input.Switch checked={checked} onCheckedChange={(value) => onValueChange(type, value)} />
101
+ {disabled ? (
102
+ <p>{t(checked ? 'boolean input true value' : 'boolean input false value')}</p>
103
+ ) : (
104
+ <Input.Switch checked={checked} onCheckedChange={(value) => onValueChange(type, value)} />
105
+ )}
88
106
  {inputOnly && <Input.DescriptionAndValidation>{error}</Input.DescriptionAndValidation>}
89
107
  </Input.Root>
90
108
  );
@@ -110,29 +128,33 @@ export const SelectInput = ({
110
128
  const value = getValue() as string | undefined;
111
129
  const handleValueChange = useCallback((value: string | number) => onValueChange(type, value), [type, onValueChange]);
112
130
 
113
- return (
131
+ return disabled && !value ? null : (
114
132
  <Input.Root validationValence={status}>
115
133
  {!inputOnly && (
116
134
  <InputHeader error={error}>
117
135
  <Input.Label>{label}</Input.Label>
118
136
  </InputHeader>
119
137
  )}
120
- <Select.Root value={value} onValueChange={handleValueChange}>
121
- {/* TODO(burdon): Placeholder not working? */}
122
- <Select.TriggerButton classNames='is-full' disabled={disabled} placeholder={placeholder} />
123
- <Select.Portal>
124
- <Select.Content>
125
- <Select.Viewport>
126
- {options?.map(({ value, label }) => (
127
- // NOTE: Numeric values are converted to and from strings.
128
- <Select.Option key={String(value)} value={String(value)}>
129
- {label ?? String(value)}
130
- </Select.Option>
131
- ))}
132
- </Select.Viewport>
133
- </Select.Content>
134
- </Select.Portal>
135
- </Select.Root>
138
+ {disabled ? (
139
+ <p>{options?.find(({ value: optionValue }) => optionValue === value)?.label ?? String(value)}</p>
140
+ ) : (
141
+ <Select.Root value={value} onValueChange={handleValueChange}>
142
+ {/* TODO(burdon): Placeholder not working? */}
143
+ <Select.TriggerButton classNames='is-full' disabled={disabled} placeholder={placeholder} />
144
+ <Select.Portal>
145
+ <Select.Content>
146
+ <Select.Viewport>
147
+ {options?.map(({ value, label }) => (
148
+ // NOTE: Numeric values are converted to and from strings.
149
+ <Select.Option key={String(value)} value={String(value)}>
150
+ {label ?? String(value)}
151
+ </Select.Option>
152
+ ))}
153
+ </Select.Viewport>
154
+ </Select.Content>
155
+ </Select.Portal>
156
+ </Select.Root>
157
+ )}
136
158
  {inputOnly && <Input.DescriptionAndValidation>{error}</Input.DescriptionAndValidation>}
137
159
  </Input.Root>
138
160
  );
@@ -180,17 +202,21 @@ export const MarkdownInput = ({
180
202
  <Input.Label>{label}</Input.Label>
181
203
  </InputHeader>
182
204
  )}
183
- <Input.TextArea
184
- ref={textareaRef}
185
- disabled={disabled}
186
- placeholder={placeholder}
187
- value={getValue() ?? ''}
188
- classNames={'min-bs-auto max-h-40 overflow-auto'}
189
- onChange={(event) => onValueChange(type, event.target.value)}
190
- onBlur={onBlur}
191
- style={{ resize: 'none' }}
192
- spellCheck={false}
193
- />
205
+ {disabled ? (
206
+ <p>{getValue() ?? ''}</p>
207
+ ) : (
208
+ <Input.TextArea
209
+ ref={textareaRef}
210
+ disabled={disabled}
211
+ placeholder={placeholder}
212
+ value={getValue() ?? ''}
213
+ classNames={'min-bs-auto max-h-40 overflow-auto'}
214
+ onChange={(event) => onValueChange(type, event.target.value)}
215
+ onBlur={onBlur}
216
+ style={{ resize: 'none' }}
217
+ spellCheck={false}
218
+ />
219
+ )}
194
220
  {inputOnly && <Input.Validation>{error}</Input.Validation>}
195
221
  </Input.Root>
196
222
  );