@nolwenture/formik-jsonschema-form 0.13.0 → 0.14.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 CHANGED
@@ -866,7 +866,7 @@ const refDefUiSchema: UiSchema = {
866
866
  ### Ordering fields
867
867
 
868
868
  The order of fields can be set using `"ui:order": ["fieldName"]` inside the uiSchema.
869
- If using "ui:order", all fields that should be visible in the form need to be included in the given array.
869
+ If using `"ui:order"`, all fields that should be visible in the form need to be included in the given array. Fields excluded will not show on the form, in the following they will be referred to as `"hidden fields"`.
870
870
 
871
871
  ```tsx
872
872
  const schema = {
@@ -882,6 +882,42 @@ const uiSchema = {
882
882
  };
883
883
  ```
884
884
 
885
+ Fields excluded will not show on the form. So in the following example "thirdField" will be a `hidden field`.
886
+
887
+ ```tsx
888
+ const schema = {
889
+ properties: {
890
+ secondField: { type: "string" },
891
+ firstField: { type: "string" },
892
+ thirdField: { type: "string" },
893
+ },
894
+ };
895
+
896
+ const uiSchema = {
897
+ "ui:order": ["firstField", "secondField"],
898
+ };
899
+ ```
900
+
901
+ Using `"*"`-wildcard inside the array will add all property fields that were not specifically added to the order-array.
902
+
903
+ ```tsx
904
+ const schema = {
905
+ properties: {
906
+ secondField: { type: "string" },
907
+ firstField: { type: "string" },
908
+ thirdField: { type: "string" },
909
+ },
910
+ };
911
+
912
+ const uiSchema = {
913
+ "ui:order": ["secondField", "*"],
914
+ };
915
+ ```
916
+
917
+ In this example, the resulting order would be ["secondField", "firstField", "thirdField"].
918
+
919
+ Wildcard `"*"` and `hidden fields` cannot be combined.
920
+
885
921
  ## Props
886
922
 
887
923
  `Form` component props:
@@ -17,6 +17,7 @@ import { ObjectFieldButtonPurpose } from "../types";
17
17
  import { resolveSchema } from "../utils/resolveSchema";
18
18
  import { useSetDefault } from "../hooks";
19
19
  import { getObjectFieldButton } from "../styledComponents";
20
+ import { orderByUiSchema } from "../utils";
20
21
  const useObjectField = (name) => {
21
22
  if (!name) {
22
23
  const { values = {}, setValues } = useFormikContext();
@@ -26,7 +27,7 @@ const useObjectField = (name) => {
26
27
  return { value, setValue };
27
28
  };
28
29
  export const ObjectField = (props) => {
29
- var _a, _b, _c;
30
+ var _a, _b;
30
31
  const { fields: { SchemaField }, templates: { ObjectFieldTemplate, CollapsibleObjectFieldTemplate }, components: { ObjectFieldItem, ObjectFieldItemButtons }, hideUnmarkedAdditionalProperties, } = useContext(FormRegistry);
31
32
  useSetDefault(props);
32
33
  const { name, schema, uiSchema } = props;
@@ -48,7 +49,8 @@ export const ObjectField = (props) => {
48
49
  const FieldTemplateComponent = ((_b = (_a = props.uiSchema) === null || _a === void 0 ? void 0 : _a.props) === null || _b === void 0 ? void 0 : _b.isCollapsible)
49
50
  ? CollapsibleObjectFieldTemplate
50
51
  : ObjectFieldTemplate;
51
- return (_jsxs(FieldTemplateComponent, Object.assign({}, props, { children: [((_c = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:order"]) !== null && _c !== void 0 ? _c : Object.keys(properties || {}))
52
+ const orderedProperties = orderByUiSchema(uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:order"], Object.keys(properties || {}));
53
+ return (_jsxs(FieldTemplateComponent, Object.assign({}, props, { children: [(orderedProperties !== null && orderedProperties !== void 0 ? orderedProperties : Object.keys(properties || {}))
52
54
  .map((childName) => {
53
55
  const childSchema = properties === null || properties === void 0 ? void 0 : properties[childName];
54
56
  if (!childSchema)
@@ -2,9 +2,8 @@ import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { useContext } from "react";
3
3
  import { FormRegistry } from "../Form";
4
4
  import { useExtractUiSchema, useSetDefault } from "../hooks";
5
- import { TextAreaInputField } from "../styledComponents";
6
5
  export const TextAreaField = (props) => {
7
- const { templates: { FieldTemplate }, } = useContext(FormRegistry);
6
+ const { templates: { FieldTemplate }, components: { TextAreaInputField }, } = useContext(FormRegistry);
8
7
  useSetDefault(props);
9
8
  const { name, uiSchema } = props;
10
9
  const { props: inputProps } = useExtractUiSchema(uiSchema);
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,63 @@
1
+ import { orderByUiSchema } from "../orderByUiSchema";
2
+ const allProperties = ["firstName", "lastName", "age", "hobbies"];
3
+ const emptyOrder = undefined;
4
+ const noAsteriskOrder = ["firstName", "age"];
5
+ const asteriskAsFirstElement = ["*", "firstName"];
6
+ const asteriskAsLastElement = ["age", "*"];
7
+ const asteriskAsMiddleElement = ["hobbies", "*", "age"];
8
+ const orderWithDuplicates = ["age", "*", "age", "*", "lastName"];
9
+ describe("orderByUiSchema", () => {
10
+ it("returns empty array when ui:order is not given", () => {
11
+ const orderedProperties = orderByUiSchema(emptyOrder, allProperties);
12
+ expect(orderedProperties).toEqual(undefined);
13
+ });
14
+ it("returns only given properties when order includes no asterisk", () => {
15
+ const orderedProperties = orderByUiSchema(noAsteriskOrder, allProperties);
16
+ expect(orderedProperties).toEqual(["firstName", "age"]);
17
+ });
18
+ it("returns all properties in the given order with asterisk as first element", () => {
19
+ const orderedProperties = orderByUiSchema(asteriskAsFirstElement, allProperties);
20
+ expect(orderedProperties).toEqual([
21
+ "lastName",
22
+ "age",
23
+ "hobbies",
24
+ "firstName",
25
+ ]);
26
+ });
27
+ it("returns all properties in the given order with asterisk as last element", () => {
28
+ const orderedProperties = orderByUiSchema(asteriskAsLastElement, allProperties);
29
+ expect(orderedProperties).toEqual([
30
+ "age",
31
+ "firstName",
32
+ "lastName",
33
+ "hobbies",
34
+ ]);
35
+ });
36
+ it("returns all properties in the given order with asterisk as middle element", () => {
37
+ const orderedProperties = orderByUiSchema(asteriskAsMiddleElement, allProperties);
38
+ expect(orderedProperties).toEqual([
39
+ "hobbies",
40
+ "firstName",
41
+ "lastName",
42
+ "age",
43
+ ]);
44
+ });
45
+ it("returns all properties in the given order with asterisk as middle element", () => {
46
+ const orderedProperties = orderByUiSchema(asteriskAsMiddleElement, allProperties);
47
+ expect(orderedProperties).toEqual([
48
+ "hobbies",
49
+ "firstName",
50
+ "lastName",
51
+ "age",
52
+ ]);
53
+ });
54
+ it("ignores duplicates in order array to avoid errors", () => {
55
+ const orderedProperties = orderByUiSchema(orderWithDuplicates, allProperties);
56
+ expect(orderedProperties).toEqual([
57
+ "age",
58
+ "firstName",
59
+ "hobbies",
60
+ "lastName",
61
+ ]);
62
+ });
63
+ });
@@ -7,4 +7,5 @@ export * from "./resolveSchema";
7
7
  export * from "./solveReferences";
8
8
  export * from "./createInitialValues";
9
9
  export * from "./createValidationSchema";
10
+ export * from "./orderByUiSchema";
10
11
  export declare const warnFromUnsupportedKeys: (componentName: string, data: Record<string, any>, supportedKeys: string[]) => void;
@@ -7,6 +7,7 @@ export * from "./resolveSchema";
7
7
  export * from "./solveReferences";
8
8
  export * from "./createInitialValues";
9
9
  export * from "./createValidationSchema";
10
+ export * from "./orderByUiSchema";
10
11
  //TODO: REMOVE WHEN ALL SUPPORTED
11
12
  export const warnFromUnsupportedKeys = (componentName, data, supportedKeys) => {
12
13
  Object.keys(data).forEach((key) => {
@@ -0,0 +1 @@
1
+ export declare function orderByUiSchema(order: string[] | undefined, allProperties: string[]): string[] | undefined;
@@ -0,0 +1,67 @@
1
+ export function orderByUiSchema(order, allProperties) {
2
+ if (order !== undefined && allProperties !== undefined) {
3
+ if (order.includes("*")) {
4
+ return orderWithAsterisk(order, allProperties);
5
+ }
6
+ else {
7
+ return order;
8
+ }
9
+ }
10
+ return undefined;
11
+ }
12
+ function orderWithAsterisk(order, allProperties) {
13
+ //to avoid issues when asterisk or other strings were wrongly given multiple times
14
+ const orderWithoutDuplicates = [...new Set(order)];
15
+ const indexOfAsterisk = orderWithoutDuplicates.indexOf("*");
16
+ const elementsBeforeAsterisk = indexOfAsterisk !== 0
17
+ ? orderWithoutDuplicates.slice(0, indexOfAsterisk)
18
+ : undefined;
19
+ const elementsAfterAsterisk = indexOfAsterisk !== orderWithoutDuplicates.length - 1
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);
52
+ }
53
+ else {
54
+ asteriskArray.map((property) => {
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);
64
+ }
65
+ }
66
+ return finalOrderedArray;
67
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nolwenture/formik-jsonschema-form",
3
- "version": "0.13.0",
3
+ "version": "0.14.0",
4
4
  "description": "Formik form generation from json schema",
5
5
  "author": "Antti Vikman <antti.vikman@nolwenture.com>",
6
6
  "contributors": [