italoiz / unform-community-packages

MIT License
61 stars 10 forks source link

Uncontroled Checkbox setData not work's #37

Closed jairokoning closed 3 years ago

jairokoning commented 3 years ago

Hello @italoiz

When I try to set checked a uncontrolled Checkbox in formRef.current.setData nothing is happening, it seems that this is not working. But the Textfield is working normal.

Code on CodeSandbox

import React, { useEffect, useRef } from "react";
import { Form } from "@unform/web";
import { FormHandles } from "@unform/core";
import { Checkbox, TextField } from "unform-material-ui";

export default function CheckboxLabels() {
  const formRef = useRef<FormHandles>(null);

  function handleSubmit(data) {
    formRef.current.setData({
      name: !data.terms,
      terms: !data.terms
    });
    console.log(data);
  }

  useEffect(() => {
    formRef.current.setData({
      name: "false",
      terms: true
    });

    formRef.current.setFieldValue("terms", true);
  }, []);

  return (
    <Form ref={formRef} onSubmit={handleSubmit}>
      <div>
        <Checkbox name="terms" />
      </div>

      <div>
        <TextField name="name" />
      </div>

      <button type="submit">Send</button>
    </Form>
  );
}

checbox

jairokoning commented 3 years ago

@italoiz Is the reported problem a bug or is there something wrong with the code? Because the property is changed to true/false but visually in the checkbox nothing happens.

italoiz commented 3 years ago

Hi @jairokoning, it looks like a bug, I will take it easy this week.

jairokoning commented 3 years ago

@italoiz any fix for the bug? I needed a solution with some urgency, because I am using the component in a professional project in the company where I work.

italoiz commented 3 years ago

@jairokoning I cannot guarantee but in the next few days, I will be releasing a new version. Thank you!