切换到formik中字段之间的复制值

发布于 2025-02-01 12:44:12 字数 13258 浏览 2 评论 0 原文

嗨,我正在NextJS上构建带有Formik的表单,我正在放置一个开关,以使用户有可能使用联系方式进行发票。

我有有关联系方式和发票详细信息的不同字段,但我想添加一个将联系人Infos复制到发票详细信息表格中的开关。

这就是我现在正在做的:

<Formik
                initialValues={{
                    passengers: passengers,
                    contact: contactDetails,
                  invoice: useContactDetails ? {...invoiceDetails, ...contactDetails} : invoiceDetails
                }}
                onSubmit={
                    (values, actions) => {
                       console.log(values)
                    }
                }
            >
                {({errors, touched, isSubmitting}: any) => (
                    <Form>
                        <PassengerItem>
                            <PassengerHeaderContainer>
                                <PassengerHeader>
                                    {(t('flightsCheckout.main.contactDetails'))}
                                </PassengerHeader>
                            </PassengerHeaderContainer>

                            <FormGroup>
                                <InputGroup>
                                    <InputLabel>
                                        {t('flightsCheckout.main.firstName')}
                                    </InputLabel>
                                    <TextInput
                                        type="text"
                                        placeholder="E.G. John (Given Name)"
                                        name={`contact.firstName`}
                                        required
                                    />
                                </InputGroup>
                                <DottedLinesContainer>
                                    <DottedLines />
                                </DottedLinesContainer>
                                <InputGroup>
                                    <InputLabel>
                                        {t('flightsCheckout.main.lastName')}
                                    </InputLabel>
                                    <TextInput
                                        type="text"
                                        placeholder="E.G. Smith (Last Name)"
                                        name={`contact.lastName`}
                                        required
                                    />
                                </InputGroup>
                            </FormGroup>

                            <FormGroup>
                                <InputGroup>
                                    <InputLabel>
                                        {t('flightsCheckout.main.phoneNumber')}
                                    </InputLabel>
                                    <TextInput
                                        type="tel"
                                        placeholder="E.G. +30 645 654 9850"
                                        name={`contact.phoneNumber`}
                                        required
                                    />
                                </InputGroup>
                                <DottedLinesContainer>
                                    <DottedLines />
                                </DottedLinesContainer>
                                <InputGroup>
                                    <InputLabel>
                                        {t('flightsCheckout.main.emailAddress')}
                                    </InputLabel>
                                    <TextInput
                                        type="email"
                                        placeholder="E.G. [email protected]"
                                        name={`contact.emailAddress`}
                                        required
                                    />
                                    {
                                        errors.contactEmail &&
                                        <ErrorText>{errors.contactEmail}</ErrorText>
                                    }
                                </InputGroup>
                            </FormGroup>

                            <FormGroup>
                                <InputGroup>
                                    <InputLabel>
                                        {t('flightsCheckout.main.country')}
                                    </InputLabel>
                                    <TextInput name={`contact.country`} required as="select" style={{background: "none"}}>
                                        <option style={{color: "#ADADAD"}} value="" disabled selected>E.G. United Kingdom</option>
                                        <option value="spain">Spain</option>
                                        <option value="morocco">Morocco</option>
                                        <option value="romania">Romania</option>
                                    </TextInput>
                                </InputGroup>
                            </FormGroup>

                        </PassengerItem>

                        <PassengerItem>
                            <PassengerHeaderContainer>
                                <PassengerHeader>
                                    {(t('flightsCheckout.main.invoiceDetails'))}
                                </PassengerHeader>
                            </PassengerHeaderContainer>

                            <FormGroup>
                                <CustomSwitch
                                    label={t('flightsCheckout.main.sameAsContact')}
                                    state={useContactDetails}
                                    setState={setUseContactDetails}
                                />
                            </FormGroup>

                            <FormGroup>
                                <InputGroup>
                                    <InputLabel>
                                        {t('flightsCheckout.main.firstName')}
                                    </InputLabel>
                                    <TextInput
                                        type="text"
                                        placeholder="E.G. John (Given Name)"
                                        name={`invoice.firstName`}
                                        required
                                    />
                                </InputGroup>
                                <DottedLinesContainer>
                                    <DottedLines />
                                </DottedLinesContainer>
                                <InputGroup>
                                    <InputLabel>
                                        {t('flightsCheckout.main.lastName')}
                                    </InputLabel>
                                    <TextInput
                                        type="text"
                                        placeholder="E.G. Smith (Last Name)"
                                        name={`invoice.lastName`}
                                        required
                                    />
                                </InputGroup>
                            </FormGroup>

                            <FormGroup>
                                <InputGroup>
                                    <InputLabel>
                                        {t('flightsCheckout.main.phoneNumber')}
                                    </InputLabel>
                                    <TextInput
                                        type="tel"
                                        placeholder="E.G. +30 645 654 9850"
                                        name={`invoice.phoneNumber`}
                                        required
                                    />
                                </InputGroup>
                                <DottedLinesContainer>
                                    <DottedLines />
                                </DottedLinesContainer>
                                <InputGroup>
                                    <InputLabel>
                                        {t('flightsCheckout.main.emailAddress')}
                                    </InputLabel>
                                    <TextInput
                                        type="email"
                                        placeholder="E.G. [email protected]"
                                        name={`invoice.emailAddress`}
                                        required
                                    />
                                    {
                                        errors.invoiceEmail &&
                                        <ErrorText>{errors.invoiceEmail}</ErrorText>
                                    }
                                </InputGroup>
                            </FormGroup>

                            <FormGroup>
                                <InputGroup>
                                    <InputLabel>
                                        {t('flightsCheckout.main.country')}
                                    </InputLabel>
                                    <TextInput name={`invoice.country`} required as="select" style={{background: "none"}}>
                                        <option style={{color: "#ADADAD"}} value="" disabled selected>E.G. United Kingdom</option>
                                        <option value="spain">Spain</option>
                                        <option value="morocco">Morocco</option>
                                        <option value="romania">Romania</option>
                                    </TextInput>
                                </InputGroup>
                            </FormGroup>

                            <FormGroup>
                                <InputGroup>
                                    <InputLabel>
                                        {t('flightsCheckout.main.address')}
                                    </InputLabel>
                                    <TextInput
                                        type="text"
                                        placeholder="E.G. 64 Notley Street"
                                        name={`invoice.address`}
                                        required
                                    />
                                </InputGroup>
                                <DottedLinesContainer>
                                    <DottedLines />
                                </DottedLinesContainer>
                                <InputGroup>
                                    <InputLabel>
                                        {t('flightsCheckout.main.city')}
                                    </InputLabel>
                                    <TextInput
                                        type="text"
                                        placeholder="E.G. London"
                                        name={`invoice.city`}
                                        required
                                    />
                                </InputGroup>
                            </FormGroup>

                            <FormGroup>
                                <InputGroup>
                                    <InputLabel>
                                        {t('flightsCheckout.main.zipCode')}
                                    </InputLabel>
                                    <TextInput
                                        type="text"
                                        placeholder="E.G. 40741"
                                        name={`invoice.zipCode`}
                                        required
                                    />
                                </InputGroup>
                            </FormGroup>
                        </PassengerItem>
                )}
            </Formik>

我尝试过,但它没有起作用...

Hi I'm building a form with Formik on NextJS and I'm putting a switch to give the user the possibility to use contact details for invoice.

I have different fields for both contact details and invoice details but I want to add a switch that will copy the contact infos into the invoice details form.

enter image description here

that's what I'm doing now :

<Formik
                initialValues={{
                    passengers: passengers,
                    contact: contactDetails,
                  invoice: useContactDetails ? {...invoiceDetails, ...contactDetails} : invoiceDetails
                }}
                onSubmit={
                    (values, actions) => {
                       console.log(values)
                    }
                }
            >
                {({errors, touched, isSubmitting}: any) => (
                    <Form>
                        <PassengerItem>
                            <PassengerHeaderContainer>
                                <PassengerHeader>
                                    {(t('flightsCheckout.main.contactDetails'))}
                                </PassengerHeader>
                            </PassengerHeaderContainer>

                            <FormGroup>
                                <InputGroup>
                                    <InputLabel>
                                        {t('flightsCheckout.main.firstName')}
                                    </InputLabel>
                                    <TextInput
                                        type="text"
                                        placeholder="E.G. John (Given Name)"
                                        name={`contact.firstName`}
                                        required
                                    />
                                </InputGroup>
                                <DottedLinesContainer>
                                    <DottedLines />
                                </DottedLinesContainer>
                                <InputGroup>
                                    <InputLabel>
                                        {t('flightsCheckout.main.lastName')}
                                    </InputLabel>
                                    <TextInput
                                        type="text"
                                        placeholder="E.G. Smith (Last Name)"
                                        name={`contact.lastName`}
                                        required
                                    />
                                </InputGroup>
                            </FormGroup>

                            <FormGroup>
                                <InputGroup>
                                    <InputLabel>
                                        {t('flightsCheckout.main.phoneNumber')}
                                    </InputLabel>
                                    <TextInput
                                        type="tel"
                                        placeholder="E.G. +30 645 654 9850"
                                        name={`contact.phoneNumber`}
                                        required
                                    />
                                </InputGroup>
                                <DottedLinesContainer>
                                    <DottedLines />
                                </DottedLinesContainer>
                                <InputGroup>
                                    <InputLabel>
                                        {t('flightsCheckout.main.emailAddress')}
                                    </InputLabel>
                                    <TextInput
                                        type="email"
                                        placeholder="E.G. [email protected]"
                                        name={`contact.emailAddress`}
                                        required
                                    />
                                    {
                                        errors.contactEmail &&
                                        <ErrorText>{errors.contactEmail}</ErrorText>
                                    }
                                </InputGroup>
                            </FormGroup>

                            <FormGroup>
                                <InputGroup>
                                    <InputLabel>
                                        {t('flightsCheckout.main.country')}
                                    </InputLabel>
                                    <TextInput name={`contact.country`} required as="select" style={{background: "none"}}>
                                        <option style={{color: "#ADADAD"}} value="" disabled selected>E.G. United Kingdom</option>
                                        <option value="spain">Spain</option>
                                        <option value="morocco">Morocco</option>
                                        <option value="romania">Romania</option>
                                    </TextInput>
                                </InputGroup>
                            </FormGroup>

                        </PassengerItem>

                        <PassengerItem>
                            <PassengerHeaderContainer>
                                <PassengerHeader>
                                    {(t('flightsCheckout.main.invoiceDetails'))}
                                </PassengerHeader>
                            </PassengerHeaderContainer>

                            <FormGroup>
                                <CustomSwitch
                                    label={t('flightsCheckout.main.sameAsContact')}
                                    state={useContactDetails}
                                    setState={setUseContactDetails}
                                />
                            </FormGroup>

                            <FormGroup>
                                <InputGroup>
                                    <InputLabel>
                                        {t('flightsCheckout.main.firstName')}
                                    </InputLabel>
                                    <TextInput
                                        type="text"
                                        placeholder="E.G. John (Given Name)"
                                        name={`invoice.firstName`}
                                        required
                                    />
                                </InputGroup>
                                <DottedLinesContainer>
                                    <DottedLines />
                                </DottedLinesContainer>
                                <InputGroup>
                                    <InputLabel>
                                        {t('flightsCheckout.main.lastName')}
                                    </InputLabel>
                                    <TextInput
                                        type="text"
                                        placeholder="E.G. Smith (Last Name)"
                                        name={`invoice.lastName`}
                                        required
                                    />
                                </InputGroup>
                            </FormGroup>

                            <FormGroup>
                                <InputGroup>
                                    <InputLabel>
                                        {t('flightsCheckout.main.phoneNumber')}
                                    </InputLabel>
                                    <TextInput
                                        type="tel"
                                        placeholder="E.G. +30 645 654 9850"
                                        name={`invoice.phoneNumber`}
                                        required
                                    />
                                </InputGroup>
                                <DottedLinesContainer>
                                    <DottedLines />
                                </DottedLinesContainer>
                                <InputGroup>
                                    <InputLabel>
                                        {t('flightsCheckout.main.emailAddress')}
                                    </InputLabel>
                                    <TextInput
                                        type="email"
                                        placeholder="E.G. [email protected]"
                                        name={`invoice.emailAddress`}
                                        required
                                    />
                                    {
                                        errors.invoiceEmail &&
                                        <ErrorText>{errors.invoiceEmail}</ErrorText>
                                    }
                                </InputGroup>
                            </FormGroup>

                            <FormGroup>
                                <InputGroup>
                                    <InputLabel>
                                        {t('flightsCheckout.main.country')}
                                    </InputLabel>
                                    <TextInput name={`invoice.country`} required as="select" style={{background: "none"}}>
                                        <option style={{color: "#ADADAD"}} value="" disabled selected>E.G. United Kingdom</option>
                                        <option value="spain">Spain</option>
                                        <option value="morocco">Morocco</option>
                                        <option value="romania">Romania</option>
                                    </TextInput>
                                </InputGroup>
                            </FormGroup>

                            <FormGroup>
                                <InputGroup>
                                    <InputLabel>
                                        {t('flightsCheckout.main.address')}
                                    </InputLabel>
                                    <TextInput
                                        type="text"
                                        placeholder="E.G. 64 Notley Street"
                                        name={`invoice.address`}
                                        required
                                    />
                                </InputGroup>
                                <DottedLinesContainer>
                                    <DottedLines />
                                </DottedLinesContainer>
                                <InputGroup>
                                    <InputLabel>
                                        {t('flightsCheckout.main.city')}
                                    </InputLabel>
                                    <TextInput
                                        type="text"
                                        placeholder="E.G. London"
                                        name={`invoice.city`}
                                        required
                                    />
                                </InputGroup>
                            </FormGroup>

                            <FormGroup>
                                <InputGroup>
                                    <InputLabel>
                                        {t('flightsCheckout.main.zipCode')}
                                    </InputLabel>
                                    <TextInput
                                        type="text"
                                        placeholder="E.G. 40741"
                                        name={`invoice.zipCode`}
                                        required
                                    />
                                </InputGroup>
                            </FormGroup>
                        </PassengerItem>
                )}
            </Formik>

I tried this but it's not working...

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

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

发布评论

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

评论(1

拥有 2025-02-08 12:44:13

我认为您想将 enableReInitialize 设置为在formik上的true,以便将其刷新新值。默认值设置为false,并且由于您正在更改初始值,因此您需要重新初始化

I think you want to set enableReinitialize to true on Formik in order for it to refresh with the new values. The default is set to false, and since you are changing the initialValues, you need to reinitialize it

https://formik.org/docs/api/formik#enablereinitialize-boolean

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