@dxos/react-ui-form 0.8.4-main.c1de068 → 0.8.4-main.c85a9c8dae

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 (204) hide show
  1. package/README.md +0 -1
  2. package/dist/lib/browser/index.mjs +2314 -2078
  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 +2314 -2078
  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 +81 -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 +37 -0
  17. package/dist/types/src/components/Form/FormField.d.ts.map +1 -0
  18. package/dist/types/src/components/Form/FormFieldComponent.d.ts +78 -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 +19 -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 +14 -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 +46 -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 +56 -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 +19 -10
  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 +2 -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 +51 -42
  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/Form.stories.tsx +156 -219
  96. package/src/components/Form/Form.tsx +347 -96
  97. package/src/components/Form/FormField.tsx +294 -0
  98. package/src/components/Form/FormFieldComponent.stories.tsx +65 -0
  99. package/src/components/Form/FormFieldComponent.tsx +172 -0
  100. package/src/components/Form/FormFieldSet.tsx +123 -0
  101. package/src/components/Form/README.md +3 -0
  102. package/src/components/Form/fields/ArrayField.tsx +149 -0
  103. package/src/components/Form/fields/BooleanField.tsx +22 -0
  104. package/src/components/Form/fields/GeoPointField.tsx +120 -0
  105. package/src/components/Form/fields/MarkdownField.tsx +78 -0
  106. package/src/components/Form/fields/NumberField.tsx +39 -0
  107. package/src/components/Form/fields/RefField.tsx +189 -0
  108. package/src/components/Form/fields/SelectField.tsx +65 -0
  109. package/src/components/Form/fields/SelectOptionField.stories.tsx +64 -0
  110. package/src/components/Form/{custom/SelectOptionsInput.tsx → fields/SelectOptionField.tsx} +46 -41
  111. package/src/components/Form/fields/TextAreaField.tsx +38 -0
  112. package/src/components/Form/fields/TextField.tsx +38 -0
  113. package/src/components/Form/{custom/TupleInput.tsx → fields/TupleField.tsx} +16 -9
  114. package/src/components/Form/fields/index.ts +15 -0
  115. package/src/components/Form/index.ts +3 -4
  116. package/src/components/ObjectPicker/ObjectPicker.stories.tsx +210 -0
  117. package/src/components/ObjectPicker/ObjectPicker.tsx +175 -0
  118. package/src/components/{ControlSection → ObjectPicker}/index.ts +1 -1
  119. package/src/components/Settings/Settings.stories.tsx +56 -0
  120. package/src/components/Settings/Settings.tsx +230 -0
  121. package/src/components/Settings/index.ts +5 -0
  122. package/src/components/ViewEditor/ViewEditor.stories.tsx +140 -62
  123. package/src/components/ViewEditor/ViewEditor.test.tsx +17 -18
  124. package/src/components/ViewEditor/ViewEditor.tsx +341 -202
  125. package/src/components/index.ts +2 -3
  126. package/src/components/testing.tsx +22 -11
  127. package/src/hooks/index.ts +2 -3
  128. package/src/hooks/useFormHandler.ts +380 -0
  129. package/src/hooks/useKeyHandler.tsx +37 -0
  130. package/src/translations.ts +21 -12
  131. package/src/util/field.ts +33 -43
  132. package/src/util/formatting.ts +22 -22
  133. package/src/util/index.ts +1 -1
  134. package/src/util/properties.ts +18 -0
  135. package/src/util/refs.ts +12 -18
  136. package/dist/types/src/components/ControlSection/ControlSection.d.ts +0 -23
  137. package/dist/types/src/components/ControlSection/ControlSection.d.ts.map +0 -1
  138. package/dist/types/src/components/ControlSection/ControlSection.stories.d.ts +0 -8
  139. package/dist/types/src/components/ControlSection/ControlSection.stories.d.ts.map +0 -1
  140. package/dist/types/src/components/ControlSection/index.d.ts +0 -2
  141. package/dist/types/src/components/ControlSection/index.d.ts.map +0 -1
  142. package/dist/types/src/components/Form/ArrayField.d.ts +0 -16
  143. package/dist/types/src/components/Form/ArrayField.d.ts.map +0 -1
  144. package/dist/types/src/components/Form/Defaults.d.ts +0 -14
  145. package/dist/types/src/components/Form/Defaults.d.ts.map +0 -1
  146. package/dist/types/src/components/Form/FormActions.d.ts +0 -9
  147. package/dist/types/src/components/Form/FormActions.d.ts.map +0 -1
  148. package/dist/types/src/components/Form/FormContent.d.ts +0 -57
  149. package/dist/types/src/components/Form/FormContent.d.ts.map +0 -1
  150. package/dist/types/src/components/Form/FormContext.d.ts +0 -23
  151. package/dist/types/src/components/Form/FormContext.d.ts.map +0 -1
  152. package/dist/types/src/components/Form/Input.d.ts +0 -26
  153. package/dist/types/src/components/Form/Input.d.ts.map +0 -1
  154. package/dist/types/src/components/Form/RefField.d.ts +0 -12
  155. package/dist/types/src/components/Form/RefField.d.ts.map +0 -1
  156. package/dist/types/src/components/Form/custom/GeoPointInput.d.ts +0 -4
  157. package/dist/types/src/components/Form/custom/GeoPointInput.d.ts.map +0 -1
  158. package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts +0 -4
  159. package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts.map +0 -1
  160. package/dist/types/src/components/Form/custom/SelectOptionsInput.stories.d.ts +0 -7
  161. package/dist/types/src/components/Form/custom/SelectOptionsInput.stories.d.ts.map +0 -1
  162. package/dist/types/src/components/Form/custom/TupleInput.d.ts +0 -6
  163. package/dist/types/src/components/Form/custom/TupleInput.d.ts.map +0 -1
  164. package/dist/types/src/components/Form/custom/index.d.ts +0 -4
  165. package/dist/types/src/components/Form/custom/index.d.ts.map +0 -1
  166. package/dist/types/src/components/Form/factory.d.ts +0 -8
  167. package/dist/types/src/components/Form/factory.d.ts.map +0 -1
  168. package/dist/types/src/components/deprecated/Form.d.ts +0 -15
  169. package/dist/types/src/components/deprecated/Form.d.ts.map +0 -1
  170. package/dist/types/src/components/deprecated/Form.stories.d.ts +0 -13
  171. package/dist/types/src/components/deprecated/Form.stories.d.ts.map +0 -1
  172. package/dist/types/src/components/deprecated/FormInput.d.ts +0 -16
  173. package/dist/types/src/components/deprecated/FormInput.d.ts.map +0 -1
  174. package/dist/types/src/components/deprecated/index.d.ts +0 -2
  175. package/dist/types/src/components/deprecated/index.d.ts.map +0 -1
  176. package/dist/types/src/hooks/useForm.d.ts +0 -67
  177. package/dist/types/src/hooks/useForm.d.ts.map +0 -1
  178. package/dist/types/src/hooks/useQueryRefOptions.d.ts +0 -23
  179. package/dist/types/src/hooks/useQueryRefOptions.d.ts.map +0 -1
  180. package/dist/types/src/hooks/useRefQueryLookupHandler.d.ts +0 -8
  181. package/dist/types/src/hooks/useRefQueryLookupHandler.d.ts.map +0 -1
  182. package/dist/types/src/util/schema.d.ts +0 -3
  183. package/dist/types/src/util/schema.d.ts.map +0 -1
  184. package/src/components/ControlSection/ControlSection.stories.tsx +0 -41
  185. package/src/components/ControlSection/ControlSection.tsx +0 -139
  186. package/src/components/Form/ArrayField.tsx +0 -124
  187. package/src/components/Form/Defaults.tsx +0 -203
  188. package/src/components/Form/FormActions.tsx +0 -53
  189. package/src/components/Form/FormContent.tsx +0 -240
  190. package/src/components/Form/FormContext.tsx +0 -110
  191. package/src/components/Form/Input.tsx +0 -51
  192. package/src/components/Form/RefField.tsx +0 -213
  193. package/src/components/Form/custom/GeoPointInput.tsx +0 -72
  194. package/src/components/Form/custom/SelectOptionsInput.stories.tsx +0 -61
  195. package/src/components/Form/custom/index.ts +0 -7
  196. package/src/components/Form/factory.tsx +0 -31
  197. package/src/components/deprecated/Form.stories.tsx +0 -62
  198. package/src/components/deprecated/Form.tsx +0 -119
  199. package/src/components/deprecated/FormInput.tsx +0 -71
  200. package/src/components/deprecated/index.ts +0 -5
  201. package/src/hooks/useForm.ts +0 -291
  202. package/src/hooks/useQueryRefOptions.ts +0 -53
  203. package/src/hooks/useRefQueryLookupHandler.ts +0 -39
  204. package/src/util/schema.ts +0 -14
@@ -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
- };
@@ -1,213 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { type SchemaAST } from 'effect';
6
- import React, { useCallback, useMemo, type FocusEvent } from 'react';
7
-
8
- import {
9
- Expando,
10
- getTypeAnnotation,
11
- Ref,
12
- ReferenceAnnotationId,
13
- type ReferenceAnnotationValue,
14
- } from '@dxos/echo-schema';
15
- import { findAnnotation } from '@dxos/effect';
16
- import { DXN } from '@dxos/keys';
17
- import { DxRefTag } from '@dxos/lit-ui/react';
18
- import { Input, useTranslation } from '@dxos/react-ui';
19
- import { TagPicker, type TagPickerMode, type TagPickerItemData } from '@dxos/react-ui-tag-picker';
20
- import { descriptionText, mx } from '@dxos/react-ui-theme';
21
- import { isNonNullable } from '@dxos/util';
22
-
23
- import { TextInput } from './Defaults';
24
- import { InputHeader, type InputProps } from './Input';
25
- import { type QueryRefOptions, useQueryRefOptions } from '../../hooks';
26
- import { translationKey } from '../../translations';
27
-
28
- type RefFieldProps = InputProps & {
29
- ast?: SchemaAST.AST;
30
- array?: boolean;
31
- onQueryRefOptions?: QueryRefOptions;
32
- };
33
-
34
- export const RefField = ({
35
- type,
36
- label,
37
- disabled,
38
- placeholder,
39
- inputOnly,
40
- array,
41
- ast,
42
- getValue,
43
- onBlur,
44
- onQueryRefOptions,
45
- onValueChange,
46
- ...restInputProps
47
- }: RefFieldProps) => {
48
- const { t } = useTranslation(translationKey);
49
- const refTypeInfo = useMemo(
50
- () => (ast ? findAnnotation<ReferenceAnnotationValue>(ast, ReferenceAnnotationId) : undefined),
51
- [ast],
52
- );
53
- const { options: availableOptions, loading: _loading } = useQueryRefOptions({ refTypeInfo, onQueryRefOptions });
54
-
55
- if ((refTypeInfo && refTypeInfo?.typename === getTypeAnnotation(Expando)?.typename) || !onQueryRefOptions) {
56
- // If ref type is expando, fall back to taking a DXN in string format.
57
- return (
58
- <RefFieldFallback
59
- {...{ type, label, placeholder, disabled, inputOnly, getValue, onBlur, onValueChange, ...restInputProps }}
60
- />
61
- );
62
- }
63
-
64
- const handleGetValue = useCallback((): TagPickerItemData[] => {
65
- const formValue = getValue();
66
-
67
- const unknownToRefOption = (val: unknown) => {
68
- if (Ref.isRef(val)) {
69
- const dxnString = val.dxn.toString();
70
- const matchingOption = availableOptions.find((option) => option.id === dxnString);
71
- if (matchingOption) {
72
- return matchingOption;
73
- }
74
- }
75
- return undefined;
76
- };
77
-
78
- if (array && Array.isArray(formValue)) {
79
- return formValue.map(unknownToRefOption).filter(isNonNullable) ?? [];
80
- }
81
-
82
- const option = unknownToRefOption(formValue);
83
- if (option) {
84
- return [option];
85
- }
86
-
87
- return [];
88
- }, [getValue, availableOptions, array]);
89
-
90
- const handleSearch = useCallback(
91
- (text: string, ids: string[]): TagPickerItemData[] =>
92
- availableOptions
93
- .filter((option) => !ids.includes(option.id))
94
- .filter((option) => option.label.toLowerCase().includes(text.toLowerCase()))
95
- .map((option) => ({ id: option.id, label: option.label, hue: option.hue as any })),
96
- [availableOptions],
97
- );
98
-
99
- const handleUpdate = useCallback(
100
- (ids: string[]) => {
101
- if (ids.length === 0) {
102
- onValueChange('object', undefined);
103
- return;
104
- }
105
-
106
- const refs = ids
107
- .map((id) => {
108
- const item = availableOptions.find((option) => option.id === id);
109
- if (item) {
110
- const dxn = DXN.parse(item.id);
111
- return Ref.fromDXN(dxn);
112
- }
113
- return null;
114
- })
115
- .filter(isNonNullable);
116
-
117
- if (array) {
118
- onValueChange('object', refs);
119
- } else {
120
- onValueChange('object', refs[0]);
121
- }
122
- },
123
- [availableOptions, array, onValueChange],
124
- );
125
-
126
- const tagPickerMode: TagPickerMode = useMemo(() => (array ? 'multi-select' : 'single-select'), [array]);
127
-
128
- if (!refTypeInfo) {
129
- return null;
130
- }
131
-
132
- const { status, error } = restInputProps.getStatus();
133
-
134
- const items = handleGetValue();
135
-
136
- // NOTE(thure): I left both predicates in-place in case we decide to add variants which do render readonly but empty values.
137
- return disabled && items.length < 1 ? null : (
138
- <Input.Root validationValence={status}>
139
- {!inputOnly && <InputHeader error={error} label={label} />}
140
- <div data-no-submit>
141
- {disabled ? (
142
- items.length < 1 ? (
143
- <p className={mx(descriptionText, 'mbe-2')}>{t('empty readonly ref field label')}</p>
144
- ) : (
145
- items.map((item) => (
146
- <DxRefTag key={item.id} refid={item.id} rootclassname='mie-1' style={{ marginInlineEnd: '.25rem' }}>
147
- {item.label}
148
- </DxRefTag>
149
- ))
150
- )
151
- ) : (
152
- <TagPicker
153
- readonly={disabled}
154
- items={items}
155
- mode={tagPickerMode}
156
- onBlur={(event) => onBlur(event as unknown as FocusEvent<HTMLElement>)}
157
- onUpdate={handleUpdate}
158
- onSearch={handleSearch}
159
- classNames='bg-inputSurface p-1.5 rounded-sm'
160
- />
161
- )}
162
- </div>
163
- {inputOnly && <Input.DescriptionAndValidation>{error}</Input.DescriptionAndValidation>}
164
- </Input.Root>
165
- );
166
- };
167
-
168
- const RefFieldFallback = ({
169
- type,
170
- label,
171
- disabled,
172
- placeholder,
173
- inputOnly,
174
- getValue,
175
- onValueChange,
176
- ...restInputProps
177
- }: InputProps) => {
178
- const handleOnValueChange = (_type: any, dxnString: string) => {
179
- const dxn = DXN.tryParse(dxnString);
180
- if (dxn) {
181
- onValueChange?.('object', Ref.fromDXN(dxn));
182
- } else if (dxnString === '') {
183
- onValueChange?.('object', undefined);
184
- } else {
185
- onValueChange?.('string', dxnString);
186
- }
187
- };
188
-
189
- const handleGetValue = () => {
190
- const formValue = getValue();
191
- if (typeof formValue === 'string') {
192
- return formValue;
193
- }
194
- if (Ref.isRef(formValue)) {
195
- return formValue.dxn.toString();
196
- }
197
-
198
- return undefined;
199
- };
200
-
201
- return (
202
- <TextInput
203
- type={type}
204
- label={label}
205
- disabled={disabled}
206
- placeholder={placeholder}
207
- inputOnly={inputOnly}
208
- getValue={handleGetValue as <V>() => V | undefined}
209
- onValueChange={handleOnValueChange}
210
- {...restInputProps}
211
- />
212
- );
213
- };
@@ -1,72 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import React, { useCallback, useState, useEffect, useMemo, type ChangeEvent } from 'react';
6
-
7
- import { type GeoPoint, GeoLocation } from '@dxos/echo-schema';
8
- import { Input, useTranslation } from '@dxos/react-ui';
9
- import { safeParseFloat } from '@dxos/util';
10
-
11
- import { translationKey } from '../../../translations';
12
- import { InputHeader, type InputProps } from '../Input';
13
-
14
- export const GeoPointInput = ({ type, label, disabled, getStatus, getValue, onValueChange, onBlur }: InputProps) => {
15
- const { t } = useTranslation(translationKey);
16
- const { status, error } = getStatus();
17
- const geoPoint = useMemo<GeoPoint>(() => getValue<GeoPoint>() ?? [0, 0], [getValue]);
18
- const geoLocation = useMemo(() => GeoLocation.fromGeoPoint(geoPoint), [geoPoint]);
19
-
20
- const [longitudeText, setLongitudeText] = useState(geoLocation.longitude?.toString());
21
- const [latitudeText, setLatitudeText] = useState(geoLocation.latitude?.toString());
22
-
23
- useEffect(() => {
24
- const location = GeoLocation.fromGeoPoint(geoPoint);
25
- setLongitudeText(location.longitude.toString());
26
- setLatitudeText(location.latitude.toString());
27
- }, [geoPoint]);
28
-
29
- const handleCoordinateChange = useCallback(
30
- (coordinateType: keyof Pick<GeoLocation, 'longitude' | 'latitude'>, setText: (text: string) => void) =>
31
- (event: ChangeEvent<HTMLInputElement>) => {
32
- const inputText = event.target.value;
33
- setText(inputText);
34
- if (inputText !== '' && inputText !== '-') {
35
- const coord = safeParseFloat(inputText);
36
- if (coord !== undefined && !isNaN(coord)) {
37
- const currentLocation = GeoLocation.fromGeoPoint(getValue<GeoPoint>() ?? [0, 0]);
38
- const newLocation = { ...currentLocation, [coordinateType]: coord };
39
- const newValue = GeoLocation.toGeoPoint(newLocation);
40
- onValueChange(type, newValue);
41
- }
42
- }
43
- },
44
- [getValue, onValueChange, type],
45
- );
46
-
47
- return (
48
- <Input.Root validationValence={status}>
49
- <InputHeader error={error} label={label} />
50
- <div role='none' className='grid grid-cols-2 gap-2'>
51
- <Input.TextInput
52
- type='text'
53
- pattern='^-?[0-9]*\.?[0-9]*$'
54
- disabled={disabled}
55
- placeholder={t('placeholder latitude')}
56
- value={latitudeText}
57
- onChange={handleCoordinateChange('latitude', setLatitudeText)}
58
- onBlur={onBlur}
59
- />
60
- <Input.TextInput
61
- type='text'
62
- pattern='^-?[0-9]*\.?[0-9]*$'
63
- disabled={disabled}
64
- placeholder={t('placeholder longitude')}
65
- value={longitudeText}
66
- onChange={handleCoordinateChange('longitude', setLongitudeText)}
67
- onBlur={onBlur}
68
- />
69
- </div>
70
- </Input.Root>
71
- );
72
- };