TextField组件中的map函数在位于material-ui组件中时不起作用。

huangapple go评论66阅读模式
英文:

The map function in TextField does not work if it is located in the component material-ui

问题

The map function in TextField does not work if it is located in the component material-ui

I have two TextFields in the first one the radio button works, in the second one the radio button does not work. How to do if the map function is located in a separate component to use the radio button?
if you try to replace the value in the first example, it will change correctly, in the second example it does not work. The only difference is that in the second example map is a separate file

https://codesandbox.io/s/green-snow-bmbcn2?file=/src/Test.js
TextField组件中的map函数在位于material-ui组件中时不起作用。

import * as React from "react";
import Box from "@mui/material/Box";
import TextField from "@mui/material/TextField";
import MenuItem from "@mui/material/MenuItem";
import MapCastom from "./MapCastom";

const currencies = [
  {
    value: "USD",
    label: "$"
  },
  {
    value: "EUR",
    label: "€"
  },
  {
    value: "BTC",
    label: "฿"
  },
  {
    value: "JPY",
    label: "¥"
  }
];

export default function SelectTextFields() {
  return (
    <Box
      component="form"
      sx={{
        "& .MuiTextField-root": { m: 1, width: "25ch" }
      }}
      noValidate
      autoComplete="off"
    >
      <div>
        <TextField
          id="outlined-select-currency"
          select
          label="Select"
          defaultValue="EUR"
          helperText="Please select your currency"
        >
          {currencies.map((option) => (
            <MenuItem key={option.value} value={option.value}>
              {option.label}
            </MenuItem>
          ))}
        </TextField>
      </div>

      <div>
        <TextField
          id="outlined-select-currency"
          select
          label="Select"
          defaultValue="EUR"
          helperText="Please select your currency"
        >
          <MapCastom data={currencies} />
        </TextField>
      </div>
    </Box>
  );
}
import React from "react";
import MenuItem from "@mui/material/MenuItem";

const MapCastom = ({ data }) => {
  return (
    <>
      {data.map((option) => (
        <MenuItem key={option.value} value={option.value}>
          {option.label}
        </MenuItem>
      ))}
    </>
  );
};

export default MapCastom;

<details>
<summary>英文:</summary>
The map function in TextField does not work if it is located in the component material-ui
I have two TextFields in the first one the radio button works, in the second one the radio button does not work. How to do if the map function is located in a separate component to use the radio button?
if you try to replace the value in the first example, it will change correctly, in the second example it does not work. The only difference is that in the second example map is a separate file
https://codesandbox.io/s/green-snow-bmbcn2?file=/src/Test.js
[![enter image description here][1]][1]
import * as React from &quot;react&quot;;
import Box from &quot;@mui/material/Box&quot;;
import TextField from &quot;@mui/material/TextField&quot;;
import MenuItem from &quot;@mui/material/MenuItem&quot;;
import MapCastom from &quot;./MapCastom&quot;;
const currencies = [
{
value: &quot;USD&quot;,
label: &quot;$&quot;
},
{
value: &quot;EUR&quot;,
label: &quot;€&quot;
},
{
value: &quot;BTC&quot;,
label: &quot;฿&quot;
},
{
value: &quot;JPY&quot;,
label: &quot;&#165;&quot;
}
];
export default function SelectTextFields() {
return (
&lt;Box
component=&quot;form&quot;
sx={{
&quot;&amp; .MuiTextField-root&quot;: { m: 1, width: &quot;25ch&quot; }
}}
noValidate
autoComplete=&quot;off&quot;
&gt;
&lt;div&gt;
&lt;TextField
id=&quot;outlined-select-currency&quot;
select
label=&quot;Select&quot;
defaultValue=&quot;EUR&quot;
helperText=&quot;Please select your currency&quot;
&gt;
{currencies.map((option) =&gt; (
&lt;MenuItem key={option.value} value={option.value}&gt;
{option.label}
&lt;/MenuItem&gt;
))}
&lt;/TextField&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;TextField
id=&quot;outlined-select-currency&quot;
select
label=&quot;Select&quot;
defaultValue=&quot;EUR&quot;
helperText=&quot;Please select your currency&quot;
&gt;
&lt;MapCastom data={currencies} /&gt;
&lt;/TextField&gt;
&lt;/div&gt;
&lt;/Box&gt;
);
}
import React from &quot;react&quot;;
import MenuItem from &quot;@mui/material/MenuItem&quot;;
const MapCastom = ({ data }) =&gt; {
return (
&lt;&gt;
{data.map((option) =&gt; (
&lt;MenuItem key={option.value} value={option.value}&gt;
{option.label}
&lt;/MenuItem&gt;
))}
&lt;/&gt;
);
};
export default MapCastom;
[1]: https://i.stack.imgur.com/6FL7R.jpg
</details>
# 答案1
**得分**: 2
在这里,您很可能遇到了警告:
&gt;MUI: 您提供了一个超出范围的
这意味着在首次渲染时,选择的值不在可能的选项之中。
修复这个问题的一种方法是不要使用一个组件,你已经需要的是一个数组,所以你可以用这个函数替换`MapCastom`:
```js
const MapCastom = (data) =&gt; {
return data.map((option) =&gt; (
&lt;MenuItem key={option.value} value={option.value}&gt;
{option.label}
&lt;/MenuItem&gt;
));
};

然后这样调用它:

&lt;TextField
  id=&quot;outlined-select-currency&quot;
  select
  label=&quot;Select&quot;
  defaultValue=&quot;EUR&quot;
  helperText=&quot;Please select your currency&quot;
&gt;
  {MapCastom(currencies)}
&lt;/TextField&gt;;
英文:

Here most likely you are encountering the warning
>MUI: You have provided an out-of-range

which means that during first rendering select value doesn't see possible options.

One way to fix it is to not use a component for this, already what you need is an array so you can replace MapCastom with this function :

const MapCastom = (data) =&gt; {
  return data.map((option) =&gt; (
    &lt;MenuItem key={option.value} value={option.value}&gt;
      {option.label}
    &lt;/MenuItem&gt;
  ));
};

and call it this way :

&lt;TextField
  id=&quot;outlined-select-currency&quot;
  select
  label=&quot;Select&quot;
  defaultValue=&quot;EUR&quot;
  helperText=&quot;Please select your currency&quot;
&gt;
  {MapCastom(currencies)}
&lt;/TextField&gt;;

答案2

得分: 1

似乎要使 MenuItem 正常工作,它必须是 Select 的直接子元素。一个解决方法是使用一个函数来返回 MenuItems 而不是一个组件。 Github 问题

英文:

It seems that for the MenuItem to work, it has to be a direct descendent of Select. One workaround is to use a function to return MenuItems instead of a component. Github issue

huangapple
  • 本文由 发表于 2023年3月15日 20:16:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744572.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定