gpt4 book ai didi

reactjs - React Formik Material UI 自动完成 : How can I populate value inside of autocomplete from localStorage?

转载 作者:行者123 更新时间:2023-12-03 13:36:52 25 4
gpt4 key购买 nike

所以,我正在尝试使用 Formik 和 Material UI 创建表单。对于所有字段,一切都正常工作,但问题在于自动完成。我找不到从本地存储填充该字段的方法。我尝试了一切,从放置 value props、inputValue、defaultValue 等,但似乎没有任何效果。

import React from 'react'
import { Grid } from '@material-ui/core'
import { Autocomplete } from '@material-ui/lab'
import { Formik, Form, Field } from 'formik'
import { TextField } from 'formik-material-ui'
import * as yup from 'yup'
import { nationality } from '../../constants/nationality'
import Button from '../Button/Button'

export default function ForeignAddress () {

let localStorageData = localStorage.getItem('foreignAddress'),
retrivedData = JSON.parse(localStorageData)


const handleNextClick = () => {
console.log('clicked next')
}

const handleBackClick = () => {
console.log('clicked back')
}

const validationSchema = yup.object({
streetName: yup.string().required('Street name is required'),
streetNumber: yup.string().required('Street number is required'),
postalCode: yup.string().required('Postal code is required'),
city: yup.string().required('City is required'),
country: yup.string().required('Country is required'),
})

console.log(retrivedData)

return (
<React.Fragment>
<div className="pages-wrapper address">
<Formik
initialValues={retrivedData ? retrivedData : {streetName: '', streetNumber: '', postalCode: '', city: '', coAddress: '', country: ''}}
onSubmit={(data) => {
console.log(data)
localStorage.setItem('foreignAddress', JSON.stringify(data))
handleNextClick()
}}
validationSchema={validationSchema}
>
{({setFieldValue}) => (
<Form>
<Grid container spacing={3}>
<Grid item xs={12} md={8}>
<Field component={TextField} name="streetName" label="Street Name" variant="outlined" fullWidth />
</Grid>
<Grid item xs={12} md={4}>
<Field component={TextField} name="streetNumber" label="Street Number" variant="outlined" fullWidth />
</Grid>
<Grid item xs={12} md={4}>
<Field component={TextField} name="postalCode" label="Postal Code" variant="outlined" fullWidth />
</Grid>
<Grid item xs={12} md={8}>
<Field component={TextField} name="city" label="City" variant="outlined" fullWidth />
</Grid>
<Grid item xs={12} md={6}>
<Field component={TextField} name="coAddress" label="C/O Address" variant="outlined" fullWidth />
</Grid>
<Grid item xs={12} md={6}>
<Autocomplete
id="foreignCountry"
className="country-select"
name="country"
options={nationality}
getOptionLabel={option => option.label}
onChange={(e, value) => {
console.log(value)
setFieldValue("country", value.code)
}}
renderInput={params => (
<Field component={TextField} {...params} name="country" label="Country" variant="outlined" fullWidth/>
)}
/>
</Grid>
</Grid>
<div className="button-wrapper">
<Button label="Back" go="back" handleClick={handleBackClick}/>
<Button label="Next" go="next" type="submit" />
</div>
</Form>
)}
</Formik>
</div>
</React.Fragment>
)
}

编辑:

感谢@Vencovsky,我能够完成它

以防将来有人需要这个,这里是工作代码。只需将自动完成组件更改为

<Autocomplete
id="foreignCountry"
className="country-select"
name="country"
options={nationality}
getOptionLabel={option => option.label}
defaultValue={values.country}
onChange={(e, value) => {
console.log(value)
setFieldValue("country", value)
}}
renderInput={params => (
<Field component={TextField} {...params} name="country" label="Country" variant="outlined" fullWidth/>
)}
/>

在 Formik 属性中只需添加 value 属性

{({setFieldValue, values}) => (
<Form>,...

最佳答案

编辑:

您需要更改一些内容。

首先,您不能只存储代码以便稍后加载,您需要存储选项中的所有内容(空洞 value 对象)。

country: ''的初始值更改为country: {code: "", label: "", Phone: ""},均为默认值选项的值(value)。

然后,要正确加载值,您应该传递 value={values.country},其中 values 来自 formik Prop 。

onChange 上,您应该保存洞值 onChange={(e, value) => {setFieldValue("country", value); }}

但是您还导入和使用了一些错误的东西,例如

  <Field
component={TextField}
{...params}
name="country"
label="Country"
variant="outlined"
fullWidth
/>

其中Field是formik表单,TextField来自formik Material ui。

不知道你为什么这样使用它,但我已经改变了它。

这是一个working example

关于reactjs - React Formik Material UI 自动完成 : How can I populate value inside of autocomplete from localStorage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59249886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com