简体   繁体   English

如何设置 react-select 组件的样式,使其与某些文本内联?

[英]How to style react-select component so that it is inline with some text?

I want to style my react-select component such that it looks like the below image (inline with the text).我想设置我的react-select组件的样式,使其看起来像下图(与文本内联)。

在此处输入图像描述

I can do this easily using the normal select component inside a span and remove the border like this:我可以使用跨度内的普通select组件轻松完成此操作,并像这样删除边框:

<select disabled={readOnlyMode} style={{ border: 'none' }}>
    <option default>Select response</option>
</select>

But I want to use react-select for this and unable to figure out what styling should I apply.但是我想为此使用 react-select 并且无法弄清楚我应该应用什么样式。

react-select provides you with the ability to adjust the styles for the components. react-select为您提供了为组件调整 styles 的能力。 You can check it here: https://react-select.com/styles .你可以在这里查看: https://react-select.com/styles

In your case, you should adjust control style key:在您的情况下,您应该调整control样式键:

const customStyles = {
  control: (provided, state) => ({
    ...provided,
    border: 'none',
  }),
};

And pass customStyles as a styles prop in react-select component并在react-select组件customStyles作为styles道具传递

you can custom change the indicator using following code您可以使用以下代码自定义更改指标

import React from "react";
import ReactDOM from "react-dom";
import Select, { components } from "react-select";


class Test extends React.Component {
  render() {
    const DropdownIndicator = (props) => {
    return (
      components.DropdownIndicator && (
        <components.DropdownIndicator {...props}>
          <img src="https://via.placeholder.com/30x30" />
        </components.DropdownIndicator>
        )
      );
    };
    return (
      <Select
        components={{ DropdownIndicator }}
        // pass other props
      />
    );
  }
}

ReactDOM.render(<Test />, document.getElementById("root"));

found this answer here and react-select styling documentations can be found here此处找到此答案,并且可以在此处找到react-select样式文档

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

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