@nolwenture/formik-jsonschema-form 0.6.0 → 0.7.1

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
@@ -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:
@@ -106,7 +106,7 @@ export const ArrayField = (props) => {
106
106
  onClick: () => setValue(moveInArray(value, index + items.length, -1)),
107
107
  uiSchema,
108
108
  }), value.length > 1 &&
109
- index < value.length - 1 &&
109
+ index < (value === null || value === void 0 ? void 0 : value.slice(items.length).length) - 1 &&
110
110
  GetArrayFieldButton({
111
111
  purpose: ArrayFieldButtonPurpose.MOVE_DOWN,
112
112
  onClick: () => setValue(moveInArray(value, index + items.length, 1)),
@@ -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: [Object.entries(properties || {}).map(([childName, childSchema]) => (_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))), (!hideUnmarkedAdditionalProperties || additionalProperties) &&
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
  };
@@ -352,22 +352,18 @@ export const SearchableMultiTagSelectField = ({ name, placeholder, enums, emptyS
352
352
  const hasValue = enums === null || enums === void 0 ? void 0 : enums.values.map((value) => value.title.toLowerCase().includes(search.toLowerCase()));
353
353
  return hasValue === null || hasValue === void 0 ? void 0 : hasValue.includes(true);
354
354
  };
355
- return (_jsx(formik.Field, { className: className, name: name, multiple: true, children: ({ field, form: { setValues }, meta, }) => {
355
+ return (_jsx(formik.Field, { className: className, name: name, multiple: true, children: ({ field, form: { setFieldValue }, meta, }) => {
356
356
  var _a;
357
357
  return (_jsxs(MultiSelectContainer, { className: className, children: [_jsx(FieldInput, Object.assign({ type: "hidden" }, field)), _jsx(MultiSelectInput, { className: className, type: "text", onChange: (e) => {
358
358
  setSearch(e.currentTarget.value);
359
359
  }, placeholder: placeholder, value: search }), _jsx(MultiSelectBadgeContainer, { children: ((_a = field.value) !== null && _a !== void 0 ? _a : []).map((value) => {
360
360
  var _a;
361
- return (_jsxs(MultiSelectBadge, { children: [(_a = enums === null || enums === void 0 ? void 0 : enums.values.find((option) => option.const === value)) === null || _a === void 0 ? void 0 : _a.title, _jsx(MultiSelectBadgeCloseElement, { onClick: () => setValues({
362
- [name]: field.value.filter((v) => v !== value),
363
- }), children: closeElement !== null && closeElement !== void 0 ? closeElement : "x" })] }, value));
361
+ return (_jsxs(MultiSelectBadge, { children: [(_a = enums === null || enums === void 0 ? void 0 : enums.values.find((option) => option.const === value)) === null || _a === void 0 ? void 0 : _a.title, _jsx(MultiSelectBadgeCloseElement, { onClick: () => setFieldValue(name, field.value.filter((v) => v !== value)), children: closeElement !== null && closeElement !== void 0 ? closeElement : "x" })] }, value));
364
362
  }) }), search.length > 0 && (_jsx(MultiSelectOptionList, { children: searchIncludedInEnum() ? (enums === null || enums === void 0 ? void 0 : enums.values.filter((item) => { var _a; return !((_a = field.value) !== null && _a !== void 0 ? _a : []).includes(item.const); }).filter((value) => value.title.toLowerCase().includes(search.toLowerCase())).map((option, index) => (_jsx(MultiSelectOptionListItem, { value: option.const, onClick: () => {
365
363
  var _a;
366
- setValues({
367
- [name]: [
368
- ...new Set([...((_a = field.value) !== null && _a !== void 0 ? _a : []), option.const]),
369
- ],
370
- });
364
+ setFieldValue(name, [
365
+ ...new Set([...((_a = field.value) !== null && _a !== void 0 ? _a : []), option.const]),
366
+ ]);
371
367
  setSearch("");
372
368
  }, children: option.title }, index)))) : (_jsx(MultiSelectOptionListWithoutItem, { children: emptySearchMessage !== null && emptySearchMessage !== void 0 ? emptySearchMessage : "No match found" })) })), meta.touched && meta.error && (_jsx("div", { className: "error", children: meta.error }))] }));
373
369
  } }));
@@ -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 deepEqual = (object1, object2) => {
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 && !deepEqual(val1, val2)) ||
8
+ if ((areObjects && !deepOverlap(val1, val2)) ||
13
9
  (!areObjects && val1 !== val2)) {
14
10
  return false;
15
11
  }
@@ -1,4 +1,4 @@
1
- export * from "./deepEqual";
1
+ export * from "./deepOverlap";
2
2
  export * from "./getEnums";
3
3
  export * from "./getTitleAndDescription";
4
4
  export * from "./getTypes";
@@ -1,4 +1,4 @@
1
- export * from "./deepEqual";
1
+ export * from "./deepOverlap";
2
2
  export * from "./getEnums";
3
3
  export * from "./getTitleAndDescription";
4
4
  export * from "./getTypes";
@@ -9,15 +9,15 @@ var __rest = (this && this.__rest) || function (s, e) {
9
9
  }
10
10
  return t;
11
11
  };
12
- import { deepEqual } from "./deepEqual";
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: { [keyOfValue[0]]: { const: valueOfValue[0] } },
18
+ properties: keyOfValue.reduce((properties, key, index) => (Object.assign(Object.assign({}, properties), { [key]: { const: valueOfValue[index] } })), {}),
19
19
  };
20
- return deepEqual(newObject, expression);
20
+ return deepOverlap(expression, newObject);
21
21
  };
22
22
  export const resolveSchema = (_a, value) => {
23
23
  var _b;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nolwenture/formik-jsonschema-form",
3
- "version": "0.6.0",
3
+ "version": "0.7.1",
4
4
  "description": "Formik form generation from json schema",
5
5
  "author": "Antti Vikman <antti.vikman@nolwenture.com>",
6
6
  "contributors": [
@@ -1 +0,0 @@
1
- export declare const deepEqual: (object1: any, object2: any) => boolean;