antoniopresto / antd-mask-input

MIT License
88 stars 38 forks source link

form.setFieldValue does not work #60

Open pogasanov opened 2 years ago

pogasanov commented 2 years ago

Consider following component. forceRerender is only for testing purposes to manually trigger component rerender.

import {Form} from "antd";
import {MaskedInput} from "antd-mask-input";
import {useState} from "react";

export function Dummy() {
  const [form] = Form.useForm()
  const [forceRerender, setForceRerender] = useState({})
  return (
    <div>
      <button onClick={() => {
        form.setFieldValue("test", "1234")
        /*
        None works:
        form.setFieldsValue({
          test: '1234'
        })
        form.setFields([{
          name: 'test',
          value: '1234'
        }])
         */
      }}>
        set
      </button>
      <button onClick={() => {
        setForceRerender({})
      }}>
        force rerender
      </button>
      <Form form={form}>
        <Form.Item
          name="test"
        >
          <MaskedInput
            mask={
              '00-00'
            }
          />
        </Form.Item>
      </Form>
    </div>
  )
}

Steps to reproduce:

  1. Type anything in input
  2. Press set button
  3. Press force rerender button

Expected outcome: Input value is 1234 as that's what form contains

Actual outcome: Input value is whatever user typed on step 1.

Package versions: react: 17.0.2 antd: 4.23.5 antd-mask-input: 2.0.7

nandodutra commented 1 year ago

I`m having the same issue here, any idea how can i solve this?