data-driven-forms / react-forms

React library for rendering forms.
https://data-driven-forms.org/
Apache License 2.0
295 stars 85 forks source link

When function in sequence called with `undefined` field #1422

Closed RomanKosovnenko closed 10 months ago

RomanKosovnenko commented 10 months ago

Scope: Form renderer

Description When changing the value of the select field condition.type, the function getFieldNumber(field) is called six times. On the fourth and sixth calls, the field parameter is set to undefined. Consequently, the dependent field does not appear as expected.

A simplified schema is provided below.

Function:

function getFieldNumber(field: any) {
    console.log("getFieldNumber ~ field:", field)
    if (field) {
        return field.name;
    }
}

Based on my debugging attempts, the issue is located in parse-condition.js:86, where parseCondition is called without field parameter.

Schema

const schema = {
  fields: [
    {
      component: componentTypes.SELECT,
      name: "condition.type",
      label: "Condition",
      simpleValue: true,
      clearOnUnmount: true,
      options: [
        { label: "is", value: "is" },
        { label: "is Empty", value: "isEmpty" },
      ],
    },
    {
      component: componentTypes.TEXT_FIELD,
      name: "condition.value",
      label: "Value",
      placeholder: "18",
      clearOnUnmount: true,
      condition: {
        sequence: [
          {
            when: (field) => `${getFieldNumber(field)}.condition.type`,
            pattern: "(is$)",
            flags: "i",
            then: {
              set: (formState: any) => {
                console.log("without empty pattern");
              },
              visible: true,
            },
          },
          {
            when: (field) => `${getFieldNumber(field)}.condition.type`,
            pattern: "Empty",
            then: {
              set: (formState: any) => {
                return { [`condition.value`]: "true" };
              },
              visible: false,
            },
          },
        ],
      },
    },
  ],
};
rvsia commented 10 months ago

Thanks for reporting this bug! I will create a fix ASAP.

rvsia commented 10 months ago

Fix here: https://github.com/data-driven-forms/react-forms/pull/1423

rvsia commented 10 months ago

:tada: This issue has been resolved in version 3.21.10 :tada:

The release is available on

Data-Driven-Forms.org!