@nolwenture/formik-jsonschema-form 0.1.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/.storybook/main.js +13 -0
- package/.storybook/preview.js +9 -0
- package/README.md +46 -0
- package/package.json +90 -0
- package/src/Form.stories.tsx +124 -0
- package/src/Form.test.tsx +10 -0
- package/src/Form.tsx +71 -0
- package/src/FormEditor.stories.tsx +17 -0
- package/src/FormEditor.tsx +53 -0
- package/src/_sampleData.ts +273 -0
- package/src/fields/AnyField.tsx +35 -0
- package/src/fields/ArrayField.tsx +177 -0
- package/src/fields/BooleanField.tsx +23 -0
- package/src/fields/DateField.tsx +20 -0
- package/src/fields/NullField.tsx +12 -0
- package/src/fields/NumberField.tsx +14 -0
- package/src/fields/ObjectField.tsx +68 -0
- package/src/fields/SchemaField.tsx +37 -0
- package/src/fields/StringField.tsx +89 -0
- package/src/fields/index.ts +9 -0
- package/src/fields/types.tsx +10 -0
- package/src/formEditorSchemas.ts +40 -0
- package/src/index.tsx +5 -0
- package/src/setupTests.ts +5 -0
- package/src/styledComponents.tsx +107 -0
- package/src/templates/ArrayFieldTemplate.tsx +46 -0
- package/src/templates/FieldTemplate.tsx +46 -0
- package/src/templates/ObjectFieldTemplate.tsx +22 -0
- package/src/templates/index.ts +3 -0
- package/src/types.ts +260 -0
- package/src/utils.ts +124 -0
- package/tsconfig.json +26 -0
package/src/utils.ts
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { useField } from "formik";
|
|
2
|
+
import { useEffect, useMemo } from "react";
|
|
3
|
+
import { FieldProps, Schema, UiSchema } from "./types";
|
|
4
|
+
|
|
5
|
+
export function getTitle({
|
|
6
|
+
title,
|
|
7
|
+
uiSchema,
|
|
8
|
+
schema,
|
|
9
|
+
}: {
|
|
10
|
+
title?: string;
|
|
11
|
+
uiSchema?: UiSchema;
|
|
12
|
+
schema?: Schema;
|
|
13
|
+
}) {
|
|
14
|
+
return (
|
|
15
|
+
title ||
|
|
16
|
+
uiSchema?.["ui:options"]?.title ||
|
|
17
|
+
uiSchema?.["ui:title"] ||
|
|
18
|
+
schema?.title
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
export function getDescription({
|
|
22
|
+
description,
|
|
23
|
+
uiSchema,
|
|
24
|
+
schema,
|
|
25
|
+
}: {
|
|
26
|
+
description?: string;
|
|
27
|
+
uiSchema?: UiSchema;
|
|
28
|
+
schema?: Schema;
|
|
29
|
+
}) {
|
|
30
|
+
return (
|
|
31
|
+
description ||
|
|
32
|
+
uiSchema?.["ui:options"]?.description ||
|
|
33
|
+
uiSchema?.["ui:description"] ||
|
|
34
|
+
schema?.description
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function useSetDefault(props: FieldProps) {
|
|
39
|
+
const [{ value }, _, { setValue }] = useField(props.name);
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
if (!value && props.schema.default) {
|
|
42
|
+
setValue(props.schema.default);
|
|
43
|
+
}
|
|
44
|
+
}, []);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function useExtractUiSchema(props: UiSchema | undefined = {}): {
|
|
48
|
+
[name: string]: any;
|
|
49
|
+
field?: UiSchema["ui:field"];
|
|
50
|
+
props?: UiSchema["ui:props"];
|
|
51
|
+
help?: UiSchema["ui:help"];
|
|
52
|
+
} {
|
|
53
|
+
return useMemo(() => {
|
|
54
|
+
const { "ui:options": options = {}, ...uiSchema } = props;
|
|
55
|
+
return {
|
|
56
|
+
...Object.keys(uiSchema || {}).reduce(
|
|
57
|
+
(result, key) => ({
|
|
58
|
+
...result,
|
|
59
|
+
[key.replace("ui:", "")]: uiSchema[key],
|
|
60
|
+
}),
|
|
61
|
+
{}
|
|
62
|
+
),
|
|
63
|
+
...options,
|
|
64
|
+
};
|
|
65
|
+
}, [props]);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export const guessType = function guessType(value: any) {
|
|
69
|
+
if (Array.isArray(value)) {
|
|
70
|
+
return "array";
|
|
71
|
+
} else if (typeof value === "string") {
|
|
72
|
+
return "string";
|
|
73
|
+
} else if (value == null) {
|
|
74
|
+
return "null";
|
|
75
|
+
} else if (typeof value === "boolean") {
|
|
76
|
+
return "boolean";
|
|
77
|
+
} else if (!isNaN(value)) {
|
|
78
|
+
return "number";
|
|
79
|
+
} else if (typeof value === "object") {
|
|
80
|
+
return "object";
|
|
81
|
+
}
|
|
82
|
+
// Default to string if we can't figure it out
|
|
83
|
+
return "string";
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export function getSchemaType(schema: Schema) {
|
|
87
|
+
if (!schema) {
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
let { type } = schema;
|
|
91
|
+
|
|
92
|
+
if (!type && schema.const) {
|
|
93
|
+
return guessType(schema.const);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (!type && schema.enum) {
|
|
97
|
+
return "string";
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (!type && (schema.properties || schema.additionalProperties)) {
|
|
101
|
+
return "object";
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (type instanceof Array && type.length === 2 && type.includes("null")) {
|
|
105
|
+
return type.find((type) => type !== "null");
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return type;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
//TODO: REMOVE WHEN ALL SUPPORTED
|
|
112
|
+
export function warnFromUnsupportedKeys(
|
|
113
|
+
componentName: string,
|
|
114
|
+
data: Record<string, any>,
|
|
115
|
+
supportedKeys: string[]
|
|
116
|
+
) {
|
|
117
|
+
Object.keys(data).forEach((key) => {
|
|
118
|
+
if (!supportedKeys.includes(key)) {
|
|
119
|
+
console.warn(
|
|
120
|
+
`Component "${componentName}" does not support (yet) key "${key}"`
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es5",
|
|
4
|
+
"lib": [
|
|
5
|
+
"dom",
|
|
6
|
+
"dom.iterable",
|
|
7
|
+
"esnext"
|
|
8
|
+
],
|
|
9
|
+
"allowJs": true,
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"esModuleInterop": true,
|
|
12
|
+
"allowSyntheticDefaultImports": true,
|
|
13
|
+
"strict": true,
|
|
14
|
+
"forceConsistentCasingInFileNames": true,
|
|
15
|
+
"noFallthroughCasesInSwitch": true,
|
|
16
|
+
"module": "esnext",
|
|
17
|
+
"moduleResolution": "node",
|
|
18
|
+
"resolveJsonModule": true,
|
|
19
|
+
"isolatedModules": true,
|
|
20
|
+
"noEmit": true,
|
|
21
|
+
"jsx": "react-jsx"
|
|
22
|
+
},
|
|
23
|
+
"include": [
|
|
24
|
+
"src"
|
|
25
|
+
]
|
|
26
|
+
}
|