如何在 Formik 的 Field 组件中使用 react-phone-number-input
How to use react-phone-number-input with Formik's Field component
我正在使用 Formik 并尝试使用此包,但 Formik 无法看到输入的值,因此无法验证或提交表单。
这是我正在使用的 PhonInputField 组件...
import PhoneInput from "react-phone-number-input";
const PhoneInputField = ({
field,
form
}) => {
return (
<div className="input-field">
<PhoneInput
placeholder="Enter phone number"
name={field.name}
value={field.value}
onChange={field.onChange}
onBlur={field.onBlur}
/>
</div>
);
};
export default PhoneInputField;
下面是我在 Formik 中使用它的方式...
<Formik
initialValues={initialValues}
validationSchema={signInSchema}
onSubmit={handleFormSubmit}
>
{({
isValid,
isSubmitting,
handleChange,
handleBlur
}) => (
<Form>
<Field
type="tel"
name="phone_number"
component={PhoneInputField}
/>
<div className="cta">
<button
type="submit"
disabled={!isValid || isSubmitting}
className="btn btn-primary big"
>Submit</button>
</div>
</Form>
)}
</Formik>
我做错了什么?
============================================= ==========
我试图在这个项目的 Github 回购页面上问这个问题,但没有成功。
我也试过这样做...
<PhoneInput
placeholder="Enter phone number"
name={field.name}
value={field.value}
onChange={value => field.onChange({ value })}
onBlur={value => field.onBlur({ value })}
/>
但是,Formik 仍然看不到字段值。
对于那些可能遇到这个问题的人,这里的 Github 回购页面已经回答了...
https://github.com/catamphetamine/react-phone-number-input/issues/298#issuecomment-557746103
onChange={(phone, country) =>
setValues({
...values,
phone: phone,
countryCode: country.countryCode
})
}
我正在使用 Formik 并尝试使用此包,但 Formik 无法看到输入的值,因此无法验证或提交表单。
这是我正在使用的 PhonInputField 组件...
import PhoneInput from "react-phone-number-input";
const PhoneInputField = ({
field,
form
}) => {
return (
<div className="input-field">
<PhoneInput
placeholder="Enter phone number"
name={field.name}
value={field.value}
onChange={field.onChange}
onBlur={field.onBlur}
/>
</div>
);
};
export default PhoneInputField;
下面是我在 Formik 中使用它的方式...
<Formik
initialValues={initialValues}
validationSchema={signInSchema}
onSubmit={handleFormSubmit}
>
{({
isValid,
isSubmitting,
handleChange,
handleBlur
}) => (
<Form>
<Field
type="tel"
name="phone_number"
component={PhoneInputField}
/>
<div className="cta">
<button
type="submit"
disabled={!isValid || isSubmitting}
className="btn btn-primary big"
>Submit</button>
</div>
</Form>
)}
</Formik>
我做错了什么?
============================================= ==========
我试图在这个项目的 Github 回购页面上问这个问题,但没有成功。
我也试过这样做...
<PhoneInput
placeholder="Enter phone number"
name={field.name}
value={field.value}
onChange={value => field.onChange({ value })}
onBlur={value => field.onBlur({ value })}
/>
但是,Formik 仍然看不到字段值。
对于那些可能遇到这个问题的人,这里的 Github 回购页面已经回答了...
https://github.com/catamphetamine/react-phone-number-input/issues/298#issuecomment-557746103
onChange={(phone, country) =>
setValues({
...values,
phone: phone,
countryCode: country.countryCode
})
}