@nolwenture/formik-jsonschema-form 0.14.0 → 0.16.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 +5 -0
- package/dist/fields/ArrayField.js +14 -28
- package/dist/fields/StringField.js +5 -1
- package/dist/sampleData/refDefSchema.js +12 -3
- package/dist/styledComponents.d.ts +2 -3
- package/dist/styledComponents.js +3 -20
- package/dist/types.d.ts +0 -11
- package/dist/types.js +0 -7
- package/dist/utils/__tests/solveReferences.test.js +16 -2
- package/dist/utils/__tests/testSchemas.js +4 -0
- package/dist/utils/createValidationSchema.js +8 -1
- package/dist/utils/getTypes.d.ts +1 -1
- package/dist/utils/orderByUiSchema.js +7 -49
- package/dist/utils/solveReferences.js +2 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -786,6 +786,8 @@ Schemas can be referenced. A referenced schema can be given to a property using
|
|
|
786
786
|
|
|
787
787
|
In order to give uiSchema to reused or recursive fields defined in `schema.definitons`, the uiSchema needs to be given to the matching prop of `definitions`-object. This way, the given uiProps are added to the referencing field, no matter how deeply nested it is inside the schema.
|
|
788
788
|
|
|
789
|
+
If certain aspects of a reused field need to be adapted, `uiSchema.definitions` of that field can be overridden inside `uiSchema` by giving the desired properties to the respective field key. In the following schema/uiSchema `petBirthDate` can be used as an example. The settings are overriding the title of the field from the original "When is your birthday?" to "When is the birthday of your pet?".
|
|
790
|
+
|
|
789
791
|
```tsx
|
|
790
792
|
const familyObjectField: Schema = {
|
|
791
793
|
type: "object",
|
|
@@ -822,6 +824,8 @@ const refDefSchema: Schema = {
|
|
|
822
824
|
},
|
|
823
825
|
},
|
|
824
826
|
},
|
|
827
|
+
pet: { type: "string", title: "Pet name" },
|
|
828
|
+
petBirthDate: { $ref: "#/definitions/birthDate" },
|
|
825
829
|
},
|
|
826
830
|
|
|
827
831
|
definitions: {
|
|
@@ -846,6 +850,7 @@ const refDefSchema: Schema = {
|
|
|
846
850
|
};
|
|
847
851
|
|
|
848
852
|
const refDefUiSchema: UiSchema = {
|
|
853
|
+
petBirthDate: { "ui:title": "When is the birthday of your pet?" },
|
|
849
854
|
definitions: {
|
|
850
855
|
birthDate: { "ui:field": "DateField" },
|
|
851
856
|
names: {
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { useContext, useMemo } from "react";
|
|
3
2
|
import { useField } from "formik";
|
|
3
|
+
import { useContext, useMemo } from "react";
|
|
4
4
|
import { FormRegistry } from "../Form";
|
|
5
|
-
import { ArrayFieldButtonPurpose } from "../types";
|
|
6
5
|
import { useExtractUiSchema, useSetDefault } from "../hooks";
|
|
7
|
-
import { FieldInput
|
|
6
|
+
import { FieldInput } from "../styledComponents";
|
|
8
7
|
import { FieldTemplate } from "../templates";
|
|
9
8
|
import { getEnums } from "../utils";
|
|
10
9
|
const appendArray = (values) => {
|
|
@@ -32,7 +31,8 @@ const moveInArray = (values, index, direction) => {
|
|
|
32
31
|
}
|
|
33
32
|
};
|
|
34
33
|
export const ArrayField = (props) => {
|
|
35
|
-
|
|
34
|
+
var _a, _b, _c, _d;
|
|
35
|
+
const { fields: { SchemaField }, templates: { ArrayFieldTemplate }, components: { ArrayFieldItem, ArrayFieldList, ArrayFieldItemButtons, ArrayFieldAddButton, ArrayFieldRemoveButton, ArrayFieldUpButton, ArrayFieldDownButton, }, useItemsAsAdditionalItems, } = useContext(FormRegistry);
|
|
36
36
|
useSetDefault(props);
|
|
37
37
|
const { name, schema, uiSchema } = props;
|
|
38
38
|
const { type, props: inputProps } = useExtractUiSchema(uiSchema);
|
|
@@ -98,28 +98,14 @@ export const ArrayField = (props) => {
|
|
|
98
98
|
// Backwards compatibility ends
|
|
99
99
|
const { disabled } = useExtractUiSchema(uiSchema);
|
|
100
100
|
const valueArray = Array.isArray(value) ? value : undefined;
|
|
101
|
-
return schema.anyOf !== undefined ? (_jsx(FieldTemplate, Object.assign({}, props, { children: _jsx(FieldInput, Object.assign({ type }, inputProps, { name: name, formikAs: "select", multiple: (enums === null || enums === void 0 ? void 0 : enums.type) === "multiSelect", children: enums === null || enums === void 0 ? void 0 : enums.values.map((option, index) => (_jsx("option", { value: option.const, children: option.title }, index))) })) }))) : (_jsxs(ArrayFieldTemplate, Object.assign({}, props, { children: [_jsxs(ArrayFieldList, { children: [(items || []).map((itemSchema, index) => (_jsx(ArrayFieldItem, { children: _jsx(SchemaField, { name: `${name}[${index}]`, schema: typeof itemSchema !== "boolean" ? itemSchema : {}, uiSchema: (uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema[index]) || (uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema.items),
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
index > 0 &&
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
onClick: () => setValue(moveInArray(value, index + items.length, -1)),
|
|
112
|
-
uiSchema,
|
|
113
|
-
}), value.length > 1 &&
|
|
114
|
-
index < (value === null || value === void 0 ? void 0 : value.slice(items.length).length) - 1 &&
|
|
115
|
-
GetArrayFieldButton({
|
|
116
|
-
purpose: ArrayFieldButtonPurpose.MOVE_DOWN,
|
|
117
|
-
onClick: () => setValue(moveInArray(value, index + items.length, 1)),
|
|
118
|
-
uiSchema,
|
|
119
|
-
})] })] }, index)))] }), additionalItems &&
|
|
120
|
-
GetArrayFieldButton({
|
|
121
|
-
purpose: ArrayFieldButtonPurpose.ADD,
|
|
122
|
-
onClick: () => setValue(appendArray(value)),
|
|
123
|
-
uiSchema,
|
|
124
|
-
})] })));
|
|
101
|
+
return schema.anyOf !== undefined ? (_jsx(FieldTemplate, Object.assign({}, props, { children: _jsx(FieldInput, Object.assign({ type }, inputProps, { name: name, formikAs: "select", multiple: (enums === null || enums === void 0 ? void 0 : enums.type) === "multiSelect", children: enums === null || enums === void 0 ? void 0 : enums.values.map((option, index) => (_jsx("option", { value: option.const, children: option.title }, index))) })) }))) : (_jsxs(ArrayFieldTemplate, Object.assign({}, props, { children: [_jsxs(ArrayFieldList, { children: [(items || []).map((itemSchema, index) => (_jsx(ArrayFieldItem, { children: _jsx(SchemaField, { name: `${name}[${index}]`, schema: typeof itemSchema !== "boolean" ? itemSchema : {}, uiSchema: (uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema[index]) || (uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema.items),
|
|
102
|
+
// "true" will prevent validation for required array fields
|
|
103
|
+
// since a value always exists in the array
|
|
104
|
+
forceDefaultValue: false }) }, index))), ((valueArray === null || valueArray === void 0 ? void 0 : valueArray.slice(items.length)) || []).map((_, index) => {
|
|
105
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
|
|
106
|
+
return (_jsxs(ArrayFieldItem, { children: [_jsx(SchemaField, { name: `${name}[${index + items.length}]`, schema: additionalItems && typeof additionalItems !== "boolean"
|
|
107
|
+
? additionalItems
|
|
108
|
+
: {}, uiSchema: Object.assign({ disabled }, ((uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema[index + items.length]) || (uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema.items))) }), _jsxs(ArrayFieldItemButtons, { children: [_jsx(ArrayFieldRemoveButton, Object.assign({}, ((_b = (_a = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:removeButtonOptions"]) === null || _a === void 0 ? void 0 : _a.props) !== null && _b !== void 0 ? _b : {}), { onClick: () => setValue(removeFromArray(value, index + items.length)), children: (_d = (_c = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:removeButtonOptions"]) === null || _c === void 0 ? void 0 : _c.content) !== null && _d !== void 0 ? _d : "-" })), value.length > 1 && index > 0 && (_jsx(ArrayFieldUpButton, Object.assign({}, ((_f = (_e = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:upButtonOptions"]) === null || _e === void 0 ? void 0 : _e.props) !== null && _f !== void 0 ? _f : {}), { onClick: () => setValue(moveInArray(value, index + items.length, -1)), children: (_h = (_g = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:upButtonOptions"]) === null || _g === void 0 ? void 0 : _g.content) !== null && _h !== void 0 ? _h : "↑" }))), value.length > 1 &&
|
|
109
|
+
index < (value === null || value === void 0 ? void 0 : value.slice(items.length).length) - 1 && (_jsx(ArrayFieldDownButton, Object.assign({}, ((_k = (_j = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:downButtonOptions"]) === null || _j === void 0 ? void 0 : _j.props) !== null && _k !== void 0 ? _k : {}), { onClick: () => setValue(moveInArray(value, index + items.length, 1)), children: (_m = (_l = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:downButtonOptions"]) === null || _l === void 0 ? void 0 : _l.content) !== null && _m !== void 0 ? _m : "↓" })))] })] }, index));
|
|
110
|
+
})] }), additionalItems && (_jsx(ArrayFieldAddButton, Object.assign({}, ((_b = (_a = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:addButtonOptions"]) === null || _a === void 0 ? void 0 : _a.props) !== null && _b !== void 0 ? _b : {}), { onClick: () => setValue(appendArray(value)), children: (_d = (_c = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:addButtonOptions"]) === null || _c === void 0 ? void 0 : _c.content) !== null && _d !== void 0 ? _d : "+" })))] })));
|
|
125
111
|
};
|
|
@@ -12,7 +12,11 @@ export const StringField = (props) => {
|
|
|
12
12
|
const enums = useMemo(() => getEnums(props), [props]);
|
|
13
13
|
switch (enums === null || enums === void 0 ? void 0 : enums.type) {
|
|
14
14
|
case "select":
|
|
15
|
-
return (_jsx(FieldTemplate, Object.assign({}, props, { children: _jsxs(FieldInput, Object.assign({ type }, inputProps, { name: name, formikAs: "select", children: [_jsx("option", Object.assign({ value: "",
|
|
15
|
+
return (_jsx(FieldTemplate, Object.assign({}, props, { children: _jsxs(FieldInput, Object.assign({ type }, inputProps, { name: name, formikAs: "select", children: [_jsx("option", Object.assign({ value: "",
|
|
16
|
+
// If both are true, the first enum will be "selected"
|
|
17
|
+
// and leads to annoying and misleading UX.
|
|
18
|
+
// These defaults will lead to an empty, "nothing selected" component.
|
|
19
|
+
disabled: false,
|
|
16
20
|
hidden: true }, (_a = uiSchema["ui:placeholderOptionOptions"]) === null || _a === void 0 ? void 0 : _a.props, { children: placeholder })), enums === null || enums === void 0 ? void 0 : enums.values.map((option, index) => (_jsx("option", { value: option.const, children: option.title }, index)))] })) })));
|
|
17
21
|
default:
|
|
18
22
|
return (_jsx(FieldTemplate, Object.assign({}, props, { children: _jsx(FieldInput, Object.assign({ type,
|
|
@@ -45,7 +45,10 @@ export const refDefSchema = {
|
|
|
45
45
|
$ref: "#/definitions/age",
|
|
46
46
|
},
|
|
47
47
|
birthDate: {
|
|
48
|
-
$ref: "#/definitions/
|
|
48
|
+
$ref: "#/definitions/someDate",
|
|
49
|
+
},
|
|
50
|
+
specialDate: {
|
|
51
|
+
$ref: "#/definitions/someDate",
|
|
49
52
|
},
|
|
50
53
|
creditCard: {
|
|
51
54
|
type: "number",
|
|
@@ -243,7 +246,7 @@ export const refDefSchema = {
|
|
|
243
246
|
},
|
|
244
247
|
},
|
|
245
248
|
age: ageFieldDefinition,
|
|
246
|
-
|
|
249
|
+
someDate: { type: "string", title: "When is your birthday?" },
|
|
247
250
|
middleName: { type: "string", title: " Middle name" },
|
|
248
251
|
},
|
|
249
252
|
};
|
|
@@ -252,6 +255,7 @@ export const refDefUiSchema = {
|
|
|
252
255
|
"names",
|
|
253
256
|
"age",
|
|
254
257
|
"birthDate",
|
|
258
|
+
"specialDate",
|
|
255
259
|
"family",
|
|
256
260
|
"creditCard",
|
|
257
261
|
"billing_address",
|
|
@@ -271,8 +275,13 @@ export const refDefUiSchema = {
|
|
|
271
275
|
},
|
|
272
276
|
},
|
|
273
277
|
feedback: { "ui:field": "TextAreaField" },
|
|
278
|
+
specialDate: {
|
|
279
|
+
"ui:title": "A day to remember",
|
|
280
|
+
},
|
|
274
281
|
definitions: {
|
|
275
|
-
|
|
282
|
+
someDate: {
|
|
283
|
+
"ui:field": "DateField",
|
|
284
|
+
},
|
|
276
285
|
names: {
|
|
277
286
|
history: { "ui:field": "TextAreaField" },
|
|
278
287
|
middleNames: { "ui:info": { content: "You can leave those blank" } },
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="hoist-non-react-statics" />
|
|
2
|
-
import type {
|
|
3
|
-
import React from "react";
|
|
2
|
+
import type { ClickableProps, InfoFieldButtonProps, MultipleChoiceFieldProps, ObjectFieldButtonProps, RadioButtonFieldProps, SelectFieldProps, TextAreaInputFieldProps, ToggleSwitchFieldProps } from "./types";
|
|
4
3
|
import * as formik from "formik";
|
|
4
|
+
import React from "react";
|
|
5
5
|
export declare const Form: import("styled-components").IStyledComponent<"web", import("styled-components/dist/types").FastOmit<Omit<Omit<React.DetailedHTMLProps<React.FormHTMLAttributes<HTMLFormElement>, HTMLFormElement>, "ref"> & React.RefAttributes<HTMLFormElement>, "ref"> & {
|
|
6
6
|
ref?: ((instance: HTMLFormElement | null) => void) | React.RefObject<HTMLFormElement> | null | undefined;
|
|
7
7
|
}, never>> & Omit<React.ForwardRefExoticComponent<Omit<React.DetailedHTMLProps<React.FormHTMLAttributes<HTMLFormElement>, HTMLFormElement>, "ref"> & React.RefAttributes<HTMLFormElement>>, keyof React.Component<any, {}, any>>;
|
|
@@ -74,7 +74,6 @@ export declare const ArrayFieldAddButton: import("styled-components").IStyledCom
|
|
|
74
74
|
}, never>, never>> & Omit<import("styled-components").IStyledComponent<"web", import("styled-components/dist/types").FastOmit<Omit<import("styled-components/dist/types").FastOmit<React.DetailedHTMLProps<React.AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>, never>, "ref"> & {
|
|
75
75
|
ref?: ((instance: HTMLAnchorElement | null) => void) | React.RefObject<HTMLAnchorElement> | null | undefined;
|
|
76
76
|
}, never>> & Omit<import("styled-components").IStyledComponent<"web", import("styled-components/dist/types").FastOmit<React.DetailedHTMLProps<React.AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>, never>>, keyof React.Component<any, {}, any>>, keyof React.Component<any, {}, any>>;
|
|
77
|
-
export declare const GetArrayFieldButton: ({ purpose, onClick, uiSchema, }: ArrayFieldButtonProps) => import("react/jsx-runtime").JSX.Element;
|
|
78
77
|
export declare const FieldWrapper: import("styled-components").IStyledComponent<"web", import("styled-components/dist/types").FastOmit<React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>, never>>;
|
|
79
78
|
export declare const FieldTitle: import("styled-components").IStyledComponent<"web", import("styled-components/dist/types").FastOmit<React.DetailedHTMLProps<React.LabelHTMLAttributes<HTMLLabelElement>, HTMLLabelElement>, never>>;
|
|
80
79
|
export declare const FieldDescription: import("styled-components").IStyledComponent<"web", import("styled-components/dist/types").FastOmit<React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>, never>>;
|
package/dist/styledComponents.js
CHANGED
|
@@ -10,11 +10,10 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
10
10
|
return t;
|
|
11
11
|
};
|
|
12
12
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
13
|
-
import { useContext, useState } from "react";
|
|
14
|
-
import styled from "styled-components";
|
|
15
13
|
import * as formik from "formik";
|
|
16
|
-
import {
|
|
17
|
-
import
|
|
14
|
+
import { useState } from "react";
|
|
15
|
+
import styled from "styled-components";
|
|
16
|
+
import { ObjectFieldButtonPurpose } from "./types";
|
|
18
17
|
import { getStyle } from "./utils/getStyle";
|
|
19
18
|
export const Form = styled(formik.Form) ``;
|
|
20
19
|
export const FormButtonWrapper = styled.div ``;
|
|
@@ -70,22 +69,6 @@ export const ArrayFieldUpButton = styled(ArrayFieldLineButton) ``;
|
|
|
70
69
|
export const ArrayFieldDownButton = styled(ArrayFieldLineButton) ``;
|
|
71
70
|
export const ArrayFieldRemoveButton = styled(ArrayFieldLineButton) ``;
|
|
72
71
|
export const ArrayFieldAddButton = styled(ArrayFieldButton) ``;
|
|
73
|
-
export const GetArrayFieldButton = ({ purpose, onClick, uiSchema, }) => {
|
|
74
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v;
|
|
75
|
-
const { components: { ArrayFieldAddButton, ArrayFieldRemoveButton, ArrayFieldUpButton, ArrayFieldDownButton, }, } = useContext(FormRegistry);
|
|
76
|
-
switch (purpose) {
|
|
77
|
-
case ArrayFieldButtonPurpose.ADD:
|
|
78
|
-
return (_jsx(ArrayFieldAddButton, Object.assign({}, ((_b = (_a = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:addButtonOptions"]) === null || _a === void 0 ? void 0 : _a.props) !== null && _b !== void 0 ? _b : {}), { onClick: onClick, children: (_d = (_c = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:addButtonOptions"]) === null || _c === void 0 ? void 0 : _c.content) !== null && _d !== void 0 ? _d : "+" })));
|
|
79
|
-
case ArrayFieldButtonPurpose.REMOVE:
|
|
80
|
-
return (_jsx(ArrayFieldRemoveButton, Object.assign({}, ((_f = (_e = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:removeButtonOptions"]) === null || _e === void 0 ? void 0 : _e.props) !== null && _f !== void 0 ? _f : {}), { onClick: onClick, children: (_h = (_g = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:removeButtonOptions"]) === null || _g === void 0 ? void 0 : _g.content) !== null && _h !== void 0 ? _h : "-" })));
|
|
81
|
-
case ArrayFieldButtonPurpose.MOVE_UP:
|
|
82
|
-
return (_jsx(ArrayFieldUpButton, Object.assign({}, ((_k = (_j = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:upButtonOptions"]) === null || _j === void 0 ? void 0 : _j.props) !== null && _k !== void 0 ? _k : {}), { onClick: onClick, children: (_m = (_l = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:upButtonOptions"]) === null || _l === void 0 ? void 0 : _l.content) !== null && _m !== void 0 ? _m : "↑" })));
|
|
83
|
-
case ArrayFieldButtonPurpose.MOVE_DOWN:
|
|
84
|
-
return (_jsx(ArrayFieldDownButton, Object.assign({}, ((_p = (_o = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:downButtonOptions"]) === null || _o === void 0 ? void 0 : _o.props) !== null && _p !== void 0 ? _p : {}), { onClick: onClick, children: (_r = (_q = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:downButtonOptions"]) === null || _q === void 0 ? void 0 : _q.content) !== null && _r !== void 0 ? _r : "↓" })));
|
|
85
|
-
default:
|
|
86
|
-
return (_jsx(ArrayFieldButton, Object.assign({}, ((_t = (_s = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:customButtonOptions"]) === null || _s === void 0 ? void 0 : _s.props) !== null && _t !== void 0 ? _t : {}), { onClick: onClick, children: (_v = (_u = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:customButtonOptions"]) === null || _u === void 0 ? void 0 : _u.content) !== null && _v !== void 0 ? _v : "" })));
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
72
|
export const FieldWrapper = styled.div `
|
|
90
73
|
margin: 0.5rem 0;
|
|
91
74
|
`;
|
package/dist/types.d.ts
CHANGED
|
@@ -159,17 +159,6 @@ export type ObjectFieldButtonProps = {
|
|
|
159
159
|
onClick: () => void;
|
|
160
160
|
uiSchema: UiSchema;
|
|
161
161
|
};
|
|
162
|
-
export declare enum ArrayFieldButtonPurpose {
|
|
163
|
-
ADD = 0,
|
|
164
|
-
REMOVE = 1,
|
|
165
|
-
MOVE_UP = 2,
|
|
166
|
-
MOVE_DOWN = 3
|
|
167
|
-
}
|
|
168
|
-
export type ArrayFieldButtonProps = {
|
|
169
|
-
purpose: ArrayFieldButtonPurpose;
|
|
170
|
-
onClick: () => void;
|
|
171
|
-
uiSchema: UiSchema;
|
|
172
|
-
};
|
|
173
162
|
export type InfoFieldButtonProps = {
|
|
174
163
|
label?: React.ReactNode;
|
|
175
164
|
content: React.ReactNode;
|
package/dist/types.js
CHANGED
|
@@ -5,10 +5,3 @@ export var ObjectFieldButtonPurpose;
|
|
|
5
5
|
ObjectFieldButtonPurpose[ObjectFieldButtonPurpose["MOVE_UP"] = 2] = "MOVE_UP";
|
|
6
6
|
ObjectFieldButtonPurpose[ObjectFieldButtonPurpose["MOVE_DOWN"] = 3] = "MOVE_DOWN";
|
|
7
7
|
})(ObjectFieldButtonPurpose || (ObjectFieldButtonPurpose = {}));
|
|
8
|
-
export var ArrayFieldButtonPurpose;
|
|
9
|
-
(function (ArrayFieldButtonPurpose) {
|
|
10
|
-
ArrayFieldButtonPurpose[ArrayFieldButtonPurpose["ADD"] = 0] = "ADD";
|
|
11
|
-
ArrayFieldButtonPurpose[ArrayFieldButtonPurpose["REMOVE"] = 1] = "REMOVE";
|
|
12
|
-
ArrayFieldButtonPurpose[ArrayFieldButtonPurpose["MOVE_UP"] = 2] = "MOVE_UP";
|
|
13
|
-
ArrayFieldButtonPurpose[ArrayFieldButtonPurpose["MOVE_DOWN"] = 3] = "MOVE_DOWN";
|
|
14
|
-
})(ArrayFieldButtonPurpose || (ArrayFieldButtonPurpose = {}));
|
|
@@ -13,8 +13,8 @@ describe("solveReferences", () => {
|
|
|
13
13
|
});
|
|
14
14
|
});
|
|
15
15
|
it("should resolve simple references with uiSchema correctly", () => {
|
|
16
|
-
var _a;
|
|
17
|
-
const schemaSolvedForReferences = solveReferences((_a = refDefSchema === null || refDefSchema === void 0 ? void 0 : refDefSchema.properties) === null || _a === void 0 ? void 0 : _a.birthday, refDefSchema.definitions, refDefUiSchema, refDefUiSchema.definitions);
|
|
16
|
+
var _a, _b;
|
|
17
|
+
const schemaSolvedForReferences = solveReferences((_a = refDefSchema === null || refDefSchema === void 0 ? void 0 : refDefSchema.properties) === null || _a === void 0 ? void 0 : _a.birthday, refDefSchema.definitions, (_b = refDefUiSchema.birthday) !== null && _b !== void 0 ? _b : {}, refDefUiSchema.definitions);
|
|
18
18
|
expect(schemaSolvedForReferences).toEqual({
|
|
19
19
|
schema: {
|
|
20
20
|
type: "string",
|
|
@@ -23,4 +23,18 @@ describe("solveReferences", () => {
|
|
|
23
23
|
uiSchema: { "ui:field": "DateField" },
|
|
24
24
|
});
|
|
25
25
|
});
|
|
26
|
+
it("should override uiSchema settings when key of field is also given outside definitions object of uiSchema", () => {
|
|
27
|
+
var _a, _b;
|
|
28
|
+
const schemaSolvedForReferences = solveReferences((_a = refDefSchema === null || refDefSchema === void 0 ? void 0 : refDefSchema.properties) === null || _a === void 0 ? void 0 : _a.friendBirthday, refDefSchema.definitions, (_b = refDefUiSchema.friendBirthday) !== null && _b !== void 0 ? _b : {}, refDefUiSchema.definitions);
|
|
29
|
+
expect(schemaSolvedForReferences).toEqual({
|
|
30
|
+
schema: {
|
|
31
|
+
type: "string",
|
|
32
|
+
title: "Birthday", //this is the original title that comes from the definitions, but is getting overridden
|
|
33
|
+
},
|
|
34
|
+
uiSchema: {
|
|
35
|
+
"ui:field": "DateField",
|
|
36
|
+
"ui:title": "The birthday of your best friend", //this is the title that will be displayed as field title
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
});
|
|
26
40
|
});
|
|
@@ -9,6 +9,9 @@ export const refDefSchema = {
|
|
|
9
9
|
birthday: {
|
|
10
10
|
$ref: "#/definitions/birthday",
|
|
11
11
|
},
|
|
12
|
+
friendBirthday: {
|
|
13
|
+
$ref: "#/definitions/birthday",
|
|
14
|
+
},
|
|
12
15
|
},
|
|
13
16
|
definitions: {
|
|
14
17
|
name: { type: "string", title: "Name" },
|
|
@@ -16,6 +19,7 @@ export const refDefSchema = {
|
|
|
16
19
|
},
|
|
17
20
|
};
|
|
18
21
|
export const refDefUiSchema = {
|
|
22
|
+
friendBirthday: { "ui:title": "The birthday of your best friend" },
|
|
19
23
|
definitions: {
|
|
20
24
|
birthday: { "ui:field": "DateField" },
|
|
21
25
|
},
|
|
@@ -34,7 +34,14 @@ export function createValidationSchema(schema, definitions = {}) {
|
|
|
34
34
|
throw new Error("Unsupported schema type");
|
|
35
35
|
}
|
|
36
36
|
if (requiredFields !== undefined && requiredFields.includes(key)) {
|
|
37
|
-
|
|
37
|
+
if (type === "array") {
|
|
38
|
+
yupType = yupType
|
|
39
|
+
.min(1, "Required")
|
|
40
|
+
.required("Required");
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
yupType = yupType["required"]("Required");
|
|
44
|
+
}
|
|
38
45
|
}
|
|
39
46
|
return yupType;
|
|
40
47
|
}
|
package/dist/utils/getTypes.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Schema } from "../types";
|
|
2
2
|
export declare const isObject: (object: any) => boolean;
|
|
3
3
|
export declare const getDefaultValueForType: (type: Schema["type"]) => {} | null;
|
|
4
|
-
export declare const guessType: (value: any) => "
|
|
4
|
+
export declare const guessType: (value: any) => "object" | "string" | "number" | "boolean" | "array" | "null";
|
|
5
5
|
export declare const getSchemaType: (schema: Schema) => import("json-schema").JSONSchema7TypeName | import("json-schema").JSONSchema7TypeName[] | undefined;
|
|
@@ -10,58 +10,16 @@ export function orderByUiSchema(order, allProperties) {
|
|
|
10
10
|
return undefined;
|
|
11
11
|
}
|
|
12
12
|
function orderWithAsterisk(order, allProperties) {
|
|
13
|
-
//to avoid issues when asterisk or other strings were wrongly given multiple times
|
|
14
13
|
const orderWithoutDuplicates = [...new Set(order)];
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
? orderWithoutDuplicates.slice(indexOfAsterisk + 1, orderWithoutDuplicates.length)
|
|
21
|
-
: undefined;
|
|
22
|
-
let asteriskArray = [];
|
|
23
|
-
let finalOrderedArray = [];
|
|
24
|
-
if (elementsBeforeAsterisk === undefined &&
|
|
25
|
-
elementsAfterAsterisk === undefined) {
|
|
26
|
-
finalOrderedArray = allProperties;
|
|
27
|
-
}
|
|
28
|
-
if (elementsBeforeAsterisk !== undefined) {
|
|
29
|
-
finalOrderedArray = finalOrderedArray.concat(elementsBeforeAsterisk);
|
|
30
|
-
allProperties.map((property) => {
|
|
31
|
-
elementsBeforeAsterisk.map((e) => {
|
|
32
|
-
if (e !== property) {
|
|
33
|
-
asteriskArray.push(property);
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
});
|
|
37
|
-
if (elementsAfterAsterisk === undefined) {
|
|
38
|
-
finalOrderedArray = finalOrderedArray.concat(asteriskArray);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
if (elementsAfterAsterisk !== undefined) {
|
|
42
|
-
if (asteriskArray.length === 0) {
|
|
43
|
-
allProperties.map((property) => {
|
|
44
|
-
elementsAfterAsterisk.map((e) => {
|
|
45
|
-
if (e !== property) {
|
|
46
|
-
asteriskArray.push(property);
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
finalOrderedArray = finalOrderedArray.concat(asteriskArray);
|
|
51
|
-
finalOrderedArray = finalOrderedArray.concat(elementsAfterAsterisk);
|
|
14
|
+
const finalOrderedArray = [];
|
|
15
|
+
orderWithoutDuplicates.forEach((element) => {
|
|
16
|
+
if (element === "*") {
|
|
17
|
+
const propertiesWithoutOrder = allProperties.filter((prop) => !orderWithoutDuplicates.includes(prop));
|
|
18
|
+
finalOrderedArray.push(...propertiesWithoutOrder);
|
|
52
19
|
}
|
|
53
20
|
else {
|
|
54
|
-
|
|
55
|
-
elementsAfterAsterisk.map((e) => {
|
|
56
|
-
if (e === property) {
|
|
57
|
-
const index = asteriskArray.indexOf(property);
|
|
58
|
-
asteriskArray.splice(index, 1);
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
finalOrderedArray = finalOrderedArray.concat(asteriskArray);
|
|
63
|
-
finalOrderedArray = finalOrderedArray.concat(elementsAfterAsterisk);
|
|
21
|
+
finalOrderedArray.push(element);
|
|
64
22
|
}
|
|
65
|
-
}
|
|
23
|
+
});
|
|
66
24
|
return finalOrderedArray;
|
|
67
25
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { mergeSchemas } from "./mergeSchemas";
|
|
1
2
|
export function solveReferences(schema, definitions, uiSchema, uiSchemaDefinitions) {
|
|
2
3
|
var _a, _b, _c;
|
|
3
4
|
let solvedSchema = schema;
|
|
@@ -5,7 +6,7 @@ export function solveReferences(schema, definitions, uiSchema, uiSchemaDefinitio
|
|
|
5
6
|
if (typeof schema === "object" && schema["$ref"] && definitions) {
|
|
6
7
|
const schemaKey = (_b = (_a = schema["$ref"]) === null || _a === void 0 ? void 0 : _a.substring(14)) !== null && _b !== void 0 ? _b : "";
|
|
7
8
|
solvedSchema = definitions[schemaKey];
|
|
8
|
-
solvedUiSchema = (_c = uiSchemaDefinitions === null || uiSchemaDefinitions === void 0 ? void 0 : uiSchemaDefinitions[schemaKey]) !== null && _c !== void 0 ? _c : {};
|
|
9
|
+
solvedUiSchema = mergeSchemas((_c = uiSchemaDefinitions === null || uiSchemaDefinitions === void 0 ? void 0 : uiSchemaDefinitions[schemaKey]) !== null && _c !== void 0 ? _c : {}, uiSchema !== null && uiSchema !== void 0 ? uiSchema : {});
|
|
9
10
|
}
|
|
10
11
|
return { schema: solvedSchema, uiSchema: solvedUiSchema };
|
|
11
12
|
}
|