@nolwenture/formik-jsonschema-form 0.6.0 → 0.7.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 +19 -0
- package/dist/fields/ObjectField.js +10 -2
- package/dist/fields/SearchMultiTagSelectField.js +1 -1
- package/dist/fields/StringField.js +1 -1
- package/dist/hooks/useSetDefault.js +1 -1
- package/dist/sampleData/conditionsSchema.js +2 -0
- package/dist/utils/deepOverlap.d.ts +1 -0
- package/dist/utils/{deepEqual.js → deepOverlap.js} +2 -6
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.js +1 -1
- package/dist/utils/resolveSchema.js +3 -3
- package/package.json +1 -1
- package/dist/utils/deepEqual.d.ts +0 -1
package/README.md
CHANGED
|
@@ -527,6 +527,25 @@ export const conditionsSchema = {
|
|
|
527
527
|
};
|
|
528
528
|
```
|
|
529
529
|
|
|
530
|
+
### Ordering fields
|
|
531
|
+
|
|
532
|
+
The order of fields can be set using `"ui:order": ["fieldName"]` inside the uiSchema.
|
|
533
|
+
If using "ui:order", all fields that should be visible in the form need to be included in the given array.
|
|
534
|
+
|
|
535
|
+
```tsx
|
|
536
|
+
const schema = {
|
|
537
|
+
properties: {
|
|
538
|
+
secondField: { type: "string" },
|
|
539
|
+
firstField: { type: "string" },
|
|
540
|
+
thirdField: { type: "string" },
|
|
541
|
+
},
|
|
542
|
+
};
|
|
543
|
+
|
|
544
|
+
const uiSchema = {
|
|
545
|
+
"ui:order": ["firstField", "secondField", "thirdField"],
|
|
546
|
+
};
|
|
547
|
+
```
|
|
548
|
+
|
|
530
549
|
## Props
|
|
531
550
|
|
|
532
551
|
`Form` component props:
|
|
@@ -14,7 +14,7 @@ import { useField, useFormikContext } from "formik";
|
|
|
14
14
|
import { useContext, useEffect, useMemo } from "react";
|
|
15
15
|
import { FormRegistry } from "../Form";
|
|
16
16
|
import { ObjectFieldButtonPurpose } from "../types";
|
|
17
|
-
import { resolveSchema } from "../utils";
|
|
17
|
+
import { resolveSchema } from "../utils/resolveSchema";
|
|
18
18
|
import { useSetDefault } from "../hooks";
|
|
19
19
|
import { getObjectFieldButton } from "../styledComponents";
|
|
20
20
|
const useObjectField = (name) => {
|
|
@@ -26,6 +26,7 @@ const useObjectField = (name) => {
|
|
|
26
26
|
return { value, setValue };
|
|
27
27
|
};
|
|
28
28
|
export const ObjectField = (props) => {
|
|
29
|
+
var _a;
|
|
29
30
|
const { fields: { SchemaField }, templates: { ObjectFieldTemplate }, components: { ObjectFieldItem, ObjectFieldItemButtons }, hideUnmarkedAdditionalProperties, } = useContext(FormRegistry);
|
|
30
31
|
useSetDefault(props);
|
|
31
32
|
const { name, schema, uiSchema } = props;
|
|
@@ -44,7 +45,14 @@ export const ObjectField = (props) => {
|
|
|
44
45
|
}, [valuesToRemove]);
|
|
45
46
|
const propertyKeys = useMemo(() => Object.keys(properties || {}), [properties]);
|
|
46
47
|
const additionalValues = useMemo(() => Object.fromEntries(Object.entries(value || {}).filter(([key, value]) => !propertyKeys.includes(key))), [value, propertyKeys]);
|
|
47
|
-
return (_jsxs(ObjectFieldTemplate, Object.assign({}, props, { children: [
|
|
48
|
+
return (_jsxs(ObjectFieldTemplate, Object.assign({}, props, { children: [((_a = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:order"]) !== null && _a !== void 0 ? _a : Object.keys(properties || {}))
|
|
49
|
+
.map((childName) => {
|
|
50
|
+
const childSchema = properties === null || properties === void 0 ? void 0 : properties[childName];
|
|
51
|
+
if (!childSchema)
|
|
52
|
+
return undefined;
|
|
53
|
+
return (_jsx(SchemaField, { required: required === null || required === void 0 ? void 0 : required.includes(childName), name: name ? [name, childName].join(".") : childName, schema: typeof childSchema !== "boolean" ? childSchema : {}, uiSchema: uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema[childName] }, childName));
|
|
54
|
+
})
|
|
55
|
+
.filter((x) => x), (!hideUnmarkedAdditionalProperties || additionalProperties) &&
|
|
48
56
|
Object.entries(additionalValues || {}).map(([childName, childSchema]) => (_jsxs(ObjectFieldItem, { children: [_jsx(SchemaField, { required: required === null || required === void 0 ? void 0 : required.includes(childName), name: name ? [name, childName].join(".") : childName, schema: typeof childSchema !== "boolean" ? childSchema : {}, uiSchema: uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema[childName] }, childName), _jsx(ObjectFieldItemButtons, { children: getObjectFieldButton({
|
|
49
57
|
purpose: ObjectFieldButtonPurpose.REMOVE,
|
|
50
58
|
onClick: () => {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { useContext, useMemo } from "react";
|
|
3
3
|
import { FormRegistry } from "../Form";
|
|
4
|
-
import { getEnums } from "../utils";
|
|
4
|
+
import { getEnums } from "../utils/getEnums";
|
|
5
5
|
import { useExtractUiSchema, useSetDefault } from "../hooks";
|
|
6
6
|
import { SearchableMultiTagSelectField } from "../styledComponents";
|
|
7
7
|
export const SearchMultiTagSelectField = (props) => {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { useContext, useMemo } from "react";
|
|
3
3
|
import { FormRegistry } from "../Form";
|
|
4
|
-
import { getEnums } from "../utils";
|
|
4
|
+
import { getEnums } from "../utils/getEnums";
|
|
5
5
|
import { useExtractUiSchema, useSetDefault } from "../hooks";
|
|
6
6
|
export const StringField = (props) => {
|
|
7
7
|
var _a;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useEffect } from "react";
|
|
2
2
|
import { useField } from "formik";
|
|
3
|
-
import { getDefaultValueForType } from "../utils";
|
|
3
|
+
import { getDefaultValueForType } from "../utils/getTypes";
|
|
4
4
|
export const useSetDefault = (props) => {
|
|
5
5
|
const [{ value }, _, { setValue }] = useField(props.name);
|
|
6
6
|
useEffect(() => {
|
|
@@ -11,6 +11,7 @@ export const conditionsSchema = {
|
|
|
11
11
|
{ const: "hamster", title: "Hamster" },
|
|
12
12
|
],
|
|
13
13
|
},
|
|
14
|
+
otherField: { type: "string", title: "Other field" },
|
|
14
15
|
},
|
|
15
16
|
allOf: [
|
|
16
17
|
{
|
|
@@ -77,4 +78,5 @@ export const conditionsUiSchema = {
|
|
|
77
78
|
props: { disabled: false, hidden: false },
|
|
78
79
|
},
|
|
79
80
|
},
|
|
81
|
+
"ui:order": ["animal", "water", "food", "otherField"],
|
|
80
82
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const deepOverlap: (object1: any, object2: any) => boolean;
|
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
import { isObject } from "./getTypes";
|
|
2
|
-
export const
|
|
2
|
+
export const deepOverlap = (object1, object2) => {
|
|
3
3
|
const keys1 = Object.keys(object1);
|
|
4
|
-
const keys2 = Object.keys(object2);
|
|
5
|
-
if (keys1.length !== keys2.length) {
|
|
6
|
-
return false;
|
|
7
|
-
}
|
|
8
4
|
for (const key of keys1) {
|
|
9
5
|
const val1 = object1[key];
|
|
10
6
|
const val2 = object2[key];
|
|
11
7
|
const areObjects = isObject(val1) && isObject(val2);
|
|
12
|
-
if ((areObjects && !
|
|
8
|
+
if ((areObjects && !deepOverlap(val1, val2)) ||
|
|
13
9
|
(!areObjects && val1 !== val2)) {
|
|
14
10
|
return false;
|
|
15
11
|
}
|
package/dist/utils/index.d.ts
CHANGED
package/dist/utils/index.js
CHANGED
|
@@ -9,15 +9,15 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
9
9
|
}
|
|
10
10
|
return t;
|
|
11
11
|
};
|
|
12
|
-
import {
|
|
12
|
+
import { deepOverlap } from "./deepOverlap";
|
|
13
13
|
import { mergeSchemas } from "./mergeSchemas";
|
|
14
14
|
const resolveEquality = (value, expression) => {
|
|
15
15
|
const keyOfValue = Object.keys(value);
|
|
16
16
|
const valueOfValue = Object.values(value);
|
|
17
17
|
const newObject = {
|
|
18
|
-
properties: { [
|
|
18
|
+
properties: keyOfValue.reduce((properties, key, index) => (Object.assign(Object.assign({}, properties), { [key]: { const: valueOfValue[index] } })), {}),
|
|
19
19
|
};
|
|
20
|
-
return
|
|
20
|
+
return deepOverlap(expression, newObject);
|
|
21
21
|
};
|
|
22
22
|
export const resolveSchema = (_a, value) => {
|
|
23
23
|
var _b;
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const deepEqual: (object1: any, object2: any) => boolean;
|