@dxos/react-ui-form 0.7.5-labs.ea4b4c2 → 0.7.5-labs.f400bbc

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 (61) hide show
  1. package/dist/lib/browser/index.mjs +299 -115
  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 +324 -139
  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 +299 -115
  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/FieldEditor/FieldEditor.d.ts +1 -2
  11. package/dist/types/src/components/FieldEditor/FieldEditor.d.ts.map +1 -1
  12. package/dist/types/src/components/Form/ArrayField.d.ts +1 -2
  13. package/dist/types/src/components/Form/ArrayField.d.ts.map +1 -1
  14. package/dist/types/src/components/Form/Defaults.d.ts +5 -5
  15. package/dist/types/src/components/Form/Defaults.d.ts.map +1 -1
  16. package/dist/types/src/components/Form/Form.d.ts +2 -2
  17. package/dist/types/src/components/Form/Form.d.ts.map +1 -1
  18. package/dist/types/src/components/Form/FormActions.d.ts +1 -2
  19. package/dist/types/src/components/Form/FormActions.d.ts.map +1 -1
  20. package/dist/types/src/components/Form/FormContent.d.ts +2 -3
  21. package/dist/types/src/components/Form/FormContent.d.ts.map +1 -1
  22. package/dist/types/src/components/Form/FormContext.d.ts +4 -1
  23. package/dist/types/src/components/Form/FormContext.d.ts.map +1 -1
  24. package/dist/types/src/components/Form/Input.d.ts +2 -2
  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/SelectOptionsInput.d.ts +1 -2
  29. package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts.map +1 -1
  30. package/dist/types/src/components/Form/custom/TupleInput.d.ts +1 -2
  31. package/dist/types/src/components/Form/custom/TupleInput.d.ts.map +1 -1
  32. package/dist/types/src/components/Form/factory.d.ts.map +1 -1
  33. package/dist/types/src/components/ViewEditor/Test.stories.d.ts +1 -2
  34. package/dist/types/src/components/ViewEditor/Test.stories.d.ts.map +1 -1
  35. package/dist/types/src/components/ViewEditor/ViewEditor.d.ts +1 -2
  36. package/dist/types/src/components/ViewEditor/ViewEditor.d.ts.map +1 -1
  37. package/dist/types/src/components/deprecated/Form.d.ts +1 -2
  38. package/dist/types/src/components/deprecated/Form.d.ts.map +1 -1
  39. package/dist/types/src/components/deprecated/Form.stories.d.ts +1 -2
  40. package/dist/types/src/components/deprecated/Form.stories.d.ts.map +1 -1
  41. package/dist/types/src/components/deprecated/FormInput.d.ts +2 -2
  42. package/dist/types/src/components/deprecated/FormInput.d.ts.map +1 -1
  43. package/dist/types/src/components/testing.d.ts +3 -3
  44. package/dist/types/src/components/testing.d.ts.map +1 -1
  45. package/dist/types/src/hooks/useForm.d.ts +1 -0
  46. package/dist/types/src/hooks/useForm.d.ts.map +1 -1
  47. package/dist/types/src/util/field.d.ts.map +1 -1
  48. package/dist/types/src/util/formatting.d.ts.map +1 -1
  49. package/package.json +25 -24
  50. package/src/components/Form/Defaults.tsx +59 -3
  51. package/src/components/Form/Form.tsx +8 -5
  52. package/src/components/Form/FormContent.tsx +9 -12
  53. package/src/components/Form/FormContext.tsx +58 -10
  54. package/src/components/Form/custom/GeoPointInput.tsx +42 -12
  55. package/src/components/Form/custom/SelectOptionsInput.tsx +7 -9
  56. package/src/components/Form/factory.tsx +4 -2
  57. package/src/components/ViewEditor/ViewEditor.tsx +2 -2
  58. package/src/hooks/useForm.ts +69 -40
  59. package/src/translations.ts +1 -1
  60. package/src/util/field.ts +19 -1
  61. package/src/util/formatting.ts +29 -1
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@dxos/react-ui-form",
3
- "version": "0.7.5-labs.ea4b4c2",
3
+ "version": "0.7.5-labs.f400bbc",
4
4
  "description": "Data components.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
7
7
  "license": "MIT",
8
8
  "author": "DXOS.org",
9
+ "type": "module",
9
10
  "exports": {
10
11
  ".": {
11
12
  "types": "./dist/types/src/index.d.ts",
@@ -28,21 +29,21 @@
28
29
  "@radix-ui/react-context": "1.1.1",
29
30
  "date-fns": "^3.3.1",
30
31
  "jsonpath": "^1.1.1",
31
- "@dxos/async": "0.7.5-labs.ea4b4c2",
32
- "@dxos/echo-db": "0.7.5-labs.ea4b4c2",
33
- "@dxos/echo-schema": "0.7.5-labs.ea4b4c2",
34
- "@dxos/invariant": "0.7.5-labs.ea4b4c2",
35
- "@dxos/effect": "0.7.5-labs.ea4b4c2",
36
- "@dxos/debug": "0.7.5-labs.ea4b4c2",
37
- "@dxos/keys": "0.7.5-labs.ea4b4c2",
38
- "@dxos/live-object": "0.7.5-labs.ea4b4c2",
39
- "@dxos/log": "0.7.5-labs.ea4b4c2",
40
- "@dxos/react-ui-pickers": "0.7.5-labs.ea4b4c2",
41
- "@dxos/react-ui-list": "0.7.5-labs.ea4b4c2",
42
- "@dxos/react-ui-types": "0.7.5-labs.ea4b4c2",
43
- "@dxos/react-client": "0.7.5-labs.ea4b4c2",
44
- "@dxos/schema": "0.7.5-labs.ea4b4c2",
45
- "@dxos/util": "0.7.5-labs.ea4b4c2"
32
+ "@dxos/async": "0.7.5-labs.f400bbc",
33
+ "@dxos/debug": "0.7.5-labs.f400bbc",
34
+ "@dxos/echo-db": "0.7.5-labs.f400bbc",
35
+ "@dxos/echo-schema": "0.7.5-labs.f400bbc",
36
+ "@dxos/effect": "0.7.5-labs.f400bbc",
37
+ "@dxos/invariant": "0.7.5-labs.f400bbc",
38
+ "@dxos/keys": "0.7.5-labs.f400bbc",
39
+ "@dxos/live-object": "0.7.5-labs.f400bbc",
40
+ "@dxos/log": "0.7.5-labs.f400bbc",
41
+ "@dxos/react-ui-pickers": "0.7.5-labs.f400bbc",
42
+ "@dxos/react-ui-list": "0.7.5-labs.f400bbc",
43
+ "@dxos/react-client": "0.7.5-labs.f400bbc",
44
+ "@dxos/react-ui-types": "0.7.5-labs.f400bbc",
45
+ "@dxos/schema": "0.7.5-labs.f400bbc",
46
+ "@dxos/util": "0.7.5-labs.f400bbc"
46
47
  },
47
48
  "devDependencies": {
48
49
  "@phosphor-icons/react": "^2.1.5",
@@ -54,12 +55,12 @@
54
55
  "react": "~18.2.0",
55
56
  "react-dom": "~18.2.0",
56
57
  "vite": "5.4.7",
57
- "@dxos/echo-signals": "0.7.5-labs.ea4b4c2",
58
- "@dxos/random": "0.7.5-labs.ea4b4c2",
59
- "@dxos/react-ui": "0.7.5-labs.ea4b4c2",
60
- "@dxos/react-ui-theme": "0.7.5-labs.ea4b4c2",
61
- "@dxos/react-ui-syntax-highlighter": "0.7.5-labs.ea4b4c2",
62
- "@dxos/storybook-utils": "0.7.5-labs.ea4b4c2"
58
+ "@dxos/echo-signals": "0.7.5-labs.f400bbc",
59
+ "@dxos/random": "0.7.5-labs.f400bbc",
60
+ "@dxos/react-ui": "0.7.5-labs.f400bbc",
61
+ "@dxos/react-ui-syntax-highlighter": "0.7.5-labs.f400bbc",
62
+ "@dxos/storybook-utils": "0.7.5-labs.f400bbc",
63
+ "@dxos/react-ui-theme": "0.7.5-labs.f400bbc"
63
64
  },
64
65
  "peerDependencies": {
65
66
  "@effect/schema": "^0.75.5",
@@ -67,8 +68,8 @@
67
68
  "effect": "^3.12.3",
68
69
  "react": "~18.2.0",
69
70
  "react-dom": "~18.2.0",
70
- "@dxos/react-ui": "0.7.5-labs.ea4b4c2",
71
- "@dxos/react-ui-theme": "0.7.5-labs.ea4b4c2"
71
+ "@dxos/react-ui": "0.7.5-labs.f400bbc",
72
+ "@dxos/react-ui-theme": "0.7.5-labs.f400bbc"
72
73
  },
73
74
  "publishConfig": {
74
75
  "access": "public"
@@ -2,7 +2,7 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import React from 'react';
5
+ import React, { useCallback, useEffect, useRef } from 'react';
6
6
 
7
7
  import { Input, Select } from '@dxos/react-ui';
8
8
 
@@ -77,8 +77,6 @@ export const BooleanInput = ({ type, label, inputOnly, getStatus, getValue, onVa
77
77
  const { status, error } = getStatus();
78
78
  const checked = Boolean(getValue());
79
79
 
80
- // TODO(burdon): Runtime error:
81
- // Warning: Unknown event handler property `onCheckedChange`. It will be ignored.
82
80
  return (
83
81
  <Input.Root validationValence={status}>
84
82
  {!inputOnly && (
@@ -136,3 +134,61 @@ export const SelectInput = ({
136
134
  </Input.Root>
137
135
  );
138
136
  };
137
+
138
+ export const MarkdownInput = ({
139
+ type,
140
+ label,
141
+ inputOnly,
142
+ disabled,
143
+ placeholder,
144
+ getStatus,
145
+ getValue,
146
+ onValueChange,
147
+ onBlur,
148
+ }: InputProps) => {
149
+ const { status, error } = getStatus();
150
+ const textareaRef = useRef<HTMLTextAreaElement>(null);
151
+
152
+ // TODO(ZaymonFC): If we start using `Enter` for form submission, we should prevent default behavior here
153
+ // since we're using a textarea element and the user needs to be able to enter new lines.
154
+
155
+ const adjustHeight = useCallback(() => {
156
+ const textarea = textareaRef.current;
157
+ if (textarea) {
158
+ textarea.style.blockSize = 'auto'; // For measurement.
159
+ textarea.style.blockSize = `${textarea.scrollHeight + 2}px`;
160
+ }
161
+ }, []);
162
+
163
+ // Adjust height on initial render.
164
+ useEffect(() => {
165
+ adjustHeight();
166
+ }, [adjustHeight]);
167
+
168
+ // Adjust height when the content changes.
169
+ useEffect(() => {
170
+ adjustHeight();
171
+ }, [getValue(), adjustHeight]);
172
+
173
+ return (
174
+ <Input.Root validationValence={status}>
175
+ {!inputOnly && (
176
+ <InputHeader error={error}>
177
+ <Input.Label>{label}</Input.Label>
178
+ </InputHeader>
179
+ )}
180
+ <Input.TextArea
181
+ ref={textareaRef}
182
+ disabled={disabled}
183
+ placeholder={placeholder}
184
+ value={getValue() ?? ''}
185
+ classNames={'min-bs-auto max-h-40 overflow-auto'}
186
+ onChange={(event) => onValueChange(type, event.target.value)}
187
+ onBlur={onBlur}
188
+ style={{ resize: 'none' }}
189
+ spellCheck={false}
190
+ />
191
+ {inputOnly && <Input.Validation>{error}</Input.Validation>}
192
+ </Input.Root>
193
+ );
194
+ };
@@ -2,7 +2,7 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import React, { type ReactElement, useEffect, useMemo } from 'react';
5
+ import React, { type ReactElement, useEffect, useMemo, useRef } from 'react';
6
6
 
7
7
  import { type BaseObject, type S, type PropertyKey } from '@dxos/echo-schema';
8
8
  import { type ThemedClassName } from '@dxos/react-ui';
@@ -68,12 +68,13 @@ export const Form = <T extends BaseObject>({
68
68
  lookupComponent,
69
69
  Custom,
70
70
  }: FormProps<T>) => {
71
+ const formRef = useRef<HTMLDivElement>(null);
71
72
  const onValid = useMemo(() => (autoSave ? onSave : undefined), [autoSave, onSave]);
72
73
 
73
- // TODO(burdon): Hack to select first input.
74
+ // Focus the first input element within this form.
74
75
  useEffect(() => {
75
- if (autoFocus) {
76
- const input = document.querySelector('input');
76
+ if (autoFocus && formRef.current) {
77
+ const input = formRef.current.querySelector('input');
77
78
  if (input) {
78
79
  input.focus();
79
80
  }
@@ -82,7 +83,9 @@ export const Form = <T extends BaseObject>({
82
83
 
83
84
  return (
84
85
  <FormProvider
86
+ formRef={formRef}
85
87
  schema={schema}
88
+ autoSave={autoSave}
86
89
  initialValues={initialValues}
87
90
  onValuesChanged={onValuesChanged}
88
91
  onValidate={onValidate}
@@ -90,7 +93,7 @@ export const Form = <T extends BaseObject>({
90
93
  onSave={onSave}
91
94
  >
92
95
  {/* TODO(burdon): Remove padding. */}
93
- <div role='none' className={mx('p-2', classNames)} data-testid={testId}>
96
+ <div ref={formRef} role='none' className={mx('p-2', classNames)} data-testid={testId}>
94
97
  <FormFields
95
98
  schema={schema}
96
99
  path={path}
@@ -70,18 +70,15 @@ export const FormField = ({ property, path, readonly, inline, lookupComponent, C
70
70
  const CustomComponent = Custom?.[jsonPath];
71
71
  if (CustomComponent) {
72
72
  return (
73
- // TODO(ZaymonFC): We should try to remove this div wrapper.
74
- <div role='none'>
75
- <CustomComponent
76
- type={type}
77
- format={format}
78
- label={label}
79
- inputOnly={inline}
80
- placeholder={placeholder}
81
- disabled={readonly}
82
- {...inputProps}
83
- />
84
- </div>
73
+ <CustomComponent
74
+ type={type}
75
+ format={format}
76
+ label={label}
77
+ inputOnly={inline}
78
+ placeholder={placeholder}
79
+ disabled={readonly}
80
+ {...inputProps}
81
+ />
85
82
  );
86
83
  }
87
84
 
@@ -2,7 +2,7 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import React, { createContext, useContext, type FocusEvent, type PropsWithChildren } from 'react';
5
+ import React, { createContext, useContext, useEffect, useMemo, type FocusEvent, type PropsWithChildren } from 'react';
6
6
 
7
7
  import { raise } from '@dxos/debug';
8
8
  import { type BaseObject, getValue } from '@dxos/echo-schema';
@@ -34,15 +34,63 @@ export type FormInputStateProps = {
34
34
  export const useInputProps = (path: (string | number)[] = []): FormInputStateProps => {
35
35
  const { getStatus, getValue: getFormValue, onValueChange, onTouched } = useFormContext();
36
36
 
37
- return {
38
- getStatus: () => getStatus(path),
39
- getValue: () => getFormValue(path),
40
- onValueChange: (type: SimpleType, value: any) => onValueChange(path, type, value),
41
- onBlur: () => onTouched(path),
42
- };
37
+ const stablePath = useMemo(() => path, [Array.isArray(path) ? path.join('.') : path]);
38
+ return useMemo(
39
+ () => ({
40
+ getStatus: () => getStatus(stablePath),
41
+ getValue: () => getFormValue(stablePath),
42
+ onValueChange: (type: SimpleType, value: any) => onValueChange(stablePath, type, value),
43
+ onBlur: () => onTouched(stablePath),
44
+ }),
45
+ [getStatus, getFormValue, onValueChange, onTouched, stablePath],
46
+ );
43
47
  };
44
48
 
45
- export const FormProvider = ({ children, ...formOptions }: PropsWithChildren<FormOptions<any>>) => {
46
- const formHandler = useForm(formOptions);
47
- return <FormContext.Provider value={formHandler}>{children}</FormContext.Provider>;
49
+ export const FormProvider = ({
50
+ children,
51
+ formRef,
52
+ autoSave,
53
+ ...formOptions
54
+ }: PropsWithChildren<
55
+ FormOptions<any> & {
56
+ formRef?: React.RefObject<HTMLDivElement>;
57
+ autoSave?: boolean;
58
+ }
59
+ >) => {
60
+ const form = useForm(formOptions);
61
+
62
+ useEffect(() => {
63
+ if (!formRef?.current) {
64
+ return;
65
+ }
66
+
67
+ const handleKeyDown = (event: KeyboardEvent) => {
68
+ const keyIsEnter = event.key === 'Enter';
69
+ const modifierUsed = event.ctrlKey || event.altKey || event.metaKey || event.shiftKey;
70
+ const inputIsTextarea = (event.target as HTMLElement).tagName.toLowerCase() === 'textarea';
71
+ const inputOptOut = (event.target as HTMLElement).hasAttribute('data-no-submit');
72
+
73
+ // Regular inputs: Submit on Enter (no modifiers).
74
+ const shouldSubmitRegularInput = !inputIsTextarea && keyIsEnter && !modifierUsed;
75
+
76
+ // Textareas: Submit only on Meta+Enter.
77
+ const shouldSubmitTextarea = inputIsTextarea && keyIsEnter && event.metaKey;
78
+
79
+ if ((shouldSubmitRegularInput || shouldSubmitTextarea) && !inputOptOut) {
80
+ if (!autoSave && form.canSave) {
81
+ form.handleSave();
82
+ }
83
+ if (autoSave && form.formIsValid) {
84
+ (event.target as HTMLElement).blur();
85
+ }
86
+ }
87
+ };
88
+
89
+ const formElement = formRef.current;
90
+
91
+ formElement.addEventListener('keydown', handleKeyDown);
92
+ return () => formElement.removeEventListener('keydown', handleKeyDown);
93
+ }, [formRef, form.canSave, form.formIsValid, form.handleSave, autoSave]);
94
+
95
+ return <FormContext.Provider value={form}>{children}</FormContext.Provider>;
48
96
  };
@@ -2,9 +2,9 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import React from 'react';
5
+ import React, { useCallback, useState, useEffect, useMemo } from 'react';
6
6
 
7
- import { type GeoPoint } from '@dxos/echo-schema';
7
+ import { type GeoPoint, GeoLocation } from '@dxos/echo-schema';
8
8
  import { Input, useTranslation } from '@dxos/react-ui';
9
9
  import { safeParseFloat } from '@dxos/util';
10
10
 
@@ -14,28 +14,58 @@ import { InputHeader, type InputProps } from '../Input';
14
14
  export const GeoPointInput = ({ type, label, disabled, getStatus, getValue, onValueChange, onBlur }: InputProps) => {
15
15
  const { t } = useTranslation(translationKey);
16
16
  const { status, error } = getStatus();
17
- const [lng = 0, lat = 0] = getValue<GeoPoint>() ?? [];
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: React.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
+ );
18
46
 
19
47
  return (
20
48
  <Input.Root validationValence={status}>
21
49
  <InputHeader error={error}>
22
50
  <Input.Label>{label}</Input.Label>
23
51
  </InputHeader>
24
- <div className='grid grid-cols-2 gap-2'>
52
+ <div role='none' className='grid grid-cols-2 gap-2'>
25
53
  <Input.TextInput
26
- type='number'
54
+ type='text'
55
+ pattern='^-?[0-9]*\.?[0-9]*$'
27
56
  disabled={disabled}
28
- placeholder={t('placeholder longitude')}
29
- value={lng}
30
- onChange={(event) => onValueChange(type, [lat, safeParseFloat(event.target.value, 0)])}
57
+ placeholder={t('placeholder latitude')}
58
+ value={latitudeText}
59
+ onChange={handleCoordinateChange('latitude', setLatitudeText)}
31
60
  onBlur={onBlur}
32
61
  />
33
62
  <Input.TextInput
34
- type='number'
63
+ type='text'
64
+ pattern='^-?[0-9]*\.?[0-9]*$'
35
65
  disabled={disabled}
36
- placeholder={t('placeholder latitude')}
37
- value={lat}
38
- onChange={(event) => onValueChange(type, [safeParseFloat(event.target.value, 0), lng])}
66
+ placeholder={t('placeholder longitude')}
67
+ value={longitudeText}
68
+ onChange={handleCoordinateChange('longitude', setLongitudeText)}
39
69
  onBlur={onBlur}
40
70
  />
41
71
  </div>
@@ -8,8 +8,8 @@ import { type SelectOption } from '@dxos/echo-schema';
8
8
  import { PublicKey } from '@dxos/keys';
9
9
  import { type ChromaticPalette, Icon, IconButton, Input, Tag, Toolbar, useTranslation } from '@dxos/react-ui';
10
10
  import { List } from '@dxos/react-ui-list';
11
- import { HuePickerToolbarButton } from '@dxos/react-ui-pickers';
12
- import { hueTokenThemes, mx } from '@dxos/react-ui-theme';
11
+ import { HuePicker } from '@dxos/react-ui-pickers';
12
+ import { hues, mx } from '@dxos/react-ui-theme';
13
13
 
14
14
  import { translationKey } from '../../../translations';
15
15
  import { InputHeader, type InputProps } from '../Input';
@@ -32,20 +32,17 @@ export const SelectOptionInput = ({ type, label, disabled, getStatus, getValue,
32
32
 
33
33
  const randomHue = () => {
34
34
  const usedHues = new Set(options?.map((option) => option.color) ?? []);
35
- const hueKeys = Object.keys(hueTokenThemes) as Array<keyof typeof hueTokenThemes>;
36
-
37
35
  // Pick a random unused option from hueTokenThemes.
38
- const unusedHues = hueKeys.filter((hue) => !usedHues.has(hue));
36
+ const unusedHues = hues.filter((hue) => !usedHues.has(hue));
39
37
  if (unusedHues.length > 0) {
40
38
  return unusedHues[Math.floor(Math.random() * unusedHues.length)];
41
39
  }
42
40
 
43
41
  // Pick a random one that hasn't been used in the last three options
44
42
  const lastThree = new Set(options?.slice(-3).map((option) => option.color) ?? []);
45
- const availableHues = hueKeys.filter((hue) => !lastThree.has(hue));
43
+ const availableHues = hues.filter((hue) => !lastThree.has(hue));
46
44
  return (
47
- availableHues[Math.floor(Math.random() * availableHues.length)] ??
48
- hueKeys[Math.floor(Math.random() * hueKeys.length)]
45
+ availableHues[Math.floor(Math.random() * availableHues.length)] ?? hues[Math.floor(Math.random() * hues.length)]
49
46
  );
50
47
  };
51
48
 
@@ -154,9 +151,10 @@ export const SelectOptionInput = ({ type, label, disabled, getStatus, getValue,
154
151
  onChange={handleTitleChange(item.id)}
155
152
  onKeyDown={handleKeyDown}
156
153
  classNames='flex-1'
154
+ data-no-submit
157
155
  />
158
156
  <Toolbar.Root classNames='p-0 m-0 !is-auto'>
159
- <HuePickerToolbarButton hue={item.color} onChangeHue={handleColorChange(item.id)} />
157
+ <HuePicker value={item.color} onChange={handleColorChange(item.id)} />
160
158
  </Toolbar.Root>
161
159
  <IconButton
162
160
  icon='ph--trash--fill'
@@ -5,7 +5,7 @@
5
5
  import { FormatEnum } from '@dxos/echo-schema';
6
6
  import { type SimpleType } from '@dxos/effect';
7
7
 
8
- import { BooleanInput, NumberInput, TextInput } from './Defaults';
8
+ import { BooleanInput, MarkdownInput, NumberInput, TextInput } from './Defaults';
9
9
  import { type InputComponent } from './Input';
10
10
  import { GeoPointInput } from './custom';
11
11
 
@@ -14,8 +14,10 @@ import { GeoPointInput } from './custom';
14
14
  */
15
15
  export const getInputComponent = (type: SimpleType, format?: FormatEnum): InputComponent | undefined => {
16
16
  switch (format) {
17
- case FormatEnum.LatLng:
17
+ case FormatEnum.GeoPoint:
18
18
  return GeoPointInput;
19
+ case FormatEnum.Markdown:
20
+ return MarkdownInput;
19
21
  }
20
22
 
21
23
  switch (type) {
@@ -21,7 +21,7 @@ const ViewMetaSchema = S.Struct({
21
21
  name: S.String.annotations({
22
22
  [AST.TitleAnnotationId]: 'View',
23
23
  }),
24
- typename: Format.DXN.annotations({
24
+ typename: Format.URL.annotations({
25
25
  [AST.TitleAnnotationId]: 'Typename',
26
26
  }),
27
27
  }).pipe(S.mutable);
@@ -103,7 +103,7 @@ export const ViewEditor = ({
103
103
 
104
104
  return (
105
105
  <div role='none' className={mx('flex flex-col w-full divide-y divide-separator', classNames)}>
106
- <Form<ViewMetaType> autoFocus autoSave schema={ViewMetaSchema} values={viewValues} onSave={handleViewUpdate} />
106
+ <Form<ViewMetaType> autoSave schema={ViewMetaSchema} values={viewValues} onSave={handleViewUpdate} />
107
107
 
108
108
  <div>
109
109
  {/* TODO(burdon): Clean up common form ux. */}
@@ -1,5 +1,5 @@
1
1
  //
2
- // Copyright 2024 DXOS.org
2
+ // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
5
  import { useCallback, useEffect, useMemo, useState } from 'react';
@@ -24,6 +24,7 @@ export type FormHandler<T extends BaseObject> = {
24
24
  touched: Record<JsonPath, boolean>;
25
25
  changed: Record<JsonPath, boolean>;
26
26
  canSave: boolean;
27
+ formIsValid: boolean;
27
28
  handleSave: () => void;
28
29
 
29
30
  //
@@ -129,6 +130,10 @@ export const useForm = <T extends BaseObject>({
129
130
  }
130
131
  }, [values, schemaDirty]);
131
132
 
133
+ const formIsValid = useMemo(() => {
134
+ return Object.keys(errors).length === 0;
135
+ }, [errors]);
136
+
132
137
  /**
133
138
  * NOTE: We can submit if there is no touched field that has an error.
134
139
  * Basically, if there's a validation message visible in the form, submit should be disabled.
@@ -136,7 +141,7 @@ export const useForm = <T extends BaseObject>({
136
141
  const canSave = useMemo(
137
142
  () =>
138
143
  !saving &&
139
- // Check if any error paths that are touched have errors
144
+ // Check if any error paths that are touched have errors.
140
145
  !Object.entries(touched).some(
141
146
  ([path, isTouched]) =>
142
147
  isTouched &&
@@ -178,32 +183,38 @@ export const useForm = <T extends BaseObject>({
178
183
  [errors],
179
184
  );
180
185
 
181
- const getFormValue = <V>(path: (string | number)[]): V | undefined => {
182
- return getValue(values, createJsonPath(path));
183
- };
186
+ const getFormValue = useCallback<FormHandler<T>['getValue']>(
187
+ <V>(path: (string | number)[]): V | undefined => {
188
+ return getValue(values, createJsonPath(path));
189
+ },
190
+ [values],
191
+ );
184
192
 
185
- const onValueChange = (path: (string | number)[], type: SimpleType, value: any) => {
186
- const jsonPath = createJsonPath(path);
187
- let parsedValue = value;
188
- try {
189
- if (type === 'number') {
190
- parsedValue = parseFloat(value as string) || 0;
193
+ const onValueChange = useCallback<FormHandler<T>['onValueChange']>(
194
+ (path: (string | number)[], type: SimpleType, value: any) => {
195
+ const jsonPath = createJsonPath(path);
196
+ let parsedValue = value;
197
+ try {
198
+ if (type === 'number') {
199
+ parsedValue = parseFloat(value as string) || 0;
200
+ }
201
+ } catch (err) {
202
+ log.catch(err);
203
+ parsedValue = undefined;
191
204
  }
192
- } catch (err) {
193
- log.catch(err);
194
- parsedValue = undefined;
195
- }
196
205
 
197
- const newValues = { ...setValue(values, jsonPath, parsedValue) };
198
- setValues(newValues);
199
- setChanged((prev) => ({ ...prev, [jsonPath]: true }));
200
- onValuesChanged?.(newValues);
206
+ const newValues = { ...setValue(values, jsonPath, parsedValue) };
207
+ setValues(newValues);
208
+ setChanged((prev) => ({ ...prev, [jsonPath]: true }));
209
+ onValuesChanged?.(newValues);
201
210
 
202
- const isValid = validate(newValues);
203
- if (isValid && onValid) {
204
- onValid(newValues, { changed });
205
- }
206
- };
211
+ const isValid = validate(newValues);
212
+ if (isValid && onValid) {
213
+ onValid(newValues, { changed });
214
+ }
215
+ },
216
+ [values, onValuesChanged, validate, onValid, changed],
217
+ );
207
218
 
208
219
  const onTouched = useCallback(
209
220
  (path: (string | number)[]) => {
@@ -214,21 +225,39 @@ export const useForm = <T extends BaseObject>({
214
225
  [validate, values],
215
226
  );
216
227
 
217
- return {
218
- // State.
219
- values,
220
- errors,
221
- touched,
222
- changed,
223
- canSave,
224
- handleSave,
225
-
226
- // Field utils.
227
- getStatus,
228
- getValue: getFormValue,
229
- onValueChange,
230
- onTouched,
231
- } satisfies FormHandler<T>;
228
+ return useMemo(
229
+ () => ({
230
+ // State.
231
+ values,
232
+ errors,
233
+ touched,
234
+ changed,
235
+ canSave,
236
+ formIsValid,
237
+
238
+ // Actions.
239
+ handleSave,
240
+
241
+ // Field utils.
242
+ getStatus,
243
+ getValue: getFormValue,
244
+ onValueChange,
245
+ onTouched,
246
+ }),
247
+ [
248
+ values,
249
+ errors,
250
+ touched,
251
+ changed,
252
+ canSave,
253
+ formIsValid,
254
+ handleSave,
255
+ getStatus,
256
+ getFormValue,
257
+ onValueChange,
258
+ onTouched,
259
+ ],
260
+ );
232
261
  };
233
262
 
234
263
  const createKeySet = <T extends BaseObject, V>(obj: T, value: V): Record<JsonPath, V> => {
@@ -239,7 +268,7 @@ const createKeySet = <T extends BaseObject, V>(obj: T, value: V): Record<JsonPat
239
268
  const flatMap = (errors: ValidationError[]) => {
240
269
  return errors.reduce(
241
270
  (result, { path, message }) => {
242
- // Convert the validation error path format to our JsonPath format
271
+ // Convert the validation error path format to our JsonPath format.
243
272
  const jsonPath = fromEffectValidationPath(path);
244
273
  if (!(jsonPath in result)) {
245
274
  result[jsonPath] = message;
@@ -36,7 +36,7 @@ export default [
36
36
  'format hostname': 'Hostname',
37
37
  'format integer': 'Integer',
38
38
  'format json': 'JSON',
39
- 'format latlng': 'LatLng',
39
+ 'format latlng': 'Geopoint',
40
40
  'format markdown': 'Markdown',
41
41
  'format percent': 'Percent',
42
42
  'format ref': 'Reference',