3

我在我的一种形式中使用 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}

通过查看示例,您可以呈现自定义组件,因此我已经测试过:

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

这应该<span>Testing Text</span>在每个孩子面前呈现。但事实并非如此。我究竟做错了什么?

我的最终目标是让我的选项显示各种数据,如下所示: 在此处输入图像描述

4

3 回答 3

8

使用react-select V2,您可以通过访问data传递给自定义组件的道具来实现此目的,您传递给您的components道具<Select />

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

<Select
    ...
    components={{ Option }}
    ...
  />
于 2019-01-17T11:17:54.080 回答
4

该属性optionComponentoptionsComponent您所写的不同。

于 2016-07-14T19:48:12.897 回答
0

react-select V2及更高版本的升级中,您可以通过访问prop传递给您的components props的自定义组件来实现此目的,<Select />因此您仍然可以保持选择组件的默认行为

从他们这里的文档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}
  />
);
于 2019-10-23T16:37:55.613 回答