[英]Autocomplete bug in material ui
I am creating a single page application with React and i have a shipment page where more than one product and product quantity will be add.我正在使用 React 创建一个单页应用程序,并且我有一个装运页面,其中将添加多个产品和产品数量。 I create some function for my case.
我为我的案例创建了一些 function。 My functions are working(sending correct value to backend ) but after i click add button the autocomplate displayed values are getting diffrent.
我的功能正在运行(向后端发送正确的值)但在我单击添加按钮后,自动完成显示的值变得不同。 How can i fix it?
我该如何解决?
Note: : The values written on the inputs in the picture are the selected values but different from the values in the autocomplate as seen注意::图片中输入上写的值是所选值,但与看到的自动编译中的值不同
My code:我的代码:
const [productList, setProductList] = useState([{ name: "", quantity: 1}]);
const [product, setProduct] = useState([""]);
const handleQuantityChange = (e, index) => {
const { name, value } = e.target;
const list = [...productList];
list[index][name] = value;
setProductList(list);
};
const handleLabelChange = (name, index, value) => {
const list = [...productList];
list[index][name] = value;
setProductList(list);
};
const handleServiceAdd = () => {
setProductList([...productList, { name: "", quantity: 0 }]);
};
return(
<div>
{productList.map((p, index) => (
<Autocomplete
isOptionEqualToValue={(option, value) => option.name === value.name}
value={p.name}
key={index}
disablePortal
onChange={(e, newValue) => {
handleLabelChange("name", index, newValue);
}}
id="controllable-states-demo"
options={product.map((option) => option.name?option.name:"loading...")}
renderInput={(params) => (
<TextField
{...register('productNameV')}
error={errors.productNameV ? true : false}
helperText={errors.productNameV?.message}
{...params}
label={ "Product"+ (index + 1) }
InputProps={{
...params.InputProps,
endAdornment: (
<React.Fragment>
{loading ? <CircularProgress color="inherit" size={20} /> : null}
{params.InputProps.endAdornment}
</React.Fragment>
),
}}
/>
)}
/>
</div>
))}
<div>)
While using yup and react hooks form, I had a validation like a single object:在使用 yup 和 react hooks 形式时,我进行了一次验证,如单个 object:
const validationSchema = Yup.object().shape({
product: Yup.string().required("product required"),
});
But i was working on array so validate must have array too so i added this:但是我正在处理数组所以验证也必须有数组所以我添加了这个:
const validationSchema = Yup.object().shape({
products: Yup.array()
.of(
Yup.object({
name: Yup.string().required("product required"),
quantity: Yup.number().required("quantity required"),
})
)
.required("")
});
last change, TextField should be like this:最后一次更改,TextField 应该是这样的:
<TextField
{...register(`products.${index}.name`)}
error={errors.products?.[index]?.name ? true : false}
helperText={errors.products?.[index]?.name?.message}
{...params}
label={ "Product"+ (index + 1) }
InputProps={{
...params.InputProps,
endAdornment: (
<React.Fragment>
{loading ? <CircularProgress color="inherit" size={20} /> : null}
{params.InputProps.endAdornment}
</React.Fragment>
),
}}
/>
)} )}
/> />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.