繁体   English   中英

如何将 react-phone-number-input 与 Formik 的 Field 组件一起使用

[英]How to use react-phone-number-input with Formik's Field component

我正在使用 Formik 并尝试使用此 package,但 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
    })
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM