@nolwenture/formik-jsonschema-form 1.0.0-beta.29 → 1.0.0-beta.30

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.
@@ -7,8 +7,9 @@ const formik_1 = require("formik");
7
7
  const utils_1 = require("../../../utils");
8
8
  const hooks_1 = require("../../hooks");
9
9
  const FormRegistry_1 = require("../../FormRegistry");
10
- const appendArray = (values) => {
11
- return values ? [...values, ""] : [""];
10
+ const appendArray = (values, schema) => {
11
+ const newItem = (0, utils_1.getDefaultValueForType)(schema?.type);
12
+ return values ? [...values, newItem] : [newItem];
12
13
  };
13
14
  const removeFromArray = (values, index) => {
14
15
  return [...values.slice(0, index), ...values.slice(index + 1)];
@@ -107,7 +108,7 @@ const ArrayField = (props) => {
107
108
  templates.ArrayFieldTemplate;
108
109
  return schema.anyOf !== undefined ? ((0, jsx_runtime_1.jsx)(SelectedFieldTemplate, { ...props, "data-fjsf-array": name, children: (0, jsx_runtime_1.jsx)(SelectFieldInput, { type,
109
110
  readOnly: schema.readOnly,
110
- ...inputProps, name: name, formikAs: "select", multiple: enums?.type === utils_1.EnumsTypes.MULTI_SELECT, children: enums?.values.map((option, index) => ((0, jsx_runtime_1.jsx)("option", { value: option.const, children: option.title }, index))) }) })) : ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(SelectedArrayFieldTemplate, { ...props, "data-fjsf-array": name, items: valueArray, renderItem: (item, index) => ((0, jsx_runtime_1.jsxs)(ArrayFieldItem, { children: [(0, jsx_runtime_1.jsx)(SchemaField, { name: `${name}[${index}]`, schema: prepareChildSchema((index < schemas.items.length && schemas.items[index]) ??
111
+ ...inputProps, name: name, formikAs: "select", multiple: enums?.type === utils_1.EnumsTypes.MULTI_SELECT, children: enums?.values.map((option, index) => ((0, jsx_runtime_1.jsx)("option", { value: option.const, children: option.title }, index))) }) })) : ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(SelectedArrayFieldTemplate, { ...props, "data-fjsf-array": name, items: valueArray, renderItem: (item, index) => ((0, jsx_runtime_1.jsxs)(ArrayFieldItem, { children: [(0, jsx_runtime_1.jsx)(SchemaField, { name: `${name}[${index}]`, schema: prepareChildSchema((index < schemas.items.length && schemas.items[index]) ||
111
112
  schemas.additionalItems, schema), uiSchema: {
112
113
  disabled,
113
114
  ...(uiSchema?.[index] || uiSchema?.items),
@@ -115,7 +116,7 @@ const ArrayField = (props) => {
115
116
  // "true" will prevent validation for required array fields
116
117
  // since a value always exists in the array
117
118
  forceDefaultValue: index >= schemas.items.length }), !preventManipulation && index >= schemas.items.length && ((0, jsx_runtime_1.jsxs)(ArrayFieldItemButtons, { children: [(0, jsx_runtime_1.jsx)(ArrayFieldRemoveButton, { ...(uiSchema?.["ui:removeButtonOptions"]?.props ?? {}), onClick: () => setValue(removeFromArray(value, index + schemas.items.length)), children: uiSchema?.["ui:removeButtonOptions"]?.content ?? "-" }), value.length > 1 && index > 0 && ((0, jsx_runtime_1.jsx)(ArrayFieldUpButton, { ...(uiSchema?.["ui:upButtonOptions"]?.props ?? {}), onClick: () => setValue(moveInArray(value, index + schemas.items.length, -1)), children: uiSchema?.["ui:upButtonOptions"]?.content ?? "↑" })), value.length > 1 &&
118
- index < value?.slice(schemas.items.length).length - 1 && ((0, jsx_runtime_1.jsx)(ArrayFieldDownButton, { ...(uiSchema?.["ui:downButtonOptions"]?.props ?? {}), onClick: () => setValue(moveInArray(value, index + schemas.items.length, 1)), children: uiSchema?.["ui:downButtonOptions"]?.content ?? "↓" }))] }))] }, index)) }), schemas.additionalItems && !preventManipulation && ((0, jsx_runtime_1.jsx)(ArrayFieldAddButton, { ...(uiSchema?.["ui:addButtonOptions"]?.props ?? {}), onClick: () => setValue(appendArray(value)), children: uiSchema?.["ui:addButtonOptions"]?.content ?? "+" }))] }));
119
+ index < value?.slice(schemas.items.length).length - 1 && ((0, jsx_runtime_1.jsx)(ArrayFieldDownButton, { ...(uiSchema?.["ui:downButtonOptions"]?.props ?? {}), onClick: () => setValue(moveInArray(value, index + schemas.items.length, 1)), children: uiSchema?.["ui:downButtonOptions"]?.content ?? "↓" }))] }))] }, index)) }), schemas.additionalItems && !preventManipulation && ((0, jsx_runtime_1.jsx)(ArrayFieldAddButton, { ...(uiSchema?.["ui:addButtonOptions"]?.props ?? {}), onClick: () => setValue(appendArray(value, schemas.additionalItems)), children: uiSchema?.["ui:addButtonOptions"]?.content ?? "+" }))] }));
119
120
  };
120
121
  exports.ArrayField = ArrayField;
121
122
  function prepareChildSchema(childSchema, parentSchema) {
@@ -12,7 +12,7 @@ const getDefaultValueForType = (type) => {
12
12
  if (type === "object")
13
13
  return {};
14
14
  }
15
- return null;
15
+ return undefined;
16
16
  };
17
17
  exports.getDefaultValueForType = getDefaultValueForType;
18
18
  const guessType = (value) => {
@@ -1,11 +1,12 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
2
  import { useContext, useMemo } from "react";
3
3
  import { useField } from "formik";
4
- import { EnumsTypes, extractUiSchema, getEnums } from "../../../utils";
4
+ import { EnumsTypes, extractUiSchema, getDefaultValueForType, getEnums, } from "../../../utils";
5
5
  import { useSetDefault } from "../../hooks";
6
6
  import { FormRegistry } from "../../FormRegistry";
7
- const appendArray = (values) => {
8
- return values ? [...values, ""] : [""];
7
+ const appendArray = (values, schema) => {
8
+ const newItem = getDefaultValueForType(schema?.type);
9
+ return values ? [...values, newItem] : [newItem];
9
10
  };
10
11
  const removeFromArray = (values, index) => {
11
12
  return [...values.slice(0, index), ...values.slice(index + 1)];
@@ -104,7 +105,7 @@ export const ArrayField = (props) => {
104
105
  templates.ArrayFieldTemplate;
105
106
  return schema.anyOf !== undefined ? (_jsx(SelectedFieldTemplate, { ...props, "data-fjsf-array": name, children: _jsx(SelectFieldInput, { type,
106
107
  readOnly: schema.readOnly,
107
- ...inputProps, name: name, formikAs: "select", multiple: enums?.type === EnumsTypes.MULTI_SELECT, children: enums?.values.map((option, index) => (_jsx("option", { value: option.const, children: option.title }, index))) }) })) : (_jsxs(_Fragment, { children: [_jsx(SelectedArrayFieldTemplate, { ...props, "data-fjsf-array": name, items: valueArray, renderItem: (item, index) => (_jsxs(ArrayFieldItem, { children: [_jsx(SchemaField, { name: `${name}[${index}]`, schema: prepareChildSchema((index < schemas.items.length && schemas.items[index]) ??
108
+ ...inputProps, name: name, formikAs: "select", multiple: enums?.type === EnumsTypes.MULTI_SELECT, children: enums?.values.map((option, index) => (_jsx("option", { value: option.const, children: option.title }, index))) }) })) : (_jsxs(_Fragment, { children: [_jsx(SelectedArrayFieldTemplate, { ...props, "data-fjsf-array": name, items: valueArray, renderItem: (item, index) => (_jsxs(ArrayFieldItem, { children: [_jsx(SchemaField, { name: `${name}[${index}]`, schema: prepareChildSchema((index < schemas.items.length && schemas.items[index]) ||
108
109
  schemas.additionalItems, schema), uiSchema: {
109
110
  disabled,
110
111
  ...(uiSchema?.[index] || uiSchema?.items),
@@ -112,7 +113,7 @@ export const ArrayField = (props) => {
112
113
  // "true" will prevent validation for required array fields
113
114
  // since a value always exists in the array
114
115
  forceDefaultValue: index >= schemas.items.length }), !preventManipulation && index >= schemas.items.length && (_jsxs(ArrayFieldItemButtons, { children: [_jsx(ArrayFieldRemoveButton, { ...(uiSchema?.["ui:removeButtonOptions"]?.props ?? {}), onClick: () => setValue(removeFromArray(value, index + schemas.items.length)), children: uiSchema?.["ui:removeButtonOptions"]?.content ?? "-" }), value.length > 1 && index > 0 && (_jsx(ArrayFieldUpButton, { ...(uiSchema?.["ui:upButtonOptions"]?.props ?? {}), onClick: () => setValue(moveInArray(value, index + schemas.items.length, -1)), children: uiSchema?.["ui:upButtonOptions"]?.content ?? "↑" })), value.length > 1 &&
115
- index < value?.slice(schemas.items.length).length - 1 && (_jsx(ArrayFieldDownButton, { ...(uiSchema?.["ui:downButtonOptions"]?.props ?? {}), onClick: () => setValue(moveInArray(value, index + schemas.items.length, 1)), children: uiSchema?.["ui:downButtonOptions"]?.content ?? "↓" }))] }))] }, index)) }), schemas.additionalItems && !preventManipulation && (_jsx(ArrayFieldAddButton, { ...(uiSchema?.["ui:addButtonOptions"]?.props ?? {}), onClick: () => setValue(appendArray(value)), children: uiSchema?.["ui:addButtonOptions"]?.content ?? "+" }))] }));
116
+ index < value?.slice(schemas.items.length).length - 1 && (_jsx(ArrayFieldDownButton, { ...(uiSchema?.["ui:downButtonOptions"]?.props ?? {}), onClick: () => setValue(moveInArray(value, index + schemas.items.length, 1)), children: uiSchema?.["ui:downButtonOptions"]?.content ?? "↓" }))] }))] }, index)) }), schemas.additionalItems && !preventManipulation && (_jsx(ArrayFieldAddButton, { ...(uiSchema?.["ui:addButtonOptions"]?.props ?? {}), onClick: () => setValue(appendArray(value, schemas.additionalItems)), children: uiSchema?.["ui:addButtonOptions"]?.content ?? "+" }))] }));
116
117
  };
117
118
  function prepareChildSchema(childSchema, parentSchema) {
118
119
  if (!childSchema || typeof childSchema === "boolean") {
@@ -8,7 +8,7 @@ export const getDefaultValueForType = (type) => {
8
8
  if (type === "object")
9
9
  return {};
10
10
  }
11
- return null;
11
+ return undefined;
12
12
  };
13
13
  export const guessType = (value) => {
14
14
  if (Array.isArray(value)) {
@@ -1,5 +1,5 @@
1
1
  import type { Schema } from "../common/types";
2
2
  export declare const isObject: (object: any) => boolean;
3
- export declare const getDefaultValueForType: (type: Schema["type"]) => {} | null;
3
+ export declare const getDefaultValueForType: (type: Schema["type"]) => {} | undefined;
4
4
  export declare const guessType: (value: any) => "string" | "number" | "boolean" | "object" | "array" | "null";
5
5
  export declare const getSchemaType: (schema: Schema) => import("json-schema").JSONSchema7TypeName | import("json-schema").JSONSchema7TypeName[] | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nolwenture/formik-jsonschema-form",
3
- "version": "1.0.0-beta.29",
3
+ "version": "1.0.0-beta.30",
4
4
  "description": "Formik form generation from json schema",
5
5
  "author": "anttivikman <antti.vikman@nolwenture.com>",
6
6
  "contributors": [