React-select:自定义控件不呈现 select 组件
React-select: custom Control does not render select components
使用 react-select@next
并按照自定义 Control
组件的示例 here 未给出预期结果。
import TextField from "@material-ui/core/TextField";
import Select from "react-select";
const InputComponent = (props) => {
return (
<TextField {...props} />
);
};
export const MaterialSelect = (props) => {
const { suggestions, ...other } = props;
return (
<Select
options={suggestions}
components={{
Control: InputComponent,
}}
{...other}
/>
);
};
const suggestions = [
{
label: "Test One",
value: "testOne",
},
{
label: "Test Two",
value: "testTwo",
},
];
<MaterialSelect suggestions={suggestions} />
使用 Material-UI TextField 不会打开下拉菜单或显示任何装饰。我还尝试将 {...props.selectProps}
而不是 {...props}
传递给 TextField
但没有运气
我还尝试使用 TextField
的 InputProps
道具单独传递组件,但没有成功。在我的 Select
组件上使用 menuIsOpen
会按预期呈现菜单,但是在 TextField
中输入不会产生任何结果,没有装饰等。
看来您正在努力使 React select 看起来像 material。
假设我可以给你举个例子:
首先你需要实现你的选项看起来像 material:
class Option extends React.Component {
handleClick = event => {
this.props.selectOption(this.props.data, event);
};
render() {
const { children, isFocused, isSelected, onFocus } = this.props;
console.log(this.props);
return (
<MenuItem
onFocus={onFocus}
selected={isFocused}
onClick={this.handleClick}
component="div"
style={{
fontWeight: isSelected ? 500 : 400
}}
>
{children}
</MenuItem>
);
}
}
然后你需要包装 react-select 并将其作为 inputComponent prop in material-ui Input.
function SelectWrapped(props) {
const { classes, ...other } = props;
return (
<Select
components={{
Option: Option,
DropdownIndicator: ArrowDropDownIcon
}}
styles={customStyles}
isClearable={true}
{...other}
/>
);
}
然后将其用作:
<div className={classes.root}>
<Input
fullWidth
inputComponent={SelectWrapped}
value={this.state.value}
onChange={this.handleChange}
placeholder="Search your values"
id="react-select-single"
inputProps={{
options: testOptions
}}
/>
</div>
请注意我在示例中将选项作为 inputProps 传递。
这是一个工作示例:https://codesandbox.io/s/nk2mkvx92p
请在演示中找到这些 customStyles,它们会让您的组件更具 material 感觉。
希望这会是你。
使用 react-select@next
并按照自定义 Control
组件的示例 here 未给出预期结果。
import TextField from "@material-ui/core/TextField";
import Select from "react-select";
const InputComponent = (props) => {
return (
<TextField {...props} />
);
};
export const MaterialSelect = (props) => {
const { suggestions, ...other } = props;
return (
<Select
options={suggestions}
components={{
Control: InputComponent,
}}
{...other}
/>
);
};
const suggestions = [
{
label: "Test One",
value: "testOne",
},
{
label: "Test Two",
value: "testTwo",
},
];
<MaterialSelect suggestions={suggestions} />
使用 Material-UI TextField 不会打开下拉菜单或显示任何装饰。我还尝试将 {...props.selectProps}
而不是 {...props}
传递给 TextField
但没有运气
我还尝试使用 TextField
的 InputProps
道具单独传递组件,但没有成功。在我的 Select
组件上使用 menuIsOpen
会按预期呈现菜单,但是在 TextField
中输入不会产生任何结果,没有装饰等。
看来您正在努力使 React select 看起来像 material。 假设我可以给你举个例子:
首先你需要实现你的选项看起来像 material:
class Option extends React.Component {
handleClick = event => {
this.props.selectOption(this.props.data, event);
};
render() {
const { children, isFocused, isSelected, onFocus } = this.props;
console.log(this.props);
return (
<MenuItem
onFocus={onFocus}
selected={isFocused}
onClick={this.handleClick}
component="div"
style={{
fontWeight: isSelected ? 500 : 400
}}
>
{children}
</MenuItem>
);
}
}
然后你需要包装 react-select 并将其作为 inputComponent prop in material-ui Input.
function SelectWrapped(props) {
const { classes, ...other } = props;
return (
<Select
components={{
Option: Option,
DropdownIndicator: ArrowDropDownIcon
}}
styles={customStyles}
isClearable={true}
{...other}
/>
);
}
然后将其用作:
<div className={classes.root}>
<Input
fullWidth
inputComponent={SelectWrapped}
value={this.state.value}
onChange={this.handleChange}
placeholder="Search your values"
id="react-select-single"
inputProps={{
options: testOptions
}}
/>
</div>
请注意我在示例中将选项作为 inputProps 传递。
这是一个工作示例:https://codesandbox.io/s/nk2mkvx92p
请在演示中找到这些 customStyles,它们会让您的组件更具 material 感觉。
希望这会是你。