@dxos/react-ui-form 0.8.4-main.fd6878d → 0.8.4-staging.60fe92afc8
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.
- package/LICENSE +102 -5
- package/README.md +0 -1
- package/dist/lib/browser/index.mjs +4054 -2217
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/translations.mjs +87 -0
- package/dist/lib/browser/translations.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +4054 -2217
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/translations.mjs +89 -0
- package/dist/lib/node-esm/translations.mjs.map +7 -0
- package/dist/types/src/components/FieldEditor/FieldEditor.d.ts +8 -8
- package/dist/types/src/components/FieldEditor/FieldEditor.d.ts.map +1 -1
- package/dist/types/src/components/FieldEditor/FieldEditor.stories.d.ts +89 -5
- package/dist/types/src/components/FieldEditor/FieldEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/Form/Form.d.ts +135 -25
- package/dist/types/src/components/Form/Form.d.ts.map +1 -1
- package/dist/types/src/components/Form/Form.stories.d.ts +207 -56
- package/dist/types/src/components/Form/Form.stories.d.ts.map +1 -1
- package/dist/types/src/components/Form/FormField.d.ts +52 -0
- package/dist/types/src/components/Form/FormField.d.ts.map +1 -0
- package/dist/types/src/components/Form/FormFieldComponent.d.ts +98 -0
- package/dist/types/src/components/Form/FormFieldComponent.d.ts.map +1 -0
- package/dist/types/src/components/Form/FormFieldComponent.stories.d.ts +9 -0
- package/dist/types/src/components/Form/FormFieldComponent.stories.d.ts.map +1 -0
- package/dist/types/src/components/Form/FormFieldSet.d.ts +26 -0
- package/dist/types/src/components/Form/FormFieldSet.d.ts.map +1 -0
- package/dist/types/src/components/Form/FormTooltipsContext.d.ts +19 -0
- package/dist/types/src/components/Form/FormTooltipsContext.d.ts.map +1 -0
- package/dist/types/src/components/Form/Layout/FormLayout.d.ts +50 -0
- package/dist/types/src/components/Form/Layout/FormLayout.d.ts.map +1 -0
- package/dist/types/src/components/Form/Layout/FormLayout.stories.d.ts +123 -0
- package/dist/types/src/components/Form/Layout/FormLayout.stories.d.ts.map +1 -0
- package/dist/types/src/components/Form/Layout/FormLayout.stories.test.d.ts +2 -0
- package/dist/types/src/components/Form/Layout/FormLayout.stories.test.d.ts.map +1 -0
- package/dist/types/src/components/Form/Layout/FormLayout.test.d.ts +2 -0
- package/dist/types/src/components/Form/Layout/FormLayout.test.d.ts.map +1 -0
- package/dist/types/src/components/Form/Layout/annotation.d.ts +30 -0
- package/dist/types/src/components/Form/Layout/annotation.d.ts.map +1 -0
- package/dist/types/src/components/Form/Layout/index.d.ts +4 -0
- package/dist/types/src/components/Form/Layout/index.d.ts.map +1 -0
- package/dist/types/src/components/Form/Layout/parser.d.ts +31 -0
- package/dist/types/src/components/Form/Layout/parser.d.ts.map +1 -0
- package/dist/types/src/components/Form/Layout/parser.test.d.ts +2 -0
- package/dist/types/src/components/Form/Layout/parser.test.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/ArrayField.d.ts +18 -0
- package/dist/types/src/components/Form/fields/ArrayField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/BooleanField.d.ts +4 -0
- package/dist/types/src/components/Form/fields/BooleanField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/DateField.d.ts +4 -0
- package/dist/types/src/components/Form/fields/DateField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/GeoPointField.d.ts +5 -0
- package/dist/types/src/components/Form/fields/GeoPointField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/InlineRefField.d.ts +13 -0
- package/dist/types/src/components/Form/fields/InlineRefField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/MarkdownField.d.ts +11 -0
- package/dist/types/src/components/Form/fields/MarkdownField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/MarkdownField.stories.d.ts +89 -0
- package/dist/types/src/components/Form/fields/MarkdownField.stories.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/NumberField.d.ts +4 -0
- package/dist/types/src/components/Form/fields/NumberField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/RefField.d.ts +34 -0
- package/dist/types/src/components/Form/fields/RefField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/RefField.test.d.ts +2 -0
- package/dist/types/src/components/Form/fields/RefField.test.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/SelectField.d.ts +13 -0
- package/dist/types/src/components/Form/fields/SelectField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/SelectOptionField.d.ts +5 -0
- package/dist/types/src/components/Form/fields/SelectOptionField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/SelectOptionField.stories.d.ts +89 -0
- package/dist/types/src/components/Form/fields/SelectOptionField.stories.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/TextAreaField.d.ts +4 -0
- package/dist/types/src/components/Form/fields/TextAreaField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/TextField.d.ts +4 -0
- package/dist/types/src/components/Form/fields/TextField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/TupleField.d.ts +6 -0
- package/dist/types/src/components/Form/fields/TupleField.d.ts.map +1 -0
- package/dist/types/src/components/Form/fields/index.d.ts +14 -0
- package/dist/types/src/components/Form/fields/index.d.ts.map +1 -0
- package/dist/types/src/components/Form/index.d.ts +5 -4
- package/dist/types/src/components/Form/index.d.ts.map +1 -1
- package/dist/types/src/components/Form/meta-tags.d.ts +30 -0
- package/dist/types/src/components/Form/meta-tags.d.ts.map +1 -0
- package/dist/types/src/components/Form/meta-tags.test.d.ts +2 -0
- package/dist/types/src/components/Form/meta-tags.test.d.ts.map +1 -0
- package/dist/types/src/components/ObjectForm/ObjectForm.d.ts +8 -0
- package/dist/types/src/components/ObjectForm/ObjectForm.d.ts.map +1 -0
- package/dist/types/src/components/ObjectForm/ObjectForm.stories.d.ts +87 -0
- package/dist/types/src/components/ObjectForm/ObjectForm.stories.d.ts.map +1 -0
- package/dist/types/src/components/ObjectForm/index.d.ts +2 -0
- package/dist/types/src/components/ObjectForm/index.d.ts.map +1 -0
- package/dist/types/src/components/ObjectPicker/ObjectPicker.d.ts +68 -0
- package/dist/types/src/components/ObjectPicker/ObjectPicker.d.ts.map +1 -0
- package/dist/types/src/components/ObjectPicker/ObjectPicker.stories.d.ts +92 -0
- package/dist/types/src/components/ObjectPicker/ObjectPicker.stories.d.ts.map +1 -0
- package/dist/types/src/components/ObjectPicker/index.d.ts +2 -0
- package/dist/types/src/components/ObjectPicker/index.d.ts.map +1 -0
- package/dist/types/src/components/ObjectProperties/ObjectProperties.d.ts +9 -0
- package/dist/types/src/components/ObjectProperties/ObjectProperties.d.ts.map +1 -0
- package/dist/types/src/components/ObjectProperties/ObjectProperties.stories.d.ts +132 -0
- package/dist/types/src/components/ObjectProperties/ObjectProperties.stories.d.ts.map +1 -0
- package/dist/types/src/components/ObjectProperties/ObjectProperties.test.d.ts +2 -0
- package/dist/types/src/components/ObjectProperties/ObjectProperties.test.d.ts.map +1 -0
- package/dist/types/src/components/ObjectProperties/index.d.ts +2 -0
- package/dist/types/src/components/ObjectProperties/index.d.ts.map +1 -0
- package/dist/types/src/components/ObjectTree/ObjectTree.d.ts +9 -0
- package/dist/types/src/components/ObjectTree/ObjectTree.d.ts.map +1 -0
- package/dist/types/src/components/ObjectTree/ObjectTree.stories.d.ts +91 -0
- package/dist/types/src/components/ObjectTree/ObjectTree.stories.d.ts.map +1 -0
- package/dist/types/src/components/ObjectTree/index.d.ts +2 -0
- package/dist/types/src/components/ObjectTree/index.d.ts.map +1 -0
- package/dist/types/src/components/RefEditor/RefEditor.d.ts +73 -0
- package/dist/types/src/components/RefEditor/RefEditor.d.ts.map +1 -0
- package/dist/types/src/components/RefEditor/RefEditor.stories.d.ts +92 -0
- package/dist/types/src/components/RefEditor/RefEditor.stories.d.ts.map +1 -0
- package/dist/types/src/components/RefEditor/index.d.ts +3 -0
- package/dist/types/src/components/RefEditor/index.d.ts.map +1 -0
- package/dist/types/src/components/RefEditor/ref-editor-extension.d.ts +47 -0
- package/dist/types/src/components/RefEditor/ref-editor-extension.d.ts.map +1 -0
- package/dist/types/src/components/Settings/Settings.d.ts +51 -0
- package/dist/types/src/components/Settings/Settings.d.ts.map +1 -0
- package/dist/types/src/components/Settings/Settings.stories.d.ts +89 -0
- package/dist/types/src/components/Settings/Settings.stories.d.ts.map +1 -0
- package/dist/types/src/components/Settings/SettingsFieldSet.d.ts +43 -0
- package/dist/types/src/components/Settings/SettingsFieldSet.d.ts.map +1 -0
- package/dist/types/src/components/Settings/SettingsItem.d.ts +16 -0
- package/dist/types/src/components/Settings/SettingsItem.d.ts.map +1 -0
- package/dist/types/src/components/Settings/SettingsSelectField.d.ts +18 -0
- package/dist/types/src/components/Settings/SettingsSelectField.d.ts.map +1 -0
- package/dist/types/src/components/Settings/index.d.ts +4 -0
- package/dist/types/src/components/Settings/index.d.ts.map +1 -0
- package/dist/types/src/components/ViewEditor/ViewEditor.d.ts +21 -11
- package/dist/types/src/components/ViewEditor/ViewEditor.d.ts.map +1 -1
- package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts +91 -9
- package/dist/types/src/components/ViewEditor/ViewEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/index.d.ts +6 -2
- package/dist/types/src/components/index.d.ts.map +1 -1
- package/dist/types/src/components/testing.d.ts +14 -4
- package/dist/types/src/components/testing.d.ts.map +1 -1
- package/dist/types/src/hooks/index.d.ts +2 -3
- package/dist/types/src/hooks/index.d.ts.map +1 -1
- package/dist/types/src/hooks/useFormHandler.d.ts +82 -0
- package/dist/types/src/hooks/useFormHandler.d.ts.map +1 -0
- package/dist/types/src/hooks/useKeyHandler.d.ts +7 -0
- package/dist/types/src/hooks/useKeyHandler.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +0 -2
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/translations.d.ts +70 -55
- package/dist/types/src/translations.d.ts.map +1 -1
- package/dist/types/src/util/field-type.d.ts +19 -0
- package/dist/types/src/util/field-type.d.ts.map +1 -0
- package/dist/types/src/util/field.d.ts +3 -3
- package/dist/types/src/util/field.d.ts.map +1 -1
- package/dist/types/src/util/formatting.d.ts +2 -2
- package/dist/types/src/util/formatting.d.ts.map +1 -1
- package/dist/types/src/util/index.d.ts +2 -1
- package/dist/types/src/util/index.d.ts.map +1 -1
- package/dist/types/src/util/properties.d.ts +35 -0
- package/dist/types/src/util/properties.d.ts.map +1 -0
- package/dist/types/src/util/properties.test.d.ts +2 -0
- package/dist/types/src/util/properties.test.d.ts.map +1 -0
- package/dist/types/src/util/refs.d.ts +3 -3
- package/dist/types/src/util/refs.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +64 -47
- package/src/components/FieldEditor/FieldEditor.stories.tsx +49 -31
- package/src/components/FieldEditor/FieldEditor.test.tsx +4 -2
- package/src/components/FieldEditor/FieldEditor.tsx +100 -97
- package/src/components/Form/AUDIT.md +17 -0
- package/src/components/Form/Form.stories.tsx +151 -221
- package/src/components/Form/Form.tsx +461 -95
- package/src/components/Form/FormField.tsx +345 -0
- package/src/components/Form/FormFieldComponent.stories.tsx +65 -0
- package/src/components/Form/FormFieldComponent.tsx +253 -0
- package/src/components/Form/FormFieldSet.tsx +213 -0
- package/src/components/Form/FormTooltipsContext.ts +25 -0
- package/src/components/Form/Layout/FormLayout.stories.test.tsx +51 -0
- package/src/components/Form/Layout/FormLayout.stories.tsx +422 -0
- package/src/components/Form/Layout/FormLayout.test.ts +59 -0
- package/src/components/Form/Layout/FormLayout.tsx +251 -0
- package/src/components/Form/Layout/annotation.ts +38 -0
- package/src/components/Form/Layout/index.ts +7 -0
- package/src/components/Form/Layout/parser.test.ts +120 -0
- package/src/components/Form/Layout/parser.ts +167 -0
- package/src/components/Form/README.md +3 -0
- package/src/components/Form/fields/ArrayField.tsx +173 -0
- package/src/components/Form/fields/BooleanField.tsx +22 -0
- package/src/components/Form/fields/DateField.tsx +101 -0
- package/src/components/Form/fields/GeoPointField.tsx +124 -0
- package/src/components/Form/fields/InlineRefField.tsx +138 -0
- package/src/components/Form/fields/MarkdownField.stories.tsx +206 -0
- package/src/components/Form/fields/MarkdownField.tsx +167 -0
- package/src/components/Form/fields/NumberField.tsx +76 -0
- package/src/components/Form/fields/RefField.test.ts +45 -0
- package/src/components/Form/fields/RefField.tsx +266 -0
- package/src/components/Form/fields/SelectField.tsx +77 -0
- package/src/components/Form/fields/SelectOptionField.stories.tsx +66 -0
- package/src/components/Form/{custom/SelectOptionsInput.tsx → fields/SelectOptionField.tsx} +39 -32
- package/src/components/Form/fields/TextAreaField.tsx +38 -0
- package/src/components/Form/fields/TextField.tsx +38 -0
- package/src/components/Form/fields/TupleField.tsx +55 -0
- package/src/components/Form/fields/index.ts +17 -0
- package/src/components/Form/index.ts +6 -4
- package/src/components/Form/meta-tags.test.ts +47 -0
- package/src/components/Form/meta-tags.ts +33 -0
- package/src/components/ObjectForm/ObjectForm.stories.tsx +62 -0
- package/src/components/ObjectForm/ObjectForm.tsx +111 -0
- package/src/components/{ControlSection → ObjectForm}/index.ts +1 -1
- package/src/components/ObjectPicker/ObjectPicker.stories.tsx +212 -0
- package/src/components/ObjectPicker/ObjectPicker.tsx +202 -0
- package/src/components/ObjectPicker/index.ts +5 -0
- package/src/components/ObjectProperties/ObjectProperties.stories.tsx +350 -0
- package/src/components/ObjectProperties/ObjectProperties.test.tsx +111 -0
- package/src/components/ObjectProperties/ObjectProperties.tsx +146 -0
- package/src/components/ObjectProperties/index.ts +5 -0
- package/src/components/ObjectTree/ObjectTree.stories.tsx +134 -0
- package/src/components/ObjectTree/ObjectTree.tsx +190 -0
- package/src/components/ObjectTree/index.ts +5 -0
- package/src/components/RefEditor/RefEditor.stories.tsx +148 -0
- package/src/components/RefEditor/RefEditor.tsx +275 -0
- package/src/components/RefEditor/index.ts +6 -0
- package/src/components/RefEditor/ref-editor-extension.ts +254 -0
- package/src/components/Settings/AUDIT.md +51 -0
- package/src/components/Settings/Settings.stories.tsx +104 -0
- package/src/components/Settings/Settings.tsx +81 -0
- package/src/components/Settings/SettingsFieldSet.tsx +257 -0
- package/src/components/Settings/SettingsItem.tsx +58 -0
- package/src/components/Settings/SettingsSelectField.tsx +59 -0
- package/src/components/Settings/index.ts +7 -0
- package/src/components/ViewEditor/ViewEditor.stories.tsx +132 -64
- package/src/components/ViewEditor/ViewEditor.test.tsx +23 -22
- package/src/components/ViewEditor/ViewEditor.tsx +378 -216
- package/src/components/index.ts +6 -3
- package/src/components/testing.tsx +31 -12
- package/src/hooks/index.ts +2 -3
- package/src/hooks/useFormHandler.ts +380 -0
- package/src/hooks/useKeyHandler.tsx +37 -0
- package/src/index.ts +0 -4
- package/src/translations.ts +76 -59
- package/src/util/field-type.ts +60 -0
- package/src/util/field.ts +33 -43
- package/src/util/formatting.ts +22 -22
- package/src/util/index.ts +2 -1
- package/src/util/properties.test.ts +153 -0
- package/src/util/properties.ts +100 -0
- package/src/util/refs.ts +20 -19
- package/src/vite-env.d.ts +10 -0
- package/dist/types/src/components/ControlSection/ControlSection.d.ts +0 -23
- package/dist/types/src/components/ControlSection/ControlSection.d.ts.map +0 -1
- package/dist/types/src/components/ControlSection/ControlSection.stories.d.ts +0 -8
- package/dist/types/src/components/ControlSection/ControlSection.stories.d.ts.map +0 -1
- package/dist/types/src/components/ControlSection/index.d.ts +0 -2
- package/dist/types/src/components/ControlSection/index.d.ts.map +0 -1
- package/dist/types/src/components/Form/ArrayField.d.ts +0 -16
- package/dist/types/src/components/Form/ArrayField.d.ts.map +0 -1
- package/dist/types/src/components/Form/Defaults.d.ts +0 -14
- package/dist/types/src/components/Form/Defaults.d.ts.map +0 -1
- package/dist/types/src/components/Form/FormActions.d.ts +0 -9
- package/dist/types/src/components/Form/FormActions.d.ts.map +0 -1
- package/dist/types/src/components/Form/FormContent.d.ts +0 -57
- package/dist/types/src/components/Form/FormContent.d.ts.map +0 -1
- package/dist/types/src/components/Form/FormContext.d.ts +0 -23
- package/dist/types/src/components/Form/FormContext.d.ts.map +0 -1
- package/dist/types/src/components/Form/Input.d.ts +0 -26
- package/dist/types/src/components/Form/Input.d.ts.map +0 -1
- package/dist/types/src/components/Form/RefField.d.ts +0 -12
- package/dist/types/src/components/Form/RefField.d.ts.map +0 -1
- package/dist/types/src/components/Form/custom/GeoPointInput.d.ts +0 -4
- package/dist/types/src/components/Form/custom/GeoPointInput.d.ts.map +0 -1
- package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts +0 -4
- package/dist/types/src/components/Form/custom/SelectOptionsInput.d.ts.map +0 -1
- package/dist/types/src/components/Form/custom/SelectOptionsInput.stories.d.ts +0 -7
- package/dist/types/src/components/Form/custom/SelectOptionsInput.stories.d.ts.map +0 -1
- package/dist/types/src/components/Form/custom/TupleInput.d.ts +0 -6
- package/dist/types/src/components/Form/custom/TupleInput.d.ts.map +0 -1
- package/dist/types/src/components/Form/custom/index.d.ts +0 -4
- package/dist/types/src/components/Form/custom/index.d.ts.map +0 -1
- package/dist/types/src/components/Form/factory.d.ts +0 -8
- package/dist/types/src/components/Form/factory.d.ts.map +0 -1
- package/dist/types/src/components/deprecated/Form.d.ts +0 -15
- package/dist/types/src/components/deprecated/Form.d.ts.map +0 -1
- package/dist/types/src/components/deprecated/Form.stories.d.ts +0 -13
- package/dist/types/src/components/deprecated/Form.stories.d.ts.map +0 -1
- package/dist/types/src/components/deprecated/FormInput.d.ts +0 -16
- package/dist/types/src/components/deprecated/FormInput.d.ts.map +0 -1
- package/dist/types/src/components/deprecated/index.d.ts +0 -2
- package/dist/types/src/components/deprecated/index.d.ts.map +0 -1
- package/dist/types/src/hooks/useForm.d.ts +0 -67
- package/dist/types/src/hooks/useForm.d.ts.map +0 -1
- package/dist/types/src/hooks/useQueryRefOptions.d.ts +0 -23
- package/dist/types/src/hooks/useQueryRefOptions.d.ts.map +0 -1
- package/dist/types/src/hooks/useRefQueryLookupHandler.d.ts +0 -8
- package/dist/types/src/hooks/useRefQueryLookupHandler.d.ts.map +0 -1
- package/dist/types/src/util/schema.d.ts +0 -3
- package/dist/types/src/util/schema.d.ts.map +0 -1
- package/src/components/ControlSection/ControlSection.stories.tsx +0 -42
- package/src/components/ControlSection/ControlSection.tsx +0 -139
- package/src/components/Form/ArrayField.tsx +0 -125
- package/src/components/Form/Defaults.tsx +0 -204
- package/src/components/Form/FormActions.tsx +0 -54
- package/src/components/Form/FormContent.tsx +0 -241
- package/src/components/Form/FormContext.tsx +0 -110
- package/src/components/Form/Input.tsx +0 -51
- package/src/components/Form/RefField.tsx +0 -214
- package/src/components/Form/custom/GeoPointInput.tsx +0 -72
- package/src/components/Form/custom/SelectOptionsInput.stories.tsx +0 -62
- package/src/components/Form/custom/TupleInput.tsx +0 -47
- package/src/components/Form/custom/index.ts +0 -7
- package/src/components/Form/factory.tsx +0 -31
- package/src/components/deprecated/Form.stories.tsx +0 -63
- package/src/components/deprecated/Form.tsx +0 -119
- package/src/components/deprecated/FormInput.tsx +0 -71
- package/src/components/deprecated/index.ts +0 -5
- package/src/hooks/useForm.ts +0 -293
- package/src/hooks/useQueryRefOptions.ts +0 -53
- package/src/hooks/useRefQueryLookupHandler.ts +0 -39
- package/src/util/schema.ts +0 -14
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import '@dxos-theme';
|
|
6
|
-
|
|
7
|
-
import { type Meta, type StoryObj } from '@storybook/react-vite';
|
|
8
|
-
import { Schema } from 'effect';
|
|
9
|
-
import React, { useState } from 'react';
|
|
10
|
-
|
|
11
|
-
import { type SelectOption, SelectOptionSchema } from '@dxos/echo-schema';
|
|
12
|
-
import { withLayout, withTheme } from '@dxos/storybook-utils';
|
|
13
|
-
|
|
14
|
-
import { translations } from '../../../translations';
|
|
15
|
-
import { TestLayout, TestPanel } from '../../testing';
|
|
16
|
-
import { Form } from '../Form';
|
|
17
|
-
|
|
18
|
-
import { SelectOptionInput } from './SelectOptionsInput';
|
|
19
|
-
|
|
20
|
-
const schema = Schema.Struct({
|
|
21
|
-
options: Schema.Array(SelectOptionSchema).pipe(Schema.mutable),
|
|
22
|
-
}).pipe(Schema.mutable);
|
|
23
|
-
|
|
24
|
-
const DefaultStory = () => {
|
|
25
|
-
const [values, setValues] = useState<{ options: SelectOption[] }>({
|
|
26
|
-
// TODO(Zaymon): Pick colors from our limited set.
|
|
27
|
-
options: [
|
|
28
|
-
{ id: 'opt_1', title: 'First option', color: 'amber' },
|
|
29
|
-
{ id: 'opt_2', title: 'Second option', color: 'emerald' },
|
|
30
|
-
{ id: 'opt_3', title: 'Third option', color: 'rose' },
|
|
31
|
-
],
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
return (
|
|
35
|
-
<TestLayout>
|
|
36
|
-
<TestPanel>
|
|
37
|
-
<Form
|
|
38
|
-
schema={schema}
|
|
39
|
-
values={values}
|
|
40
|
-
onSave={(vals) => setValues(vals)}
|
|
41
|
-
Custom={{ options: SelectOptionInput }}
|
|
42
|
-
/>
|
|
43
|
-
</TestPanel>
|
|
44
|
-
</TestLayout>
|
|
45
|
-
);
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
const meta: Meta = {
|
|
49
|
-
title: 'ui/react-ui-form/SelectOptionsInput',
|
|
50
|
-
component: SelectOptionInput,
|
|
51
|
-
render: DefaultStory,
|
|
52
|
-
decorators: [withTheme, withLayout({ fullscreen: true })],
|
|
53
|
-
parameters: {
|
|
54
|
-
translations,
|
|
55
|
-
},
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
export default meta;
|
|
59
|
-
|
|
60
|
-
type Story = StoryObj;
|
|
61
|
-
|
|
62
|
-
export const Default: Story = {};
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import React from 'react';
|
|
6
|
-
|
|
7
|
-
import { Input } from '@dxos/react-ui';
|
|
8
|
-
import { mx } from '@dxos/react-ui-theme';
|
|
9
|
-
import { safeParseFloat } from '@dxos/util';
|
|
10
|
-
|
|
11
|
-
import { InputHeader, type InputProps } from '../Input';
|
|
12
|
-
|
|
13
|
-
const gridCols = ['grid-cols-1', 'grid-cols-2', 'grid-cols-3', 'grid-cols-4'];
|
|
14
|
-
|
|
15
|
-
export const TupleInput = ({
|
|
16
|
-
binding,
|
|
17
|
-
type,
|
|
18
|
-
label,
|
|
19
|
-
disabled,
|
|
20
|
-
getStatus,
|
|
21
|
-
getValue,
|
|
22
|
-
onValueChange,
|
|
23
|
-
onBlur,
|
|
24
|
-
}: InputProps & { binding: string[] }) => {
|
|
25
|
-
const { status, error } = getStatus();
|
|
26
|
-
|
|
27
|
-
// TODO(burdon): Generalize number/float/string, etc.
|
|
28
|
-
const values: Record<string, number> = getValue<Record<string, number>>() ?? {};
|
|
29
|
-
|
|
30
|
-
return (
|
|
31
|
-
<Input.Root validationValence={status}>
|
|
32
|
-
<InputHeader error={error} label={label} />
|
|
33
|
-
<div className={mx('grid gap-2', gridCols[binding.length - 1])}>
|
|
34
|
-
{binding.map((prop) => (
|
|
35
|
-
<Input.TextInput
|
|
36
|
-
key={prop}
|
|
37
|
-
type='number'
|
|
38
|
-
disabled={disabled}
|
|
39
|
-
value={values[prop]}
|
|
40
|
-
onChange={(event) => onValueChange(type, { ...values, [prop]: safeParseFloat(event.target.value, 0) })}
|
|
41
|
-
onBlur={onBlur}
|
|
42
|
-
/>
|
|
43
|
-
))}
|
|
44
|
-
</div>
|
|
45
|
-
</Input.Root>
|
|
46
|
-
);
|
|
47
|
-
};
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { FormatEnum } from '@dxos/echo-schema';
|
|
6
|
-
import { type SimpleType } from '@dxos/effect';
|
|
7
|
-
|
|
8
|
-
import { GeoPointInput } from './custom';
|
|
9
|
-
import { BooleanInput, MarkdownInput, NumberInput, TextInput } from './Defaults';
|
|
10
|
-
import { type InputComponent } from './Input';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Get property input component.
|
|
14
|
-
*/
|
|
15
|
-
export const getInputComponent = (type: SimpleType, format?: FormatEnum | string): InputComponent | undefined => {
|
|
16
|
-
switch (format) {
|
|
17
|
-
case FormatEnum.GeoPoint:
|
|
18
|
-
return GeoPointInput;
|
|
19
|
-
case FormatEnum.Markdown:
|
|
20
|
-
return MarkdownInput;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
switch (type) {
|
|
24
|
-
case 'string':
|
|
25
|
-
return TextInput;
|
|
26
|
-
case 'number':
|
|
27
|
-
return NumberInput;
|
|
28
|
-
case 'boolean':
|
|
29
|
-
return BooleanInput;
|
|
30
|
-
}
|
|
31
|
-
};
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import '@dxos-theme';
|
|
6
|
-
|
|
7
|
-
import { type Meta, type StoryObj } from '@storybook/react-vite';
|
|
8
|
-
import React from 'react';
|
|
9
|
-
|
|
10
|
-
import { live } from '@dxos/live-object';
|
|
11
|
-
import { TestSchema, type TestType, testData, testView } from '@dxos/schema/testing';
|
|
12
|
-
import { withLayout, withTheme } from '@dxos/storybook-utils';
|
|
13
|
-
|
|
14
|
-
import { translations } from '../../translations';
|
|
15
|
-
import { TestLayout, TestPanel } from '../testing';
|
|
16
|
-
|
|
17
|
-
import { DeprecatedForm, type DeprecatedFormProps } from './Form';
|
|
18
|
-
|
|
19
|
-
const DefaultStory = (props: DeprecatedFormProps) => (
|
|
20
|
-
<TestLayout json={{ props }}>
|
|
21
|
-
<TestPanel>
|
|
22
|
-
<DeprecatedForm {...props} />
|
|
23
|
-
</TestPanel>
|
|
24
|
-
</TestLayout>
|
|
25
|
-
);
|
|
26
|
-
|
|
27
|
-
const meta: Meta<typeof DefaultStory> = {
|
|
28
|
-
title: 'ui/react-ui-form/DeprecatedForm',
|
|
29
|
-
component: DeprecatedForm,
|
|
30
|
-
render: DefaultStory,
|
|
31
|
-
decorators: [withLayout({ fullscreen: true }), withTheme],
|
|
32
|
-
parameters: {
|
|
33
|
-
translations,
|
|
34
|
-
},
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
export default meta;
|
|
38
|
-
|
|
39
|
-
type Story = StoryObj<DeprecatedFormProps<TestType>>;
|
|
40
|
-
|
|
41
|
-
export const Default: Story = {
|
|
42
|
-
args: {
|
|
43
|
-
object: live(testData),
|
|
44
|
-
projection: live(testView).projection,
|
|
45
|
-
schema: TestSchema,
|
|
46
|
-
},
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
export const Empty: Story = {
|
|
50
|
-
args: {
|
|
51
|
-
projection: live(testView).projection,
|
|
52
|
-
schema: TestSchema,
|
|
53
|
-
},
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
export const Readonly: Story = {
|
|
57
|
-
args: {
|
|
58
|
-
object: live(testData),
|
|
59
|
-
projection: live(testView).projection,
|
|
60
|
-
schema: TestSchema,
|
|
61
|
-
readonly: true,
|
|
62
|
-
},
|
|
63
|
-
};
|
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { Option, type Schema, SchemaAST, pipe } from 'effect';
|
|
6
|
-
import React, { useState } from 'react';
|
|
7
|
-
|
|
8
|
-
import { findProperty } from '@dxos/effect';
|
|
9
|
-
import { Input, type TextInputProps as NativeTextInputProps, type ThemedClassName } from '@dxos/react-ui';
|
|
10
|
-
import { mx } from '@dxos/react-ui-theme';
|
|
11
|
-
import { type Projection, getFieldValue, setFieldValue } from '@dxos/schema';
|
|
12
|
-
|
|
13
|
-
export type DeprecatedFormProps<T extends {} = {}> = ThemedClassName<{
|
|
14
|
-
projection: Projection;
|
|
15
|
-
object: T;
|
|
16
|
-
schema?: Schema.Schema<T>;
|
|
17
|
-
readonly?: boolean;
|
|
18
|
-
}>;
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Schema-based object form.
|
|
22
|
-
*/
|
|
23
|
-
export const DeprecatedForm = <T extends {} = {}>({
|
|
24
|
-
classNames,
|
|
25
|
-
projection,
|
|
26
|
-
object,
|
|
27
|
-
schema,
|
|
28
|
-
readonly,
|
|
29
|
-
}: DeprecatedFormProps<T>) => {
|
|
30
|
-
return (
|
|
31
|
-
<div role='none' className={mx('flex flex-col w-full gap-2 p-2', classNames)}>
|
|
32
|
-
{projection.fields.map((field) => {
|
|
33
|
-
const prop = schema && findProperty(schema, field.path);
|
|
34
|
-
const title = (prop && pipe(SchemaAST.getTitleAnnotation(prop), Option.getOrUndefined)) ?? '';
|
|
35
|
-
const description = (prop && pipe(SchemaAST.getDescriptionAnnotation(prop), Option.getOrUndefined)) ?? title;
|
|
36
|
-
// const format =
|
|
37
|
-
// (prop && (getPatternAnnotation(prop) ?? (SchemaAST.isNumberKeyword(prop) && RealNumberFormat))) || undefined;
|
|
38
|
-
|
|
39
|
-
//
|
|
40
|
-
// Boolean
|
|
41
|
-
//
|
|
42
|
-
|
|
43
|
-
if (prop && SchemaAST.isBooleanKeyword(prop)) {
|
|
44
|
-
const value = object && getFieldValue(object, field);
|
|
45
|
-
const onChange = (checked: boolean) => {
|
|
46
|
-
setFieldValue(object, field, checked);
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
return (
|
|
50
|
-
<div key={field.path} className='flex flex-col w-full gap-1'>
|
|
51
|
-
<Input.Root>
|
|
52
|
-
<Input.Label classNames='px-1'>{title}</Input.Label>
|
|
53
|
-
<Input.Switch disabled={readonly} checked={!!value} onCheckedChange={(checked) => onChange(checked)} />
|
|
54
|
-
</Input.Root>
|
|
55
|
-
</div>
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
//
|
|
60
|
-
// Strings
|
|
61
|
-
// TODO(burdon): Other types:
|
|
62
|
-
// - RichText editor
|
|
63
|
-
// - Numbers (with formatting: commas, currency, etc.)
|
|
64
|
-
// - Date (https://ark-ui.com/react/docs/components/date-picker)
|
|
65
|
-
// - Enum (single/multi-select)
|
|
66
|
-
//
|
|
67
|
-
|
|
68
|
-
const value = object ? getFieldValue(object, field) : undefined;
|
|
69
|
-
|
|
70
|
-
return (
|
|
71
|
-
<div key={field.path} className='flex flex-col w-full gap-1'>
|
|
72
|
-
<Input.Root>
|
|
73
|
-
<Input.Label classNames='px-1'>{title}</Input.Label>
|
|
74
|
-
<TextInput
|
|
75
|
-
disabled={readonly}
|
|
76
|
-
placeholder={description}
|
|
77
|
-
// format={format}
|
|
78
|
-
value={value ? String(value) : ''}
|
|
79
|
-
onChange={(event) => setFieldValue(object, field, event.target.value)}
|
|
80
|
-
/>
|
|
81
|
-
</Input.Root>
|
|
82
|
-
</div>
|
|
83
|
-
);
|
|
84
|
-
})}
|
|
85
|
-
</div>
|
|
86
|
-
);
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
type TextInputProps = {
|
|
90
|
-
// format?: PatternAnnotation;
|
|
91
|
-
} & Omit<NativeTextInputProps, 'style'>;
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* @deprecated
|
|
95
|
-
*/
|
|
96
|
-
// TODO(burdon): Remove.
|
|
97
|
-
// TODO(burdon): Use schema validation directly.
|
|
98
|
-
const TextInput = ({ onChange, ...props }: TextInputProps) => {
|
|
99
|
-
const [valid, _setValid] = useState(true);
|
|
100
|
-
|
|
101
|
-
const handleChange: NativeTextInputProps['onChange'] = (ev) => {
|
|
102
|
-
// const text = ev.target.value;
|
|
103
|
-
// if (!format?.filter || text.match(format.filter)) {
|
|
104
|
-
onChange?.(ev);
|
|
105
|
-
// if (format?.valid) {
|
|
106
|
-
// setValid(format.valid.test(text));
|
|
107
|
-
// }
|
|
108
|
-
// }
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
return (
|
|
112
|
-
<Input.TextInput
|
|
113
|
-
{...props}
|
|
114
|
-
// TODO(burdon): validationValence on Input.
|
|
115
|
-
style={valid ? {} : ({ '--tw-ring-color': 'red' } as any)}
|
|
116
|
-
onChange={handleChange}
|
|
117
|
-
/>
|
|
118
|
-
);
|
|
119
|
-
};
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2023 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import React, { type JSX, type PropsWithChildren } from 'react';
|
|
6
|
-
|
|
7
|
-
import { Input, type ThemedClassName } from '@dxos/react-ui';
|
|
8
|
-
import { mx, textBlockWidth } from '@dxos/react-ui-theme';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* @deprecated
|
|
12
|
-
*/
|
|
13
|
-
export const DeprecatedFormContainer = ({ children, classNames }: ThemedClassName<PropsWithChildren>) => {
|
|
14
|
-
return (
|
|
15
|
-
<div role='none' className='container-max-width p-4 justify-center overflow-x-hidden overflow-y-auto'>
|
|
16
|
-
<div role='form' className={mx('flex flex-col', textBlockWidth, classNames)}>
|
|
17
|
-
{children}
|
|
18
|
-
</div>
|
|
19
|
-
</div>
|
|
20
|
-
);
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export type DeprecatedFormInputProps = {
|
|
24
|
-
label: string;
|
|
25
|
-
description?: JSX.Element;
|
|
26
|
-
secondary?: JSX.Element;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* @deprecated
|
|
31
|
-
*/
|
|
32
|
-
export const DeprecatedFormInput = ({
|
|
33
|
-
label,
|
|
34
|
-
description,
|
|
35
|
-
secondary,
|
|
36
|
-
children,
|
|
37
|
-
}: PropsWithChildren<DeprecatedFormInputProps>) => {
|
|
38
|
-
const primary = (
|
|
39
|
-
<div role='none' className='flex w-full gap-4 py-1'>
|
|
40
|
-
<Input.Root>
|
|
41
|
-
<div role='none' className='flex flex-col w-full'>
|
|
42
|
-
<Input.Label classNames='flex min-bs-[--rail-action] items-center' style={{ fontSize: 'unset' }}>
|
|
43
|
-
{label}
|
|
44
|
-
</Input.Label>
|
|
45
|
-
{description && (
|
|
46
|
-
<Input.DescriptionAndValidation classNames='mbs-0.5'>
|
|
47
|
-
<Input.Description>{description}</Input.Description>
|
|
48
|
-
</Input.DescriptionAndValidation>
|
|
49
|
-
)}
|
|
50
|
-
</div>
|
|
51
|
-
|
|
52
|
-
<div role='none'>
|
|
53
|
-
<div role='none' className='flex min-bs-[--rail-action] items-center'>
|
|
54
|
-
{children}
|
|
55
|
-
</div>
|
|
56
|
-
</div>
|
|
57
|
-
</Input.Root>
|
|
58
|
-
</div>
|
|
59
|
-
);
|
|
60
|
-
|
|
61
|
-
if (secondary) {
|
|
62
|
-
return (
|
|
63
|
-
<div role='none'>
|
|
64
|
-
{primary}
|
|
65
|
-
{secondary}
|
|
66
|
-
</div>
|
|
67
|
-
);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
return primary;
|
|
71
|
-
};
|
package/src/hooks/useForm.ts
DELETED
|
@@ -1,293 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2025 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { type Schema, SchemaAST } from 'effect';
|
|
6
|
-
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
7
|
-
|
|
8
|
-
import { type BaseObject, getValue, setValue } from '@dxos/echo-schema';
|
|
9
|
-
import { type JsonPath, type SimpleType, createJsonPath, fromEffectValidationPath } from '@dxos/effect';
|
|
10
|
-
import { invariant } from '@dxos/invariant';
|
|
11
|
-
import { log } from '@dxos/log';
|
|
12
|
-
import { type ValidationError, validateSchema } from '@dxos/schema';
|
|
13
|
-
import { type MaybePromise } from '@dxos/util';
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Return type from `useForm` hook.
|
|
17
|
-
*/
|
|
18
|
-
export type FormHandler<T extends BaseObject> = {
|
|
19
|
-
//
|
|
20
|
-
// Form state management.
|
|
21
|
-
//
|
|
22
|
-
|
|
23
|
-
values: Partial<T>;
|
|
24
|
-
errors: Record<JsonPath, string>;
|
|
25
|
-
touched: Record<JsonPath, boolean>;
|
|
26
|
-
changed: Record<JsonPath, boolean>;
|
|
27
|
-
canSave: boolean;
|
|
28
|
-
formIsValid: boolean;
|
|
29
|
-
|
|
30
|
-
// TODO(burdon): Return onSave.
|
|
31
|
-
handleSave: () => void;
|
|
32
|
-
|
|
33
|
-
//
|
|
34
|
-
// Form input component helpers.
|
|
35
|
-
//
|
|
36
|
-
|
|
37
|
-
getStatus: (path: string | (string | number)[]) => { status?: 'error'; error?: string };
|
|
38
|
-
getValue: <V>(path: (string | number)[]) => V | undefined;
|
|
39
|
-
onValueChange: <V>(path: (string | number)[], type: SimpleType, value: V) => void;
|
|
40
|
-
onTouched: (path: (string | number)[]) => void;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Hook options.
|
|
45
|
-
*/
|
|
46
|
-
export interface FormOptions<T extends BaseObject> {
|
|
47
|
-
/**
|
|
48
|
-
* Effect schema (Type literal).
|
|
49
|
-
*/
|
|
50
|
-
schema: Schema.Schema<T, any>;
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Initial values (which may not pass validation).
|
|
54
|
-
*/
|
|
55
|
-
initialValues: Partial<T>;
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Callback for value changes. Note: This is called even when values are invalid.
|
|
59
|
-
* Sometimes the parent component may want to know about changes even if the form is
|
|
60
|
-
* in an invalid state.
|
|
61
|
-
*/
|
|
62
|
-
onValuesChanged?: (values: Partial<T>) => void;
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Custom validation function that runs only after schema validation passes.
|
|
66
|
-
* Use this for complex validation logic that can't be expressed in the schema.
|
|
67
|
-
* @returns Array of validation errors, or undefined if validation passes
|
|
68
|
-
*/
|
|
69
|
-
// TODO(burdon): Change to key x value?
|
|
70
|
-
onValidate?: (values: T) => ValidationError[] | undefined;
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Called when the form is valid.
|
|
74
|
-
*/
|
|
75
|
-
onValid?: (values: T, meta: { changed: FormHandler<T>['changed'] }) => void;
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Called when the form is submitted and passes validation.
|
|
79
|
-
*/
|
|
80
|
-
onSave?: (values: T, meta: { changed: FormHandler<T>['changed'] }) => MaybePromise<void>;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Creates a hook for managing form state, including values, validation, and submission.
|
|
85
|
-
* Deeply integrated with `@dxos/schema` for schema-based validation.
|
|
86
|
-
*/
|
|
87
|
-
export const useForm = <T extends BaseObject>({
|
|
88
|
-
schema,
|
|
89
|
-
initialValues,
|
|
90
|
-
onValuesChanged,
|
|
91
|
-
onValidate,
|
|
92
|
-
onValid,
|
|
93
|
-
onSave,
|
|
94
|
-
}: FormOptions<T>): FormHandler<T> => {
|
|
95
|
-
invariant(SchemaAST.isTypeLiteral(schema.ast));
|
|
96
|
-
|
|
97
|
-
const [values, setValues] = useState<Partial<T>>(initialValues);
|
|
98
|
-
|
|
99
|
-
useEffect(() => {
|
|
100
|
-
setValues(initialValues);
|
|
101
|
-
}, [initialValues]);
|
|
102
|
-
|
|
103
|
-
const [touched, setTouched] = useState<Record<JsonPath, boolean>>(createKeySet(initialValues, false));
|
|
104
|
-
const [changed, setChanged] = useState<Record<JsonPath, boolean>>(createKeySet(initialValues, false));
|
|
105
|
-
const [errors, setErrors] = useState<Record<JsonPath, string>>({});
|
|
106
|
-
const [saving, setSaving] = useState(false);
|
|
107
|
-
|
|
108
|
-
//
|
|
109
|
-
// Validation.
|
|
110
|
-
//
|
|
111
|
-
|
|
112
|
-
const validate = useCallback(
|
|
113
|
-
(values: Partial<T>): values is T => {
|
|
114
|
-
let errors: ValidationError[] = validateSchema(schema, values) ?? [];
|
|
115
|
-
if (errors.length === 0 && onValidate) {
|
|
116
|
-
const validatedValues = values as T;
|
|
117
|
-
errors = onValidate(validatedValues) ?? [];
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
setErrors(flatMap(errors));
|
|
121
|
-
const valid = errors.length === 0;
|
|
122
|
-
return valid;
|
|
123
|
-
},
|
|
124
|
-
[schema, onValidate],
|
|
125
|
-
);
|
|
126
|
-
|
|
127
|
-
// Validate on schema change.
|
|
128
|
-
const [schemaDirty, setSchemaDirty] = useState(false);
|
|
129
|
-
useEffect(() => setSchemaDirty(true), [schema]);
|
|
130
|
-
useEffect(() => {
|
|
131
|
-
if (schemaDirty) {
|
|
132
|
-
validate(values);
|
|
133
|
-
setSchemaDirty(false);
|
|
134
|
-
}
|
|
135
|
-
}, [values, schemaDirty]);
|
|
136
|
-
|
|
137
|
-
const formIsValid = useMemo(() => {
|
|
138
|
-
return Object.keys(errors).length === 0;
|
|
139
|
-
}, [errors]);
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* NOTE: We can submit if there is no touched field that has an error.
|
|
143
|
-
* Basically, if there's a validation message visible in the form, submit should be disabled.
|
|
144
|
-
*/
|
|
145
|
-
const canSave = useMemo(
|
|
146
|
-
() =>
|
|
147
|
-
!saving &&
|
|
148
|
-
// Check if any error paths that are touched have errors.
|
|
149
|
-
!Object.entries(touched).some(
|
|
150
|
-
([path, isTouched]) =>
|
|
151
|
-
isTouched &&
|
|
152
|
-
Object.keys(errors).some(
|
|
153
|
-
(errorPath) => errorPath === path || errorPath.startsWith(`${path}.`) || errorPath.startsWith(`${path}[`),
|
|
154
|
-
),
|
|
155
|
-
),
|
|
156
|
-
[touched, errors, saving],
|
|
157
|
-
);
|
|
158
|
-
|
|
159
|
-
const handleSave = useCallback(async () => {
|
|
160
|
-
if (validate(values)) {
|
|
161
|
-
setSaving(true);
|
|
162
|
-
try {
|
|
163
|
-
await onSave?.(values, { changed });
|
|
164
|
-
} finally {
|
|
165
|
-
setSaving(false);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
}, [values, validate, onSave, changed]);
|
|
169
|
-
|
|
170
|
-
//
|
|
171
|
-
// Fields.
|
|
172
|
-
//
|
|
173
|
-
|
|
174
|
-
const getStatus = useCallback<FormHandler<T>['getStatus']>(
|
|
175
|
-
(path) => {
|
|
176
|
-
const jsonPath = Array.isArray(path) ? createJsonPath(path) : path;
|
|
177
|
-
const matchingError = Object.entries(errors).find(
|
|
178
|
-
([errorPath]) =>
|
|
179
|
-
errorPath === jsonPath || errorPath.startsWith(`${jsonPath}.`) || errorPath.startsWith(`${jsonPath}[`),
|
|
180
|
-
);
|
|
181
|
-
|
|
182
|
-
// Only show errors for touched fields.
|
|
183
|
-
const isTouched = touched[jsonPath as JsonPath];
|
|
184
|
-
if (!isTouched) {
|
|
185
|
-
return {
|
|
186
|
-
status: undefined,
|
|
187
|
-
error: undefined,
|
|
188
|
-
};
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
return {
|
|
192
|
-
status: matchingError ? 'error' : undefined,
|
|
193
|
-
error: matchingError ? matchingError[1] : undefined,
|
|
194
|
-
};
|
|
195
|
-
},
|
|
196
|
-
[errors, touched],
|
|
197
|
-
);
|
|
198
|
-
|
|
199
|
-
const getFormValue = useCallback<FormHandler<T>['getValue']>(
|
|
200
|
-
<V>(path: (string | number)[]): V | undefined => {
|
|
201
|
-
return getValue(values, createJsonPath(path));
|
|
202
|
-
},
|
|
203
|
-
[values],
|
|
204
|
-
);
|
|
205
|
-
|
|
206
|
-
const onValueChange = useCallback<FormHandler<T>['onValueChange']>(
|
|
207
|
-
(path: (string | number)[], type: SimpleType, value: any) => {
|
|
208
|
-
const jsonPath = createJsonPath(path);
|
|
209
|
-
let parsedValue = value;
|
|
210
|
-
try {
|
|
211
|
-
if (type === 'number') {
|
|
212
|
-
parsedValue = parseFloat(value as string) || 0;
|
|
213
|
-
}
|
|
214
|
-
} catch (err) {
|
|
215
|
-
log.catch(err);
|
|
216
|
-
parsedValue = undefined;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
const newValues = { ...setValue(values, jsonPath, parsedValue) };
|
|
220
|
-
setValues(newValues);
|
|
221
|
-
setChanged((prev) => ({ ...prev, [jsonPath]: true }));
|
|
222
|
-
onValuesChanged?.(newValues);
|
|
223
|
-
|
|
224
|
-
const isValid = validate(newValues);
|
|
225
|
-
if (isValid && onValid) {
|
|
226
|
-
onValid(newValues, { changed });
|
|
227
|
-
}
|
|
228
|
-
},
|
|
229
|
-
[values, onValuesChanged, validate, onValid, changed],
|
|
230
|
-
);
|
|
231
|
-
|
|
232
|
-
const onTouched = useCallback(
|
|
233
|
-
(path: (string | number)[]) => {
|
|
234
|
-
const jsonPath = createJsonPath(path);
|
|
235
|
-
setTouched((touched) => ({ ...touched, [jsonPath]: true }));
|
|
236
|
-
validate(values);
|
|
237
|
-
},
|
|
238
|
-
[validate, values],
|
|
239
|
-
);
|
|
240
|
-
|
|
241
|
-
return useMemo(
|
|
242
|
-
() => ({
|
|
243
|
-
// State.
|
|
244
|
-
values,
|
|
245
|
-
errors,
|
|
246
|
-
touched,
|
|
247
|
-
changed,
|
|
248
|
-
canSave,
|
|
249
|
-
formIsValid,
|
|
250
|
-
|
|
251
|
-
// Actions.
|
|
252
|
-
handleSave,
|
|
253
|
-
|
|
254
|
-
// Field utils.
|
|
255
|
-
getStatus,
|
|
256
|
-
getValue: getFormValue,
|
|
257
|
-
onValueChange,
|
|
258
|
-
onTouched,
|
|
259
|
-
}),
|
|
260
|
-
[
|
|
261
|
-
values,
|
|
262
|
-
errors,
|
|
263
|
-
touched,
|
|
264
|
-
changed,
|
|
265
|
-
canSave,
|
|
266
|
-
formIsValid,
|
|
267
|
-
handleSave,
|
|
268
|
-
getStatus,
|
|
269
|
-
getFormValue,
|
|
270
|
-
onValueChange,
|
|
271
|
-
onTouched,
|
|
272
|
-
],
|
|
273
|
-
);
|
|
274
|
-
};
|
|
275
|
-
|
|
276
|
-
const createKeySet = <T extends BaseObject, V>(obj: T, value: V): Record<JsonPath, V> => {
|
|
277
|
-
invariant(obj);
|
|
278
|
-
return Object.keys(obj).reduce((acc, key) => ({ ...acc, [key]: value }), {} as Record<JsonPath, V>);
|
|
279
|
-
};
|
|
280
|
-
|
|
281
|
-
const flatMap = (errors: ValidationError[]) => {
|
|
282
|
-
return errors.reduce(
|
|
283
|
-
(result, { path, message }) => {
|
|
284
|
-
// Convert the validation error path format to our JsonPath format.
|
|
285
|
-
const jsonPath = fromEffectValidationPath(path);
|
|
286
|
-
if (!(jsonPath in result)) {
|
|
287
|
-
result[jsonPath] = message;
|
|
288
|
-
}
|
|
289
|
-
return result;
|
|
290
|
-
},
|
|
291
|
-
{} as Record<JsonPath, string>,
|
|
292
|
-
);
|
|
293
|
-
};
|