@nolwenture/formik-jsonschema-form 0.4.1 → 0.5.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 +113 -0
- package/dist/Form.js +13 -24
- package/dist/Form.stories.js +98 -85
- package/dist/Form.test.js +2 -2
- package/dist/FormEditor.js +11 -56
- package/dist/FormEditor.stories.js +2 -13
- package/dist/_sampleData.d.ts +1 -0
- package/dist/_sampleData.js +67 -12
- package/dist/fields/AnyField.js +8 -19
- package/dist/fields/ArrayField.js +32 -54
- package/dist/fields/BooleanField.js +4 -15
- package/dist/fields/DateField.js +4 -15
- package/dist/fields/NullField.js +3 -14
- package/dist/fields/NumberField.js +1 -12
- package/dist/fields/ObjectField.js +18 -41
- package/dist/fields/RadioField.d.ts +3 -0
- package/dist/fields/RadioField.js +11 -0
- package/dist/fields/SchemaField.js +7 -18
- package/dist/fields/SearchMultiTagSelectField.d.ts +3 -0
- package/dist/fields/SearchMultiTagSelectField.js +13 -0
- package/dist/fields/StringField.js +9 -49
- package/dist/fields/ToggleField.d.ts +3 -0
- package/dist/fields/ToggleField.js +11 -0
- package/dist/fields/index.d.ts +3 -0
- package/dist/fields/index.js +3 -0
- package/dist/fields/types.js +1 -1
- package/dist/formEditorSchemas.js +3 -3
- package/dist/styledComponents.d.ts +50 -1
- package/dist/styledComponents.js +340 -135
- package/dist/templates/ArrayFieldTemplate.js +8 -20
- package/dist/templates/FieldTemplate.js +7 -20
- package/dist/templates/ObjectFieldTemplate.js +10 -20
- package/dist/types.d.ts +32 -1
- package/dist/utils.d.ts +7 -0
- package/dist/utils.js +50 -34
- package/package.json +1 -1
|
@@ -1,27 +1,17 @@
|
|
|
1
|
-
var __assign = (this && this.__assign) || function () {
|
|
2
|
-
__assign = Object.assign || function(t) {
|
|
3
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
-
s = arguments[i];
|
|
5
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
-
t[p] = s[p];
|
|
7
|
-
}
|
|
8
|
-
return t;
|
|
9
|
-
};
|
|
10
|
-
return __assign.apply(this, arguments);
|
|
11
|
-
};
|
|
12
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
13
2
|
import { useContext } from "react";
|
|
14
3
|
import { FormRegistry } from "../Form";
|
|
15
4
|
import { getDescription, getTitle, useExtractUiSchema } from "../utils";
|
|
5
|
+
import { RequiredIndicator } from "../styledComponents";
|
|
16
6
|
export function ObjectFieldTemplate(props) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
7
|
+
const { components: { FieldDescription, ObjectFieldTitle, ObjectFieldWrapper, FieldHelp, InfoFieldButton, }, } = useContext(FormRegistry);
|
|
8
|
+
const { children, uiSchema } = props;
|
|
9
|
+
const title = getTitle(props);
|
|
10
|
+
const description = getDescription(props);
|
|
11
|
+
const { help, info,
|
|
22
12
|
//TODO: This is not right
|
|
23
|
-
|
|
24
|
-
//
|
|
25
|
-
|
|
26
|
-
return (_jsxs(ObjectFieldWrapper,
|
|
13
|
+
props: { wrapper: wrapperProps, title: titleProps, description: descriptionProps, help: helpProps,
|
|
14
|
+
// error: errorProps,
|
|
15
|
+
} = {}, } = useExtractUiSchema(uiSchema);
|
|
16
|
+
return (_jsxs(ObjectFieldWrapper, Object.assign({}, wrapperProps, { children: [title && (_jsxs(ObjectFieldTitle, Object.assign({}, titleProps, { children: [title, (props === null || props === void 0 ? void 0 : props.required) && _jsx(RequiredIndicator, { children: "*" })] }))), info && _jsx(InfoFieldButton, Object.assign({}, info)), description && (_jsx(FieldDescription, Object.assign({}, descriptionProps, { children: description }))), children, help && _jsx(FieldHelp, Object.assign({}, helpProps, { children: help }))] })));
|
|
27
17
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -37,6 +37,11 @@ export type UiSchema = {
|
|
|
37
37
|
};
|
|
38
38
|
"ui:help"?: React.ReactNode | React.ReactNode[];
|
|
39
39
|
"ui:info"?: InfoFieldButtonProps;
|
|
40
|
+
"ui:placeholderOptionOptions"?: {
|
|
41
|
+
props: {
|
|
42
|
+
[key: string]: any;
|
|
43
|
+
};
|
|
44
|
+
};
|
|
40
45
|
[name: string]: any;
|
|
41
46
|
};
|
|
42
47
|
export type FormRegistryContextValue = {
|
|
@@ -121,6 +126,9 @@ export type ComponentsType = {
|
|
|
121
126
|
ErrorMessage: React.FunctionComponent<any>;
|
|
122
127
|
FieldInput: React.FunctionComponent<any>;
|
|
123
128
|
RequiredIndicator: React.FunctionComponent<any>;
|
|
129
|
+
RadioButtonField: React.FunctionComponent<any>;
|
|
130
|
+
ToggleSwitchField: React.FunctionComponent<any>;
|
|
131
|
+
SearchableMultiTagSelectField: React.FunctionComponent<any>;
|
|
124
132
|
};
|
|
125
133
|
export declare enum ObjectFieldButtonPurpose {
|
|
126
134
|
ADD = 0,
|
|
@@ -147,10 +155,33 @@ export type ArrayFieldButtonProps = {
|
|
|
147
155
|
export type InfoFieldButtonProps = {
|
|
148
156
|
label?: React.ReactNode;
|
|
149
157
|
content: React.ReactNode;
|
|
150
|
-
position?:
|
|
158
|
+
position?: "top" | "right" | "bottom" | "left";
|
|
151
159
|
className?: string;
|
|
152
160
|
props: any;
|
|
153
161
|
};
|
|
162
|
+
type RadioButtonLabelObject = {
|
|
163
|
+
true?: string;
|
|
164
|
+
false?: string;
|
|
165
|
+
};
|
|
166
|
+
export type RadioButtonFieldProps = {
|
|
167
|
+
label?: RadioButtonLabelObject;
|
|
168
|
+
inputProps?: any;
|
|
169
|
+
name?: React.ReactNode;
|
|
170
|
+
className?: string;
|
|
171
|
+
};
|
|
172
|
+
export type ToggleSwitchFieldProps = {
|
|
173
|
+
inputProps: any;
|
|
174
|
+
name: React.ReactNode;
|
|
175
|
+
className?: string;
|
|
176
|
+
};
|
|
177
|
+
export type SelectFieldProps = {
|
|
178
|
+
name: string;
|
|
179
|
+
enums: any;
|
|
180
|
+
emptySearchMessage?: string;
|
|
181
|
+
placeholder?: string;
|
|
182
|
+
closeElement?: React.ReactNode;
|
|
183
|
+
className?: string;
|
|
184
|
+
};
|
|
154
185
|
/*** ***/
|
|
155
186
|
export type AjvError = {
|
|
156
187
|
message: string;
|
package/dist/utils.d.ts
CHANGED
|
@@ -11,6 +11,13 @@ export declare function getDescription({ description, uiSchema, schema, }: {
|
|
|
11
11
|
uiSchema?: UiSchema;
|
|
12
12
|
schema?: Schema;
|
|
13
13
|
}): any;
|
|
14
|
+
export declare function getEnums({ schema }: FieldProps): {
|
|
15
|
+
type: string;
|
|
16
|
+
values: {
|
|
17
|
+
const: any;
|
|
18
|
+
title: string;
|
|
19
|
+
}[];
|
|
20
|
+
} | undefined;
|
|
14
21
|
export declare function getDefaultValueForType(type: JSONSchema7["type"]): {} | null;
|
|
15
22
|
export declare function useSetDefault(props: FieldProps): void;
|
|
16
23
|
export declare function useExtractUiSchema(props?: UiSchema | undefined): {
|
package/dist/utils.js
CHANGED
|
@@ -1,14 +1,3 @@
|
|
|
1
|
-
var __assign = (this && this.__assign) || function () {
|
|
2
|
-
__assign = Object.assign || function(t) {
|
|
3
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
-
s = arguments[i];
|
|
5
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
-
t[p] = s[p];
|
|
7
|
-
}
|
|
8
|
-
return t;
|
|
9
|
-
};
|
|
10
|
-
return __assign.apply(this, arguments);
|
|
11
|
-
};
|
|
12
1
|
var __rest = (this && this.__rest) || function (s, e) {
|
|
13
2
|
var t = {};
|
|
14
3
|
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
@@ -22,23 +11,51 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
22
11
|
};
|
|
23
12
|
import { useField } from "formik";
|
|
24
13
|
import { useEffect, useMemo } from "react";
|
|
25
|
-
export function getTitle(
|
|
26
|
-
var
|
|
27
|
-
var title = _a.title, uiSchema = _a.uiSchema, schema = _a.schema, name = _a.name;
|
|
14
|
+
export function getTitle({ title, uiSchema, schema, name, }) {
|
|
15
|
+
var _a;
|
|
28
16
|
return (title ||
|
|
29
|
-
((
|
|
17
|
+
((_a = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:options"]) === null || _a === void 0 ? void 0 : _a.title) ||
|
|
30
18
|
(uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:title"]) ||
|
|
31
19
|
(schema === null || schema === void 0 ? void 0 : schema.title) ||
|
|
32
20
|
(name === null || name === void 0 ? void 0 : name.split(".")[(name === null || name === void 0 ? void 0 : name.split(".").length) - 1]));
|
|
33
21
|
}
|
|
34
|
-
export function getDescription(
|
|
35
|
-
var
|
|
36
|
-
var description = _a.description, uiSchema = _a.uiSchema, schema = _a.schema;
|
|
22
|
+
export function getDescription({ description, uiSchema, schema, }) {
|
|
23
|
+
var _a;
|
|
37
24
|
return (description ||
|
|
38
|
-
((
|
|
25
|
+
((_a = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:options"]) === null || _a === void 0 ? void 0 : _a.description) ||
|
|
39
26
|
(uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:description"]) ||
|
|
40
27
|
(schema === null || schema === void 0 ? void 0 : schema.description));
|
|
41
28
|
}
|
|
29
|
+
export function getEnums({ schema }) {
|
|
30
|
+
const numberOfDefined = [schema.enum, schema.oneOf, schema.anyOf].filter((x) => x).length;
|
|
31
|
+
if (numberOfDefined === 0) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
if (numberOfDefined > 1) {
|
|
35
|
+
throw new Error('Only one of "enum", "oneOf" or "anyOf" can be defined!');
|
|
36
|
+
}
|
|
37
|
+
if (schema.enum) {
|
|
38
|
+
return {
|
|
39
|
+
type: "select",
|
|
40
|
+
values: schema.enum.map((option) => ({
|
|
41
|
+
const: option,
|
|
42
|
+
title: `${option}`,
|
|
43
|
+
})),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
if (schema.oneOf) {
|
|
47
|
+
return {
|
|
48
|
+
type: "select",
|
|
49
|
+
values: schema.oneOf,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
if (schema.anyOf) {
|
|
53
|
+
return {
|
|
54
|
+
type: "multiSelect",
|
|
55
|
+
values: schema.anyOf,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
}
|
|
42
59
|
export function getDefaultValueForType(type) {
|
|
43
60
|
if (type && !Array.isArray(type)) {
|
|
44
61
|
if (type === "array")
|
|
@@ -49,8 +66,8 @@ export function getDefaultValueForType(type) {
|
|
|
49
66
|
return null;
|
|
50
67
|
}
|
|
51
68
|
export function useSetDefault(props) {
|
|
52
|
-
|
|
53
|
-
useEffect(
|
|
69
|
+
const [{ value }, _, { setValue }] = useField(props.name);
|
|
70
|
+
useEffect(() => {
|
|
54
71
|
if (!value) {
|
|
55
72
|
if (props.schema.default) {
|
|
56
73
|
setValue(props.schema.default);
|
|
@@ -61,17 +78,16 @@ export function useSetDefault(props) {
|
|
|
61
78
|
}
|
|
62
79
|
}, []);
|
|
63
80
|
}
|
|
64
|
-
export function useExtractUiSchema(props) {
|
|
65
|
-
if (props
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}, {})), options);
|
|
81
|
+
export function useExtractUiSchema(props = {}) {
|
|
82
|
+
if (props.props !== undefined) {
|
|
83
|
+
props["ui:props"] = Object.assign(Object.assign({}, props["ui:props"]), props.props);
|
|
84
|
+
}
|
|
85
|
+
return useMemo(() => {
|
|
86
|
+
const { "ui:options": options = {} } = props, uiSchema = __rest(props, ["ui:options"]);
|
|
87
|
+
return Object.assign(Object.assign({}, Object.keys(uiSchema || {}).reduce((result, key) => (Object.assign(Object.assign({}, result), { [key.replace("ui:", "")]: uiSchema[key] })), {})), options);
|
|
72
88
|
}, [props]);
|
|
73
89
|
}
|
|
74
|
-
export
|
|
90
|
+
export const guessType = function guessType(value) {
|
|
75
91
|
if (Array.isArray(value)) {
|
|
76
92
|
return "array";
|
|
77
93
|
}
|
|
@@ -97,7 +113,7 @@ export function getSchemaType(schema) {
|
|
|
97
113
|
if (!schema) {
|
|
98
114
|
return undefined;
|
|
99
115
|
}
|
|
100
|
-
|
|
116
|
+
let { type } = schema;
|
|
101
117
|
if (!type && schema.const) {
|
|
102
118
|
return guessType(schema.const);
|
|
103
119
|
}
|
|
@@ -108,15 +124,15 @@ export function getSchemaType(schema) {
|
|
|
108
124
|
return "object";
|
|
109
125
|
}
|
|
110
126
|
if (type instanceof Array && type.length === 2 && type.includes("null")) {
|
|
111
|
-
return type.find(
|
|
127
|
+
return type.find((type) => type !== "null");
|
|
112
128
|
}
|
|
113
129
|
return type;
|
|
114
130
|
}
|
|
115
131
|
//TODO: REMOVE WHEN ALL SUPPORTED
|
|
116
132
|
export function warnFromUnsupportedKeys(componentName, data, supportedKeys) {
|
|
117
|
-
Object.keys(data).forEach(
|
|
133
|
+
Object.keys(data).forEach((key) => {
|
|
118
134
|
if (!supportedKeys.includes(key)) {
|
|
119
|
-
console.warn(
|
|
135
|
+
console.warn(`Component "${componentName}" does not support (yet) key "${key}"`);
|
|
120
136
|
}
|
|
121
137
|
});
|
|
122
138
|
}
|