Form
import { Form, ObjectForm } from '@dxos/react-ui-form';npm install @dxos/react-ui-formForm builds a form from an Effect schema.
- Each property becomes a field of the right kind.
- The property’s
titleannotation becomes the field’s label. - The schema’s checks become the form’s validation.
When you change the schema, the form follows.
Add the package’s strings from @dxos/react-ui-form/translations to the resourceExtensions of Theme.Provider.
| Part | What it is |
|---|---|
Form.Root |
The root, which takes the schema and the values or defaultValues. onSave gets the checked values |
Form.Viewport |
The form’s container. With scroll it fills its parent and scrolls |
Form.Content |
The form element |
Form.Fields |
A field for each property of the schema, or of the properties in include |
Form.Field |
One row, bound to the form at path or holding controls of your own |
Form.FieldSet |
A labeled group of fields, which can fold |
Form.Layout |
The schema’s fields laid out by a template |
Form.Actions |
The Save and Cancel buttons |
Form.Submit |
A submit button alone, with a label and icon of your own |
Form.ErrorText |
The text of a field’s error |
ObjectForm is a Form over an ECHO object. It writes each change to the object as the user makes it.
Describe the fields
Section titled “Describe the fields”The type of each property decides the kind of field it gets.
Example (Describing a contact)
import * as import Schema
Schema from 'effect/Schema';import { import Format
Format } from '@dxos/echo';
const const ContactSchema: Schema.Struct<{ readonly name: Schema.String; readonly email: Schema.optional<Schema.String>; readonly role: Schema.Literals<readonly ["viewer", "editor", "admin"]>; readonly notes: Schema.optional<Schema.String>;}>
ContactSchema = import Schema
Schema.function Struct<{ readonly name: Schema.String; readonly email: Schema.optional<Schema.String>; readonly role: Schema.Literals<readonly ["viewer", "editor", "admin"]>; readonly notes: Schema.optional<Schema.String>;}>(fields: { readonly name: Schema.String; readonly email: Schema.optional<Schema.String>; readonly role: Schema.Literals<readonly ["viewer", "editor", "admin"]>; readonly notes: Schema.optional<Schema.String>;}): Schema.Struct<{ readonly name: Schema.String; readonly email: Schema.optional<Schema.String>; readonly role: Schema.Literals<readonly ["viewer", "editor", "admin"]>; readonly notes: Schema.optional<Schema.String>;}>
Defines a struct schema from a map of field schemas.
Details
Each field value is a schema. Use
optionalKey
or
optional
to
mark fields as optional, and
mutableKey
to mark them as mutable.
Field definitions are discovered from own enumerable string and symbol
properties of fields. Non-enumerable and inherited definitions are ignored.
The resulting schema's Type is a readonly object type with the fields'
decoded types. The Encoded form mirrors the field schemas' encoded types.
Declared fields on the input may be non-enumerable or inherited and are copied
to own properties in the output. The __proto__ field is accepted only when
it is an own property.
Parsing does not guarantee that output keys retain their input order.
Example (Defining a basic struct)
import { Schema } from "effect"
const Person = Schema.Struct({ name: Schema.String, age: Schema.Number, email: Schema.optionalKey(Schema.String)})
// { readonly name: string; readonly age: number; readonly email?: string }type Person = typeof Person.Type
Schema.decodeUnknownSync(Person)({ name: "Alice", age: 30 }) // => { name: "Alice", age: 30 }
Struct({ name: Schema.String
name: import Schema
Schema.const String: Schema.String
Type-level representation of
String
.
Schema for string values. Validates that the input is typeof "string".
String.Pipeable.pipe<Schema.String, Schema.String>(this: Schema.String, ab: (_: Schema.String) => Schema.String): Schema.String (+21 overloads)
pipe(import Schema
Schema.function check<Schema.String>(checks_0: Check<string>, ...checks: Check<string>[]): (self: Schema.String) => Schema.String
Attaches one or more filter checks to a schema without changing the
TypeScript type.
Example (Adding checks to a schema)
import { Schema } from "effect"
const AgeSchema = Schema.Finite.pipe( Schema.check(Schema.isGreaterThanOrEqualTo(0), Schema.isLessThanOrEqualTo(120)))Schema.is(AgeSchema)(42) // => trueSchema.is(AgeSchema)(121) // => false
check(import Schema
Schema.function isMinLength(minLength: number, annotations?: Schema.Annotations.Filter): Filter<{ readonly length: number;}>
Validates that a value has at least the specified length. Works with strings
and arrays.
Details
JSON Schema:
The bound must be finite; it is rounded down and clamped to zero. For arrays,
this check corresponds to minItems. JavaScript counts UTF-16 code units
while JSON Schema counts Unicode code points, so strings use
minLength: Math.ceil(minLength / 2), the tightest lower bound that cannot
reject a string accepted by this check.
Arbitrary:
During arbitrary generation, this applies a minLength
constraint to ensure generated strings or arrays have at least the required
length.
Example (Checking minimum length)
import { Schema } from "effect"
const NonEmptyStringSchema = Schema.String.check(Schema.isMinLength(1))const NonEmptyArraySchema = Schema.Array(Schema.Number).check(Schema.isMinLength(1))Schema.is(NonEmptyStringSchema)("a") // => trueSchema.is(NonEmptyArraySchema)([1]) // => true
isMinLength(1))).BottomWithoutNew<string, string, never, never, String, String, string, string, readonly [], string, "readonly", "required", "no-default", "readonly", "required">.annotate(annotations: Schema.Annotations.Bottom<string, readonly []>): Schema.String
annotate({ Annotations.Augment.title?: string | undefined
title: 'Name', }), email: Schema.optional<Schema.String>
email: import Schema
Schema.const optional: optionalLambda<Schema.String>(self: Schema.String) => Schema.optional<Schema.String>
Type-level representation returned by
optional
.
Marks a struct field as optional, allowing the key to be absent or
undefined.
Details
The resulting property may be absent or explicitly set to undefined.
Equivalent to optionalKey(UndefinedOr(S)).
Use
optionalKey
instead if you want exact optional semantics (absent
only, not undefined).
Example (Defining an optional field accepting undefined)
import { Schema } from "effect"
const schema = Schema.Struct({ name: Schema.String, age: Schema.optional(Schema.Number)})
// { readonly name: string; readonly age?: number | undefined }type Person = typeof schema.Type
optional(import Format
Format.const Email: Schema.String
Email address (RFC 5321). The local part also allows an apostrophe, which real mail carries (o'day@corp.com).
https://datatracker.ietf.org/doc/html/rfc5321#section-4.1.2
Email.BottomWithoutNew<string, string, never, never, String, String, string, string, readonly [], string, "readonly", "required", "no-default", "readonly", "required">.annotate(annotations: Schema.Annotations.Bottom<string, readonly []>): Schema.String
annotate({ Annotations.Augment.title?: string | undefined
title: 'Email' })), role: Schema.Literals<readonly ["viewer", "editor", "admin"]>
role: import Schema
Schema.function Literals<readonly ["viewer", "editor", "admin"]>(literals: readonly ["viewer", "editor", "admin"]): Schema.Literals<readonly ["viewer", "editor", "admin"]>
Creates a union schema from a fixed-length tuple of literal values.
Gotchas
Use an inline tuple or as const to preserve positions for mapMembers and
transform. Widened arrays and open-ended tuples are unsupported.
Example (Defining status codes)
import { Schema } from "effect"
const schema = Schema.Literals(["active", "inactive", "pending"])Schema.decodeSync(schema)("active") // => "active"
Literals(['viewer', 'editor', 'admin']).BottomWithoutNew<"viewer" | "editor" | "admin", "viewer" | "editor" | "admin", never, never, Union<Literal>, Literals<readonly ["viewer", "editor", "admin"]>, ... 8 more ..., "required">.annotate(annotations: Schema.Annotations.Bottom<"viewer" | "editor" | "admin", readonly []>): Schema.Literals<readonly ["viewer", "editor", "admin"]>
annotate({ Annotations.Augment.title?: string | undefined
title: 'Role', }), notes: Schema.optional<Schema.String>
notes: import Schema
Schema.const optional: optionalLambda<Schema.String>(self: Schema.String) => Schema.optional<Schema.String>
Type-level representation returned by
optional
.
Marks a struct field as optional, allowing the key to be absent or
undefined.
Details
The resulting property may be absent or explicitly set to undefined.
Equivalent to optionalKey(UndefinedOr(S)).
Use
optionalKey
instead if you want exact optional semantics (absent
only, not undefined).
Example (Defining an optional field accepting undefined)
import { Schema } from "effect"
const schema = Schema.Struct({ name: Schema.String, age: Schema.optional(Schema.Number)})
// { readonly name: string; readonly age?: number | undefined }type Person = typeof schema.Type
optional(import Format
Format.const Text: Schema.String
Multi-line text.
Text.BottomWithoutNew<string, string, never, never, String, String, string, string, readonly [], string, "readonly", "required", "no-default", "readonly", "required">.annotate(annotations: Schema.Annotations.Bottom<string, readonly []>): Schema.String
annotate({ Annotations.Augment.title?: string | undefined
title: 'Notes' })),});
type type Contact = { readonly name: string; readonly role: "viewer" | "editor" | "admin"; readonly email?: string | undefined; readonly notes?: string | undefined;}
Contact = import Schema
Schema.namespace Schema
Namespace of type-level helpers for
Schema
.
A typed view of a schema that tracks only the decoded (output) type T.
Details
Use Schema<T> as a constraint when you want to accept "any schema that
decodes to T" and do not need to know or constrain the encoded
representation, required services, or any other type parameters.
This is a structural interface — concrete schema values are produced by the
constructors in this module (e.g.
Struct
,
String
,
Number
).
When you also need the encoded type or service requirements, use
Codec
.
Example (Accepting any schema decoding to string)
import { Schema } from "effect"
const accept = (_schema: Schema.Schema<string>): void => {}
accept(Schema.String)accept(Schema.NonEmptyString)
Schema.type Schema<out T>.Type<S> = S extends { readonly Type: infer T;} ? T : never
Extracts the decoded Type from a schema.
Example (Extracting the decoded type)
import { Schema } from "effect"
const Person = Schema.Struct({ name: Schema.String, age: Schema.Number })type Person = Schema.Schema.Type<typeof Person>// { readonly name: string; readonly age: number }
Type<typeof const ContactSchema: Schema.Struct<{ readonly name: Schema.String; readonly email: Schema.optional<Schema.String>; readonly role: Schema.Literals<readonly ["viewer", "editor", "admin"]>; readonly notes: Schema.optional<Schema.String>;}>
ContactSchema>;
Format.Email asks for an email field, the set of literals in role asks for a select, and Format.Text asks for a
text area.
Render the form
Section titled “Render the form”Form.Fields renders a field for each property, and Form.Actions adds Save and Cancel.
The form checks the values as the user works:
- A field shows its error once the user leaves it.
- Save is disabled while a field the user touched has an error.
- Save checks every value against the schema, then calls
onSavewith the values.
Example (Rendering a form for a new contact)
import { function useState<S>(initialState: S | (() => S)): [S, React.Dispatch<React.SetStateAction<S>>] (+1 overload)
Returns a stateful value, and a function to update it.
useState } from 'react';import { const Form: { Root: { <T extends AnyProperties = AnyProperties>({ children, ...props }: FormRootProps<T>): React.JSX.Element; displayName: string; }; Viewport: React.ForwardRefExoticComponent<Omit<import("@dxos/react-ui-form").FormViewportProps, "className"> & { classNames?: import("@dxos/ui-types").ClassNameValue; } & Pick<React.HTMLAttributes<Element>, "children" | "role" | "style"> & React.RefAttributes<HTMLDivElement>>; Content: React.ForwardRefExoticComponent<{ children?: React.ReactNode | undefined; } & React.RefAttributes<HTMLDivElement>>; ... 6 more ...; ErrorText: typeof FormErrorText;}
The Form namespace of @dxos/react-ui-form, rendered with the Next namespace of @dxos/react-ui; Root is the shared one.
Form } from '@dxos/react-ui-form';import * as import Typography
Typography from '@dxos/react-ui/Typography';
/** A form for a new contact: its fields come from the schema, and Save validates the values before `onSave`. */export const const NewContact: () => React.JSX.Element
A form for a new contact: its fields come from the schema, and Save validates the values before onSave.
NewContact = () => { const [const saved: { readonly role: "viewer" | "editor" | "admin"; readonly name: string; readonly email?: string | undefined; readonly notes?: string | undefined;} | undefined
saved, const setSaved: React.Dispatch<React.SetStateAction<{ readonly role: "viewer" | "editor" | "admin"; readonly name: string; readonly email?: string | undefined; readonly notes?: string | undefined;} | undefined>>
setSaved] = useState<{ readonly role: "viewer" | "editor" | "admin"; readonly name: string; readonly email?: string | undefined; readonly notes?: string | undefined;}>(): [{ readonly role: "viewer" | "editor" | "admin"; readonly name: string; readonly email?: string | undefined; readonly notes?: string | undefined;} | undefined, React.Dispatch<React.SetStateAction<{ readonly role: "viewer" | "editor" | "admin"; readonly name: string; readonly email?: string | undefined; readonly notes?: string | undefined;} | undefined>>] (+1 overload)
Returns a stateful value, and a function to update it.
useState<type Contact = { readonly role: "viewer" | "editor" | "admin"; readonly name: string; readonly email?: string | undefined; readonly notes?: string | undefined;}
Contact>(); return ( <> <const Form: { Root: { <T extends AnyProperties = AnyProperties>({ children, ...props }: FormRootProps<T>): React.JSX.Element; displayName: string; }; Viewport: React.ForwardRefExoticComponent<Omit<import("@dxos/react-ui-form").FormViewportProps, "className"> & { classNames?: import("@dxos/ui-types").ClassNameValue; } & Pick<React.HTMLAttributes<Element>, "children" | "role" | "style"> & React.RefAttributes<HTMLDivElement>>; Content: React.ForwardRefExoticComponent<{ children?: React.ReactNode | undefined; } & React.RefAttributes<HTMLDivElement>>; ... 6 more ...; ErrorText: typeof FormErrorText;}
The Form namespace of @dxos/react-ui-form, rendered with the Next namespace of @dxos/react-ui; Root is the shared one.
Form.type Root: { <T extends AnyProperties = AnyProperties>({ children, ...props }: FormRootProps<T>): React.JSX.Element; displayName: string;}
Root schema?: Schema.Codec<{ readonly role: "viewer" | "editor" | "admin"; readonly name: string; readonly email?: string | undefined; readonly notes?: string | undefined;}, any, never, never> | undefined
Effect schema (Type literal).
schema={const ContactSchema: Schema.Struct<{ readonly name: Schema.String; readonly email: Schema.optional<Schema.String>; readonly role: Schema.Literals<readonly ["viewer", "editor", "admin"]>; readonly notes: Schema.optional<Schema.String>;}>
ContactSchema} values?: Partial<{ readonly role: "viewer" | "editor" | "admin"; readonly name: string; readonly email?: string | undefined; readonly notes?: string | undefined;}> | undefined
Source values. Fields the user is not editing reflect this value; in-progress edits (including intermediate
invalid values) are held locally and never snap back until they are committed and the source catches up. Persist
committed values from onValuesChanged/onSave. The form re-renders when a new value is passed, so to reflect
external/remote mutations pass a reactive source (e.g. a useObject snapshot that changes on mutation) rather
than a raw live object — a render-time read of an ECHO object does not itself subscribe to it.
values={{ role?: "viewer" | "editor" | "admin" | undefined
role: 'viewer' }} onSave?: ((values: { readonly role: "viewer" | "editor" | "admin"; readonly name: string; readonly email?: string | undefined; readonly notes?: string | undefined;}, meta: FormUpdateMeta<{ readonly role: "viewer" | "editor" | "admin"; readonly name: string; readonly email?: string | undefined; readonly notes?: string | undefined;}>) => void) | undefined
Called when the form is submitted and passes validation.
onSave={(values: { readonly role: "viewer" | "editor" | "admin"; readonly name: string; readonly email?: string | undefined; readonly notes?: string | undefined;}
values) => const setSaved: (value: React.SetStateAction<{ readonly role: "viewer" | "editor" | "admin"; readonly name: string; readonly email?: string | undefined; readonly notes?: string | undefined;} | undefined>) => void
setSaved(values: { readonly role: "viewer" | "editor" | "admin"; readonly name: string; readonly email?: string | undefined; readonly notes?: string | undefined;}
values)} > <const Form: { Root: { <T extends AnyProperties = AnyProperties>({ children, ...props }: FormRootProps<T>): React.JSX.Element; displayName: string; }; Viewport: React.ForwardRefExoticComponent<Omit<import("@dxos/react-ui-form").FormViewportProps, "className"> & { classNames?: import("@dxos/ui-types").ClassNameValue; } & Pick<React.HTMLAttributes<Element>, "children" | "role" | "style"> & React.RefAttributes<HTMLDivElement>>; Content: React.ForwardRefExoticComponent<{ children?: React.ReactNode | undefined; } & React.RefAttributes<HTMLDivElement>>; ... 6 more ...; ErrorText: typeof FormErrorText;}
The Form namespace of @dxos/react-ui-form, rendered with the Next namespace of @dxos/react-ui; Root is the shared one.
Form.type Content: React.ForwardRefExoticComponent<{ children?: React.ReactNode | undefined;} & React.RefAttributes<HTMLDivElement>>
Content> <const Form: { Root: { <T extends AnyProperties = AnyProperties>({ children, ...props }: FormRootProps<T>): React.JSX.Element; displayName: string; }; Viewport: React.ForwardRefExoticComponent<Omit<import("@dxos/react-ui-form").FormViewportProps, "className"> & { classNames?: import("@dxos/ui-types").ClassNameValue; } & Pick<React.HTMLAttributes<Element>, "children" | "role" | "style"> & React.RefAttributes<HTMLDivElement>>; Content: React.ForwardRefExoticComponent<{ children?: React.ReactNode | undefined; } & React.RefAttributes<HTMLDivElement>>; ... 6 more ...; ErrorText: typeof FormErrorText;}
The Form namespace of @dxos/react-ui-form, rendered with the Next namespace of @dxos/react-ui; Root is the shared one.
Form.type Fields: typeof FormFields
Fields /> <const Form: { Root: { <T extends AnyProperties = AnyProperties>({ children, ...props }: FormRootProps<T>): React.JSX.Element; displayName: string; }; Viewport: React.ForwardRefExoticComponent<Omit<import("@dxos/react-ui-form").FormViewportProps, "className"> & { classNames?: import("@dxos/ui-types").ClassNameValue; } & Pick<React.HTMLAttributes<Element>, "children" | "role" | "style"> & React.RefAttributes<HTMLDivElement>>; Content: React.ForwardRefExoticComponent<{ children?: React.ReactNode | undefined; } & React.RefAttributes<HTMLDivElement>>; ... 6 more ...; ErrorText: typeof FormErrorText;}
The Form namespace of @dxos/react-ui-form, rendered with the Next namespace of @dxos/react-ui; Root is the shared one.
Form.type Actions: typeof FormActions
Actions /> </const Form: { Root: { <T extends AnyProperties = AnyProperties>({ children, ...props }: FormRootProps<T>): React.JSX.Element; displayName: string; }; Viewport: React.ForwardRefExoticComponent<Omit<import("@dxos/react-ui-form").FormViewportProps, "className"> & { classNames?: import("@dxos/ui-types").ClassNameValue; } & Pick<React.HTMLAttributes<Element>, "children" | "role" | "style"> & React.RefAttributes<HTMLDivElement>>; Content: React.ForwardRefExoticComponent<{ children?: React.ReactNode | undefined; } & React.RefAttributes<HTMLDivElement>>; ... 6 more ...; ErrorText: typeof FormErrorText;}
The Form namespace of @dxos/react-ui-form, rendered with the Next namespace of @dxos/react-ui; Root is the shared one.
Form.type Content: React.ForwardRefExoticComponent<{ children?: React.ReactNode | undefined;} & React.RefAttributes<HTMLDivElement>>
Content> </const Form: { Root: { <T extends AnyProperties = AnyProperties>({ children, ...props }: FormRootProps<T>): React.JSX.Element; displayName: string; }; Viewport: React.ForwardRefExoticComponent<Omit<import("@dxos/react-ui-form").FormViewportProps, "className"> & { classNames?: import("@dxos/ui-types").ClassNameValue; } & Pick<React.HTMLAttributes<Element>, "children" | "role" | "style"> & React.RefAttributes<HTMLDivElement>>; Content: React.ForwardRefExoticComponent<{ children?: React.ReactNode | undefined; } & React.RefAttributes<HTMLDivElement>>; ... 6 more ...; ErrorText: typeof FormErrorText;}
The Form namespace of @dxos/react-ui-form, rendered with the Next namespace of @dxos/react-ui; Root is the shared one.
Form.type Root: { <T extends AnyProperties = AnyProperties>({ children, ...props }: FormRootProps<T>): React.JSX.Element; displayName: string;}
Root> <import Typography
Typography.const Text: React.ForwardRefExoticComponent<Omit<Typography.TextProps & { asChild?: boolean;}, "className"> & { classNames?: import("@dxos/ui-types").ClassNameValue;} & Pick<React.HTMLAttributes<Element>, "children" | "role" | "style"> & React.RefAttributes<HTMLParagraphElement>>export Text
Text whose first line is centred in a block, so it lines up with a Block or control beside it however many lines
it wraps to. Renders a <p>; asChild puts the metrics on a heading or other text element instead.
Text tone?: Typography.TextTone | undefined
Emphasis below the default (DESIGN.md "Text emphasis"): description for secondary content, subdued for
interface text.
tone='muted'> {const saved: { readonly role: "viewer" | "editor" | "admin"; readonly name: string; readonly email?: string | undefined; readonly notes?: string | undefined;} | undefined
saved ? `Saved ${const saved: { readonly role: "viewer" | "editor" | "admin"; readonly name: string; readonly email?: string | undefined; readonly notes?: string | undefined;}
saved.name: string
name} as ${const saved: { readonly role: "viewer" | "editor" | "admin"; readonly name: string; readonly email?: string | undefined; readonly notes?: string | undefined;}
saved.role: "viewer" | "editor" | "admin"
role}` : 'Not saved yet'} </import Typography
Typography.const Text: React.ForwardRefExoticComponent<Omit<Typography.TextProps & { asChild?: boolean;}, "className"> & { classNames?: import("@dxos/ui-types").ClassNameValue;} & Pick<React.HTMLAttributes<Element>, "children" | "role" | "style"> & React.RefAttributes<HTMLParagraphElement>>export Text
Text whose first line is centred in a block, so it lines up with a Block or control beside it however many lines
it wraps to. Renders a <p>; asChild puts the metrics on a heading or other text element instead.
Text> </> );};
values starts role at viewer, and onSave keeps the checked values in state.
To show only some of the fields, list them in include. They keep the schema’s order. Pass sort to reorder them.
Example (Showing two of the fields)
import { const Form: { Root: { <T extends AnyProperties = AnyProperties>({ children, ...props }: FormRootProps<T>): React.JSX.Element; displayName: string; }; Viewport: React.ForwardRefExoticComponent<Omit<import("@dxos/react-ui-form").FormViewportProps, "className"> & { classNames?: import("@dxos/ui-types").ClassNameValue; } & Pick<React.HTMLAttributes<Element>, "children" | "role" | "style"> & React.RefAttributes<HTMLDivElement>>; Content: React.ForwardRefExoticComponent<{ children?: React.ReactNode | undefined; } & React.RefAttributes<HTMLDivElement>>; ... 6 more ...; ErrorText: typeof FormErrorText;}
The Form namespace of @dxos/react-ui-form, rendered with the Next namespace of @dxos/react-ui; Root is the shared one.
Form } from '@dxos/react-ui-form';
/** Only the fields a quick-add needs. */export const const QuickAdd: ({ onSave, }: { onSave: (contact: Contact) => void;}) => React.JSX.Element
Only the fields a quick-add needs.
QuickAdd = ({ onSave: (contact: Contact) => void
onSave,}: { onSave: (contact: Contact) => void
onSave: (contact: { readonly role: "viewer" | "editor" | "admin"; readonly name: string; readonly email?: string | undefined; readonly notes?: string | undefined;}
contact: type Contact = { readonly role: "viewer" | "editor" | "admin"; readonly name: string; readonly email?: string | undefined; readonly notes?: string | undefined;}
Contact) => void;}) => ( <const Form: { Root: { <T extends AnyProperties = AnyProperties>({ children, ...props }: FormRootProps<T>): React.JSX.Element; displayName: string; }; Viewport: React.ForwardRefExoticComponent<Omit<import("@dxos/react-ui-form").FormViewportProps, "className"> & { classNames?: import("@dxos/ui-types").ClassNameValue; } & Pick<React.HTMLAttributes<Element>, "children" | "role" | "style"> & React.RefAttributes<HTMLDivElement>>; Content: React.ForwardRefExoticComponent<{ children?: React.ReactNode | undefined; } & React.RefAttributes<HTMLDivElement>>; ... 6 more ...; ErrorText: typeof FormErrorText;}
The Form namespace of @dxos/react-ui-form, rendered with the Next namespace of @dxos/react-ui; Root is the shared one.
Form.type Root: { <T extends AnyProperties = AnyProperties>({ children, ...props }: FormRootProps<T>): React.JSX.Element; displayName: string;}
Root schema?: Schema.Codec<{ readonly role: "viewer" | "editor" | "admin"; readonly name: string; readonly email?: string | undefined; readonly notes?: string | undefined;}, any, never, never> | undefined
Effect schema (Type literal).
schema={const ContactSchema: Schema.Struct<{ readonly name: Schema.String; readonly email: Schema.optional<Schema.String>; readonly role: Schema.Literals<readonly ["viewer", "editor", "admin"]>; readonly notes: Schema.optional<Schema.String>;}>
ContactSchema} values?: Partial<{ readonly role: "viewer" | "editor" | "admin"; readonly name: string; readonly email?: string | undefined; readonly notes?: string | undefined;}> | undefined
Source values. Fields the user is not editing reflect this value; in-progress edits (including intermediate
invalid values) are held locally and never snap back until they are committed and the source catches up. Persist
committed values from onValuesChanged/onSave. The form re-renders when a new value is passed, so to reflect
external/remote mutations pass a reactive source (e.g. a useObject snapshot that changes on mutation) rather
than a raw live object — a render-time read of an ECHO object does not itself subscribe to it.
values={{ role?: "viewer" | "editor" | "admin" | undefined
role: 'viewer' }} onSave?: ((values: { readonly role: "viewer" | "editor" | "admin"; readonly name: string; readonly email?: string | undefined; readonly notes?: string | undefined;}, meta: FormUpdateMeta<{ readonly role: "viewer" | "editor" | "admin"; readonly name: string; readonly email?: string | undefined; readonly notes?: string | undefined;}>) => void) | undefined
Called when the form is submitted and passes validation.
onSave={onSave: (contact: Contact) => void
onSave}> <const Form: { Root: { <T extends AnyProperties = AnyProperties>({ children, ...props }: FormRootProps<T>): React.JSX.Element; displayName: string; }; Viewport: React.ForwardRefExoticComponent<Omit<import("@dxos/react-ui-form").FormViewportProps, "className"> & { classNames?: import("@dxos/ui-types").ClassNameValue; } & Pick<React.HTMLAttributes<Element>, "children" | "role" | "style"> & React.RefAttributes<HTMLDivElement>>; Content: React.ForwardRefExoticComponent<{ children?: React.ReactNode | undefined; } & React.RefAttributes<HTMLDivElement>>; ... 6 more ...; ErrorText: typeof FormErrorText;}
The Form namespace of @dxos/react-ui-form, rendered with the Next namespace of @dxos/react-ui; Root is the shared one.
Form.type Content: React.ForwardRefExoticComponent<{ children?: React.ReactNode | undefined;} & React.RefAttributes<HTMLDivElement>>
Content> <const Form: { Root: { <T extends AnyProperties = AnyProperties>({ children, ...props }: FormRootProps<T>): React.JSX.Element; displayName: string; }; Viewport: React.ForwardRefExoticComponent<Omit<import("@dxos/react-ui-form").FormViewportProps, "className"> & { classNames?: import("@dxos/ui-types").ClassNameValue; } & Pick<React.HTMLAttributes<Element>, "children" | "role" | "style"> & React.RefAttributes<HTMLDivElement>>; Content: React.ForwardRefExoticComponent<{ children?: React.ReactNode | undefined; } & React.RefAttributes<HTMLDivElement>>; ... 6 more ...; ErrorText: typeof FormErrorText;}
The Form namespace of @dxos/react-ui-form, rendered with the Next namespace of @dxos/react-ui; Root is the shared one.
Form.type Fields: typeof FormFields
Fields include?: string[] | undefined
Renders these properties only, in schema order.
include={['name', 'email']} /> <const Form: { Root: { <T extends AnyProperties = AnyProperties>({ children, ...props }: FormRootProps<T>): React.JSX.Element; displayName: string; }; Viewport: React.ForwardRefExoticComponent<Omit<import("@dxos/react-ui-form").FormViewportProps, "className"> & { classNames?: import("@dxos/ui-types").ClassNameValue; } & Pick<React.HTMLAttributes<Element>, "children" | "role" | "style"> & React.RefAttributes<HTMLDivElement>>; Content: React.ForwardRefExoticComponent<{ children?: React.ReactNode | undefined; } & React.RefAttributes<HTMLDivElement>>; ... 6 more ...; ErrorText: typeof FormErrorText;}
The Form namespace of @dxos/react-ui-form, rendered with the Next namespace of @dxos/react-ui; Root is the shared one.
Form.type Actions: typeof FormActions
Actions /> </const Form: { Root: { <T extends AnyProperties = AnyProperties>({ children, ...props }: FormRootProps<T>): React.JSX.Element; displayName: string; }; Viewport: React.ForwardRefExoticComponent<Omit<import("@dxos/react-ui-form").FormViewportProps, "className"> & { classNames?: import("@dxos/ui-types").ClassNameValue; } & Pick<React.HTMLAttributes<Element>, "children" | "role" | "style"> & React.RefAttributes<HTMLDivElement>>; Content: React.ForwardRefExoticComponent<{ children?: React.ReactNode | undefined; } & React.RefAttributes<HTMLDivElement>>; ... 6 more ...; ErrorText: typeof FormErrorText;}
The Form namespace of @dxos/react-ui-form, rendered with the Next namespace of @dxos/react-ui; Root is the shared one.
Form.type Content: React.ForwardRefExoticComponent<{ children?: React.ReactNode | undefined;} & React.RefAttributes<HTMLDivElement>>
Content> </const Form: { Root: { <T extends AnyProperties = AnyProperties>({ children, ...props }: FormRootProps<T>): React.JSX.Element; displayName: string; }; Viewport: React.ForwardRefExoticComponent<Omit<import("@dxos/react-ui-form").FormViewportProps, "className"> & { classNames?: import("@dxos/ui-types").ClassNameValue; } & Pick<React.HTMLAttributes<Element>, "children" | "role" | "style"> & React.RefAttributes<HTMLDivElement>>; Content: React.ForwardRefExoticComponent<{ children?: React.ReactNode | undefined; } & React.RefAttributes<HTMLDivElement>>; ... 6 more ...; ErrorText: typeof FormErrorText;}
The Form namespace of @dxos/react-ui-form, rendered with the Next namespace of @dxos/react-ui; Root is the shared one.
Form.type Root: { <T extends AnyProperties = AnyProperties>({ children, ...props }: FormRootProps<T>): React.JSX.Element; displayName: string;}
Root>);
include leaves name and email, in the schema’s order.
Edit an ECHO object
Section titled “Edit an ECHO object”ObjectForm takes the type and the object. It needs no Save button, because it writes each change as the user makes
it. A property that refers to another object gets a picker of the objects of that type.
The types from ECHO’s Defining types page work as they are.
Example (Editing a person)
import * as import Schema
Schema from 'effect/Schema';import { import DXN
DXN, import Format
Format, import Type
Type } from '@dxos/echo';import { const ObjectForm: ({ object, type, schema, showTags }: ObjectFormProps) => React.JSX.Element
An object's properties (and meta tags) as a next Form in its host's grid, writing each change back.
ObjectForm } from '@dxos/react-ui-form';
class class Person
Person extends import Type
Type.const makeObject: <Person>(dxn: DXN.DXN, options?: { id?: EntityId;}) => Type.MakeObject<Person>
Factory function to create an ECHO object type.
Returns a Type.Type entity — a live, in-memory TypeSchema instance,
NOT a Schema.Schema. Use Type.InstanceType<typeof Foo> for the instance
type and Type.getSchema(Foo) to obtain the underlying Effect Schema.
The entity's id defaults to EntityId.deterministic(typename, version) so
constructing a type never reaches crypto.getRandomValues() — required for
Cloudflare workerd, which forbids RNG calls in global (module-evaluation)
scope. Pass { id } to override (e.g. with EntityId.random() from a
request handler).
Giving the type a nominal class name keeps its inferred type portable for
declaration emit: structural expansion (which can pull in unnameable union
members from referenced schemas) is replaced by a reference to the class.
The second call takes either a schema, or struct fields (wrapped in Schema.Struct) with an optional function
that pipes annotations onto the struct.
makeObject<class Person
Person>( import DXN
DXN.const make: <"com.example.type.person">(nsid: "com.example.type.person", version?: string) => DXN.DXN
Constructs a DXN from an NSID (and optional version). Throws if the result
is not a valid DXN. Use tryMake for non-throwing string parsing.
Static NSID strings are validated at compile time via
Name
:
the final segment must be camelCase (no hyphens). Template-literal strings
with a runtime prefix are accepted here but still validated at runtime.
make('com.example.type.person', '0.1.0'),)( import Schema
Schema.function Struct<{ readonly name: Schema.String; readonly email: Schema.optional<Schema.String>;}>(fields: { readonly name: Schema.String; readonly email: Schema.optional<Schema.String>;}): Schema.Struct<{ readonly name: Schema.String; readonly email: Schema.optional<Schema.String>;}>
Defines a struct schema from a map of field schemas.
Details
Each field value is a schema. Use
optionalKey
or
optional
to
mark fields as optional, and
mutableKey
to mark them as mutable.
Field definitions are discovered from own enumerable string and symbol
properties of fields. Non-enumerable and inherited definitions are ignored.
The resulting schema's Type is a readonly object type with the fields'
decoded types. The Encoded form mirrors the field schemas' encoded types.
Declared fields on the input may be non-enumerable or inherited and are copied
to own properties in the output. The __proto__ field is accepted only when
it is an own property.
Parsing does not guarantee that output keys retain their input order.
Example (Defining a basic struct)
import { Schema } from "effect"
const Person = Schema.Struct({ name: Schema.String, age: Schema.Number, email: Schema.optionalKey(Schema.String)})
// { readonly name: string; readonly age: number; readonly email?: string }type Person = typeof Person.Type
Schema.decodeUnknownSync(Person)({ name: "Alice", age: 30 }) // => { name: "Alice", age: 30 }
Struct({ name: Schema.String
name: import Schema
Schema.const String: Schema.String
Type-level representation of
String
.
Schema for string values. Validates that the input is typeof "string".
String, email: Schema.optional<Schema.String>
email: import Schema
Schema.const optional: optionalLambda<Schema.String>(self: Schema.String) => Schema.optional<Schema.String>
Type-level representation returned by
optional
.
Marks a struct field as optional, allowing the key to be absent or
undefined.
Details
The resulting property may be absent or explicitly set to undefined.
Equivalent to optionalKey(UndefinedOr(S)).
Use
optionalKey
instead if you want exact optional semantics (absent
only, not undefined).
Example (Defining an optional field accepting undefined)
import { Schema } from "effect"
const schema = Schema.Struct({ name: Schema.String, age: Schema.optional(Schema.Number)})
// { readonly name: string; readonly age?: number | undefined }type Person = typeof schema.Type
optional(import Format
Format.const Email: Schema.String
Email address (RFC 5321). The local part also allows an apostrophe, which real mail carries (o'day@corp.com).
https://datatracker.ietf.org/doc/html/rfc5321#section-4.1.2
Email), }),) {}
/** An ECHO object's fields; each change is written to the object as it is made. */export const const PersonForm: ({ person }: { person: Person;}) => React.JSX.Element
An ECHO object's fields; each change is written to the object as it is made.
PersonForm = ({ person: Person
person }: { person: Person
person: class Person
Person }) => ( <const ObjectForm: ({ object, type, schema, showTags }: ObjectFormProps) => React.JSX.Element
An object's properties (and meta tags) as a next Form in its host's grid, writing each change back.
ObjectForm type: Type.AnyEntity
type={class Person
Person} object: Unknown
object={person: Person
person} showTags?: boolean | undefined
Render the meta-tags field. Defaults to true.
showTags={false} />);
Person is an ECHO type with a name and an optional email, and ObjectForm edits them on person. ObjectForm
also shows a field for the object’s tags, and showTags={false} leaves it out.
Connect to ECHO uses ObjectForm for the contacts screen’s edit dialog.