使用数组而不是 JSON 的 Mui 自动完成

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

Mui Autocomplete using Array instead of Json

问题

我正在尝试复制自动完成 MUI 示例 这里。但我不是使用 JSON 数据结构,而是只传递一个数组。

我正在尝试以下操作:

export default function SearchTutorialArray() {

    const top100Films = [
        '肖申克的救赎',
        '教父',
        '教父第二部',
        '黑暗骑士',
        '十二怒汉',
        '辛德勒的名单',
        '低俗小说',
        '指环王:王者归来',
        '好家伙坏家伙丑家伙'
    ];

    const options = top100Films.map((option) => {
        const firstLetter = option[0].toUpperCase();
        return {
            firstLetter: /[0-9]/.test(firstLetter) ? '0-9' : firstLetter,
            ...option,
        };
    });

    return (
        <Autocomplete
            id="grouped-demo"
            options={options.sort((a, b) => -b.firstLetter.localeCompare(a.firstLetter))}
            groupBy={(option) => option.firstLetter}
            getOptionLabel={(option) => option}
            sx={{width: 300}}
            renderInput={(params) => <TextField {...params} label="带类别的搜索" />}
        />
    );
}

我认为主要问题在于这一行 getOptionLabel={(option) => option},它导致函数返回以下错误:

对象不适用作 React 子元素(发现: 具有键 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, firstLetter} 的对象)。如果你打算呈现一组子元素,请改用数组。

当将此行更改为 getOptionLabel={(option) => option[0] 时,下拉菜单将正常工作,但只显示每部电影的第一个字母。

英文:

I am trying to replicate the autocomplete MUI example here. But instead of using a JSON data structure, I am just passing an Array.

I am trying the following:

export default function SearchTutorialArray() {

const top100Films = [
    &#39;The Shawshank Redemption&#39;,
    &#39;The Godfather&#39;,
    &#39;The Godfather: Part II&#39;,
    &#39;The Dark Knight&#39;,
    &#39;12 Angry Men&#39;,
    &quot;Schindler&#39;s List&quot;,
    &#39;Pulp Fiction&#39;,
    &#39;The Lord of the Rings: The Return of the King&#39;,
    &#39;The Good, the Bad and the Ugly&#39;
];

const options = top100Films.map((option) =&gt; {
    const firstLetter = option[0].toUpperCase();
    return {
        firstLetter: /[0-9]/.test(firstLetter) ? &#39;0-9&#39; : firstLetter,
        ...option,
    };
});

return (
    &lt;Autocomplete
        id=&quot;grouped-demo&quot;
        options={options.sort((a, b) =&gt; -b.firstLetter.localeCompare(a.firstLetter))}
        groupBy={(option) =&gt; option.firstLetter}
        getOptionLabel={(option) =&gt; option}
        sx={{width: 300}}
        renderInput={(params) =&gt; &lt;TextField {...params} label=&quot;With categories&quot;/&gt;}
    /&gt;
);}

I think the main issue is in the line getOptionLabel={(option) =&gt; option} which makes the function to return this error:

> Objects are not valid as a React child (found: object with keys {0, 1,
> 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
> 21, 22, 23, 24, 25, 26, firstLetter}). If you meant to render a
> collection of children, use an array instead.

When changing this line to getOptionLabel={(option) =&gt; option[0]} the dropdown is working correctly, but only showing the first letter for every film.

答案1

得分: 2

你可以使用以下方法:

const options = top100Films.map((option) => {
    const firstLetter = option[0].toUpperCase();
    return {
        firstLetter: /[0-9]/.test(firstLetter) ? '0-9' : firstLetter,
        // no spreading ... here
        label: option,
    };
});

稍后:

<Autocomplete
        id="grouped-demo"
        options={options.sort((a, b) => -b.firstLetter.localeCompare(a.firstLetter))}
        groupBy={(option) => option.firstLetter}
        // read from label attribute
        getOptionLabel={(option) => option.label}
        sx={{width: 300}}
        renderInput={(params) => <TextField {...params} label="With categories"/>}
    />
英文:

You can use the following appraoch:

const options = top100Films.map((option) =&gt; {
    const firstLetter = option[0].toUpperCase();
    return {
        firstLetter: /[0-9]/.test(firstLetter) ? &#39;0-9&#39; : firstLetter,
        // no spreading ... here
        label: option,
    };
});

later:

&lt;Autocomplete
        id=&quot;grouped-demo&quot;
        options={options.sort((a, b) =&gt; -b.firstLetter.localeCompare(a.firstLetter))}
        groupBy={(option) =&gt; option.firstLetter}
        // read from label attribute
        getOptionLabel={(option) =&gt; option.label}
        sx={{width: 300}}
        renderInput={(params) =&gt; &lt;TextField {...params} label=&quot;With categories&quot;/&gt;}
    /&gt;

huangapple
  • 本文由 发表于 2023年2月23日 19:19:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544109.html
匿名

发表评论

匿名网友

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

确定