@nolwenture/formik-jsonschema-form 0.13.0 → 0.15.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:
@@ -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, GetArrayFieldButton } from "../styledComponents";
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
- const { fields: { SchemaField }, templates: { ArrayFieldTemplate }, components: { ArrayFieldItem, ArrayFieldList, ArrayFieldItemButtons }, useItemsAsAdditionalItems, } = useContext(FormRegistry);
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,11 @@ 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), forceDefaultValue: true }) }, index))), ((valueArray === null || valueArray === void 0 ? void 0 : valueArray.slice(items.length)) || []).map((_, index) => (_jsxs(ArrayFieldItem, { children: [_jsx(SchemaField, { name: `${name}[${index + items.length}]`, schema: additionalItems && typeof additionalItems !== "boolean"
102
- ? additionalItems
103
- : {}, 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: [GetArrayFieldButton({
104
- purpose: ArrayFieldButtonPurpose.REMOVE,
105
- onClick: () => setValue(removeFromArray(value, index + items.length)),
106
- uiSchema,
107
- }), value.length > 1 &&
108
- index > 0 &&
109
- GetArrayFieldButton({
110
- purpose: ArrayFieldButtonPurpose.MOVE_UP,
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), forceDefaultValue: true }) }, index))), ((valueArray === null || valueArray === void 0 ? void 0 : valueArray.slice(items.length)) || []).map((_, index) => {
102
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
103
+ return (_jsxs(ArrayFieldItem, { children: [_jsx(SchemaField, { name: `${name}[${index + items.length}]`, schema: additionalItems && typeof additionalItems !== "boolean"
104
+ ? additionalItems
105
+ : {}, 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 &&
106
+ 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));
107
+ })] }), 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
108
  };
@@ -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)
@@ -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: "", disabled: true,
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,
@@ -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);
@@ -1,7 +1,7 @@
1
1
  /// <reference types="hoist-non-react-statics" />
2
- import type { ArrayFieldButtonProps, InfoFieldButtonProps, ObjectFieldButtonProps, RadioButtonFieldProps, ToggleSwitchFieldProps, SelectFieldProps, MultipleChoiceFieldProps, TextAreaInputFieldProps, ClickableProps } from "./types";
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>>;
@@ -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 { FormRegistry } from "./Form";
17
- import { ArrayFieldButtonPurpose, ObjectFieldButtonPurpose } from "./types";
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 = {}));
@@ -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
+ });
@@ -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) => "string" | "number" | "boolean" | "object" | "array" | "null";
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;
@@ -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,25 @@
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
+ const orderWithoutDuplicates = [...new Set(order)];
14
+ const finalOrderedArray = [];
15
+ orderWithoutDuplicates.forEach((element) => {
16
+ if (element === "*") {
17
+ const propertiesWithoutOrder = allProperties.filter((prop) => !orderWithoutDuplicates.includes(prop));
18
+ finalOrderedArray.push(...propertiesWithoutOrder);
19
+ }
20
+ else {
21
+ finalOrderedArray.push(element);
22
+ }
23
+ });
24
+ return finalOrderedArray;
25
+ }
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.15.0",
4
4
  "description": "Formik form generation from json schema",
5
5
  "author": "Antti Vikman <antti.vikman@nolwenture.com>",
6
6
  "contributors": [