@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
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
stories: ["../src/**/*.stories.mdx", "../src/**/*.stories.@(js|jsx|ts|tsx)"],
|
|
3
|
+
addons: [
|
|
4
|
+
"@storybook/addon-links",
|
|
5
|
+
"@storybook/addon-essentials",
|
|
6
|
+
"@storybook/addon-interactions",
|
|
7
|
+
"@storybook/preset-create-react-app",
|
|
8
|
+
],
|
|
9
|
+
framework: "@storybook/react",
|
|
10
|
+
core: {
|
|
11
|
+
builder: "@storybook/builder-webpack5",
|
|
12
|
+
},
|
|
13
|
+
};
|
package/README.md
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# Getting Started with Create React App
|
|
2
|
+
|
|
3
|
+
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
|
|
4
|
+
|
|
5
|
+
## Available Scripts
|
|
6
|
+
|
|
7
|
+
In the project directory, you can run:
|
|
8
|
+
|
|
9
|
+
### `npm start`
|
|
10
|
+
|
|
11
|
+
Runs the app in the development mode.\
|
|
12
|
+
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
|
|
13
|
+
|
|
14
|
+
The page will reload if you make edits.\
|
|
15
|
+
You will also see any lint errors in the console.
|
|
16
|
+
|
|
17
|
+
### `npm test`
|
|
18
|
+
|
|
19
|
+
Launches the test runner in the interactive watch mode.\
|
|
20
|
+
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
|
|
21
|
+
|
|
22
|
+
### `npm run build`
|
|
23
|
+
|
|
24
|
+
Builds the app for production to the `build` folder.\
|
|
25
|
+
It correctly bundles React in production mode and optimizes the build for the best performance.
|
|
26
|
+
|
|
27
|
+
The build is minified and the filenames include the hashes.\
|
|
28
|
+
Your app is ready to be deployed!
|
|
29
|
+
|
|
30
|
+
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
|
|
31
|
+
|
|
32
|
+
### `npm run eject`
|
|
33
|
+
|
|
34
|
+
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
|
|
35
|
+
|
|
36
|
+
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
|
|
37
|
+
|
|
38
|
+
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
|
|
39
|
+
|
|
40
|
+
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
|
|
41
|
+
|
|
42
|
+
## Learn More
|
|
43
|
+
|
|
44
|
+
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
|
|
45
|
+
|
|
46
|
+
To learn React, check out the [React documentation](https://reactjs.org/).
|
package/package.json
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nolwenture/formik-jsonschema-form",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Formik form generation from json schema",
|
|
5
|
+
"author": "Antti Vikman <antti.vikman@nolwenture.com>",
|
|
6
|
+
"contributors": [
|
|
7
|
+
"Antti Vikman <antti.vikman@nolwenture.com>"
|
|
8
|
+
],
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/nolwenture/formik-jsonschema-form.git"
|
|
13
|
+
},
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/nolwenture/formik-jsonschema-form/issues"
|
|
16
|
+
},
|
|
17
|
+
"homepage": "https://github.com/nolwenture/formik-jsonschema-form#readme",
|
|
18
|
+
"keywords": [
|
|
19
|
+
"formik",
|
|
20
|
+
"jsonschema"
|
|
21
|
+
],
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@testing-library/jest-dom": "^5.16.4",
|
|
24
|
+
"@testing-library/react": "^13.3.0",
|
|
25
|
+
"@testing-library/user-event": "^13.5.0",
|
|
26
|
+
"@types/jest": "^27.5.2",
|
|
27
|
+
"@types/node": "^16.11.45",
|
|
28
|
+
"@types/react": "^18.0.15",
|
|
29
|
+
"@types/react-dom": "^18.0.6",
|
|
30
|
+
"@types/styled-components": "^5.1.25",
|
|
31
|
+
"formik": "^2.2.9",
|
|
32
|
+
"json-schema": "^0.4.0",
|
|
33
|
+
"react": "^18.2.0",
|
|
34
|
+
"react-dom": "^18.2.0",
|
|
35
|
+
"react-scripts": "5.0.1",
|
|
36
|
+
"styled-components": "^5.3.5",
|
|
37
|
+
"typescript": "^4.7.4",
|
|
38
|
+
"web-vitals": "^2.1.4"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"start": "react-scripts start",
|
|
42
|
+
"build": "react-scripts build",
|
|
43
|
+
"test": "react-scripts test",
|
|
44
|
+
"eject": "react-scripts eject",
|
|
45
|
+
"storybook": "start-storybook -p 6006",
|
|
46
|
+
"build-storybook": "build-storybook"
|
|
47
|
+
},
|
|
48
|
+
"eslintConfig": {
|
|
49
|
+
"extends": [
|
|
50
|
+
"react-app",
|
|
51
|
+
"react-app/jest"
|
|
52
|
+
],
|
|
53
|
+
"overrides": [
|
|
54
|
+
{
|
|
55
|
+
"files": [
|
|
56
|
+
"**/*.stories.*"
|
|
57
|
+
],
|
|
58
|
+
"rules": {
|
|
59
|
+
"import/no-anonymous-default-export": "off"
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
]
|
|
63
|
+
},
|
|
64
|
+
"browserslist": {
|
|
65
|
+
"production": [
|
|
66
|
+
">0.2%",
|
|
67
|
+
"not dead",
|
|
68
|
+
"not op_mini all"
|
|
69
|
+
],
|
|
70
|
+
"development": [
|
|
71
|
+
"last 1 chrome version",
|
|
72
|
+
"last 1 firefox version",
|
|
73
|
+
"last 1 safari version"
|
|
74
|
+
]
|
|
75
|
+
},
|
|
76
|
+
"devDependencies": {
|
|
77
|
+
"@storybook/addon-essentials": "^6.5.9",
|
|
78
|
+
"@storybook/addon-interactions": "^6.5.9",
|
|
79
|
+
"@storybook/addon-links": "^6.5.9",
|
|
80
|
+
"@storybook/builder-webpack5": "^6.5.9",
|
|
81
|
+
"@storybook/manager-webpack5": "^6.5.9",
|
|
82
|
+
"@storybook/node-logger": "^6.5.9",
|
|
83
|
+
"@storybook/preset-create-react-app": "^4.1.2",
|
|
84
|
+
"@storybook/react": "^6.5.9",
|
|
85
|
+
"@storybook/testing-library": "^0.0.13",
|
|
86
|
+
"babel-plugin-named-exports-order": "^0.0.2",
|
|
87
|
+
"prop-types": "^15.8.1",
|
|
88
|
+
"webpack": "^5.73.0"
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { ComponentStory, ComponentMeta } from "@storybook/react";
|
|
2
|
+
import { action } from "@storybook/addon-actions";
|
|
3
|
+
import styled from "styled-components";
|
|
4
|
+
import { Formik, useFormikContext } from "formik";
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
completeSchema,
|
|
8
|
+
completeUiSchema,
|
|
9
|
+
SimpleFormDataType,
|
|
10
|
+
simpleSchema,
|
|
11
|
+
simpleUiSchema,
|
|
12
|
+
generatedSchema,
|
|
13
|
+
} from "./_sampleData";
|
|
14
|
+
import { Form, styledComponents } from "./";
|
|
15
|
+
|
|
16
|
+
export default {
|
|
17
|
+
title: "Form",
|
|
18
|
+
component: Form,
|
|
19
|
+
// More on argTypes: https://storybook.js.org/docs/react/api/argtypes
|
|
20
|
+
argTypes: {
|
|
21
|
+
schema: { control: { type: "object" } },
|
|
22
|
+
uiSchema: { control: { type: "object" } },
|
|
23
|
+
},
|
|
24
|
+
} as ComponentMeta<typeof Form>;
|
|
25
|
+
|
|
26
|
+
const Template: ComponentStory<typeof Form> = (args) => (
|
|
27
|
+
<Formik
|
|
28
|
+
initialValues={{}}
|
|
29
|
+
onSubmit={async (values, formikHelpers) => {
|
|
30
|
+
action("onSubmit(_, formHelper)")(formikHelpers);
|
|
31
|
+
action("onSubmit(values)")(values);
|
|
32
|
+
formikHelpers.setSubmitting(false);
|
|
33
|
+
}}
|
|
34
|
+
>
|
|
35
|
+
<Form {...args} />
|
|
36
|
+
</Formik>
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
export const Simple = Template.bind({});
|
|
40
|
+
Simple.args = {
|
|
41
|
+
schema: simpleSchema,
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export const WithUiSchema = Template.bind({});
|
|
45
|
+
WithUiSchema.args = {
|
|
46
|
+
schema: simpleSchema,
|
|
47
|
+
uiSchema: simpleUiSchema,
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export const CompleteSchema = Template.bind({});
|
|
51
|
+
CompleteSchema.args = {
|
|
52
|
+
schema: completeSchema,
|
|
53
|
+
uiSchema: completeUiSchema,
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export const StrictlyTyped = () => {
|
|
57
|
+
return (
|
|
58
|
+
<Formik<SimpleFormDataType>
|
|
59
|
+
initialValues={{ firstName: "", lastName: "" }}
|
|
60
|
+
onSubmit={async (values, formikHelpers) => {
|
|
61
|
+
action("onSubmit(_, formHelper)")(formikHelpers);
|
|
62
|
+
action("onSubmit(values)")(values);
|
|
63
|
+
formikHelpers.setSubmitting(false);
|
|
64
|
+
}}
|
|
65
|
+
>
|
|
66
|
+
<Form<SimpleFormDataType> schema={simpleSchema} />
|
|
67
|
+
</Formik>
|
|
68
|
+
);
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/*** CUSTOMIZATION ***/
|
|
72
|
+
const CustomizedForm = styled(Form)`
|
|
73
|
+
${styledComponents.FieldTitle} {
|
|
74
|
+
color: red;
|
|
75
|
+
}
|
|
76
|
+
`;
|
|
77
|
+
|
|
78
|
+
const CustomizedFieldWrapper = styled.div`
|
|
79
|
+
outline: 2px solid
|
|
80
|
+
${(props) => (props.title === "ABC" ? "blue" : "transparent")};
|
|
81
|
+
`;
|
|
82
|
+
|
|
83
|
+
const CustomizedErrorMessage = (props: { name: keyof SimpleFormDataType }) => {
|
|
84
|
+
const { name } = props;
|
|
85
|
+
const { errors } = useFormikContext<SimpleFormDataType>();
|
|
86
|
+
const error = errors[name];
|
|
87
|
+
return error ? <p>{error}</p> : <p>Hurray! No errors</p>;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
export const StylesCustomized = () => {
|
|
91
|
+
return (
|
|
92
|
+
<Formik
|
|
93
|
+
initialValues={{}}
|
|
94
|
+
onSubmit={async (values, formikHelpers) => {
|
|
95
|
+
action("onSubmit(_, formHelper)")(formikHelpers);
|
|
96
|
+
action("onSubmit(values)")(values);
|
|
97
|
+
formikHelpers.setSubmitting(false);
|
|
98
|
+
}}
|
|
99
|
+
>
|
|
100
|
+
<CustomizedForm
|
|
101
|
+
schema={simpleSchema}
|
|
102
|
+
uiSchema={{
|
|
103
|
+
firstName: {
|
|
104
|
+
props: {
|
|
105
|
+
wrapper: {
|
|
106
|
+
title: "ABC",
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
}}
|
|
111
|
+
components={{
|
|
112
|
+
FieldWrapper: CustomizedFieldWrapper,
|
|
113
|
+
ErrorMessage: CustomizedErrorMessage,
|
|
114
|
+
}}
|
|
115
|
+
/>
|
|
116
|
+
</Formik>
|
|
117
|
+
);
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
export const WithGeneratedSchema = Template.bind({});
|
|
121
|
+
WithGeneratedSchema.args = {
|
|
122
|
+
schema: generatedSchema,
|
|
123
|
+
uiSchema: {},
|
|
124
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { render, screen } from "@testing-library/react";
|
|
3
|
+
import { Form } from "./Form";
|
|
4
|
+
import { SimpleFormDataType, simpleSchema } from "./_sampleData";
|
|
5
|
+
|
|
6
|
+
test("renders title", () => {
|
|
7
|
+
render(<Form<SimpleFormDataType> schema={simpleSchema} />);
|
|
8
|
+
const titleElement = screen.getByText(/TITLE/i);
|
|
9
|
+
expect(titleElement).toBeInTheDocument();
|
|
10
|
+
});
|
package/src/Form.tsx
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { FormProps, FormRegistryContextValue } from "./types";
|
|
3
|
+
import { useFormikContext } from "formik";
|
|
4
|
+
|
|
5
|
+
import * as defaultComponents from "./styledComponents";
|
|
6
|
+
import { warnFromUnsupportedKeys } from "./utils";
|
|
7
|
+
|
|
8
|
+
import * as defaultFields from "./fields";
|
|
9
|
+
import * as defaultTemplates from "./templates";
|
|
10
|
+
|
|
11
|
+
export const FormRegistry = React.createContext<FormRegistryContextValue>({
|
|
12
|
+
fields: defaultFields,
|
|
13
|
+
templates: defaultTemplates,
|
|
14
|
+
components: defaultComponents,
|
|
15
|
+
definitions: {},
|
|
16
|
+
formContext: {},
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
export function Form<T>(props: FormProps<T>) {
|
|
20
|
+
warnFromUnsupportedKeys("Form", props, [
|
|
21
|
+
"schema",
|
|
22
|
+
"onSubmit",
|
|
23
|
+
"uiSchema",
|
|
24
|
+
"className",
|
|
25
|
+
"formData",
|
|
26
|
+
"className",
|
|
27
|
+
"children",
|
|
28
|
+
"fields",
|
|
29
|
+
"formContext",
|
|
30
|
+
]);
|
|
31
|
+
const { isSubmitting } = useFormikContext<T>();
|
|
32
|
+
const {
|
|
33
|
+
schema,
|
|
34
|
+
uiSchema = {},
|
|
35
|
+
className,
|
|
36
|
+
children,
|
|
37
|
+
fields = {},
|
|
38
|
+
templates = {},
|
|
39
|
+
components = {},
|
|
40
|
+
formContext = {},
|
|
41
|
+
} = props;
|
|
42
|
+
|
|
43
|
+
const registry = {
|
|
44
|
+
fields: { ...defaultFields, ...fields },
|
|
45
|
+
templates: { ...defaultTemplates, ...templates },
|
|
46
|
+
components: { ...defaultComponents, ...components },
|
|
47
|
+
definitions: {},
|
|
48
|
+
formContext,
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const { Form: FormEl, FormButton, FormButtonWrapper } = registry.components;
|
|
52
|
+
|
|
53
|
+
const _SchemaField = registry.fields.SchemaField;
|
|
54
|
+
|
|
55
|
+
return (
|
|
56
|
+
<FormRegistry.Provider value={registry}>
|
|
57
|
+
<FormEl className={className}>
|
|
58
|
+
<_SchemaField name={""} schema={schema} uiSchema={uiSchema} />
|
|
59
|
+
{children ? (
|
|
60
|
+
children
|
|
61
|
+
) : (
|
|
62
|
+
<FormButtonWrapper>
|
|
63
|
+
<FormButton type="submit" disabled={isSubmitting}>
|
|
64
|
+
Send
|
|
65
|
+
</FormButton>
|
|
66
|
+
</FormButtonWrapper>
|
|
67
|
+
)}
|
|
68
|
+
</FormEl>
|
|
69
|
+
</FormRegistry.Provider>
|
|
70
|
+
);
|
|
71
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ComponentStory, ComponentMeta } from "@storybook/react";
|
|
2
|
+
import { action } from "@storybook/addon-actions";
|
|
3
|
+
import { FormEditor } from "./FormEditor";
|
|
4
|
+
|
|
5
|
+
export default {
|
|
6
|
+
title: "FormEditor",
|
|
7
|
+
component: FormEditor,
|
|
8
|
+
// More on argTypes: https://storybook.js.org/docs/react/api/argtypes
|
|
9
|
+
argTypes: {},
|
|
10
|
+
} as ComponentMeta<typeof FormEditor>;
|
|
11
|
+
|
|
12
|
+
const Template: ComponentStory<typeof FormEditor> = (args) => (
|
|
13
|
+
<FormEditor {...args} />
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
export const Default = Template.bind({});
|
|
17
|
+
Default.args = {};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Formik } from "formik";
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
import { Form } from "./Form";
|
|
4
|
+
import { Schema, UiSchema } from "./types";
|
|
5
|
+
import * as formEditorSchemas from "./formEditorSchemas";
|
|
6
|
+
|
|
7
|
+
export type FormEditorProps = {};
|
|
8
|
+
export type FormEditorValues = {
|
|
9
|
+
schema: Schema;
|
|
10
|
+
uiSchema: UiSchema;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export function FormEditor(props: FormEditorProps) {
|
|
14
|
+
const [editorGeneratedSchemas, setEditorGeneratedSchemas] =
|
|
15
|
+
useState<FormEditorValues>({
|
|
16
|
+
schema: {},
|
|
17
|
+
uiSchema: {},
|
|
18
|
+
});
|
|
19
|
+
useEffect(
|
|
20
|
+
() => console.log(editorGeneratedSchemas),
|
|
21
|
+
[editorGeneratedSchemas]
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
return (
|
|
25
|
+
<div style={{ display: "flex", justifyContent: "space-between" }}>
|
|
26
|
+
<div>
|
|
27
|
+
<h1>Editor</h1>
|
|
28
|
+
<Formik<FormEditorValues>
|
|
29
|
+
initialValues={{ schema: {}, uiSchema: {} }}
|
|
30
|
+
onSubmit={async (values, formikHelpers) => {
|
|
31
|
+
setEditorGeneratedSchemas(values);
|
|
32
|
+
formikHelpers.setSubmitting(false);
|
|
33
|
+
}}
|
|
34
|
+
>
|
|
35
|
+
<Form<FormEditorValues> {...formEditorSchemas} />
|
|
36
|
+
</Formik>
|
|
37
|
+
</div>
|
|
38
|
+
<div>
|
|
39
|
+
<h1>Preview</h1>
|
|
40
|
+
<Formik
|
|
41
|
+
initialValues={{}}
|
|
42
|
+
onSubmit={async (values, formikHelpers) => {
|
|
43
|
+
// action("onSubmit(_, formHelper)")(formikHelpers);
|
|
44
|
+
// action("onSubmit(values)")(values);
|
|
45
|
+
formikHelpers.setSubmitting(false);
|
|
46
|
+
}}
|
|
47
|
+
>
|
|
48
|
+
<Form {...editorGeneratedSchemas} />
|
|
49
|
+
</Formik>
|
|
50
|
+
</div>
|
|
51
|
+
</div>
|
|
52
|
+
);
|
|
53
|
+
}
|