javascript - React-select: custom Control does not render select components - Stack Overflow

Working with react-select@next and following the example here for custom Control ponents does not give

Working with react-select@next and following the example here for custom Control ponents does not give the expected result.

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}
                ponents={{
                    Control: InputComponent,
                }}
                {...other}
            />
    );
};

const suggestions = [
    {
        label: "Test One",
        value: "testOne",
    },
    {
        label: "Test Two",
        value: "testTwo",
    },
];

<MaterialSelect suggestions={suggestions} />

Using the Material-UI TextField does not open the dropdown or display any of the adornments. I also tried passing in {...props.selectProps} instead of {...props} to the TextField with no luck

I also tried passing the ponents in individually using the InputProps prop for TextField with no luck. Using menuIsOpen on my Select ponent renders the menu as expected, however typing in to the TextField produces no results, no adornments, etc.

Working with react-select@next and following the example here for custom Control ponents does not give the expected result.

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}
                ponents={{
                    Control: InputComponent,
                }}
                {...other}
            />
    );
};

const suggestions = [
    {
        label: "Test One",
        value: "testOne",
    },
    {
        label: "Test Two",
        value: "testTwo",
    },
];

<MaterialSelect suggestions={suggestions} />

Using the Material-UI TextField does not open the dropdown or display any of the adornments. I also tried passing in {...props.selectProps} instead of {...props} to the TextField with no luck

I also tried passing the ponents in individually using the InputProps prop for TextField with no luck. Using menuIsOpen on my Select ponent renders the menu as expected, however typing in to the TextField produces no results, no adornments, etc.

Share Improve this question edited Jul 6, 2018 at 23:11 Naji asked Jul 6, 2018 at 19:21 NajiNaji 7422 gold badges18 silver badges36 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

It seems you are struggling to make react select looks like material. assuming that I can give you an example:

first you need to implement your Options looks like material:

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}
        ponent="div"
        style={{
          fontWeight: isSelected ? 500 : 400
        }}
      >
        {children}
      </MenuItem>
    );
  }
}

then you need to wrap react-select and put is as a inputComponent prop in material-ui Input.

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

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

then use it as:

 <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>

please fins that I have passes the options as inputProps in the example.

here is a working example: https://codesandbox.io/s/nk2mkvx92p

please find these customStyles in the demo which make more material feel in your ponent.

hope this will you.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745196213a4616098.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信