React Select 渲染自定义组件

ReactSelect rendering custom component

我正在以我的一种形式使用 ReactSelect:

<Select name='event-title' className="event-title" ref="stateSelect" autofocus optionsComponent={DropdownOptions} onInputChange={this.handleChangeTitle} options={this.state.titleResults} simpleValue clearable={this.state.clearable} name="selected-state"  value={this.state.selectedTitleValue} onChange={this.handleTitleChosen} searchable={this.state.searchable} />

我想渲染自定义 optionsComponent:

optionsComponent={DropdownOptions}

通过查看 example,您可以呈现自定义组件,所以我已经测试过:

const DropdownOptions = React.createClass({
    propTypes: {
        children: React.PropTypes.node,
        className: React.PropTypes.string,
        isDisabled: React.PropTypes.bool,
        isFocused: React.PropTypes.bool,
        isSelected: React.PropTypes.bool,
        onFocus: React.PropTypes.func,
        onSelect: React.PropTypes.func,
        option: React.PropTypes.object.isRequired,
    },
    handleMouseDown (event) {
        event.preventDefault();
        event.stopPropagation();
        this.props.onSelect(this.props.option, event);
    },
    handleMouseEnter (event) {
        this.props.onFocus(this.props.option, event);
    },
    handleMouseMove (event) {
        if (this.props.isFocused) return;
        this.props.onFocus(this.props.option, event);
    },
    render () {
        return (
            <div className={this.props.className}
                onMouseDown={this.handleMouseDown}
                onMouseEnter={this.handleMouseEnter}
                onMouseMove={this.handleMouseMove}
                title={this.props.option.title}>
                <span>Testing Text</span>
                {this.props.children}
            </div>
        );
    }
});

这应该在每个 child 之前渲染 <span>Testing Text</span>。但事实并非如此。我做错了什么?

我的最终目标是让我的选项显示如下各种数据:

属性 是 optionComponent 而不是你写的 optionsComponent

使用 react-select V2 你可以通过访问传递给你传递给 components 你的道具 <Select />

const Option = (props) => {
  const {
    ...
    data,
  } = props
  return (
    <div ...>
      {`${data.firstName} - ${data.lastName}`}
    </div>
  )
}

<Select
    ...
    components={{ Option }}
    ...
  />

react-select V2 及更高版本的升级,您可以通过访问传递给您传递的自定义组件的 prop 来实现此目的到 <Select />components 属性,这样你仍然可以保持 select component

的默认行为

来自他们的文档https://react-select.com/styles#cx-and-custom-components

import React from 'react';
import { css } from 'emotion';
import Select from 'react-select';
import { colourOptions } from '../data';

const Option = (props: OptionProps) => {
  const {
    children,
    className,
    cx,
    getStyles,
    isDisabled,
    isFocused,
    isSelected,
    innerRef,
    innerProps,
  } = props;
  return (
    <div
      ref={innerRef}
      className={cx(
        css(getStyles('option', props)),
        {
          option: true,
          'option--is-disabled': isDisabled,
          'option--is-focused': isFocused,
          'option--is-selected': isSelected,
        },
        className
      )}
      {...innerProps}
    >
      {children}
    </div>
  );
};

export default () => (
  <Select
    closeMenuOnSelect={false}
    components={{ Option }}
    styles={{
      option: base => ({
        ...base,
        border: `1px dotted ${colourOptions[2].color}`,
        height: '100%',
      }),
    }}
    defaultValue={colourOptions[4]}
    options={colourOptions}
  />
);