Using Formik with Ant Design, here is a simple setup.
You can see the entire code pasted here:
import React from "react";
import ReactDOM from "react-dom";
import "./styles.css";
import { Formik } from "formik";
import { Form, Input, InputNumber } from "antd";
function App() {
const name = "name";
const count = "count";
return (
<div className="App">
<h1>Formik Testing</h1>
<Formik
initialValues={{ count: 32, name: "sd" }}
validate={async values => {
console.log("Validate", values);
return {
count: `Value ${values.count} is not valid.`
};
}}
onSubmit={(values, actions) => {
console.log(values);
}}
render={props => {
return (
<div>
<Form>
<Form.Item help={props.errors && props.errors[name]}>
<Input
id={name}
name={name}
type={"text"}
onChange={v => {
console.log("Text change ", v);
props.handleChange(v);
}}
onBlur={props.handleBlur}
defaultValue={props.initialValues[name]}
placeholder="Name"
/>
</Form.Item>
<Form.Item help={props.errors && props.errors[count]}>
<InputNumber
id={count}
name={count}
type={"number"}
onChange={v => {
console.log("Number change ", v);
props.handleChange(v);
}}
onBlur={props.handleBlur}
defaultValue={props.initialValues[count]}
placeholder="Count"
/>
</Form.Item>
</Form>
</div>
);
}}
/>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
So basically:
handleChangeis called correctly by theInputNumbercomponent- validation is not triggered when the number field changes
- validation is triggered when the text field changes
- validation is triggered with the default value when
onblur(after clicking away from the input number field)
This seems like a bug, but I assume I am doing something wrong.
Any help is appreciated.
Code in action:
https://codesandbox.io/s/gallant-architecture-0c8p2
(The preview css seems to have a bug - in case you see multiple arrows, use the top one only)