8000 final-form ^4.20.3 introduced a breaking change for array field names (e.g. choices[]), which worked in v4.20.2 · Issue #456 · final-form/final-form · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
final-form ^4.20.3 introduced a breaking change for array field names (e.g. choices[]), which worked in v4.20.2 #456
Closed
@antonvialibri1

Description

@antonvialibri1

Are you submitting a bug report or a feature request?

Bug report

What is the current behavior?

Starting from final-form v4.20.3 and above, checkboxes with array field names (e.g. choices[]) started working unexpectedly, they used to work properly until v4.20.2.

final-form@4.20.2

When using final-form@4.20.2:

"final-form": "4.20.2",
"react-final-form": "^6.5.9",

The code below works as expected (v4.20.2 Codesandbox):

/* eslint-disable jsx-a11y/accessible-emoji */
import React from "react";
import { render } from "react-dom";
import { Form, useField } from "react-final-form";

const FinalFormCheckbox = ({ name, value, label }) => {
  const renderProps = useField(name, {
    type: "checkbox",
    value
  });

  return (
    <label>
      <input name={name} type="checkbox" value={value} {...renderProps.input} />
      <span>{label}</span>
    </label>
  );
};

const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

const onSubmit = async (values) => {
  await sleep(300);
  window.alert(JSON.stringify(values, 0, 2));
};

const App = () => (
  <>
    <h1>React Final Form - Checkbox Example - v4.20.2 - OK </h1>
    <Form
      onSubmit={onSubmit}
      initialValues={{}}
      render={({ handleSubmit, form, submitting, values }) => (
        <form onSubmit={handleSubmit}>
          <div>
            <FinalFormCheckbox
              name="choices[]"
              value="choice_1"
              label="Choice 1"
            />
            <FinalFormCheckbox
              name="choices[]"
              value="choice_2"
              label="Choice 2"
            />
            <FinalFormCheckbox
              name="choices[]"
              value="choice_3"
              label="Choice 3"
            />
          </div>

          <div className="buttons">
            <button type="submit" disabled={submitting}>
              Submit
            </button>
            <button type="button" onClick={form.reset} disabled={submitting}>
              Reset
            </button>
          </div>
          <pre>{JSON.stringify(values, 0, 2)}</pre>
        </form>
      )}
    />
  </>
);

render(<App />, document.getElementById("root"));

If you check both Choice 2 and Choice 3 you get this shape of data for values:

{
  "choices": [
    "choice_2",
    "choice_3"
  ]
}

Checkboxes appear to be checked:

final-form_v4 20 2_checkbox_array_OK

final-form@^4.20.3, up to v4.20.7

On the other hand, when using final-form@4.20.3 and above (e.g. v4.20.7):

"final-form": "^4.20.3",
"react-final-form": "^6.5.9",

The same code used above for v4.20.2 doesn't work as expected in version 4.20.3 and upwards:

If you check both Choice 2 and Choice 3 you get this wrong shape of data for values:

{
  "choices": [
    [
      "choice_3"
    ]
  ]
}

Also, checkboxes remain in an unchecked state, despite having been checked:

final-form_v4 20 3_checkbox_array_ISSUE
final-form_v4 20 7_checkbox_array_ISSUE

What is the expected behavior?

v4.20.3 and above (e.g. v4.20.7) should behave consistently as version v4.20.2.

Sandbox Link

What's your environment?

Google Chrome

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0