我正在使用BlueprintJs Select组件,它能够渲染。但是它不显示任何值文本,因为它应该显示。我不确定为什么没有显示。因为Select正在使用InputGroup,所以doc说要使用inputProps来设置value和onchange函数,但这似乎不起作用。我的代码如下
import React from 'react';
import { Select } from '@blueprintjs/labs';
import { MenuItem, Button } from '@blueprintjs/core';
import PropTypes from 'prop-types';
class BlueprintSelect extends React.Component {
constructor(props) {
super(props);
const elementSelectItems = [
{ id: 1, query: 'term(s)' },
{ id: 2, query: 'range' },
];
this.state = {
elementSelectItems,
selectedValue: elementSelectItems[0].query,
};
}
handleElementSelect = ({ query }) => {
console.log('printint our', query);
}
renderSelect = ({ handleElementSelect, item }) => (
<MenuItem
key={item.id}
label={item.query}
onClick={handleElementSelect}
text={item.query}
shouldDismissPopover
/>
)
render() {
const elementSelectItems = [
{ id: 1, query: 'term(s)' },
{ id: 2, query: 'range' },
];
return (
<div className="elementSelector">
<Select
inputProps={{ value: this.state.selectedValue, onChange: this.handleElementSelect }}
items={elementSelectItems}
filterable={false}
itemRenderer={this.renderSelect}
onItemSelect={this.handleElementSelect}
noResults={<MenuItem disabled text="No results." />}
>
<Button className="querySelectButton" text="query Type" rightIconName="caret-down" />
</Select>
</div>
);
}
}
为什么在我选择两个菜单项之一时无法显示该值,尤其是在使用Select组件的inputProps属性实现受控状态时?
答案 0 :(得分:2)
在该示例中,他们在Section Component的嵌套子Button的text属性中设置值。有一个示例https://blueprintjs.com/docs/#select/select-component。 我会做类似的事情:
<Section> ... <Button text={this.state.selectedValue || elementSelectItems[0].value} /></Section>