AutoComplete: In material-ui (@mui) How to render all the properties in the json in options list and highlight the matching text in the option?

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

AutoComplete: In material-ui (@mui) How to render all the properties in the json in options list and highlight the matching text in the option?

问题

在我的需求中,我需要在选项列表中显示名称和电子邮件地址,但现在我只能渲染一个参数,现在要渲染所有选项。

我的代码:

<Autocomplete
    freeSolo
    disabled={false}
    fullWidth={false}
    label={'label'}
    multiple={true}
    componentsProps={{
        paper: {
           sx: {
              border: '1px solid lightgray',
              marginLeft: 10.5,
              width: 484,
           }
        }
    }}
    options={[
      { email: 'apple@gmail.com', label: 'apple', value: 'apple' },
      { email: 'pear@gmail.com', label: 'pear', value: 'pear' },
      { email: 'orange@gmail.com', label: 'orange', value: 'orange' },
      { email: 'grape@gmail.com', label: 'grape', value: 'grape' },
      { email: 'banana@gmail.com', label: 'banana', value: 'banana' }
    ]}
    renderOption={React.useCallback((props, option, { inputValue }) => {
      return (
          <Box component='li' {...props}>
              {option.label} - {option.email}
          </Box>
      )
  })}
/>
英文:

In my requirement I need to show name and email address in options list but now i can able to render only one parameter, now to render all options.

AutoComplete: In material-ui (@mui) How to render all the properties in the json in options list and highlight the matching text in the option?

my code:

&lt;Autocomplete
    freeSolo
    disabled={false}
    fullWidth={false}
    label={&#39;label&#39;}
    multiple={true}
    componentsProps={{
        paper: {
           sx: {
              border: &#39;1px solid lightgray&#39;,
              marginLeft: 10.5,
              width: 484,
           }
        }
    }}
    options={[
      { email: &#39;apple@gmail.com&#39;, label: &#39;apple&#39;, value: &#39;apple&#39; },
      { email: &#39;pear@gmail.com&#39;, label: &#39;pear&#39;, value: &#39;pear&#39; },
      { email: &#39;orange@gmail.com&#39;, label: &#39;orange&#39;, value: &#39;orange&#39; },
      { email: &#39;grape@gmail.com&#39;, label: &#39;grape&#39;, value: &#39;grape&#39; },
      { email: &#39;banana@gmail.com&#39;, label: &#39;banana&#39;, value: &#39;banana&#39; }
    ]}
    renderOption={React.useCallback((props, option, { inputValue }) =&gt; {
      return (
          &lt;Box component=&#39;li&#39; {...props}&gt;
              {option.label}
          &lt;/Box&gt;
      )
  })}
/&gt;

答案1

得分: 0

我已经使用dangerouslySetInnerHTML来自定义高亮选项并渲染电子邮件和标签,我以以下方式设置了宽度。

<Autocomplete
    options={[
      { email: 'apple@gmail.com', label: 'apple', value: 'apple' },
      { email: 'pear@gmail.com', label: 'pear', value: 'pear' },
      { email: 'orange@gmail.com', label: 'orange', value: 'orange' },
      { email: 'grape@gmail.com', label: 'grape', value: 'grape' },
      { email: 'banana@gmail.com', label: 'banana', value: 'banana' }
    ]}
    renderOption={React.useCallback((props, option, { inputValue }) => {
      function boldString(str, substr) {
          const strRegExp = new RegExp(substr, 'g')
          return str.replace(strRegExp, '<b style=color:#1e99fa !important;>' + substr + '</b>')
      }
      return (
          <Box component='li' {...props}>
              <div style={{width: '50%'}} dangerouslySetInnerHTML={{ __html: boldString(option.label, inputValue) }}/>{option.email}
          </Box>
      )
  })}
/>

output:

AutoComplete: In material-ui (@mui) How to render all the properties in the json in options list and highlight the matching text in the option?

英文:

I have used dangerouslySetInnerHTML used to cutomized the highlight option and to render email and label I have setted the width in the following way.

&lt;Autocomplete
    options={[
      { email: &#39;apple@gmail.com&#39;, label: &#39;apple&#39;, value: &#39;apple&#39; },
      { email: &#39;pear@gmail.com&#39;, label: &#39;pear&#39;, value: &#39;pear&#39; },
      { email: &#39;orange@gmail.com&#39;, label: &#39;orange&#39;, value: &#39;orange&#39; },
      { email: &#39;grape@gmail.com&#39;, label: &#39;grape&#39;, value: &#39;grape&#39; },
      { email: &#39;banana@gmail.com&#39;, label: &#39;banana&#39;, value: &#39;banana&#39; }
    ]}
    renderOption={React.useCallback((props, option, { inputValue }) =&gt; {
      function boldString(str, substr) {
          const strRegExp = new RegExp(substr, &#39;g&#39;)
          return str.replace(strRegExp, &#39;&lt;b style=color:#1e99fa !important;&gt;&#39; + substr + &#39;&lt;/b&gt;&#39;)
      }
      return (
          &lt;Box component=&#39;li&#39; {...props}&gt;
              &lt;div style={{width: &#39;50%&#39;}} dangerouslySetInnerHTML={{ __html: boldString(option.label, inputValue) }}/&gt;{option.email}
          &lt;/Box&gt;
      )
  })}
/&gt;

output:

AutoComplete: In material-ui (@mui) How to render all the properties in the json in options list and highlight the matching text in the option?

huangapple
  • 本文由 发表于 2023年6月18日 22:35:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76501061.html
匿名

发表评论

匿名网友

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

确定