简体   繁体   English

自定义 SingleValue 和 Option react-select - 选项显示,但 SingleValue 不显示

[英]Custom SingleValue and Option react-select - Option displays, but SingleValue doesn't

I'm trying to create a custom Select using react-select.我正在尝试使用 react-select 创建自定义选择。 I want to create a custom Option and SingleValue.我想创建一个自定义选项和 SingleValue。 The custom Option renders, but SingleValue doesn't.自定义 Option 呈现,但 SingleValue 不呈现。 The single value (selected value) displays as per the default styling.单个值(选定值)按照默认样式显示。

Here's my code,这是我的代码,

const Option = (props) => {
    const { data, innerProps, innerRef, cx, getStyles, className } = props;
    const [hover, setHover] = useState(false);

    return (
        <div ref={innerRef} {...innerProps}
            style={getStyles('option', props)}
            className={cx(
                {
                    option: true,
                },
                className
            )}
        >
            <div style={{ marginLeft: 10}} onMouseEnter={() => setHover(true)} onMouseLeave={() => setHover(false)}>
            <p> {data.label} </p>
                { hover ? <Alert bsStyle="success" style={{ opacity: 0.8 }}>
                    <p> {data.description} </p>
                  </Alert> : null}
            </div>
        </div>
    )
};

I have tried SingleValue like this,我试过这样的 SingleValue,

const SingleValue = ({
    cx,
    getStyles,
    selectProps,
    data,
    isDisabled,
    className,
    ...props
}) => {
    console.log(props);
    return (
        <div
            className={cx(
                emotionCss(getStyles("singleValue", props)),
                {
                    "single-value": true,
                    "single-value--is-disabled": isDisabled
                },
                className
            )}
        >
       <div>{data.label}</div>
            <div style={{ fontSize: "10px" }}>{data.description}</div>
        </div>
    );
};

And this,和这个,

const SingleValue = props => (
    <components.SingleValue {...props}>
        {props.data.description}
    </components.SingleValue>
);

I render it like this,我是这样渲染的

<Select
   id="color"
   options={this.props.options}
   isMulti={true}
   onChange={this.handleChange}
   onBlur={this.handleBlur}
   value={this.props.value}
   components={{ Option, SingleValue }}
/>

Both ways of SingleValue don't work. SingleValue 的两种方式都不起作用。 I have tried to just include SingleValue in components, but that also does't work.我试图只在组件中包含 SingleValue,但这也不起作用。 Could anyone please help?有人可以帮忙吗? Thanks!谢谢!

I had the same problem and I solved it using components.ValueContainer instead of components.SingleValue.我遇到了同样的问题,我使用 components.ValueContainer 而不是 components.SingleValue 解决了它。 In my case, I used react-select to have an icon select component.就我而言,我使用 react-select 来创建一个图标选择组件。

This is my constructor:这是我的构造函数:

    constructor(props) {
        super(props);
        this.state = {
        icons: [{label: "temperature-low", value: "temperature-low", icon: "temperature-low"}, {label: "utensils", value: "utensils", icon: "utensils"}, {label: "venus", value: "venus", icon: "venus"}, {label: "volume-up", value: "volume-up", icon: "volume-up"}, {label: "wifi", value: "wifi", icon: "wifi"}],
        inputValueIcon: "",

        };

    this.handleChangeIcon = this.handleChangeIcon.bind(this);
   }

This is my handleChange function:这是我的 handleChange 函数:

handleChangeIcon(selectedItem) {
    this.setState({
        inputValueIcon: selectedItem
    })
}

And finally, my render function:最后,我的渲染功能:

render() {
    const { Option, ValueOption, ValueContainer } = components;
    const IconOption = (props) => (
        <Option {...props}>
            <MDBIcon icon={props.data.label} />
        </Option>
    );

    const ValueOptionLabel = (props) => (
        <ValueContainer {...props}>
            <MDBIcon icon={props.data.label} />
        </ValueContainer>
    );

    return(
        <Fragment>
          <Select
          placeholder="Select an icon"
          value={this.state.inputValueIcon}
          onChange={ this.handleChangeIcon }
          components={{ Option: IconOption, SingleValue: ValueOptionLabel }}
          options={ this.state.icons }
          />              
        </Fragment>
    );
}

It's not the best solution, but it works :)这不是最好的解决方案,但它有效:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM