将 react-select v2 样式化为 material-ui - 替换输入组件

Styling react-select v2 with material-ui - Replace Input component

我在将 react-select v2 的输入组件替换为 Material UI.

的输入组件时遇到问题

到目前为止,我已经在下面的代码和框中进行了尝试,但无法在输入输入时调用过滤?

https://codesandbox.io/s/jjjwoj3yz9

此外,如有任何关于选项替换实施的反馈,我们将不胜感激。我是否以正确的方式处理它,抓住单击选项的文本并从我的选项列表中搜索 Option 对象以传递给 selectOption 函数?

非常感谢, 埃里克

V1

从这里参考文档:https://material-ui.com/demos/autocomplete/

它提供了关于如何使用 react-select 和 material-ui

的清晰文档

这是您的问题的一个工作示例:https://codesandbox.io/s/p9jpl9l827

如你所见material-ui输入组件可以将react-select当成inputComponent.

V2

与之前的方法几乎相同:

实施输入组件:

<div className={classes.root}>
  <Input
   fullWidth
    inputComponent={SelectWrapped}
    value={this.state.value}
    onChange={this.handleChange}
    placeholder="Search your color"
    id="react-select-single"
    inputProps={{
     options: colourOptions
    }}
  />
</div>

然后 SelectWrapped 组件实现应该是:

function SelectWrapped(props) {
  const { classes, ...other } = props;

  return (
    <Select
      components={{
        Option: Option,
        DropdownIndicator: ArrowDropDownIcon
      }}
      styles={customStyles}
      isClearable={true}
      {...other}
    />
  );
}

我重写了组件 Option 和 DropdownIndicator 以使其更加 material 并添加了 customStyles 还:

const customStyles = {
  control: () => ({
    display: "flex",
    alignItems: "center",
    border: 0,
    height: "auto",
    background: "transparent",
    "&:hover": {
      boxShadow: "none"
    }
  }),
  menu: () => ({
    backgroundColor: "white",
    boxShadow: "1px 2px 6px #888888", // should be changed as material-ui
    position: "absolute",
    left: 0,
    top: `calc(100% + 1px)`,
    width: "100%",
    zIndex: 2,
    maxHeight: ITEM_HEIGHT * 4.5
  }),
  menuList: () => ({
    maxHeight: ITEM_HEIGHT * 4.5,
    overflowY: "auto"
  })
};

和选项:

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>
    );
  }
}

请从这里找到示例:https://codesandbox.io/s/7k82j5j1qx

参考 React select 的文档,您可以根据需要添加更多更改。

希望这些对您有所帮助。