【问题标题】:React-select: custom Control does not render select componentsReact-select:自定义控件不呈现选择组件
【发布时间】:2018-12-15 10:13:18
【问题描述】:

使用react-select@next 并遵循示例here 自定义Control 组件不会产生预期的结果。

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,但没有成功

我还尝试使用 InputProps 属性为 TextField 单独传递组件,但没有成功。在我的Select 组件上使用menuIsOpen 可以按预期呈现菜单,但是输入TextField 不会产生任何结果、没有装饰等。

【问题讨论】:

    标签: javascript reactjs material-design material-ui react-select


    【解决方案1】:

    您似乎正在努力使反应选择看起来像材料。 假设我可以举个例子:

    首先你需要实现你的 Options 看起来像材料:

    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 道具放入 material-ui 输入中。

    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,它们可以让您的组件更具材料感。

    希望你会这样。

    【讨论】:

    • 谢谢@gluttony!此后不久,我意识到无法在Select 中使用TextField 组件作为我的Control。我希望避免使用Input 组件,因为TextField 具有更多功能,但没关系。这正是我最终的结果:)
    猜你喜欢
    • 2013-07-26
    • 2011-11-24
    • 1970-01-01
    • 2021-12-29
    • 2021-08-26
    • 2019-03-20
    • 2012-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多