@nolwenture/formik-jsonschema-form 0.1.0 → 0.1.2
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/package.json +90 -86
- package/src/Form.d.ts +4 -0
- package/src/Form.js +52 -0
- package/src/Form.stories.d.ts +11 -0
- package/src/Form.stories.js +135 -0
- package/src/Form.test.d.ts +1 -0
- package/src/Form.test.js +9 -0
- package/src/FormEditor.d.ts +8 -0
- package/src/FormEditor.js +74 -0
- package/src/FormEditor.stories.d.ts +5 -0
- package/src/FormEditor.stories.js +22 -0
- package/src/_sampleData.d.ts +10 -0
- package/src/_sampleData.js +262 -0
- package/src/fields/AnyField.d.ts +3 -0
- package/src/fields/AnyField.js +34 -0
- package/src/fields/ArrayField.d.ts +3 -0
- package/src/fields/ArrayField.js +119 -0
- package/src/fields/BooleanField.d.ts +3 -0
- package/src/fields/BooleanField.js +22 -0
- package/src/fields/DateField.d.ts +3 -0
- package/src/fields/DateField.js +22 -0
- package/src/fields/NullField.d.ts +3 -0
- package/src/fields/NullField.js +19 -0
- package/src/fields/NumberField.d.ts +3 -0
- package/src/fields/NumberField.js +17 -0
- package/src/fields/ObjectField.d.ts +3 -0
- package/src/fields/ObjectField.js +56 -0
- package/src/fields/SchemaField.d.ts +3 -0
- package/src/fields/SchemaField.js +39 -0
- package/src/fields/StringField.d.ts +3 -0
- package/src/fields/StringField.js +60 -0
- package/src/fields/{index.ts → index.d.ts} +0 -0
- package/src/fields/index.js +9 -0
- package/src/fields/types.d.ts +10 -0
- package/src/fields/types.js +10 -0
- package/src/formEditorSchemas.d.ts +3 -0
- package/src/formEditorSchemas.js +36 -0
- package/src/{index.tsx → index.d.ts} +0 -0
- package/src/index.js +5 -0
- package/src/setupTests.d.ts +1 -0
- package/src/{setupTests.ts → setupTests.js} +0 -0
- package/src/styledComponents.d.ts +26 -0
- package/src/styledComponents.js +89 -0
- package/src/templates/ArrayFieldTemplate.d.ts +3 -0
- package/src/templates/ArrayFieldTemplate.js +27 -0
- package/src/templates/FieldTemplate.d.ts +3 -0
- package/src/templates/FieldTemplate.js +27 -0
- package/src/templates/ObjectFieldTemplate.d.ts +3 -0
- package/src/templates/ObjectFieldTemplate.js +11 -0
- package/src/templates/{index.ts → index.d.ts} +0 -0
- package/src/templates/index.js +3 -0
- package/src/types.d.ts +168 -0
- package/src/types.js +14 -0
- package/src/utils.d.ts +22 -0
- package/src/utils.js +108 -0
- package/.storybook/main.js +0 -13
- package/.storybook/preview.js +0 -9
- package/README.md +0 -46
- package/src/Form.stories.tsx +0 -124
- package/src/Form.test.tsx +0 -10
- package/src/Form.tsx +0 -71
- package/src/FormEditor.stories.tsx +0 -17
- package/src/FormEditor.tsx +0 -53
- package/src/_sampleData.ts +0 -273
- package/src/fields/AnyField.tsx +0 -35
- package/src/fields/ArrayField.tsx +0 -177
- package/src/fields/BooleanField.tsx +0 -23
- package/src/fields/DateField.tsx +0 -20
- package/src/fields/NullField.tsx +0 -12
- package/src/fields/NumberField.tsx +0 -14
- package/src/fields/ObjectField.tsx +0 -68
- package/src/fields/SchemaField.tsx +0 -37
- package/src/fields/StringField.tsx +0 -89
- package/src/fields/types.tsx +0 -10
- package/src/formEditorSchemas.ts +0 -40
- package/src/styledComponents.tsx +0 -107
- package/src/templates/ArrayFieldTemplate.tsx +0 -46
- package/src/templates/FieldTemplate.tsx +0 -46
- package/src/templates/ObjectFieldTemplate.tsx +0 -22
- package/src/types.ts +0 -260
- package/src/utils.ts +0 -124
- package/tsconfig.json +0 -26
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import { useContext, useEffect, useMemo } from "react";
|
|
2
|
-
import { idText } from "typescript";
|
|
3
|
-
import { FormRegistry } from "../Form";
|
|
4
|
-
import { FieldProps } from "../types";
|
|
5
|
-
import { useExtractUiSchema, useSetDefault } from "../utils";
|
|
6
|
-
|
|
7
|
-
function getEnums({ schema }: FieldProps) {
|
|
8
|
-
const numberOfDefined = [schema.enum, schema.oneOf, schema.anyOf].filter(
|
|
9
|
-
(x) => x
|
|
10
|
-
).length;
|
|
11
|
-
|
|
12
|
-
if (numberOfDefined === 0) {
|
|
13
|
-
return;
|
|
14
|
-
}
|
|
15
|
-
if (numberOfDefined > 1) {
|
|
16
|
-
throw new Error('Only one of "enum", "oneOf" or "anyOf" can be defined!');
|
|
17
|
-
}
|
|
18
|
-
if (schema.enum) {
|
|
19
|
-
return {
|
|
20
|
-
type: "select",
|
|
21
|
-
values: schema.enum.map((option: any) => ({
|
|
22
|
-
const: option,
|
|
23
|
-
title: `${option}`,
|
|
24
|
-
})),
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
if (schema.oneOf) {
|
|
28
|
-
return {
|
|
29
|
-
type: "select",
|
|
30
|
-
values: schema.oneOf as { const: any; title: string }[],
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
if (schema.anyOf) {
|
|
34
|
-
return {
|
|
35
|
-
type: "multiSelect",
|
|
36
|
-
values: schema.anyOf as { const: any; title: string }[],
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export function StringField(props: FieldProps) {
|
|
42
|
-
const {
|
|
43
|
-
templates: { FieldTemplate },
|
|
44
|
-
components: { FieldInput },
|
|
45
|
-
} = useContext(FormRegistry);
|
|
46
|
-
useSetDefault(props);
|
|
47
|
-
const { name, schema, uiSchema } = props;
|
|
48
|
-
const { type, placeholder, props: inputProps } = useExtractUiSchema(uiSchema);
|
|
49
|
-
const enums = useMemo(() => getEnums(props), [props]);
|
|
50
|
-
|
|
51
|
-
switch (enums?.type) {
|
|
52
|
-
case "select":
|
|
53
|
-
case "multiSelect":
|
|
54
|
-
return (
|
|
55
|
-
<FieldTemplate {...props}>
|
|
56
|
-
<FieldInput
|
|
57
|
-
{...{
|
|
58
|
-
type,
|
|
59
|
-
placeholder,
|
|
60
|
-
...inputProps,
|
|
61
|
-
}}
|
|
62
|
-
name={name}
|
|
63
|
-
formikAs="select"
|
|
64
|
-
multiple={enums.type === "multiSelect"}
|
|
65
|
-
>
|
|
66
|
-
{enums.type !== "multiSelect" && <option value=""></option>}
|
|
67
|
-
{enums?.values.map((option, index) => (
|
|
68
|
-
<option key={index} value={option.const}>
|
|
69
|
-
{option.title}
|
|
70
|
-
</option>
|
|
71
|
-
))}
|
|
72
|
-
</FieldInput>
|
|
73
|
-
</FieldTemplate>
|
|
74
|
-
);
|
|
75
|
-
default:
|
|
76
|
-
return (
|
|
77
|
-
<FieldTemplate {...props}>
|
|
78
|
-
<FieldInput
|
|
79
|
-
{...{
|
|
80
|
-
type,
|
|
81
|
-
placeholder,
|
|
82
|
-
...inputProps,
|
|
83
|
-
}}
|
|
84
|
-
name={name}
|
|
85
|
-
/>
|
|
86
|
-
</FieldTemplate>
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
}
|
package/src/fields/types.tsx
DELETED
package/src/formEditorSchemas.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { Schema, UiSchema } from "./types";
|
|
2
|
-
|
|
3
|
-
const field: Schema = {
|
|
4
|
-
type: "object",
|
|
5
|
-
properties: {
|
|
6
|
-
type: {
|
|
7
|
-
type: "string",
|
|
8
|
-
title: "Field type",
|
|
9
|
-
oneOf: [
|
|
10
|
-
{ const: "string", title: "String" },
|
|
11
|
-
{ const: "number", title: "Number" },
|
|
12
|
-
{ const: "array", title: "Array" },
|
|
13
|
-
{ const: "object", title: "Object" },
|
|
14
|
-
],
|
|
15
|
-
},
|
|
16
|
-
title: {
|
|
17
|
-
type: "string",
|
|
18
|
-
title: "Field title",
|
|
19
|
-
},
|
|
20
|
-
},
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export const schema: Schema = {
|
|
24
|
-
type: "object",
|
|
25
|
-
properties: {
|
|
26
|
-
"schema.type": {
|
|
27
|
-
type: "string",
|
|
28
|
-
default: "object",
|
|
29
|
-
},
|
|
30
|
-
"schema.title": {
|
|
31
|
-
type: "string",
|
|
32
|
-
title: "Form title",
|
|
33
|
-
},
|
|
34
|
-
},
|
|
35
|
-
additionalProperties: field,
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
export const uiSchema: UiSchema = {
|
|
39
|
-
"schema.type": { props: { type: "hidden" } },
|
|
40
|
-
};
|
package/src/styledComponents.tsx
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import styled from "styled-components";
|
|
2
|
-
import * as formik from "formik";
|
|
3
|
-
import {
|
|
4
|
-
ArrayFieldButtonProps,
|
|
5
|
-
ArrayFieldButtonPurpose,
|
|
6
|
-
ObjectFieldButtonProps,
|
|
7
|
-
ObjectFieldButtonPurpose,
|
|
8
|
-
} from "./types";
|
|
9
|
-
|
|
10
|
-
export const Form = styled(formik.Form)``;
|
|
11
|
-
export const FormButtonWrapper = styled.div``;
|
|
12
|
-
export const FormButton = styled.button``;
|
|
13
|
-
|
|
14
|
-
export const ObjectFieldWrapper = styled.fieldset``;
|
|
15
|
-
export const ObjectFieldTitle = styled.legend``;
|
|
16
|
-
export const ObjectFieldList = styled.ul`
|
|
17
|
-
list-style-type: none;
|
|
18
|
-
padding: 0;
|
|
19
|
-
`;
|
|
20
|
-
export const ObjectFieldItem = styled.li`
|
|
21
|
-
display: flex;
|
|
22
|
-
`;
|
|
23
|
-
export const ObjectFieldItemButtons = styled.div`
|
|
24
|
-
display: flex;
|
|
25
|
-
margin: 0.5rem;
|
|
26
|
-
`;
|
|
27
|
-
export const ObjectFieldButtonEl = styled.a`
|
|
28
|
-
cursor: pointer;
|
|
29
|
-
border: 1px solid black;
|
|
30
|
-
display: block;
|
|
31
|
-
line-height: 1em;
|
|
32
|
-
height: 1em;
|
|
33
|
-
width: 1em;
|
|
34
|
-
text-align: center;
|
|
35
|
-
`;
|
|
36
|
-
|
|
37
|
-
export function ObjectFieldButton({
|
|
38
|
-
purpose,
|
|
39
|
-
onClick,
|
|
40
|
-
}: ObjectFieldButtonProps) {
|
|
41
|
-
switch (purpose) {
|
|
42
|
-
case ObjectFieldButtonPurpose.ADD:
|
|
43
|
-
return <ObjectFieldButtonEl onClick={onClick}>+</ObjectFieldButtonEl>;
|
|
44
|
-
case ObjectFieldButtonPurpose.REMOVE:
|
|
45
|
-
return <ObjectFieldButtonEl onClick={onClick}>-</ObjectFieldButtonEl>;
|
|
46
|
-
case ObjectFieldButtonPurpose.MOVE_UP:
|
|
47
|
-
return <ObjectFieldButtonEl onClick={onClick}>↑</ObjectFieldButtonEl>;
|
|
48
|
-
case ObjectFieldButtonPurpose.MOVE_DOWN:
|
|
49
|
-
return <ObjectFieldButtonEl onClick={onClick}>↓</ObjectFieldButtonEl>;
|
|
50
|
-
default:
|
|
51
|
-
return (
|
|
52
|
-
<ObjectFieldButtonEl onClick={onClick}>
|
|
53
|
-
UNKNOWN PURPOSE
|
|
54
|
-
</ObjectFieldButtonEl>
|
|
55
|
-
);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export const ArrayFieldWrapper = ObjectFieldWrapper;
|
|
60
|
-
export const ArrayFieldTitle = ObjectFieldTitle;
|
|
61
|
-
export const ArrayFieldList = ObjectFieldList;
|
|
62
|
-
export const ArrayFieldItem = ObjectFieldItem;
|
|
63
|
-
export const ArrayFieldItemButtons = ObjectFieldItemButtons;
|
|
64
|
-
export const ArrayFieldButtonEl = ObjectFieldButtonEl;
|
|
65
|
-
|
|
66
|
-
export function ArrayFieldButton({ purpose, onClick }: ArrayFieldButtonProps) {
|
|
67
|
-
switch (purpose) {
|
|
68
|
-
case ArrayFieldButtonPurpose.ADD:
|
|
69
|
-
return <ArrayFieldButtonEl onClick={onClick}>+</ArrayFieldButtonEl>;
|
|
70
|
-
case ArrayFieldButtonPurpose.REMOVE:
|
|
71
|
-
return <ArrayFieldButtonEl onClick={onClick}>-</ArrayFieldButtonEl>;
|
|
72
|
-
case ArrayFieldButtonPurpose.MOVE_UP:
|
|
73
|
-
return <ArrayFieldButtonEl onClick={onClick}>↑</ArrayFieldButtonEl>;
|
|
74
|
-
case ArrayFieldButtonPurpose.MOVE_DOWN:
|
|
75
|
-
return <ArrayFieldButtonEl onClick={onClick}>↓</ArrayFieldButtonEl>;
|
|
76
|
-
default:
|
|
77
|
-
return (
|
|
78
|
-
<ArrayFieldButtonEl onClick={onClick}>
|
|
79
|
-
UNKNOWN PURPOSE
|
|
80
|
-
</ArrayFieldButtonEl>
|
|
81
|
-
);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export const FieldWrapper = styled.div`
|
|
86
|
-
margin: 0.5rem 0;
|
|
87
|
-
`;
|
|
88
|
-
export const FieldTitle = styled.label`
|
|
89
|
-
font-weight: 700;
|
|
90
|
-
`;
|
|
91
|
-
export const FieldDescription = styled.p`
|
|
92
|
-
margin: 0;
|
|
93
|
-
font-size: 0.9em;
|
|
94
|
-
`;
|
|
95
|
-
export const FieldHelp = styled.p`
|
|
96
|
-
margin: 0;
|
|
97
|
-
font-size: 0.9em;
|
|
98
|
-
`;
|
|
99
|
-
export const ErrorMessage = styled(formik.ErrorMessage)``;
|
|
100
|
-
|
|
101
|
-
/*
|
|
102
|
-
* Works like Formik field, but "as" props needs to be given with "formikAs", since styled components uses as.
|
|
103
|
-
*
|
|
104
|
-
*/
|
|
105
|
-
export const FieldInput = styled(({ formikAs, ...props }) => {
|
|
106
|
-
return <formik.Field {...props} as={formikAs} />;
|
|
107
|
-
})``;
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { useContext } from "react";
|
|
2
|
-
import { FormRegistry } from "../Form";
|
|
3
|
-
import { ArrayFieldTemplateProps } from "../types";
|
|
4
|
-
import { useExtractUiSchema, getDescription, getTitle } from "../utils";
|
|
5
|
-
|
|
6
|
-
export function ArrayFieldTemplate(props: ArrayFieldTemplateProps) {
|
|
7
|
-
const {
|
|
8
|
-
components: {
|
|
9
|
-
ArrayFieldWrapper,
|
|
10
|
-
ArrayFieldTitle,
|
|
11
|
-
FieldDescription,
|
|
12
|
-
ErrorMessage,
|
|
13
|
-
FieldHelp,
|
|
14
|
-
},
|
|
15
|
-
} = useContext(FormRegistry);
|
|
16
|
-
const { name, uiSchema, children } = props;
|
|
17
|
-
const title = getTitle(props);
|
|
18
|
-
const description = getDescription(props);
|
|
19
|
-
const {
|
|
20
|
-
help,
|
|
21
|
-
//TODO: This is not right
|
|
22
|
-
props: {
|
|
23
|
-
wrapper: wrappertProps,
|
|
24
|
-
title: titleProps,
|
|
25
|
-
description: descriptionProps,
|
|
26
|
-
help: helpProps,
|
|
27
|
-
error: errorProps,
|
|
28
|
-
} = {},
|
|
29
|
-
} = useExtractUiSchema(uiSchema);
|
|
30
|
-
|
|
31
|
-
return (
|
|
32
|
-
<ArrayFieldWrapper {...wrappertProps}>
|
|
33
|
-
{title && (
|
|
34
|
-
<ArrayFieldTitle {...titleProps} htmlFor={name}>
|
|
35
|
-
{title}
|
|
36
|
-
</ArrayFieldTitle>
|
|
37
|
-
)}
|
|
38
|
-
{description && (
|
|
39
|
-
<FieldDescription {...descriptionProps}>{description}</FieldDescription>
|
|
40
|
-
)}
|
|
41
|
-
{children}
|
|
42
|
-
{help && <FieldHelp {...helpProps}>{help}</FieldHelp>}
|
|
43
|
-
<ErrorMessage {...errorProps} name={name} />
|
|
44
|
-
</ArrayFieldWrapper>
|
|
45
|
-
);
|
|
46
|
-
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { useContext } from "react";
|
|
2
|
-
import { FormRegistry } from "../Form";
|
|
3
|
-
import { FieldTemplateProps } from "../types";
|
|
4
|
-
import { useExtractUiSchema, getDescription, getTitle } from "../utils";
|
|
5
|
-
|
|
6
|
-
export function FieldTemplate(props: FieldTemplateProps) {
|
|
7
|
-
const {
|
|
8
|
-
components: {
|
|
9
|
-
FieldWrapper,
|
|
10
|
-
FieldTitle,
|
|
11
|
-
FieldDescription,
|
|
12
|
-
ErrorMessage,
|
|
13
|
-
FieldHelp,
|
|
14
|
-
},
|
|
15
|
-
} = useContext(FormRegistry);
|
|
16
|
-
const { name, uiSchema, children } = props;
|
|
17
|
-
const title = getTitle(props);
|
|
18
|
-
const description = getDescription(props);
|
|
19
|
-
const {
|
|
20
|
-
help,
|
|
21
|
-
//TODO: This is not right
|
|
22
|
-
props: {
|
|
23
|
-
wrapper: wrappertProps,
|
|
24
|
-
title: titleProps,
|
|
25
|
-
description: descriptionProps,
|
|
26
|
-
help: helpProps,
|
|
27
|
-
error: errorProps,
|
|
28
|
-
} = {},
|
|
29
|
-
} = useExtractUiSchema(uiSchema);
|
|
30
|
-
|
|
31
|
-
return (
|
|
32
|
-
<FieldWrapper {...wrappertProps}>
|
|
33
|
-
{title && (
|
|
34
|
-
<FieldTitle {...titleProps} htmlFor={name}>
|
|
35
|
-
{title}
|
|
36
|
-
</FieldTitle>
|
|
37
|
-
)}
|
|
38
|
-
{description && (
|
|
39
|
-
<FieldDescription {...descriptionProps}>{description}</FieldDescription>
|
|
40
|
-
)}
|
|
41
|
-
{children}
|
|
42
|
-
{help && <FieldHelp {...helpProps}>{help}</FieldHelp>}
|
|
43
|
-
<ErrorMessage {...errorProps} name={name} />
|
|
44
|
-
</FieldWrapper>
|
|
45
|
-
);
|
|
46
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { useContext } from "react";
|
|
2
|
-
import { FormRegistry } from "../Form";
|
|
3
|
-
import { ObjectFieldTemplateProps } from "../types";
|
|
4
|
-
import { getDescription, getTitle } from "../utils";
|
|
5
|
-
|
|
6
|
-
export function ObjectFieldTemplate(props: ObjectFieldTemplateProps) {
|
|
7
|
-
const {
|
|
8
|
-
components: { FieldDescription, ObjectFieldTitle, ObjectFieldWrapper },
|
|
9
|
-
} = useContext(FormRegistry);
|
|
10
|
-
const { children } = props;
|
|
11
|
-
|
|
12
|
-
const title = getTitle(props);
|
|
13
|
-
const description = getDescription(props);
|
|
14
|
-
|
|
15
|
-
return (
|
|
16
|
-
<ObjectFieldWrapper>
|
|
17
|
-
{title && <ObjectFieldTitle>{title}</ObjectFieldTitle>}
|
|
18
|
-
{description && <FieldDescription>{description}</FieldDescription>}
|
|
19
|
-
{children}
|
|
20
|
-
</ObjectFieldWrapper>
|
|
21
|
-
);
|
|
22
|
-
}
|
package/src/types.ts
DELETED
|
@@ -1,260 +0,0 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
|
-
import { JSONSchema7 } from "json-schema";
|
|
3
|
-
import { FormikValues, FormikHelpers } from "formik";
|
|
4
|
-
|
|
5
|
-
type ErrorSchema = {
|
|
6
|
-
[k: string]: ErrorSchema;
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
export type FormProps<T> = React.PropsWithChildren<{
|
|
10
|
-
/** If true, disabled prop is passed down to each field on the form */
|
|
11
|
-
disabled?: boolean | undefined;
|
|
12
|
-
// /** The value that will be passed to `class` HTML attribute of form */
|
|
13
|
-
className?: string | undefined;
|
|
14
|
-
/** Form schema */
|
|
15
|
-
schema: Schema;
|
|
16
|
-
/** Form uiSchema */
|
|
17
|
-
uiSchema?: UiSchema | undefined;
|
|
18
|
-
/** Data that is passed down to all fields. Used for implementing context aware fields */
|
|
19
|
-
formContext?: Record<string, any>;
|
|
20
|
-
/** Dictionary of registered fields */
|
|
21
|
-
fields?: { [name: string]: Field } | undefined;
|
|
22
|
-
/* Customize the templates to construct the core field */
|
|
23
|
-
templates?: Partial<TemplatesType>;
|
|
24
|
-
components?: Partial<ComponentsType>;
|
|
25
|
-
/**
|
|
26
|
-
* If set to true, turns off HTML5 validation
|
|
27
|
-
* @default false
|
|
28
|
-
*/
|
|
29
|
-
noHtml5Validate?: boolean | undefined;
|
|
30
|
-
// safeRenderCompletion?: boolean | undefined;
|
|
31
|
-
// /** Allows you to validate formdata against another JSON Schema meta schema */
|
|
32
|
-
// additionalMetaSchemas?: ReadonlyArray<object> | undefined;
|
|
33
|
-
// /** Allows you to define custom formats for validation */
|
|
34
|
-
// customFormats?:
|
|
35
|
-
// | { [k: string]: string | RegExp | ((data: string) => boolean) }
|
|
36
|
-
// | undefined;
|
|
37
|
-
// // HTML Attributes
|
|
38
|
-
// /** The value that will be passed to `id` HTML attribute of form */
|
|
39
|
-
// id?: string | undefined;
|
|
40
|
-
// /** The value that will be passed to `name` HTML attribute of form */
|
|
41
|
-
// name?: string | undefined;
|
|
42
|
-
// /** The value that will be passed to `method` HTML attribute of form */
|
|
43
|
-
// method?: string | undefined;
|
|
44
|
-
// /** The value that will be passed to `target` HTML attribute of form */
|
|
45
|
-
// target?: string | undefined;
|
|
46
|
-
// /** The value that will be passed to `action` HTML attribute of form */
|
|
47
|
-
// action?: string | undefined;
|
|
48
|
-
// /** The value that will be passed to `autocomplete ` HTML attribute of form */
|
|
49
|
-
// autoComplete?: string | undefined;
|
|
50
|
-
// /** The value that will be passed to `enctype` HTML attribute of form */
|
|
51
|
-
// enctype?: string | undefined;
|
|
52
|
-
// /** The value that will be passed to `accept-charset` HTML attribute of form */
|
|
53
|
-
// acceptcharset?: string | undefined;
|
|
54
|
-
// /**
|
|
55
|
-
// * If set to true, extra form data values not in form field will be removed when `onSubmit` is called
|
|
56
|
-
// * @default false
|
|
57
|
-
// */
|
|
58
|
-
// omitExtraData?: boolean | undefined;
|
|
59
|
-
// /**
|
|
60
|
-
// * If `omitExtraData` and `liveOmit` are both set to true, then extra form data values that are not in any form field will be removed whenever `onChange` is called.
|
|
61
|
-
// * @default false
|
|
62
|
-
// */
|
|
63
|
-
// liveOmit?: boolean | undefined;
|
|
64
|
-
// /** Used to change the default `form` tag into a different HTML tag */
|
|
65
|
-
// tagName?: keyof JSX.IntrinsicElements | React.FunctionComponent | undefined;
|
|
66
|
-
}>;
|
|
67
|
-
|
|
68
|
-
export type Schema = JSONSchema7;
|
|
69
|
-
|
|
70
|
-
export type UiSchema = {
|
|
71
|
-
"ui:field"?: Field | string;
|
|
72
|
-
"ui:options"?: {
|
|
73
|
-
[key: string]: boolean | number | string | object | any[] | null;
|
|
74
|
-
};
|
|
75
|
-
"ui:props"?: { [key: string]: any };
|
|
76
|
-
"ui:help"?: React.ReactNode | React.ReactNode[];
|
|
77
|
-
// "ui:order"?: string[] | undefined;
|
|
78
|
-
// "ui:FieldTemplate"?: React.FunctionComponent<FieldTemplateProps> | undefined;
|
|
79
|
-
// "ui:ArrayFieldTemplate"?:
|
|
80
|
-
// | React.FunctionComponent<ArrayFieldTemplateProps>
|
|
81
|
-
// | undefined;
|
|
82
|
-
// "ui:ObjectFieldTemplate"?:
|
|
83
|
-
// | React.FunctionComponent<ObjectFieldTemplateProps>
|
|
84
|
-
// | undefined;
|
|
85
|
-
[name: string]: any;
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
export type FormRegistryContextValue = {
|
|
89
|
-
fields: Record<string, Field>;
|
|
90
|
-
templates: TemplatesType;
|
|
91
|
-
components: ComponentsType;
|
|
92
|
-
definitions: Record<string, any>;
|
|
93
|
-
formContext: Record<string, any>;
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
export interface FieldProps
|
|
97
|
-
extends Pick<
|
|
98
|
-
React.HTMLAttributes<HTMLElement>,
|
|
99
|
-
Exclude<keyof React.HTMLAttributes<HTMLElement>, "onBlur">
|
|
100
|
-
> {
|
|
101
|
-
name: string;
|
|
102
|
-
schema: Schema;
|
|
103
|
-
uiSchema: UiSchema;
|
|
104
|
-
// errorSchema: ErrorSchema;
|
|
105
|
-
// onChange: (e: IChangeEvent<T> | any, es?: ErrorSchema) => any;
|
|
106
|
-
// onBlur: (name: string, value: boolean | number | string | null) => void;
|
|
107
|
-
// autofocus: boolean;
|
|
108
|
-
disabled?: boolean;
|
|
109
|
-
readonly?: boolean;
|
|
110
|
-
required?: boolean;
|
|
111
|
-
[prop: string]: any;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
export type Field = React.FunctionComponent<FieldProps>;
|
|
115
|
-
|
|
116
|
-
/*** TEMPLATES ***/
|
|
117
|
-
export type FieldTemplateProps = React.PropsWithChildren<{
|
|
118
|
-
name: string;
|
|
119
|
-
schema: Schema;
|
|
120
|
-
uiSchema?: UiSchema;
|
|
121
|
-
hidden?: boolean;
|
|
122
|
-
required?: boolean;
|
|
123
|
-
readonly?: boolean;
|
|
124
|
-
disabled?: boolean;
|
|
125
|
-
}>;
|
|
126
|
-
|
|
127
|
-
export type ArrayFieldTemplateProps = React.PropsWithChildren<{
|
|
128
|
-
name: string;
|
|
129
|
-
schema: Schema;
|
|
130
|
-
uiSchema?: UiSchema;
|
|
131
|
-
hidden?: boolean;
|
|
132
|
-
required?: boolean;
|
|
133
|
-
readonly?: boolean;
|
|
134
|
-
disabled?: boolean;
|
|
135
|
-
// items: {
|
|
136
|
-
// children: React.ReactElement;
|
|
137
|
-
// className: string;
|
|
138
|
-
// disabled: boolean;
|
|
139
|
-
// hasMoveDown: boolean;
|
|
140
|
-
// hasMoveUp: boolean;
|
|
141
|
-
// hasRemove: boolean;
|
|
142
|
-
// hasToolbar: boolean;
|
|
143
|
-
// index: number;
|
|
144
|
-
// onDropIndexClick: (index: number) => (event: any) => void;
|
|
145
|
-
// onReorderClick: (index: number, newIndex: number) => (event: any) => void;
|
|
146
|
-
// readonly: boolean;
|
|
147
|
-
// key: string;
|
|
148
|
-
// }[];
|
|
149
|
-
}>;
|
|
150
|
-
|
|
151
|
-
export type ObjectFieldTemplateProps = {
|
|
152
|
-
schema: Schema;
|
|
153
|
-
uiSchema?: UiSchema;
|
|
154
|
-
required?: boolean;
|
|
155
|
-
children: React.ReactNode;
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
export type TemplatesType = {
|
|
159
|
-
FieldTemplate: React.FunctionComponent<FieldTemplateProps>;
|
|
160
|
-
ObjectFieldTemplate: React.FunctionComponent<ObjectFieldTemplateProps>;
|
|
161
|
-
ArrayFieldTemplate: React.FunctionComponent<ArrayFieldTemplateProps>;
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
/*** COMPONENTS ***/
|
|
165
|
-
export type ComponentsType = {
|
|
166
|
-
Form: React.FunctionComponent<any>;
|
|
167
|
-
FormButtonWrapper: React.FunctionComponent<any>;
|
|
168
|
-
FormButton: React.FunctionComponent<any>;
|
|
169
|
-
ObjectFieldWrapper: React.FunctionComponent<any>;
|
|
170
|
-
ObjectFieldTitle: React.FunctionComponent<any>;
|
|
171
|
-
ObjectFieldItem: React.FunctionComponent<any>;
|
|
172
|
-
ObjectFieldItemButtons: React.FunctionComponent<any>;
|
|
173
|
-
ObjectFieldList: React.FunctionComponent<any>;
|
|
174
|
-
ObjectFieldButton: React.FunctionComponent<ObjectFieldButtonProps>;
|
|
175
|
-
ArrayFieldWrapper: React.FunctionComponent<any>;
|
|
176
|
-
ArrayFieldTitle: React.FunctionComponent<any>;
|
|
177
|
-
ArrayFieldItem: React.FunctionComponent<any>;
|
|
178
|
-
ArrayFieldItemButtons: React.FunctionComponent<any>;
|
|
179
|
-
ArrayFieldList: React.FunctionComponent<any>;
|
|
180
|
-
ArrayFieldButton: React.FunctionComponent<ArrayFieldButtonProps>;
|
|
181
|
-
FieldWrapper: React.FunctionComponent<any>;
|
|
182
|
-
FieldTitle: React.FunctionComponent<any>;
|
|
183
|
-
FieldDescription: React.FunctionComponent<any>;
|
|
184
|
-
FieldHelp: React.FunctionComponent<any>;
|
|
185
|
-
ErrorMessage: React.FunctionComponent<any>;
|
|
186
|
-
FieldInput: React.FunctionComponent<any>;
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
export enum ObjectFieldButtonPurpose {
|
|
190
|
-
ADD,
|
|
191
|
-
REMOVE,
|
|
192
|
-
MOVE_UP,
|
|
193
|
-
MOVE_DOWN,
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
export type ObjectFieldButtonProps = {
|
|
197
|
-
purpose: ObjectFieldButtonPurpose;
|
|
198
|
-
onClick: () => void;
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
export enum ArrayFieldButtonPurpose {
|
|
202
|
-
ADD,
|
|
203
|
-
REMOVE,
|
|
204
|
-
MOVE_UP,
|
|
205
|
-
MOVE_DOWN,
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
export type ArrayFieldButtonProps = {
|
|
209
|
-
purpose: ArrayFieldButtonPurpose;
|
|
210
|
-
onClick: () => void;
|
|
211
|
-
};
|
|
212
|
-
/*** ***/
|
|
213
|
-
|
|
214
|
-
export type AjvError = {
|
|
215
|
-
message: string;
|
|
216
|
-
name: string;
|
|
217
|
-
params: any;
|
|
218
|
-
property: string;
|
|
219
|
-
stack: string;
|
|
220
|
-
};
|
|
221
|
-
|
|
222
|
-
export type ErrorListProps = {
|
|
223
|
-
errorSchema: FormValidation;
|
|
224
|
-
errors: AjvError[];
|
|
225
|
-
schema: Schema;
|
|
226
|
-
uiSchema: UiSchema;
|
|
227
|
-
};
|
|
228
|
-
|
|
229
|
-
export type IChangeEvent<T = any> = {
|
|
230
|
-
edit: boolean;
|
|
231
|
-
formData: T;
|
|
232
|
-
errors: AjvError[];
|
|
233
|
-
errorSchema: FormValidation;
|
|
234
|
-
schema: Schema;
|
|
235
|
-
uiSchema: UiSchema;
|
|
236
|
-
status?: string | undefined;
|
|
237
|
-
};
|
|
238
|
-
|
|
239
|
-
export type ISubmitEvent<T> = IChangeEvent<T>;
|
|
240
|
-
|
|
241
|
-
export type FieldError = string;
|
|
242
|
-
|
|
243
|
-
type FieldValidation = {
|
|
244
|
-
__errors: FieldError[];
|
|
245
|
-
addError: (message: string) => void;
|
|
246
|
-
};
|
|
247
|
-
|
|
248
|
-
type FormValidation = FieldValidation & {
|
|
249
|
-
[fieldName: string]: FieldValidation;
|
|
250
|
-
};
|
|
251
|
-
|
|
252
|
-
type FormSubmit<T = any> = {
|
|
253
|
-
formData: T;
|
|
254
|
-
};
|
|
255
|
-
|
|
256
|
-
export type AddButtonProps = {
|
|
257
|
-
className: string;
|
|
258
|
-
onClick: (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void;
|
|
259
|
-
disabled: boolean;
|
|
260
|
-
};
|