You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Semantic-UI React下拉框的文本字段中添加换行?

Solution: Multi-line Text in Semantic UI Dropdown Options

Got it! To get multi-line text in your dropdown options, you just need to swap out the plain string text value with a custom React element—Semantic UI's Dropdown supports this perfectly. Here's how to adjust your code:

render(){
  const { members, user } = this.props;
  let memberOptions = [];
  members.forEach((member, i) => {
    memberOptions.push({
      key: i,
      // Replace plain string with a React element for multi-line content
      text: (
        <div style={{ whiteSpace: 'pre-line', padding: '4px 0' }}>
          {member.user.name}
          {member.user.email && <br />{member.user.email}}
        </div>
      ),
      value: member.user.id,
      image: { avatar: true, src: member.user.gravatar },
    });
  });
  return (
    <Dropdown placeholder='Select User' fluid selection options={memberOptions} />
  )
}

What’s changed:

  • Instead of passing a simple string to text, we’re using a <div> to wrap both the username and email. This lets us add line breaks and styling.
  • The whiteSpace: 'pre-line' style ensures line breaks are rendered correctly (you could also use separate <div> elements for each line if you want more control over spacing).
  • We added a quick check member.user.email && to avoid showing an empty line if a member doesn’t have an email on file.

This will make each dropdown option display the username on the first line and the email on the second—exactly the multi-line effect you want!

内容的提问来源于stack exchange,提问作者Coder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:12:39