MUI:提供给自动完成的值无效。这些选项都与``''''''

发布于 2025-02-01 08:16:08 字数 1905 浏览 4 评论 0原文

当在自动完成组件的输入中输入一个值时,我会收到此警告,我无法删除... 这就是我的输入看起来,

 <Autocomplete
            id="cboAdresse"
            sx={{ width: 100 + "%", fontFamily: "Poppins Bold" }}
            getOptionLabel={(option) =>
              typeof option === "string" ? option : option.label
            }
            filterOptions={(x) => {
              return x;
            }}
            options={lstadresse}
            isOptionEqualToValue={(option, value) =>
              value.label === option.label
            }
            autoComplete
            includeInputInList
            filterSelectedOptions
            value={adresse}
            noOptionsText="Aucune adresse trouvée"
            onChange={(event, newValue) => {
              setLstAdresse(
                newValue.name ? [newValue.name, ...lstadresse] : lstadresse
              );
              setAdresse(newValue.name);
              if (newValue.name != "") {
                setVille(newValue.city);
                setCodePostal(newValue.postcode);
              }
            }}
            onInputChange={(event, newInputValue) => {
              setInputRue(newInputValue);
            }}
            renderInput={(params) => (
              <div
                ref={params.InputProps.ref}
                className="login-block__input form_input_white"
              >
                <input
                  type="text"
                  name="adresse"
                  placeholder="Adresse"
                  {...params.inputProps}
                />
              </div>
            )}
      />

我们可以看到我已经集成了iSoptionequaltovalue参数而没有解决问题。在我的研究期间,其他人面临这个问题,并用我用Isoptionequaltovalue编写的内容解决了这一问题。如果有人有解决方案,我有兴趣。提前致谢。

enter image description here

When a value is entered in the input of the autocomplete component I get this warning that I can't remove...
This is what my input looks like

 <Autocomplete
            id="cboAdresse"
            sx={{ width: 100 + "%", fontFamily: "Poppins Bold" }}
            getOptionLabel={(option) =>
              typeof option === "string" ? option : option.label
            }
            filterOptions={(x) => {
              return x;
            }}
            options={lstadresse}
            isOptionEqualToValue={(option, value) =>
              value.label === option.label
            }
            autoComplete
            includeInputInList
            filterSelectedOptions
            value={adresse}
            noOptionsText="Aucune adresse trouvée"
            onChange={(event, newValue) => {
              setLstAdresse(
                newValue.name ? [newValue.name, ...lstadresse] : lstadresse
              );
              setAdresse(newValue.name);
              if (newValue.name != "") {
                setVille(newValue.city);
                setCodePostal(newValue.postcode);
              }
            }}
            onInputChange={(event, newInputValue) => {
              setInputRue(newInputValue);
            }}
            renderInput={(params) => (
              <div
                ref={params.InputProps.ref}
                className="login-block__input form_input_white"
              >
                <input
                  type="text"
                  name="adresse"
                  placeholder="Adresse"
                  {...params.inputProps}
                />
              </div>
            )}
      />

We can see that I have integrated the IsOptionEqualToValue parameter without solving the problem. During my research other people have faced this problem and solved it with what I wrote with the IsOptionEqualToValue. If anyone has a solution I'm interested. Thanks in advance.

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

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

发布评论

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

评论(5

羁拥 2025-02-08 08:16:08

在MUI V5中使用此

isOptionEqualToValue={(option, value) => option.value === value.value}

in mui v5 use this

isOptionEqualToValue={(option, value) => option.value === value.value}
黯淡〆 2025-02-08 08:16:08

该解决方案是使用null代表自动完成组件的任何值。我在自动完成组件周围包装器中有以下代码。

const { 
    // ... other props
    value: valueProp,
     } = props;

// ...

let value = valueProp;
if (valueProp == null || valueProp = '') {
    valueProp = null
}
return (
    <Autocomplete
        clearOnBlur
        value={valueProp}
    />
);

由于使用NULL时会出现其他问题,因此需要清晰的Onblur。参见我的评论

The solution is to use null to represent no value for the Autocomplete component. I have the following code in my wrapper around the Autocomplete component.

const { 
    // ... other props
    value: valueProp,
     } = props;

// ...

let value = valueProp;
if (valueProp == null || valueProp = '') {
    valueProp = null
}
return (
    <Autocomplete
        clearOnBlur
        value={valueProp}
    />
);

The clear onBlur is needed because of other issues that pop up when using null. See my comment on their github for more details

凉薄对峙 2025-02-08 08:16:08

我的解决方案是,假设输入:

警告出现:

const [dropDownValue, setDropDownValue] = React.useState<any>('');

没有警告,没有出现交战:

const [dropDownValue, setDropDownValue] = React.useState<any>(null);

将状态绑定到自动填充以使其受到控制:

        <Autocomplete
            value={dropDownValue}
            onChange={(e, newDropDownValue)=> 
            setDropDownValue(newDropDownValue)}

我的选择是:

[
            { label: 'The Shawshank Redemption', year: 1994 },
            { label: 'The Godfather', year: 1972 },
            { label: 'The Godfather: Part II', year: 1974 },
            { label: 'The Dark Knight', year: 2008 },
]

底线:将null放入受控状态,为空,或者“无效” ,它将出现错误。

或者,自动完成作品的“ freesolo”属性,但它删除了下拉符号 /图标,因此并不理想。

my solution was, assuming CONTROLLED input:

WARNING appears:

const [dropDownValue, setDropDownValue] = React.useState<any>('');

NO WARNING, no warring appears:

const [dropDownValue, setDropDownValue] = React.useState<any>(null);

Binding of state to the Autocomplete to make it controlled:

        <Autocomplete
            value={dropDownValue}
            onChange={(e, newDropDownValue)=> 
            setDropDownValue(newDropDownValue)}

My options are:

[
            { label: 'The Shawshank Redemption', year: 1994 },
            { label: 'The Godfather', year: 1972 },
            { label: 'The Godfather: Part II', year: 1974 },
            { label: 'The Dark Knight', year: 2008 },
]

Bottom line: put NULL into the controlled state, empty OR '' will NOT work, it will give error.

Alternatively 'freeSolo' property of AutoComplete WORKS, BUT it removes the DropDown symbol / icon, so not ideal.

南渊 2025-02-08 08:16:08

使用isoptionequaltovalue属性如下

<Autocomplete
   ...
   isOptionEqualToValue={(option, value) => (option === value || value === "")}
>
</Autocomplete>

Use the isOptionEqualToValue attribute like below

<Autocomplete
   ...
   isOptionEqualToValue={(option, value) => (option === value || value === "")}
>
</Autocomplete>
面如桃花 2025-02-08 08:16:08

我发现的唯一解决方案是仅在您的选项中添加一个空字符串。

The only solution I've found is to just add an empty string to your options.

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