@react-typed-forms/schemas 1.0.0-dev.2 → 1.0.0-dev.21

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/README.md ADDED
@@ -0,0 +1,223 @@
1
+
2
+ # React typed forms schemas
3
+
4
+ A simple abstraction on top of `@react-typed-forms/core` for defining JSON compatible schemas and
5
+ rendering UIs for users to enter that data.
6
+
7
+ ## Install
8
+
9
+ ```npm
10
+ npm install @react-typed-forms/schemas
11
+ ```
12
+
13
+ ## Example
14
+
15
+ <!-- AUTO-GENERATED-CONTENT:START (CODE:src=../examples/src/docs/schemas-example.tsx) -->
16
+ <!-- The below code snippet is automatically added from ../examples/src/docs/schemas-example.tsx -->
17
+ ```tsx
18
+ import { useControl } from "@react-typed-forms/core";
19
+ import React from "react";
20
+ import {
21
+ buildSchema,
22
+ createDefaultRenderers,
23
+ createFormRenderer,
24
+ defaultFormEditHooks,
25
+ defaultTailwindTheme,
26
+ defaultValueForFields,
27
+ FormRenderer,
28
+ intField,
29
+ renderControl,
30
+ stringField,
31
+ useControlDefinitionForSchema,
32
+ } from "@react-typed-forms/schemas";
33
+
34
+ /** Define your form */
35
+ interface SimpleForm {
36
+ firstName: string;
37
+ lastName: string;
38
+ yearOfBirth: number;
39
+ }
40
+
41
+ /* Build your schema fields. Importantly giving them Display Names for showing in a UI */
42
+ const simpleSchema = buildSchema<SimpleForm>({
43
+ firstName: stringField("First Name"),
44
+ lastName: stringField("Last Name", { required: true }),
45
+ yearOfBirth: intField("Year of birth", { defaultValue: 1980 }),
46
+ });
47
+
48
+ /* Create a form renderer based on a simple tailwind css based theme */
49
+ const renderer: FormRenderer = createFormRenderer(
50
+ [],
51
+ createDefaultRenderers(defaultTailwindTheme),
52
+ );
53
+
54
+ export default function SimpleSchemasExample() {
55
+ /* Create a `Control` for collecting the data, the schema fields can be used to get a default value */
56
+ const data = useControl<SimpleForm>(() =>
57
+ defaultValueForFields(simpleSchema),
58
+ );
59
+
60
+ /* Generate a ControlDefinition automatically from the schema */
61
+ const controlDefinition = useControlDefinitionForSchema(simpleSchema);
62
+
63
+ return (
64
+ <div className="container my-4 max-w-2xl">
65
+ {/* Render the ControlDefinition using `data` for the form state */}
66
+ {renderControl(controlDefinition, data, {
67
+ fields: simpleSchema,
68
+ renderer,
69
+ hooks: defaultFormEditHooks,
70
+ })}
71
+ <pre>{JSON.stringify(data.value, null, 2)}</pre>
72
+ </div>
73
+ );
74
+ }
75
+ ```
76
+ <!-- AUTO-GENERATED-CONTENT:END -->
77
+
78
+ This will produce this UI:
79
+
80
+ <img src="../../images/schemas.png">
81
+
82
+ ## Schema Fields and Control Definitions
83
+
84
+ ### `SchemaField`
85
+
86
+ A `SchemaField` is a JSON object which describes the definition of a field inside the context of a JSON object. Each `SchemaField` must have a field name and a `FieldType` which will map to JSON. The following built in types are defined with these JSON mappings:
87
+
88
+ * `String` - A JSON string
89
+ * `Bool` - A JSON boolean
90
+ * `Int` - A JSON number
91
+ * `Double` - A JSON number
92
+ * `Date` - A date stored as 'yyyy-MM-dd' in a JSON string
93
+ * `DateTime` - A date and time stored in ISO8601 format in a JSON string
94
+ * `Compound` - A JSON object with a list of `SchemaField` children
95
+
96
+ Each `SchemaField` can also be marked as a `collection` which means that it will be mapped to a JSON array of the defined `FieldType`.
97
+
98
+ TODO - buildSchema, FieldOptions, extending FieldType
99
+
100
+ ### `ControlDefinition`
101
+ A `ControlDefinition` is a JSON object which describes what should be rendered in a UI. Each `ControlDefinition` can be one of 4 distinct types:
102
+
103
+ * `DataControlDefinition` - Points to a `SchemaField` in order to render a control for editing of data.
104
+ * `GroupedControlsDefinition` - Contains an optional title and a list of `ControlDefinition` children which should be rendered as a group. Optionally can refer to a `SchemaField` with type `Compound` in order to capture nested data.
105
+ * `DisplayControlDefinition` - Render readonly content, current text and HTML variants are defined.
106
+ * `ActionControlDefinition` - Renders an action button, useful for hooking forms up with outside functionality.
107
+
108
+ If you don't care about the layout of the form that much you can generate the definition automatically by using `useControlDefinitionForSchema()`.
109
+
110
+ TODO renderOptions, DataRenderType for choosing render style.
111
+
112
+ ## Form Renderer
113
+
114
+ The actual rendering of the UI is abstracted into an object which contains functions for rendering the various `ControlDefinition`s and various parts of the UI:
115
+
116
+ <!-- AUTO-GENERATED-CONTENT:START (CODE:src=./src/controlRender.tsx&lines=128-138) -->
117
+ <!-- The below code snippet is automatically added from ./src/controlRender.tsx -->
118
+ ```tsx
119
+ export interface FormRenderer {
120
+ renderData: (props: DataRendererProps) => ReactElement;
121
+ renderGroup: (props: GroupRendererProps) => ReactElement;
122
+ renderDisplay: (props: DisplayRendererProps) => ReactElement;
123
+ renderAction: (props: ActionRendererProps) => ReactElement;
124
+ renderArray: (props: ArrayRendererProps) => ReactElement;
125
+ renderLabel: (props: LabelRendererProps, elem: ReactElement) => ReactElement;
126
+ renderVisibility: (visible: Visibility, elem: ReactElement) => ReactElement;
127
+ renderAdornment: (props: AdornmentProps) => AdornmentRenderer;
128
+ }
129
+ ```
130
+ <!-- AUTO-GENERATED-CONTENT:END -->
131
+
132
+ The `createFormRenderer` function takes an array of `RendererRegistration` which allows for customising the rendering.
133
+
134
+ <!-- AUTO-GENERATED-CONTENT:START (CODE:src=./src/renderers.tsx&lines=109-118) -->
135
+ <!-- The below code snippet is automatically added from ./src/renderers.tsx -->
136
+ ```tsx
137
+ export type RendererRegistration =
138
+ | DataRendererRegistration
139
+ | GroupRendererRegistration
140
+ | DisplayRendererRegistration
141
+ | ActionRendererRegistration
142
+ | LabelRendererRegistration
143
+ | ArrayRendererRegistration
144
+ | AdornmentRendererRegistration
145
+ | VisibilityRendererRegistration;
146
+ ```
147
+ <!-- AUTO-GENERATED-CONTENT:END -->
148
+
149
+ Probably the most common customisation would be to add a `DataRendererRegistration` which will change the way a `DataControlDefinition` is rendered for a particular FieldType:
150
+
151
+ <!-- AUTO-GENERATED-CONTENT:START (CODE:src=./src/renderers.tsx&lines=48-61) -->
152
+ <!-- The below code snippet is automatically added from ./src/renderers.tsx -->
153
+ ```tsx
154
+ export interface DataRendererRegistration {
155
+ type: "data";
156
+ schemaType?: string | string[];
157
+ renderType?: string | string[];
158
+ options?: boolean;
159
+ collection?: boolean;
160
+ match?: (props: DataRendererProps) => boolean;
161
+ render: (
162
+ props: DataRendererProps,
163
+ defaultLabel: (label?: Partial<LabelRendererProps>) => LabelRendererProps,
164
+ renderers: FormRenderer,
165
+ ) => ReactElement;
166
+ }
167
+ ```
168
+ <!-- AUTO-GENERATED-CONTENT:END -->
169
+ * The `schemaType` field specifies which `FieldType`(s) should use this `DataRendererRegistration`, unspecified means allow any.
170
+ * The `renderType` field specifies which `DataRenderType` this registration applies to.
171
+ * The `match` function can be used if the matching logic is more complicated than provided by the other.
172
+ * The `render` function does the actual rendering if the ControlDefinition/SchemaField matches the registration.
173
+
174
+ A good example of a custom DataRendererRegistration is the `muiTextField` which renders `String` fields using the `FTextField` wrapper of the `@react-typed-forms/mui` library:
175
+
176
+ <!-- AUTO-GENERATED-CONTENT:START (CODE:src=../schemas-mui/src/index.tsx&lines=12-35) -->
177
+ <!-- The below code snippet is automatically added from ../schemas-mui/src/index.tsx -->
178
+ ```tsx
179
+ export function muiTextfieldRenderer(
180
+ variant?: "standard" | "outlined" | "filled",
181
+ ): DataRendererRegistration {
182
+ return {
183
+ type: "data",
184
+ schemaType: FieldType.String,
185
+ renderType: DataRenderType.Standard,
186
+ render: (r, makeLabel, { renderVisibility }) => {
187
+ const { title, required } = makeLabel();
188
+ return renderVisibility(
189
+ r.visible,
190
+ <FTextField
191
+ variant={variant}
192
+ required={required}
193
+ fullWidth
194
+ size="small"
195
+ state={r.control}
196
+ label={title}
197
+ />,
198
+ );
199
+ },
200
+ };
201
+ }
202
+ ```
203
+ <!-- AUTO-GENERATED-CONTENT:END -->
204
+
205
+ Changing the simple example above to use the following:
206
+
207
+ ```tsx
208
+ const renderer: FormRenderer = createFormRenderer(
209
+ [muiTextFieldRenderer()],
210
+ createDefaultRenderer (defaultTailwindTheme));
211
+ ```
212
+
213
+ This will produce this UI:
214
+
215
+ <img src="../../images/schemas-muifield.png">
216
+
217
+ ## TODO
218
+
219
+ * Label rendering
220
+ * Visibility
221
+ * Arrays
222
+ * Display controls
223
+
@@ -1,95 +1,112 @@
1
- import { ActionControlDefinition, AnyControlDefinition, CompoundField, ControlDefinition, DataControlDefinition, DisplayControlDefinition, FieldOption, GroupedControlsDefinition, SchemaField } from "./types";
2
- import React, { Key, ReactElement } from "react";
1
+ import { ActionControlDefinition, AdornmentPlacement, CompoundField, ControlAdornment, ControlDefinition, DataControlDefinition, DisplayControlDefinition, EntityExpression, FieldOption, GroupedControlsDefinition, RenderOptions, SchemaField, SchemaValidator } from "./types";
2
+ import { Key, ReactElement, ReactNode } from "react";
3
3
  import { Control } from "@react-typed-forms/core";
4
+ export interface SchemaHooks {
5
+ useExpression(expr: EntityExpression, formState: FormEditState): Control<any | undefined>;
6
+ useValidators(formState: FormEditState, isVisible: boolean, control: Control<any>, required: boolean, validations?: SchemaValidator[] | null): void;
7
+ }
4
8
  export interface FormEditHooks {
5
- useDataProperties(formState: FormEditState, definition: DataControlDefinition, field: SchemaField): DataControlProperties;
6
- useGroupProperties(formState: FormEditState, definition: GroupedControlsDefinition, currentHooks: FormEditHooks): GroupControlProperties;
7
- useDisplayProperties(formState: FormEditState, definition: DisplayControlDefinition): DisplayControlProperties;
8
- useActionProperties(formState: FormEditState, definition: ActionControlDefinition): ActionControlProperties;
9
+ useDataProperties(formState: FormEditState, definition: DataControlDefinition, field: SchemaField): DataRendererProps;
10
+ useGroupProperties(formState: FormEditState, definition: GroupedControlsDefinition): GroupRendererProps;
11
+ useDisplayProperties(formState: FormEditState, definition: DisplayControlDefinition): DisplayRendererProps;
12
+ useActionProperties(formState: FormEditState, definition: ActionControlDefinition): ActionRendererProps;
13
+ schemaHooks: SchemaHooks;
9
14
  }
10
- export interface DataControlProperties {
15
+ export interface DataRendererProps {
16
+ definition: DataControlDefinition;
17
+ renderOptions: RenderOptions;
18
+ visible: Visibility;
19
+ control: Control<any>;
20
+ field: SchemaField;
21
+ array?: ArrayRendererProps;
11
22
  readonly: boolean;
12
- visible: boolean;
13
- options: FieldOption[] | undefined;
14
23
  defaultValue: any;
15
24
  required: boolean;
16
- customRender?: (props: DataRendererProps, control: Control<any>) => ReactElement;
25
+ options: FieldOption[] | undefined | null;
26
+ customRender?: (props: DataRendererProps) => ReactElement;
27
+ formState: FormEditState;
17
28
  }
18
- export interface GroupControlProperties {
19
- visible: boolean;
29
+ export interface GroupRendererProps {
30
+ definition: Omit<GroupedControlsDefinition, "children">;
31
+ visible: Visibility;
32
+ field?: CompoundField;
33
+ array?: ArrayRendererProps;
34
+ hideTitle: boolean;
20
35
  hooks: FormEditHooks;
21
- }
22
- export interface DisplayControlProperties {
23
- visible: boolean;
24
- }
25
- export interface ActionControlProperties {
26
- visible: boolean;
27
- onClick: () => void;
36
+ childCount: number;
37
+ renderChild: (child: number) => ReactElement;
28
38
  }
29
39
  export interface ControlData {
30
40
  [field: string]: any;
31
41
  }
32
- export interface FormEditState {
42
+ export interface FormDataContext {
33
43
  fields: SchemaField[];
34
44
  data: Control<ControlData>;
45
+ }
46
+ export interface FormEditState extends FormDataContext {
47
+ hooks: FormEditHooks;
48
+ renderer: FormRenderer;
35
49
  readonly?: boolean;
50
+ invisible?: boolean;
51
+ }
52
+ export type RenderControlOptions = Omit<FormEditState, "data">;
53
+ export interface ArrayRendererProps {
54
+ definition: DataControlDefinition | GroupedControlsDefinition;
55
+ control: Control<any[]>;
56
+ field: SchemaField;
57
+ addAction?: ActionRendererProps;
58
+ removeAction?: (childCount: number) => ActionRendererProps;
59
+ childCount: number;
60
+ renderChild: (childCount: number) => ReactElement;
61
+ childKey: (childCount: number) => Key;
36
62
  }
37
- export interface FormRendererComponents {
38
- renderData: (props: DataRendererProps, control: Control<any>, element: boolean, renderers: FormRendererComponents) => ReactElement;
39
- renderCompound: (props: CompoundGroupRendererProps, control: Control<any>, renderers: FormRendererComponents) => ReactElement;
63
+ export interface AdornmentProps {
64
+ key: Key;
65
+ definition: ControlAdornment;
66
+ }
67
+ export interface AdornmentRenderer {
68
+ wrap?: (children: ReactElement) => ReactElement;
69
+ child?: [AdornmentPlacement, ReactNode];
70
+ }
71
+ export interface FormRenderer {
72
+ renderData: (props: DataRendererProps) => ReactElement;
40
73
  renderGroup: (props: GroupRendererProps) => ReactElement;
41
74
  renderDisplay: (props: DisplayRendererProps) => ReactElement;
42
75
  renderAction: (props: ActionRendererProps) => ReactElement;
76
+ renderArray: (props: ArrayRendererProps) => ReactElement;
77
+ renderLabel: (props: LabelRendererProps, elem: ReactElement) => ReactElement;
78
+ renderVisibility: (visible: Visibility, elem: ReactElement) => ReactElement;
79
+ renderAdornment: (props: AdornmentProps) => AdornmentRenderer;
80
+ }
81
+ export interface Visibility {
82
+ value: boolean;
83
+ canChange: boolean;
84
+ }
85
+ export interface LabelRendererProps {
86
+ visible: Visibility;
87
+ title?: ReactNode;
88
+ forId?: string;
89
+ required: boolean;
90
+ control?: Control<any>;
91
+ group?: boolean;
92
+ renderAdornment: (placement: AdornmentPlacement) => ReactElement;
43
93
  }
44
- export declare const FormRendererComponentsContext: React.Context<FormRendererComponents | undefined>;
45
- export declare function useFormRendererComponents(): FormRendererComponents;
46
94
  export interface DisplayRendererProps {
47
95
  definition: DisplayControlDefinition;
48
- properties: DisplayControlProperties;
96
+ visible: Visibility;
49
97
  }
50
98
  export interface ActionRendererProps {
51
99
  definition: ActionControlDefinition;
52
- properties: ActionControlProperties;
53
- }
54
- export interface DataRendererProps {
55
- definition: DataControlDefinition;
56
- properties: DataControlProperties;
57
- field: SchemaField;
58
- formEditState?: FormEditState;
59
- }
60
- export interface GroupRendererProps {
61
- definition: Omit<GroupedControlsDefinition, "children">;
62
- properties: GroupControlProperties;
63
- childCount: number;
64
- renderChild: (child: number, wrapChild: (key: Key, childElem: ReactElement) => ReactElement) => ReactElement;
65
- }
66
- export interface CompoundGroupRendererProps {
67
- definition: GroupedControlsDefinition;
68
- field: CompoundField;
69
- properties: GroupControlProperties;
70
- renderChild: (key: Key, control: ControlDefinition, data: Control<{
71
- [field: string]: any;
72
- }>, wrapChild: (key: Key, childElem: ReactElement) => ReactElement) => ReactElement;
100
+ visible: Visibility;
101
+ onClick: () => void;
73
102
  }
74
- export declare function isScalarField(sf: SchemaField): sf is SchemaField;
75
- export declare function isCompoundField(sf: SchemaField): sf is CompoundField;
76
103
  export type AnySchemaFields = SchemaField | (Omit<CompoundField, "children"> & {
77
104
  children: AnySchemaFields[];
78
105
  });
79
- export declare function applyDefaultValues(v: {
80
- [k: string]: any;
81
- } | undefined, fields: SchemaField[]): any;
82
- export declare function applyDefaultForField(v: any, field: SchemaField, parent: SchemaField[], notElement?: boolean): any;
83
- export declare function defaultValueForFields(fields: SchemaField[]): any;
84
- export declare function defaultValueForField(sf: SchemaField): any;
85
- export declare function elementValueForField(sf: SchemaField): any;
86
- export declare function findScalarField(fields: SchemaField[], field: string): SchemaField | undefined;
87
- export declare function findCompoundField(fields: SchemaField[], field: string): CompoundField | undefined;
88
- export declare function findField(fields: SchemaField[], field: string): SchemaField | undefined;
89
- export declare function fieldDisplayName(sf: SchemaField): string;
90
- export declare function controlTitle(title: string | undefined, field: SchemaField): string;
91
- export declare function renderControl(definition: AnyControlDefinition, formState: FormEditState, hooks: FormEditHooks, key: Key, wrapChild?: (key: Key, db: ReactElement) => ReactElement): ReactElement;
106
+ export declare function controlTitle(title: string | undefined | null, field: SchemaField): string;
107
+ export declare function renderControl<S extends ControlDefinition>(definition: S, data: Control<any>, options: RenderControlOptions, key?: Key): ReactElement;
92
108
  export declare function controlForField(field: string, formState: FormEditState): Control<any>;
93
- export declare function fieldForControl(c: ControlDefinition): string | undefined;
94
- export declare function isDataControl(c: ControlDefinition): c is DataControlDefinition;
95
- export declare function isGroupControl(c: ControlDefinition): c is GroupedControlsDefinition;
109
+ export declare function fieldForControl(c: ControlDefinition): string | null | undefined;
110
+ export declare const AlwaysVisible: Visibility;
111
+ export declare function createAction(label: string, onClick: () => void, actionId?: string): ActionRendererProps;
112
+ export declare function visitControlData<S extends ControlDefinition, A>(definition: S, { fields, data }: FormDataContext, cb: (definition: DataControlDefinition, control: Control<any>) => A | undefined): A | undefined;
package/lib/hooks.d.ts CHANGED
@@ -1,9 +1,11 @@
1
- import { DataControlDefinition, EntityExpression, FieldOption, ControlDefinition, SchemaField } from "./types";
2
- import { DataControlProperties, FormEditHooks, FormEditState } from "./controlRender";
3
- export type ExpressionHook = (expr: EntityExpression, formState: FormEditState) => any;
4
- export declare function useDefaultValue(definition: DataControlDefinition, field: SchemaField, formState: FormEditState, useExpression: ExpressionHook): any;
5
- export declare function useIsControlVisible(definition: ControlDefinition, formState: FormEditState, useExpression: ExpressionHook): boolean;
6
- export declare function getDefaultScalarControlProperties(control: DataControlDefinition, field: SchemaField, visible: boolean, defaultValue: any, readonly?: boolean): DataControlProperties;
7
- export declare function getOptionsForScalarField(field: SchemaField): FieldOption[] | undefined;
8
- export declare const defaultExpressionHook: ExpressionHook;
9
- export declare function createFormEditHooks(useExpression: ExpressionHook): FormEditHooks;
1
+ import { ControlDefinition, DataControlDefinition, FieldOption, GroupedControlsDefinition, SchemaField } from "./types";
2
+ import { DataRendererProps, FormEditHooks, FormEditState, SchemaHooks, Visibility } from "./controlRender";
3
+ import { Control } from "@react-typed-forms/core";
4
+ export declare function useDefaultValue(definition: DataControlDefinition, field: SchemaField, formState: FormEditState, hooks: SchemaHooks): any;
5
+ export declare function useIsControlVisible(definition: ControlDefinition, formState: FormEditState, hooks: SchemaHooks): Visibility;
6
+ export declare function getDefaultScalarControlProperties(definition: DataControlDefinition, field: SchemaField, visible: Visibility, defaultValue: any, control: Control<any>, formState: FormEditState): DataRendererProps;
7
+ export declare function getOptionsForScalarField(field: SchemaField): FieldOption[] | undefined | null;
8
+ export declare function createDefaultSchemaHooks(): SchemaHooks;
9
+ export declare const defaultFormEditHooks: FormEditHooks;
10
+ export declare function createFormEditHooks(schemaHooks: SchemaHooks): FormEditHooks;
11
+ export declare function useControlDefinitionForSchema(sf: SchemaField[], definition?: GroupedControlsDefinition): GroupedControlsDefinition;
package/lib/index.d.ts CHANGED
@@ -2,3 +2,6 @@ export * from "./types";
2
2
  export * from "./schemaBuilder";
3
3
  export * from "./controlRender";
4
4
  export * from "./hooks";
5
+ export * from "./util";
6
+ export * from "./renderers";
7
+ export * from "./tailwind";