@nolwenture/formik-jsonschema-form 1.0.0-beta.33 → 1.0.0-beta.34

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.
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CollapsibleObjectFieldTemplate = void 0;
4
4
  const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const formik_1 = require("formik");
5
6
  const react_1 = require("react");
6
7
  const utils_1 = require("../../../utils");
7
8
  const FormRegistry_1 = require("../../FormRegistry");
@@ -10,7 +11,9 @@ const CollapsibleObjectFieldTemplate = (props) => {
10
11
  const { components } = (0, react_1.useContext)(FormRegistry_1.FormRegistry);
11
12
  const { Toggle, ChildrenWrapper, RequiredIndicator, Icon, Description, Title, Wrapper, Help, Info, } = (0, styledComponents_1.getComponents)(components);
12
13
  const { children, name, uiSchema, ...otherProps } = props;
13
- const title = (0, utils_1.getTitle)(props);
14
+ const formikCtx = (0, formik_1.useFormikContext)();
15
+ const values = formikCtx.values ? formikCtx.values : {};
16
+ const title = (0, utils_1.setTitleValues)(name, ((0, utils_1.getTitle)(props) ?? ""), values);
14
17
  const description = (0, utils_1.getDescription)(props);
15
18
  const { help, info, isCollapsedOnLoad = true, openIcon, collapsedIcon,
16
19
  //TODO: This is not right
@@ -27,6 +27,7 @@ __exportStar(require("./createInitialValues"), exports);
27
27
  __exportStar(require("./createValidationSchema"), exports);
28
28
  __exportStar(require("./orderByUiSchema"), exports);
29
29
  __exportStar(require("./orderAdditionalProperties"), exports);
30
+ __exportStar(require("./setTitleValues"), exports);
30
31
  //TODO: REMOVE WHEN ALL SUPPORTED
31
32
  const warnFromUnsupportedKeys = (componentName, data, supportedKeys) => {
32
33
  Object.keys(data).forEach((key) => {
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setTitleValues = void 0;
4
+ const formik_1 = require("formik");
5
+ // Replaces template string variables with form data for CollapsibleObjectFieldTemplate titles
6
+ const setTitleValues = (name, title, values) => {
7
+ const match = name.match(/^([a-zA-Z_$][\w$]*)\[(\d+)\]$/);
8
+ if (match) {
9
+ name = match[1];
10
+ }
11
+ const index = match ? parseInt(match[2]) : -1;
12
+ const itemValues = match ? values[name][index] : values[name];
13
+ return title.replace(/\$\{([\w$.]+)\}/g, (match) => {
14
+ const keyMatch = match.match(/^\$\{([\w$.]+)\}$/);
15
+ const key = keyMatch ? keyMatch[1] : "";
16
+ return key === "index" ? (index + 1).toString() :
17
+ (0, formik_1.getIn)(itemValues, key) !== undefined ? (0, formik_1.getIn)(itemValues, key) : key;
18
+ });
19
+ };
20
+ exports.setTitleValues = setTitleValues;
@@ -1,13 +1,16 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useFormikContext } from "formik";
2
3
  import { useContext, useMemo, useState } from "react";
3
- import { extractUiSchema, getDescription, getTitle } from "../../../utils";
4
+ import { extractUiSchema, getDescription, getTitle, setTitleValues } from "../../../utils";
4
5
  import { FormRegistry } from "../../FormRegistry";
5
6
  import { getComponents } from "./styledComponents";
6
7
  export const CollapsibleObjectFieldTemplate = (props) => {
7
8
  const { components } = useContext(FormRegistry);
8
9
  const { Toggle, ChildrenWrapper, RequiredIndicator, Icon, Description, Title, Wrapper, Help, Info, } = getComponents(components);
9
10
  const { children, name, uiSchema, ...otherProps } = props;
10
- const title = getTitle(props);
11
+ const formikCtx = useFormikContext();
12
+ const values = formikCtx.values ? formikCtx.values : {};
13
+ const title = setTitleValues(name, (getTitle(props) ?? ""), values);
11
14
  const description = getDescription(props);
12
15
  const { help, info, isCollapsedOnLoad = true, openIcon, collapsedIcon,
13
16
  //TODO: This is not right
@@ -10,6 +10,7 @@ export * from "./createInitialValues";
10
10
  export * from "./createValidationSchema";
11
11
  export * from "./orderByUiSchema";
12
12
  export * from "./orderAdditionalProperties";
13
+ export * from "./setTitleValues";
13
14
  //TODO: REMOVE WHEN ALL SUPPORTED
14
15
  export const warnFromUnsupportedKeys = (componentName, data, supportedKeys) => {
15
16
  Object.keys(data).forEach((key) => {
@@ -0,0 +1,16 @@
1
+ import { getIn } from "formik";
2
+ // Replaces template string variables with form data for CollapsibleObjectFieldTemplate titles
3
+ export const setTitleValues = (name, title, values) => {
4
+ const match = name.match(/^([a-zA-Z_$][\w$]*)\[(\d+)\]$/);
5
+ if (match) {
6
+ name = match[1];
7
+ }
8
+ const index = match ? parseInt(match[2]) : -1;
9
+ const itemValues = match ? values[name][index] : values[name];
10
+ return title.replace(/\$\{([\w$.]+)\}/g, (match) => {
11
+ const keyMatch = match.match(/^\$\{([\w$.]+)\}$/);
12
+ const key = keyMatch ? keyMatch[1] : "";
13
+ return key === "index" ? (index + 1).toString() :
14
+ getIn(itemValues, key) !== undefined ? getIn(itemValues, key) : key;
15
+ });
16
+ };
@@ -10,4 +10,5 @@ export * from "./createInitialValues";
10
10
  export * from "./createValidationSchema";
11
11
  export * from "./orderByUiSchema";
12
12
  export * from "./orderAdditionalProperties";
13
+ export * from "./setTitleValues";
13
14
  export declare const warnFromUnsupportedKeys: (componentName: string, data: Record<string, any>, supportedKeys: string[]) => void;
@@ -0,0 +1 @@
1
+ export declare const setTitleValues: (name: string, title: string, values: Record<string, Array<Record<string, unknown>>>) => string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nolwenture/formik-jsonschema-form",
3
- "version": "1.0.0-beta.33",
3
+ "version": "1.0.0-beta.34",
4
4
  "description": "Formik form generation from json schema",
5
5
  "author": "anttivikman <antti.vikman@nolwenture.com>",
6
6
  "contributors": [