@react-typed-forms/schemas-rn 1.0.0

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,292 @@
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
+ ### Defining fields
99
+
100
+ While you can define a `SchemaField` as plain JSON, e.g.
101
+ ```json
102
+ [
103
+ {
104
+ "type": "String",
105
+ "field": "firstName",
106
+ "displayName": "First Name"
107
+ },
108
+ {
109
+ "type": "String",
110
+ "field": "lastName",
111
+ "displayName": "Last Name",
112
+ "required": true
113
+ },
114
+ {
115
+ "type": "Int",
116
+ "field": "yearOfBirth",
117
+ "displayName": "Year of birth",
118
+ "defaultValue": 1980
119
+ }
120
+ ]
121
+ ```
122
+
123
+ However if you have existing types which you would like to define `SchemaField`s for the library contains a function called `buildSchema` a type safe way of generating fields for a type:
124
+
125
+ ```tsx
126
+ interface SimpleForm {
127
+ firstName: string;
128
+ lastName: string;
129
+ yearOfBirth: number;
130
+ }
131
+
132
+ const simpleSchema = buildSchema<SimpleForm>({
133
+ firstName: stringField("First Name"),
134
+ lastName: stringField("Last Name", { required: true }),
135
+ yearOfBirth: intField("Year of birth", { defaultValue: 1980 }),
136
+ });
137
+ ```
138
+
139
+ ### Field options
140
+
141
+ Often a field only has a set of allowed values, e.g. a enum. `SchemaField` allows this to be modeled by
142
+ providing an array of `FieldOption`:
143
+
144
+ <!-- AUTO-GENERATED-CONTENT:START (CODE:src=./src/types.ts&lines=37-41) -->
145
+ <!-- The below code snippet is automatically added from ./src/types.ts -->
146
+ ```ts
147
+ export interface FieldOption {
148
+ name: string;
149
+ value: any;
150
+ }
151
+ ```
152
+ <!-- AUTO-GENERATED-CONTENT:END -->
153
+
154
+ For example you could only allow certain last names:
155
+
156
+ ```ts
157
+ stringField('Last Name', {
158
+ required: true,
159
+ options:[
160
+ { name: "Smith", value: "smith" },
161
+ { name: "Jones", value: "jones" }
162
+ ]
163
+ });
164
+ ```
165
+
166
+ <img src="../../images/schemas-option.png">
167
+
168
+ ### `ControlDefinition`
169
+
170
+ A `ControlDefinition` is a JSON object which describes what should be rendered in a UI. Each `ControlDefinition` can be one of 4 distinct types:
171
+
172
+ * `DataControlDefinition` - Points to a `SchemaField` in order to render a control for editing of data.
173
+ * `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.
174
+ * `DisplayControlDefinition` - Render readonly content, current text and HTML variants are defined.
175
+ * `ActionControlDefinition` - Renders an action button, useful for hooking forms up with outside functionality.
176
+
177
+ If you don't care about the layout of the form that much you can generate the definition automatically by using `useControlDefinitionForSchema()`.
178
+
179
+ TODO renderOptions, DataRenderType for choosing render style.
180
+
181
+ ## Form Renderer
182
+
183
+ 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:
184
+
185
+ <!-- AUTO-GENERATED-CONTENT:START (CODE:src=./src/controlRender.tsx&lines=128-138) -->
186
+ <!-- The below code snippet is automatically added from ./src/controlRender.tsx -->
187
+ ```tsx
188
+ export interface FormRenderer {
189
+ renderData: (props: DataRendererProps) => ReactElement;
190
+ renderGroup: (props: GroupRendererProps) => ReactElement;
191
+ renderDisplay: (props: DisplayRendererProps) => ReactElement;
192
+ renderAction: (props: ActionRendererProps) => ReactElement;
193
+ renderArray: (props: ArrayRendererProps) => ReactElement;
194
+ renderLabel: (props: LabelRendererProps, elem: ReactElement) => ReactElement;
195
+ renderVisibility: (visible: Visibility, elem: ReactElement) => ReactElement;
196
+ renderAdornment: (props: AdornmentProps) => AdornmentRenderer;
197
+ }
198
+ ```
199
+ <!-- AUTO-GENERATED-CONTENT:END -->
200
+
201
+ The `createFormRenderer` function takes an array of `RendererRegistration` which allows for customising the rendering.
202
+
203
+ <!-- AUTO-GENERATED-CONTENT:START (CODE:src=./src/renderers.tsx&lines=109-118) -->
204
+ <!-- The below code snippet is automatically added from ./src/renderers.tsx -->
205
+ ```tsx
206
+ export type RendererRegistration =
207
+ | DataRendererRegistration
208
+ | GroupRendererRegistration
209
+ | DisplayRendererRegistration
210
+ | ActionRendererRegistration
211
+ | LabelRendererRegistration
212
+ | ArrayRendererRegistration
213
+ | AdornmentRendererRegistration
214
+ | VisibilityRendererRegistration;
215
+ ```
216
+ <!-- AUTO-GENERATED-CONTENT:END -->
217
+
218
+ Probably the most common customisation would be to add a `DataRendererRegistration` which will change the way a `DataControlDefinition` is rendered for a particular FieldType:
219
+
220
+ <!-- AUTO-GENERATED-CONTENT:START (CODE:src=./src/renderers.tsx&lines=48-61) -->
221
+ <!-- The below code snippet is automatically added from ./src/renderers.tsx -->
222
+ ```tsx
223
+ export interface DataRendererRegistration {
224
+ type: "data";
225
+ schemaType?: string | string[];
226
+ renderType?: string | string[];
227
+ options?: boolean;
228
+ collection?: boolean;
229
+ match?: (props: DataRendererProps) => boolean;
230
+ render: (
231
+ props: DataRendererProps,
232
+ defaultLabel: (label?: Partial<LabelRendererProps>) => LabelRendererProps,
233
+ renderers: FormRenderer,
234
+ ) => ReactElement;
235
+ }
236
+ ```
237
+ <!-- AUTO-GENERATED-CONTENT:END -->
238
+ * The `schemaType` field specifies which `FieldType`(s) should use this `DataRendererRegistration`, unspecified means allow any.
239
+ * The `renderType` field specifies which `DataRenderType` this registration applies to.
240
+ * The `match` function can be used if the matching logic is more complicated than provided by the other.
241
+ * The `render` function does the actual rendering if the ControlDefinition/SchemaField matches the registration.
242
+
243
+ 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:
244
+
245
+ <!-- AUTO-GENERATED-CONTENT:START (CODE:src=../schemas-mui/src/index.tsx&lines=12-35) -->
246
+ <!-- The below code snippet is automatically added from ../schemas-mui/src/index.tsx -->
247
+ ```tsx
248
+ export function muiTextfieldRenderer(
249
+ variant?: "standard" | "outlined" | "filled",
250
+ ): DataRendererRegistration {
251
+ return {
252
+ type: "data",
253
+ schemaType: FieldType.String,
254
+ renderType: DataRenderType.Standard,
255
+ render: (r, makeLabel, { renderVisibility }) => {
256
+ const { title, required } = makeLabel();
257
+ return renderVisibility(
258
+ r.visible,
259
+ <FTextField
260
+ variant={variant}
261
+ required={required}
262
+ fullWidth
263
+ size="small"
264
+ state={r.control}
265
+ label={title}
266
+ />,
267
+ );
268
+ },
269
+ };
270
+ }
271
+ ```
272
+ <!-- AUTO-GENERATED-CONTENT:END -->
273
+
274
+ Changing the simple example above to use the following:
275
+
276
+ ```tsx
277
+ const renderer: FormRenderer = createFormRenderer(
278
+ [muiTextFieldRenderer()],
279
+ createDefaultRenderer (defaultTailwindTheme));
280
+ ```
281
+
282
+ This will produce this UI:
283
+
284
+ <img src="../../images/schemas-muifield.png">
285
+
286
+ ## TODO
287
+
288
+ * Label rendering
289
+ * Visibility
290
+ * Arrays
291
+ * Display controls
292
+
@@ -0,0 +1,7 @@
1
+ import { IconLibrary } from "@react-typed-forms/schemas";
2
+ export type RNIconProps = {
3
+ name: string;
4
+ className?: string;
5
+ iconLibrary?: IconLibrary;
6
+ };
7
+ export declare function Icon({ name, className, iconLibrary, }: RNIconProps): JSX.Element;
@@ -0,0 +1,18 @@
1
+ import { type VariantProps } from "class-variance-authority";
2
+ import * as React from "react";
3
+ import { Pressable } from "react-native";
4
+ declare const buttonVariants: (props?: ({
5
+ variant?: "default" | "destructive" | "outline" | "secondary" | "ghost" | "link" | null | undefined;
6
+ size?: "default" | "sm" | "lg" | "icon" | null | undefined;
7
+ } & import("class-variance-authority/dist/types").ClassProp) | undefined) => string;
8
+ declare const buttonTextVariants: (props?: ({
9
+ variant?: "default" | "destructive" | "outline" | "secondary" | "ghost" | "link" | null | undefined;
10
+ size?: "default" | "sm" | "lg" | "icon" | null | undefined;
11
+ } & import("class-variance-authority/dist/types").ClassProp) | undefined) => string;
12
+ type ButtonProps = React.ComponentPropsWithoutRef<typeof Pressable> & VariantProps<typeof buttonVariants>;
13
+ declare const RNButton: React.ForwardRefExoticComponent<Omit<import("react-native").PressableProps & React.RefAttributes<import("react-native").View>, "ref"> & VariantProps<(props?: ({
14
+ variant?: "default" | "destructive" | "outline" | "secondary" | "ghost" | "link" | null | undefined;
15
+ size?: "default" | "sm" | "lg" | "icon" | null | undefined;
16
+ } & import("class-variance-authority/dist/types").ClassProp) | undefined) => string> & React.RefAttributes<import("react-native").View>>;
17
+ export { RNButton, buttonTextVariants, buttonVariants };
18
+ export type { ButtonProps };
@@ -0,0 +1,12 @@
1
+ import * as React from "react";
2
+ declare const RNCheckbox: React.ForwardRefExoticComponent<Omit<import("react-native").PressableProps & React.RefAttributes<import("react-native").View>, "ref"> & {
3
+ asChild?: boolean;
4
+ } & {
5
+ onKeyDown?: (ev: React.KeyboardEvent) => void;
6
+ onKeyUp?: (ev: React.KeyboardEvent) => void;
7
+ } & {
8
+ checked: boolean;
9
+ onCheckedChange: (checked: boolean) => void;
10
+ disabled?: boolean;
11
+ } & React.RefAttributes<import("react-native").View>>;
12
+ export { RNCheckbox };
@@ -0,0 +1,10 @@
1
+ import { DateTimePickerProps } from "react-native-modal-datetime-picker";
2
+ import { Control } from "@react-typed-forms/core";
3
+ import { DefaultDataRendererOptions } from "@react-typed-forms/schemas-html";
4
+ export type RNDateTimeProps = Pick<DateTimePickerProps, "mode" | "locale" | "maximumDate" | "minimumDate" | "is24Hour"> & {
5
+ control: Control<string | null>;
6
+ readonly?: boolean;
7
+ className?: string;
8
+ placeholder?: string;
9
+ };
10
+ export declare function createRNDateTimePickerRenderer(options?: DefaultDataRendererOptions): import("@react-typed-forms/schemas").DataRendererRegistration;
@@ -0,0 +1,14 @@
1
+ import * as React from "react";
2
+ import { Control } from "@react-typed-forms/core";
3
+ export type RNDialogProps = {
4
+ title: React.ReactNode;
5
+ trigger?: React.ReactNode;
6
+ content?: React.ReactNode;
7
+ footer?: React.ReactNode;
8
+ containerClass?: string;
9
+ open?: Control<boolean>;
10
+ onOpenChange?: (open: boolean) => void;
11
+ closeOnOutsidePress?: boolean;
12
+ portalHost?: string;
13
+ };
14
+ export declare function RNDialog({ title, trigger, content, footer, open, onOpenChange, closeOnOutsidePress, containerClass, portalHost, }: RNDialogProps): JSX.Element;
@@ -0,0 +1,5 @@
1
+ export type RNHtmlRendererProps = {
2
+ className?: string;
3
+ html?: string;
4
+ };
5
+ export declare function RNHtmlRenderer({ className, html }: RNHtmlRendererProps): JSX.Element | undefined;
@@ -0,0 +1,12 @@
1
+ import * as React from "react";
2
+ import { View } from "react-native";
3
+ declare const RNRadioItem: React.ForwardRefExoticComponent<{
4
+ checked: boolean;
5
+ className?: string;
6
+ disabled?: boolean;
7
+ id: string;
8
+ name: string;
9
+ readonly: boolean;
10
+ onChange: () => void;
11
+ } & React.RefAttributes<View>>;
12
+ export { RNRadioItem };
@@ -0,0 +1,5 @@
1
+ import { SelectRendererOptions } from "@react-typed-forms/schemas-html";
2
+ export interface ExtendedDropdown {
3
+ portalHost?: string;
4
+ }
5
+ export declare function createRNSelectRenderer(options?: SelectRendererOptions): import("@react-typed-forms/schemas").DataRendererRegistration;
@@ -0,0 +1,7 @@
1
+ import * as React from "react";
2
+ import { Text } from "react-native";
3
+ declare const TextClassContext: React.Context<string | undefined>;
4
+ declare const RNText: React.ForwardRefExoticComponent<import("react-native").TextProps & {
5
+ asChild?: boolean;
6
+ } & React.RefAttributes<Text>>;
7
+ export { RNText, TextClassContext };
@@ -0,0 +1,6 @@
1
+ import * as React from "react";
2
+ import { TextInput, type TextInputProps } from "react-native";
3
+ declare const RNTextInput: React.ForwardRefExoticComponent<TextInputProps & {
4
+ disabled?: boolean;
5
+ } & React.RefAttributes<TextInput>>;
6
+ export { RNTextInput };