如何与React-Phone-number输入的LIB成为朋友?

发布于 2025-01-25 20:37:59 字数 3434 浏览 2 评论 0 原文

我正在尝试使用Formik,Material UI和React-Phone-number输入LIB(用于电话号码格式)来开发一种表单。我遇到了一个问题。输入电话号码时,已经按预期进行了格式,但是该号码未插入formik state。因此,在formik中看不到电话号码值,因此,当输入某个值时,Formik无法将错误标记“必需”带走。猜测之后,我以不正确的方式使用了反应手机输入的lib和formik。如何正确使用它们?

github:

我有基本文件src/app.js。我使用PhonenumberInput组件的位置。这实际上是我的电话号码输入。

export const App = () => {
    return (
        <>
            <Formik
                initialValues={{
                    firstName: '',
                    lastName: '',
                    email: '',
                    phoneNumber: '',
                }}
                validationSchema={Yup.object({
                    firstName: Yup.string()
                        .max(15, 'Have to be 15 characters or less')
                        .required('Required'),
                    lastName: Yup.string()
                        .max(20, 'Have to be 20 or less characters')
                        .required('Required'),
                    email: Yup.string().required('Required.'),
                    phoneNumber: Yup.string().required('Required'),
                })}
                onSubmit={(values, { setSubmitting }) => {
                    setTimeout(() => {
                        alert(JSON.stringify(values, null, 2))
                        setSubmitting(false)
                    }, 400)
                }}
            >
                {context => (
                    <Form>
                        <MainContainer>
                            <Title text={'Step 2'} iconRender={<AccountCircleRoundedIcon />} />
                            <TextInput text={'Email'} name={'email'} />
                            <PhoneNumberInput
                                name={'phoneNumber'}
                                label={'Phone Number'}
                                context={context}
                            />
                            <MyButton>Next</MyButton>
                        </MainContainer>
                    </Form>
                )}
            </Formik>
        </>
    )
}

在src/components/phonenumberinput/phonenumberinput.js中,我定义了PhonenumberInput组件。 我使用来自React-Phone-number输入的输入组件,有机会使用自定义输入。

const MyField = React.forwardRef(function custom(props, ref) {
    const { name, label } = props
    return (
        <Field
            {...props}
            component={TextField}
            label={label}
            name={name}
            variant="outlined"
            inputRef={ref}
            fullWidth
        />
    )
})

export const PhoneNumberInput = ({ text, ...props }) => {
    const [value, setValue] = useState()
    const [focus, setFocus] = useState(false)

    console.log(props.context)
    return (
        <>
            <Input
                {...props}
                country="UA"
                international={focus}
                value={value}
                withCountryCallingCode
                onChange={setValue}
                inputComponent={MyField}
                onFocus={() => setFocus(true)}
                control={props.control}
            />
        </>
    )
}

怎么了?如何解决这个问题?

I am trying to develop a form using Formik, Material UI, and React-phone-number-input lib (for phone number formatting). I faced a problem. When a phone number is being entered, one is already formatted as intended, but that number is not inserted into Formik state. So, the phone number value is not visible for Formik, and as a result, Formik can not take away an error marker "Required", when some value is entered. Having Guessed, I use react-phone-number-input lib and Formik in the not right way together. How to use them right?

github:https://github.com/AlexKor-5/FormChallenge/tree/0d37064ef54c8e87a6effb950575a5a3817d9220

I have the base file src/App.js. Where I use PhoneNumberInput component. This is actually my phone number input.

export const App = () => {
    return (
        <>
            <Formik
                initialValues={{
                    firstName: '',
                    lastName: '',
                    email: '',
                    phoneNumber: '',
                }}
                validationSchema={Yup.object({
                    firstName: Yup.string()
                        .max(15, 'Have to be 15 characters or less')
                        .required('Required'),
                    lastName: Yup.string()
                        .max(20, 'Have to be 20 or less characters')
                        .required('Required'),
                    email: Yup.string().required('Required.'),
                    phoneNumber: Yup.string().required('Required'),
                })}
                onSubmit={(values, { setSubmitting }) => {
                    setTimeout(() => {
                        alert(JSON.stringify(values, null, 2))
                        setSubmitting(false)
                    }, 400)
                }}
            >
                {context => (
                    <Form>
                        <MainContainer>
                            <Title text={'Step 2'} iconRender={<AccountCircleRoundedIcon />} />
                            <TextInput text={'Email'} name={'email'} />
                            <PhoneNumberInput
                                name={'phoneNumber'}
                                label={'Phone Number'}
                                context={context}
                            />
                            <MyButton>Next</MyButton>
                        </MainContainer>
                    </Form>
                )}
            </Formik>
        </>
    )
}

And in src/components/PhoneNumberInput/PhoneNumberInput.js I define PhoneNumberInput component.
I use Input component from react-phone-number-input to have the opportunity to use a custom input.

const MyField = React.forwardRef(function custom(props, ref) {
    const { name, label } = props
    return (
        <Field
            {...props}
            component={TextField}
            label={label}
            name={name}
            variant="outlined"
            inputRef={ref}
            fullWidth
        />
    )
})

export const PhoneNumberInput = ({ text, ...props }) => {
    const [value, setValue] = useState()
    const [focus, setFocus] = useState(false)

    console.log(props.context)
    return (
        <>
            <Input
                {...props}
                country="UA"
                international={focus}
                value={value}
                withCountryCallingCode
                onChange={setValue}
                inputComponent={MyField}
                onFocus={() => setFocus(true)}
                control={props.control}
            />
        </>
    )
}

What is wrong? How to tackle that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒岛晴空 2025-02-01 20:37:59

实际上,在Formik Hooks的帮助下实现非常容易,

这是我的工作解决方案:

import PhoneInput from "react-phone-number-input";
import { useField } from "formik";

import FieldWrapper from "../FieldWrapper";

const PhoneInputField = ({ label, ...props }) => {
  const [field, meta, helpers] = useField(props.name);

  return (
    <FieldWrapper label={label} meta={meta} {...props}>
      <PhoneInput
        {...props}
        {...field}
        value={field.value}
        defaultCountry="NO"
        onChange={(value) => {
          helpers.setValue(value);
        }}
      />
    </FieldWrapper>
  );
};

export default PhoneInputField;

然后在您的Formik表单中使用此组件

// your boilerplate code ect...

<Formik
      initialValues={initialValues}
      validationSchema={validationSchema}
      onSubmit={onSubmit}
    >
     {({ handleSubmit, isSubmitting }) => (    
            <FieldPhoneInput
              label="Phone Number"
              name="phone"
              type="tel"
              placeholder="Write your phone number here"
            />
     )}
</Formik>

Actually it's pretty easy to implement with the help of Formik hooks

This is my working solution:

import PhoneInput from "react-phone-number-input";
import { useField } from "formik";

import FieldWrapper from "../FieldWrapper";

const PhoneInputField = ({ label, ...props }) => {
  const [field, meta, helpers] = useField(props.name);

  return (
    <FieldWrapper label={label} meta={meta} {...props}>
      <PhoneInput
        {...props}
        {...field}
        value={field.value}
        defaultCountry="NO"
        onChange={(value) => {
          helpers.setValue(value);
        }}
      />
    </FieldWrapper>
  );
};

export default PhoneInputField;

Then use this component in your Formik Form as so

// your boilerplate code ect...

<Formik
      initialValues={initialValues}
      validationSchema={validationSchema}
      onSubmit={onSubmit}
    >
     {({ handleSubmit, isSubmitting }) => (    
            <FieldPhoneInput
              label="Phone Number"
              name="phone"
              type="tel"
              placeholder="Write your phone number here"
            />
     )}
</Formik>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文