@dxos/react-ui-form 0.8.4-main.b97322e → 0.8.4-main.bcb3aa67d6

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 (205) hide show
  1. package/README.md +0 -1
  2. package/dist/lib/browser/index.mjs +2315 -2116
  3. package/dist/lib/browser/index.mjs.map +4 -4
  4. package/dist/lib/browser/meta.json +1 -1
  5. package/dist/lib/node-esm/index.mjs +2315 -2116
  6. package/dist/lib/node-esm/index.mjs.map +4 -4
  7. package/dist/lib/node-esm/meta.json +1 -1
  8. package/dist/types/src/components/FieldEditor/FieldEditor.d.ts +8 -7
  9. package/dist/types/src/components/FieldEditor/FieldEditor.d.ts.map +1 -1
  10. package/dist/types/src/components/FieldEditor/FieldEditor.stories.d.ts +82 -5
  11. package/dist/types/src/components/FieldEditor/FieldEditor.stories.d.ts.map +1 -1
  12. package/dist/types/src/components/Form/Form.d.ts +84 -25
  13. package/dist/types/src/components/Form/Form.d.ts.map +1 -1
  14. package/dist/types/src/components/Form/Form.stories.d.ts +127 -55
  15. package/dist/types/src/components/Form/Form.stories.d.ts.map +1 -1
  16. package/dist/types/src/components/Form/FormField.d.ts +43 -0
  17. package/dist/types/src/components/Form/FormField.d.ts.map +1 -0
  18. package/dist/types/src/components/Form/FormFieldComponent.d.ts +77 -0
  19. package/dist/types/src/components/Form/FormFieldComponent.d.ts.map +1 -0
  20. package/dist/types/src/components/Form/FormFieldComponent.stories.d.ts +9 -0
  21. package/dist/types/src/components/Form/FormFieldComponent.stories.d.ts.map +1 -0
  22. package/dist/types/src/components/Form/FormFieldSet.d.ts +18 -0
  23. package/dist/types/src/components/Form/FormFieldSet.d.ts.map +1 -0
  24. package/dist/types/src/components/Form/fields/ArrayField.d.ts +18 -0
  25. package/dist/types/src/components/Form/fields/ArrayField.d.ts.map +1 -0
  26. package/dist/types/src/components/Form/fields/BooleanField.d.ts +4 -0
  27. package/dist/types/src/components/Form/fields/BooleanField.d.ts.map +1 -0
  28. package/dist/types/src/components/Form/fields/GeoPointField.d.ts +5 -0
  29. package/dist/types/src/components/Form/fields/GeoPointField.d.ts.map +1 -0
  30. package/dist/types/src/components/Form/fields/MarkdownField.d.ts +4 -0
  31. package/dist/types/src/components/Form/fields/MarkdownField.d.ts.map +1 -0
  32. package/dist/types/src/components/Form/fields/NumberField.d.ts +4 -0
  33. package/dist/types/src/components/Form/fields/NumberField.d.ts.map +1 -0
  34. package/dist/types/src/components/Form/fields/RefField.d.ts +16 -0
  35. package/dist/types/src/components/Form/fields/RefField.d.ts.map +1 -0
  36. package/dist/types/src/components/Form/fields/SelectField.d.ts +10 -0
  37. package/dist/types/src/components/Form/fields/SelectField.d.ts.map +1 -0
  38. package/dist/types/src/components/Form/fields/SelectOptionField.d.ts +5 -0
  39. package/dist/types/src/components/Form/fields/SelectOptionField.d.ts.map +1 -0
  40. package/dist/types/src/components/Form/fields/SelectOptionField.stories.d.ts +82 -0
  41. package/dist/types/src/components/Form/fields/SelectOptionField.stories.d.ts.map +1 -0
  42. package/dist/types/src/components/Form/fields/TextAreaField.d.ts +4 -0
  43. package/dist/types/src/components/Form/fields/TextAreaField.d.ts.map +1 -0
  44. package/dist/types/src/components/Form/fields/TextField.d.ts +4 -0
  45. package/dist/types/src/components/Form/fields/TextField.d.ts.map +1 -0
  46. package/dist/types/src/components/Form/fields/TupleField.d.ts +6 -0
  47. package/dist/types/src/components/Form/fields/TupleField.d.ts.map +1 -0
  48. package/dist/types/src/components/Form/fields/index.d.ts +12 -0
  49. package/dist/types/src/components/Form/fields/index.d.ts.map +1 -0
  50. package/dist/types/src/components/Form/index.d.ts +2 -4
  51. package/dist/types/src/components/Form/index.d.ts.map +1 -1
  52. package/dist/types/src/components/ObjectPicker/ObjectPicker.d.ts +54 -0
  53. package/dist/types/src/components/ObjectPicker/ObjectPicker.d.ts.map +1 -0
  54. package/dist/types/src/components/ObjectPicker/ObjectPicker.stories.d.ts +85 -0
  55. package/dist/types/src/components/ObjectPicker/ObjectPicker.stories.d.ts.map +1 -0
  56. package/dist/types/src/components/ObjectPicker/index.d.ts +2 -0
  57. package/dist/types/src/components/ObjectPicker/index.d.ts.map +1 -0
  58. package/dist/types/src/components/Settings/Settings.d.ts +39 -0
  59. package/dist/types/src/components/Settings/Settings.d.ts.map +1 -0
  60. package/dist/types/src/components/Settings/Settings.stories.d.ts +81 -0
  61. package/dist/types/src/components/Settings/Settings.stories.d.ts.map +1 -0
  62. package/dist/types/src/components/Settings/index.d.ts +2 -0
  63. package/dist/types/src/components/Settings/index.d.ts.map +1 -0
  64. package/dist/types/src/components/ViewEditor/ViewEditor.d.ts +22 -10
  65. package/dist/types/src/components/ViewEditor/ViewEditor.d.ts.map +1 -1
  66. package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts +84 -8
  67. package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts.map +1 -1
  68. package/dist/types/src/components/index.d.ts +2 -2
  69. package/dist/types/src/components/index.d.ts.map +1 -1
  70. package/dist/types/src/components/testing.d.ts +4 -3
  71. package/dist/types/src/components/testing.d.ts.map +1 -1
  72. package/dist/types/src/hooks/index.d.ts +2 -3
  73. package/dist/types/src/hooks/index.d.ts.map +1 -1
  74. package/dist/types/src/hooks/useFormHandler.d.ts +82 -0
  75. package/dist/types/src/hooks/useFormHandler.d.ts.map +1 -0
  76. package/dist/types/src/hooks/useKeyHandler.d.ts +6 -0
  77. package/dist/types/src/hooks/useKeyHandler.d.ts.map +1 -0
  78. package/dist/types/src/translations.d.ts +63 -54
  79. package/dist/types/src/translations.d.ts.map +1 -1
  80. package/dist/types/src/util/field.d.ts +3 -3
  81. package/dist/types/src/util/field.d.ts.map +1 -1
  82. package/dist/types/src/util/formatting.d.ts +2 -2
  83. package/dist/types/src/util/formatting.d.ts.map +1 -1
  84. package/dist/types/src/util/index.d.ts +1 -1
  85. package/dist/types/src/util/index.d.ts.map +1 -1
  86. package/dist/types/src/util/properties.d.ts +7 -0
  87. package/dist/types/src/util/properties.d.ts.map +1 -0
  88. package/dist/types/src/util/refs.d.ts +3 -3
  89. package/dist/types/src/util/refs.d.ts.map +1 -1
  90. package/dist/types/tsconfig.tsbuildinfo +1 -1
  91. package/package.json +52 -43
  92. package/src/components/FieldEditor/FieldEditor.stories.tsx +51 -32
  93. package/src/components/FieldEditor/FieldEditor.test.tsx +3 -2
  94. package/src/components/FieldEditor/FieldEditor.tsx +87 -83
  95. package/src/components/Form/AUDIT.md +17 -0
  96. package/src/components/Form/Form.stories.tsx +154 -219
  97. package/src/components/Form/Form.tsx +314 -97
  98. package/src/components/Form/FormField.tsx +305 -0
  99. package/src/components/Form/FormFieldComponent.stories.tsx +65 -0
  100. package/src/components/Form/FormFieldComponent.tsx +171 -0
  101. package/src/components/Form/FormFieldSet.tsx +129 -0
  102. package/src/components/Form/README.md +3 -0
  103. package/src/components/Form/fields/ArrayField.tsx +154 -0
  104. package/src/components/Form/fields/BooleanField.tsx +22 -0
  105. package/src/components/Form/fields/GeoPointField.tsx +120 -0
  106. package/src/components/Form/fields/MarkdownField.tsx +78 -0
  107. package/src/components/Form/fields/NumberField.tsx +39 -0
  108. package/src/components/Form/fields/RefField.tsx +199 -0
  109. package/src/components/Form/fields/SelectField.tsx +65 -0
  110. package/src/components/Form/fields/SelectOptionField.stories.tsx +64 -0
  111. package/src/components/Form/{custom/SelectOptionsInput.tsx → fields/SelectOptionField.tsx} +49 -45
  112. package/src/components/Form/fields/TextAreaField.tsx +38 -0
  113. package/src/components/Form/fields/TextField.tsx +38 -0
  114. package/src/components/Form/{custom/TupleInput.tsx → fields/TupleField.tsx} +17 -10
  115. package/src/components/Form/fields/index.ts +15 -0
  116. package/src/components/Form/index.ts +3 -4
  117. package/src/components/ObjectPicker/ObjectPicker.stories.tsx +210 -0
  118. package/src/components/ObjectPicker/ObjectPicker.tsx +186 -0
  119. package/src/components/{ControlSection → ObjectPicker}/index.ts +1 -1
  120. package/src/components/Settings/Settings.stories.tsx +56 -0
  121. package/src/components/Settings/Settings.tsx +129 -0
  122. package/src/components/Settings/index.ts +5 -0
  123. package/src/components/ViewEditor/ViewEditor.stories.tsx +141 -63
  124. package/src/components/ViewEditor/ViewEditor.test.tsx +17 -18
  125. package/src/components/ViewEditor/ViewEditor.tsx +353 -203
  126. package/src/components/index.ts +2 -3
  127. package/src/components/testing.tsx +33 -13
  128. package/src/hooks/index.ts +2 -3
  129. package/src/hooks/useFormHandler.ts +380 -0
  130. package/src/hooks/useKeyHandler.tsx +37 -0
  131. package/src/translations.ts +66 -58
  132. package/src/util/field.ts +33 -43
  133. package/src/util/formatting.ts +22 -22
  134. package/src/util/index.ts +1 -1
  135. package/src/util/properties.ts +18 -0
  136. package/src/util/refs.ts +17 -19
  137. package/dist/types/src/components/ControlSection/ControlSection.d.ts +0 -23
  138. package/dist/types/src/components/ControlSection/ControlSection.d.ts.map +0 -1
  139. package/dist/types/src/components/ControlSection/ControlSection.stories.d.ts +0 -8
  140. package/dist/types/src/components/ControlSection/ControlSection.stories.d.ts.map +0 -1
  141. package/dist/types/src/components/ControlSection/index.d.ts +0 -2
  142. package/dist/types/src/components/ControlSection/index.d.ts.map +0 -1
  143. package/dist/types/src/components/Form/ArrayField.d.ts +0 -16
  144. package/dist/types/src/components/Form/ArrayField.d.ts.map +0 -1
  145. package/dist/types/src/components/Form/Defaults.d.ts +0 -14
  146. package/dist/types/src/components/Form/Defaults.d.ts.map +0 -1
  147. package/dist/types/src/components/Form/FormActions.d.ts +0 -9
  148. package/dist/types/src/components/Form/FormActions.d.ts.map +0 -1
  149. package/dist/types/src/components/Form/FormContent.d.ts +0 -57
  150. package/dist/types/src/components/Form/FormContent.d.ts.map +0 -1
  151. package/dist/types/src/components/Form/FormContext.d.ts +0 -23
  152. package/dist/types/src/components/Form/FormContext.d.ts.map +0 -1
  153. package/dist/types/src/components/Form/Input.d.ts +0 -26
  154. package/dist/types/src/components/Form/Input.d.ts.map +0 -1
  155. package/dist/types/src/components/Form/RefField.d.ts +0 -12
  156. package/dist/types/src/components/Form/RefField.d.ts.map +0 -1
  157. package/dist/types/src/components/Form/custom/GeoPointInput.d.ts +0 -4
  158. package/dist/types/src/components/Form/custom/GeoPointInput.d.ts.map +0 -1
  159. package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts +0 -4
  160. package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts.map +0 -1
  161. package/dist/types/src/components/Form/custom/SelectOptionsInput.stories.d.ts +0 -7
  162. package/dist/types/src/components/Form/custom/SelectOptionsInput.stories.d.ts.map +0 -1
  163. package/dist/types/src/components/Form/custom/TupleInput.d.ts +0 -6
  164. package/dist/types/src/components/Form/custom/TupleInput.d.ts.map +0 -1
  165. package/dist/types/src/components/Form/custom/index.d.ts +0 -4
  166. package/dist/types/src/components/Form/custom/index.d.ts.map +0 -1
  167. package/dist/types/src/components/Form/factory.d.ts +0 -8
  168. package/dist/types/src/components/Form/factory.d.ts.map +0 -1
  169. package/dist/types/src/components/deprecated/Form.d.ts +0 -15
  170. package/dist/types/src/components/deprecated/Form.d.ts.map +0 -1
  171. package/dist/types/src/components/deprecated/Form.stories.d.ts +0 -13
  172. package/dist/types/src/components/deprecated/Form.stories.d.ts.map +0 -1
  173. package/dist/types/src/components/deprecated/FormInput.d.ts +0 -16
  174. package/dist/types/src/components/deprecated/FormInput.d.ts.map +0 -1
  175. package/dist/types/src/components/deprecated/index.d.ts +0 -2
  176. package/dist/types/src/components/deprecated/index.d.ts.map +0 -1
  177. package/dist/types/src/hooks/useForm.d.ts +0 -67
  178. package/dist/types/src/hooks/useForm.d.ts.map +0 -1
  179. package/dist/types/src/hooks/useQueryRefOptions.d.ts +0 -23
  180. package/dist/types/src/hooks/useQueryRefOptions.d.ts.map +0 -1
  181. package/dist/types/src/hooks/useRefQueryLookupHandler.d.ts +0 -8
  182. package/dist/types/src/hooks/useRefQueryLookupHandler.d.ts.map +0 -1
  183. package/dist/types/src/util/schema.d.ts +0 -3
  184. package/dist/types/src/util/schema.d.ts.map +0 -1
  185. package/src/components/ControlSection/ControlSection.stories.tsx +0 -41
  186. package/src/components/ControlSection/ControlSection.tsx +0 -139
  187. package/src/components/Form/ArrayField.tsx +0 -124
  188. package/src/components/Form/Defaults.tsx +0 -203
  189. package/src/components/Form/FormActions.tsx +0 -53
  190. package/src/components/Form/FormContent.tsx +0 -240
  191. package/src/components/Form/FormContext.tsx +0 -110
  192. package/src/components/Form/Input.tsx +0 -51
  193. package/src/components/Form/RefField.tsx +0 -213
  194. package/src/components/Form/custom/GeoPointInput.tsx +0 -72
  195. package/src/components/Form/custom/SelectOptionsInput.stories.tsx +0 -61
  196. package/src/components/Form/custom/index.ts +0 -7
  197. package/src/components/Form/factory.tsx +0 -31
  198. package/src/components/deprecated/Form.stories.tsx +0 -62
  199. package/src/components/deprecated/Form.tsx +0 -119
  200. package/src/components/deprecated/FormInput.tsx +0 -71
  201. package/src/components/deprecated/index.ts +0 -5
  202. package/src/hooks/useForm.ts +0 -291
  203. package/src/hooks/useQueryRefOptions.ts +0 -53
  204. package/src/hooks/useRefQueryLookupHandler.ts +0 -39
  205. package/src/util/schema.ts +0 -14
@@ -1,203 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import React, { useCallback, useEffect, useRef } from 'react';
6
-
7
- import { Input, Select, useTranslation } from '@dxos/react-ui';
8
-
9
- import { type InputProps, InputHeader } from './Input';
10
- import { translationKey } from '../../translations';
11
-
12
- export const TextInput = ({
13
- type,
14
- label,
15
- inputOnly,
16
- disabled,
17
- placeholder,
18
- getStatus,
19
- getValue,
20
- onValueChange,
21
- onBlur,
22
- }: InputProps) => {
23
- const { status, error } = getStatus();
24
-
25
- return disabled && !getValue() ? null : disabled && inputOnly ? (
26
- <p>{getValue() ?? ''}</p>
27
- ) : (
28
- <Input.Root validationValence={status}>
29
- {!inputOnly && <InputHeader error={error} label={label} />}
30
- {disabled ? (
31
- <p>{getValue() ?? ''}</p>
32
- ) : (
33
- <Input.TextInput
34
- disabled={disabled}
35
- placeholder={placeholder}
36
- value={getValue() ?? ''}
37
- onChange={(event) => onValueChange(type, event.target.value)}
38
- onBlur={onBlur}
39
- />
40
- )}
41
- {inputOnly && <Input.Validation>{error}</Input.Validation>}
42
- </Input.Root>
43
- );
44
- };
45
-
46
- export const NumberInput = ({
47
- type,
48
- label,
49
- inputOnly,
50
- disabled,
51
- placeholder,
52
- getStatus,
53
- getValue,
54
- onValueChange,
55
- onBlur,
56
- }: InputProps) => {
57
- const { status, error } = getStatus();
58
-
59
- return disabled && !getValue() ? null : disabled && inputOnly ? (
60
- <p>{getValue() ?? ''}</p>
61
- ) : (
62
- <Input.Root validationValence={status}>
63
- {!inputOnly && <InputHeader error={error} label={label} />}
64
- {disabled ? (
65
- <p>{getValue() ?? ''}</p>
66
- ) : (
67
- <Input.TextInput
68
- type='number'
69
- disabled={disabled}
70
- placeholder={placeholder}
71
- value={getValue() ?? 0}
72
- onChange={(event) => onValueChange(type, event.target.value)}
73
- onBlur={onBlur}
74
- />
75
- )}
76
- {inputOnly && <Input.DescriptionAndValidation>{error}</Input.DescriptionAndValidation>}
77
- </Input.Root>
78
- );
79
- };
80
-
81
- export const BooleanInput = ({ type, label, inputOnly, getStatus, getValue, onValueChange, disabled }: InputProps) => {
82
- const { status, error } = getStatus();
83
- const checked = Boolean(getValue());
84
- const { t } = useTranslation(translationKey);
85
-
86
- return (
87
- <Input.Root validationValence={status}>
88
- {!inputOnly && <InputHeader error={error} label={label} />}
89
- {disabled ? (
90
- <p>{t(checked ? 'boolean input true value' : 'boolean input false value')}</p>
91
- ) : (
92
- <Input.Switch checked={checked} onCheckedChange={(value) => onValueChange(type, value)} />
93
- )}
94
- {inputOnly && <Input.DescriptionAndValidation>{error}</Input.DescriptionAndValidation>}
95
- </Input.Root>
96
- );
97
- };
98
-
99
- export type SelectInputOptions = InputProps & {
100
- options?: Array<{ value: string | number; label?: string }>;
101
- };
102
-
103
- export const SelectInput = ({
104
- type,
105
- label,
106
- inputOnly,
107
- disabled,
108
- placeholder,
109
- options,
110
- getStatus,
111
- getValue,
112
- onValueChange,
113
- }: SelectInputOptions) => {
114
- const { status, error } = getStatus();
115
-
116
- const value = getValue() as string | undefined;
117
- const handleValueChange = useCallback((value: string | number) => onValueChange(type, value), [type, onValueChange]);
118
-
119
- return disabled && !value ? null : (
120
- <Input.Root validationValence={status}>
121
- {!inputOnly && <InputHeader error={error} label={label} />}
122
- {disabled ? (
123
- <p>{options?.find(({ value: optionValue }) => optionValue === value)?.label ?? String(value)}</p>
124
- ) : (
125
- <Select.Root value={value} onValueChange={handleValueChange}>
126
- {/* TODO(burdon): Placeholder not working? */}
127
- <Select.TriggerButton classNames='is-full' disabled={disabled} placeholder={placeholder} />
128
- <Select.Portal>
129
- <Select.Content>
130
- <Select.Viewport>
131
- {options?.map(({ value, label }) => (
132
- // NOTE: Numeric values are converted to and from strings.
133
- <Select.Option key={String(value)} value={String(value)}>
134
- {label ?? String(value)}
135
- </Select.Option>
136
- ))}
137
- </Select.Viewport>
138
- </Select.Content>
139
- </Select.Portal>
140
- </Select.Root>
141
- )}
142
- {inputOnly && <Input.DescriptionAndValidation>{error}</Input.DescriptionAndValidation>}
143
- </Input.Root>
144
- );
145
- };
146
-
147
- export const MarkdownInput = ({
148
- type,
149
- label,
150
- inputOnly,
151
- disabled,
152
- placeholder,
153
- getStatus,
154
- getValue,
155
- onValueChange,
156
- onBlur,
157
- }: InputProps) => {
158
- const { status, error } = getStatus();
159
- const textareaRef = useRef<HTMLTextAreaElement>(null);
160
-
161
- // TODO(ZaymonFC): If we start using `Enter` for form submission, we should prevent default behavior here
162
- // since we're using a textarea element and the user needs to be able to enter new lines.
163
-
164
- const adjustHeight = useCallback(() => {
165
- const textarea = textareaRef.current;
166
- if (textarea) {
167
- textarea.style.blockSize = 'auto'; // For measurement.
168
- textarea.style.blockSize = `${textarea.scrollHeight + 2}px`;
169
- }
170
- }, []);
171
-
172
- // Adjust height on initial render.
173
- useEffect(() => {
174
- adjustHeight();
175
- }, [adjustHeight]);
176
-
177
- // Adjust height when the content changes.
178
- useEffect(() => {
179
- adjustHeight();
180
- }, [getValue(), adjustHeight]);
181
-
182
- return (
183
- <Input.Root validationValence={status}>
184
- {!inputOnly && <InputHeader error={error} label={label} />}
185
- {disabled ? (
186
- <p>{getValue() ?? ''}</p>
187
- ) : (
188
- <Input.TextArea
189
- ref={textareaRef}
190
- disabled={disabled}
191
- placeholder={placeholder}
192
- value={getValue() ?? ''}
193
- classNames={'min-bs-auto max-h-40 overflow-auto'}
194
- onChange={(event) => onValueChange(type, event.target.value)}
195
- onBlur={onBlur}
196
- style={{ resize: 'none' }}
197
- spellCheck={false}
198
- />
199
- )}
200
- {inputOnly && <Input.Validation>{error}</Input.Validation>}
201
- </Input.Root>
202
- );
203
- };
@@ -1,53 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import React from 'react';
6
-
7
- import { IconButton, useTranslation } from '@dxos/react-ui';
8
- import { cardSpacing } from '@dxos/react-ui-stack';
9
- import { mx } from '@dxos/react-ui-theme';
10
-
11
- import { useFormContext } from './FormContext';
12
- import { translationKey } from '../../translations';
13
-
14
- export type FormOuterSpacing = boolean | 'blockStart-0' | 'scroll-fields';
15
-
16
- export type FormActionsProps = {
17
- readonly?: boolean;
18
- onCancel?: () => void;
19
- outerSpacing?: FormOuterSpacing;
20
- };
21
-
22
- export const FormActions = ({ readonly, onCancel, outerSpacing = true }: FormActionsProps) => {
23
- const { t } = useTranslation(translationKey);
24
- const { canSave, handleSave } = useFormContext();
25
-
26
- return (
27
- <div
28
- role='none'
29
- className={mx(
30
- outerSpacing === 'scroll-fields'
31
- ? 'pli-cardSpacingInline mbe-cardSpacingBlock'
32
- : outerSpacing
33
- ? cardSpacing
34
- : 'mbs-cardSpacingBlock',
35
- 'flex [&_button]:grow gap-1 first:mbs-0',
36
- )}
37
- >
38
- {onCancel && !readonly && (
39
- <IconButton data-testid='cancel-button' icon='ph--x--regular' label={t('button cancel')} onClick={onCancel} />
40
- )}
41
- {handleSave && (
42
- <IconButton
43
- type='submit'
44
- data-testid='save-button'
45
- disabled={!canSave}
46
- icon='ph--check--regular'
47
- label={t('button save')}
48
- onClick={handleSave}
49
- />
50
- )}
51
- </div>
52
- );
53
- };
@@ -1,240 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { Schema, SchemaAST, pipe } from 'effect';
6
- import { capitalize } from 'effect/String';
7
- import React, { forwardRef, useMemo } from 'react';
8
-
9
- import { createJsonPath, findNode, getDiscriminatedType, isDiscriminatedUnion } from '@dxos/effect';
10
- import { type ThemedClassName } from '@dxos/react-ui';
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 { RefField } from './RefField';
21
- import { getInputComponent } from './factory';
22
- import { type QueryRefOptions } from '../../hooks';
23
- import { getRefProps } from '../../util';
24
-
25
- export type FormFieldProps = {
26
- property: SchemaProperty<any>;
27
- path?: (string | number)[];
28
- readonly?: boolean;
29
- /** Used to indicate if input should be presented inline (e.g. for array items). */
30
- inline?: boolean;
31
- onQueryRefOptions?: QueryRefOptions;
32
- lookupComponent?: ComponentLookup;
33
- Custom?: Partial<Record<string, InputComponent>>;
34
- };
35
-
36
- export const FormField = ({
37
- property,
38
- path,
39
- readonly,
40
- inline,
41
- onQueryRefOptions,
42
- lookupComponent,
43
- Custom,
44
- }: FormFieldProps) => {
45
- const { ast, name, type, format, title, description, options, examples, array } = property;
46
- const inputProps = useInputProps(path);
47
-
48
- const label = useMemo(() => title ?? pipe(name, capitalize), [title, name]);
49
- const placeholder = useMemo(
50
- () => (examples?.length ? `Example: "${examples[0]}"` : description),
51
- [examples, description],
52
- );
53
-
54
- //
55
- // Registry and Custom.
56
- //
57
-
58
- const FoundComponent = lookupComponent?.({
59
- prop: name,
60
- schema: Schema.make(ast),
61
- inputProps: {
62
- type,
63
- format,
64
- label,
65
- disabled: readonly,
66
- placeholder,
67
- ...inputProps,
68
- },
69
- });
70
-
71
- if (FoundComponent) {
72
- return FoundComponent;
73
- }
74
-
75
- const jsonPath = createJsonPath(path ?? []);
76
- const CustomComponent = Custom?.[jsonPath];
77
- if (CustomComponent) {
78
- return (
79
- <CustomComponent
80
- type={type}
81
- format={format}
82
- label={label}
83
- inputOnly={inline}
84
- placeholder={placeholder}
85
- disabled={readonly}
86
- {...inputProps}
87
- />
88
- );
89
- }
90
-
91
- //
92
- // Refs.
93
- //
94
-
95
- const refProps = getRefProps(property);
96
- if (refProps) {
97
- return (
98
- <RefField
99
- ast={refProps.ast}
100
- array={refProps.isArray}
101
- type={type}
102
- format={format}
103
- label={label}
104
- placeholder={placeholder}
105
- disabled={readonly}
106
- inputOnly={inline}
107
- onQueryRefOptions={onQueryRefOptions}
108
- {...inputProps}
109
- />
110
- );
111
- }
112
-
113
- //
114
- // Arrays.
115
- //
116
-
117
- if (array) {
118
- return <ArrayField property={property} path={path} inputProps={inputProps} readonly={readonly} Custom={Custom} />;
119
- }
120
-
121
- //
122
- // Standard Inputs.
123
- //
124
-
125
- const InputComponent = getInputComponent(type, format);
126
- if (InputComponent) {
127
- return (
128
- <InputComponent
129
- type={type}
130
- format={format}
131
- label={label}
132
- inputOnly={inline}
133
- placeholder={placeholder}
134
- disabled={readonly}
135
- {...inputProps}
136
- />
137
- );
138
- }
139
-
140
- //
141
- // Select.
142
- //
143
-
144
- if (options) {
145
- return (
146
- <SelectInput
147
- type={type}
148
- format={format}
149
- disabled={readonly}
150
- inputOnly={inline}
151
- label={label}
152
- options={options.map((option) => ({ value: option, label: String(option) }))}
153
- placeholder={placeholder}
154
- {...inputProps}
155
- />
156
- );
157
- }
158
-
159
- //
160
- // Nested Objects.
161
- //
162
-
163
- if (type === 'object') {
164
- const baseNode = findNode(ast, isDiscriminatedUnion);
165
- const typeLiteral = baseNode
166
- ? getDiscriminatedType(baseNode, inputProps.getValue() as any)
167
- : findNode(ast, SchemaAST.isTypeLiteral);
168
-
169
- if (typeLiteral) {
170
- return (
171
- <>
172
- {!inline && <h3 className={mx('text-lg mlb-inputSpacingBlock first:mbs-0')}>{label}</h3>}
173
- <FormFields
174
- schema={Schema.make(typeLiteral)}
175
- path={path}
176
- readonly={readonly}
177
- onQueryRefOptions={onQueryRefOptions}
178
- Custom={Custom}
179
- lookupComponent={lookupComponent}
180
- />
181
- </>
182
- );
183
- }
184
- }
185
-
186
- return null;
187
- };
188
-
189
- export type FormFieldsProps = ThemedClassName<{
190
- testId?: string;
191
- readonly?: boolean;
192
- schema: Schema.Schema.All;
193
- /**
194
- * Path to the current object from the root. Used with nested forms.
195
- */
196
- path?: (string | number)[];
197
- filter?: (props: SchemaProperty<any>[]) => SchemaProperty<any>[];
198
- sort?: string[];
199
- lookupComponent?: ComponentLookup;
200
- /**
201
- * Map of custom renderers for specific properties.
202
- * Prefer lookupComponent for plugin specific input surfaces.
203
- */
204
- Custom?: Partial<Record<string, InputComponent>>;
205
- onQueryRefOptions?: QueryRefOptions;
206
- }>;
207
-
208
- export const FormFields = forwardRef<HTMLDivElement, FormFieldsProps>(
209
- (
210
- { classNames, testId, schema, path, filter, sort, readonly, lookupComponent, Custom, onQueryRefOptions },
211
- forwardRef,
212
- ) => {
213
- const values = useFormValues(path);
214
- const properties = useMemo(() => {
215
- const props = getSchemaProperties(schema.ast, values);
216
- const filtered = filter ? filter(props) : props;
217
- return sort ? filtered.sort((a, b) => sort.indexOf(a.name) - sort.indexOf(b.name)) : filtered;
218
- }, [schema, values, filter, sort]);
219
-
220
- return (
221
- <div role='form' className={mx('is-full', classNames)} ref={forwardRef}>
222
- {properties
223
- .map((property) => {
224
- return (
225
- <FormField
226
- key={property.name}
227
- property={property}
228
- path={[...(path ?? []), property.name]}
229
- readonly={readonly}
230
- onQueryRefOptions={onQueryRefOptions}
231
- lookupComponent={lookupComponent}
232
- Custom={Custom}
233
- />
234
- );
235
- })
236
- .filter(isNotFalsy)}
237
- </div>
238
- );
239
- },
240
- );
@@ -1,110 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import React, {
6
- createContext,
7
- type RefObject,
8
- useContext,
9
- useEffect,
10
- useMemo,
11
- type FocusEvent,
12
- type PropsWithChildren,
13
- useCallback,
14
- } from 'react';
15
-
16
- import { raise } from '@dxos/debug';
17
- import { type BaseObject, getValue } from '@dxos/echo-schema';
18
- import { createJsonPath, type SimpleType } from '@dxos/effect';
19
-
20
- import { type FormHandler, type FormOptions, useForm } from '../../hooks';
21
-
22
- type FormContextValue<T extends BaseObject> = FormHandler<T>;
23
-
24
- const FormContext = createContext<FormContextValue<any> | undefined>(undefined);
25
-
26
- export const useFormContext = <T extends BaseObject>(): FormContextValue<T> => {
27
- return useContext(FormContext) ?? raise(new Error('Missing FormContext'));
28
- };
29
-
30
- export const useFormValues = (path: (string | number)[] = []): any => {
31
- const { values: formValues } = useFormContext();
32
- const jsonPath = createJsonPath(path);
33
- return getValue(formValues, jsonPath) as BaseObject;
34
- };
35
-
36
- export type FormInputStateProps = {
37
- getStatus: () => { status?: 'error'; error?: string };
38
- getValue: <V>() => V | undefined;
39
- onValueChange: (type: SimpleType, value: any) => void;
40
- onBlur: (event: FocusEvent<HTMLElement>) => void;
41
- };
42
-
43
- export const useInputProps = (path: (string | number)[] = []): FormInputStateProps => {
44
- const { getStatus, getValue: getFormValue, onValueChange, onTouched } = useFormContext();
45
-
46
- const stablePath = useMemo(() => path, [Array.isArray(path) ? path.join('.') : path]);
47
- return useMemo(
48
- () => ({
49
- getStatus: () => getStatus(stablePath),
50
- getValue: () => getFormValue(stablePath),
51
- onValueChange: (type: SimpleType, value: any) => onValueChange(stablePath, type, value),
52
- onBlur: () => onTouched(stablePath),
53
- }),
54
- [getStatus, getFormValue, onValueChange, onTouched, stablePath],
55
- );
56
- };
57
-
58
- export const FormProvider = ({
59
- children,
60
- formRef,
61
- autoSave,
62
- ...formOptions
63
- }: PropsWithChildren<
64
- FormOptions<any> & {
65
- formRef?: RefObject<HTMLDivElement>;
66
- autoSave?: boolean;
67
- }
68
- >) => {
69
- const form = useForm(formOptions);
70
-
71
- const handleKeyDown = useCallback(
72
- (event: KeyboardEvent) => {
73
- const keyIsEnter = event.key === 'Enter';
74
- const modifierUsed = event.ctrlKey || event.altKey || event.metaKey || event.shiftKey;
75
- const inputIsTextarea = (event.target as HTMLElement).tagName.toLowerCase() === 'textarea';
76
- const inputOptOut =
77
- (event.target as HTMLElement).hasAttribute('data-no-submit') ||
78
- (event.target as HTMLElement).closest('[data-no-submit]') !== null;
79
-
80
- // Regular inputs: Submit on Enter (no modifiers).
81
- const shouldSubmitRegularInput = !inputIsTextarea && keyIsEnter && !modifierUsed;
82
-
83
- // Textareas: Submit only on Meta+Enter.
84
- const shouldSubmitTextarea = inputIsTextarea && keyIsEnter && event.metaKey;
85
-
86
- if ((shouldSubmitRegularInput || shouldSubmitTextarea) && !inputOptOut) {
87
- if (!autoSave && form.canSave) {
88
- form.handleSave();
89
- }
90
- if (autoSave && form.formIsValid) {
91
- (event.target as HTMLElement).blur();
92
- }
93
- }
94
- },
95
- [form, autoSave],
96
- );
97
-
98
- useEffect(() => {
99
- if (!formRef?.current) {
100
- return;
101
- }
102
-
103
- const formElement = formRef.current;
104
-
105
- formElement.addEventListener('keydown', handleKeyDown);
106
- return () => formElement.removeEventListener('keydown', handleKeyDown);
107
- }, [formRef, form.canSave, form.formIsValid, form.handleSave, autoSave]);
108
-
109
- return <FormContext.Provider value={form}>{children}</FormContext.Provider>;
110
- };
@@ -1,51 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import React, { type FC } from 'react';
6
-
7
- import { type FormatEnum } from '@dxos/echo-schema';
8
- import { type SimpleType } from '@dxos/effect';
9
- import { Icon, Input, Tooltip } from '@dxos/react-ui';
10
- import { labelSpacing } from '@dxos/react-ui-stack';
11
- import { errorText, mx } from '@dxos/react-ui-theme';
12
-
13
- import { type FormInputStateProps } from './FormContext';
14
-
15
- /**
16
- * Props passed to input components.
17
- */
18
- export type InputProps = {
19
- type: SimpleType;
20
- format?: FormatEnum;
21
- label: string;
22
- disabled?: boolean;
23
- placeholder?: string;
24
- inputOnly?: boolean;
25
- } & FormInputStateProps;
26
-
27
- /**
28
- * Form input component.
29
- */
30
- export type InputComponent = FC<InputProps>;
31
-
32
- export type InputHeaderProps = {
33
- error?: string;
34
- label: string;
35
- readonly?: boolean;
36
- };
37
-
38
- export const InputHeader = ({ error, label, readonly }: InputHeaderProps) => {
39
- const Label = readonly ? 'span' : Input.Label;
40
- const labelProps = readonly ? {} : { classNames: '!mlb-0' };
41
- return (
42
- <div role='none' className={mx('flex justify-between items-center', labelSpacing)}>
43
- <Label {...labelProps}>{label}</Label>
44
- {error && (
45
- <Tooltip.Trigger asChild content={error} side='bottom'>
46
- <Icon icon='ph--warning--regular' size={4} classNames={errorText} />
47
- </Tooltip.Trigger>
48
- )}
49
- </div>
50
- );
51
- };