如何让Fluent UI Dropdown选中选项后仍显示占位文本?
解决方案
要让Dropdown始终显示Examples of sentences,核心是不让Dropdown的选中状态同步到自身的显示文本上。你只需要给Dropdown组件添加value=""属性,强制它始终显示占位符文本即可。
修改后的代码如下:
import { FluentProvider, webLightTheme, } from '@fluentui/react-components'; import { Dropdown, Option } from '@fluentui/react-components/unstable'; import { TextField } from '@fluentui/react'; import * as React from 'react'; export default class Grouped extends React.Component<{}, { value: any }> { constructor(props) { super(props); this.state = { value: '' }; } land = ['A long sentence', 'Another long sentence', 'Another another long sentence']; render() { return ( <div> <FluentProvider className="fluent-provider" style={{ display: 'flex' }} theme={webLightTheme} > {/* 添加value="",强制Dropdown始终显示placeholder */} <Dropdown placeholder="Examples of sentences" value="" onOptionSelect={(e, data) => { if (data.optionText !== undefined) this.setState({ value: data.optionText }); }} > {this.land.map((option) => ( <Option key={option}>{option}</Option> ))} </Dropdown> </FluentProvider> <br/> <TextField label="Write a sentence:" value={this.state.value} multiline rows={3} autoAdjustHeight resizable={false} /> </div> ); } }
关于ComboBox的补充说明
你说得没错,Fluent UI的ComboBox确实是单行输入组件,不支持多行文本输入。所以你选择用多行TextField搭配Dropdown提供参考示例的方案完全合理,不需要替换成其他组件。
内容的提问来源于stack exchange,提问作者SoftTimur
相关产品推荐
相关产品推荐

