css - 使用 CSS-in-JS 对 ReactSelect 进行样式化

标签 css reactjs react-select css-in-js

Here是指向我最初回答的另一个很好的 stackoverflow 帖子的链接。我正在尝试设置我的 ReactSelect 组件的样式,使其看起来像这样:

enter image description here

从上面的屏幕截图来看并不明显,但是选择框的高度(总共 29 像素)比默认的 ReactSelect 小得多,这是我设计样式的第一个目标(降低高度)。这是我现在尝试降低高度的代码,主要来 self 链接到的 stackoverflow 帖子:

const customStyles = {
    control: base => ({
        ...base,
        height: 22,
        minHeight: 22
    })
};

const customControlStyles = base => ({
    ...base,
    height: 22,
    minHeight: 22
});

const selectOptions = [
    { value: 'pct', label: 'FG Percentage' },
    { value: 'attFreq', label: 'FG Frequency' },
    { value: 'made', label: 'Total Makes' },
    { value: 'att', label: 'Total Attempts' }];

const shotdistSelect =
    (<Select
        // arrowRenderer={null}
        maxMenuHeight={30}
        placeholder={'Knickerbockers'}
        isClearable={false}
        isDisabled={this.props.loading}
        backspaceRemovesValue={false}
        isSearchable={true}
        value={this.state.shotdistType}
        onChange={this.handleShotdistChange}
        options={selectOptions}
        styles={{ control: customControlStyles }}
        // styles={{ customStyles }}
    />);

这是上面例子的结果:

enter image description here

... 不完全是我想要的。此外,当我在上面的示例中使用 customStyles 而不是 customControlStyles 时,样式不再有效,而且我不确定在创建 时我做错了什么customStyles 导致它不起作用。我想我需要做一些类似于 customStyles 的事情,因为看起来我需要的样式不仅仅是 ReactSelect 的 control 部分。

第二,我想删除 ReactSelect 中的垂直条和向下插入符号,类似于初始屏幕截图。

如果您对此样式有任何帮助,我们将不胜感激!!我已经为此工作了很长一段时间,但还没有成功。谢谢!

最佳答案

选项 1

确保您使用的是最新版本的 react-select (v2.3.0)。我能够通过使用一些 CSS 和 react-select 提供的 style keys 来完成你想要的。

工作示例:https://codesandbox.io/s/7y6901y950

containers/Form/Form.js

import React, { Component } from "react";
import CustomSelect from "../../components/CustomSelect/CustomSelect";

const fgOptions = [
  { value: "pct", label: "FG Percentage" },
  { value: "attFreq", label: "FG Frequency" },
  { value: "made", label: "Total Makes" },
  { value: "att", label: "Total Attempts" }
];

const saveOptions = [
  { value: "pct", label: "Save Percentage" },
  { value: "sFreq", label: "Save Frequency" },
  { value: "tSaves", label: "Total Saves" }
];

const assistOptions = [
  { value: "pct", label: "Assist Percentage" },
  { value: "aFreq", label: "Assist Frequency" },
  { value: "tAssist", label: "Total Assists" }
];

export default class Form extends Component {
  handleChange = (name, value) => {
    this.setState({ [name]: value });
  };

  handleSubmit = e => {
    e.preventDefault();
    alert(JSON.stringify(this.state, null, 4));
  };

  render = () => (
    <form onSubmit={this.handleSubmit} className="app-container">
      <h1>React Select Styling</h1>
      <CustomSelect
        name="fg"
        label="FG:"
        placeholder="Field Goals"
        handleChange={this.handleChange}
        selectOptions={fgOptions}
      />
      <CustomSelect
        name="assists"
        label="AS:"
        placeholder="Assists"
        handleChange={this.handleChange}
        selectOptions={assistOptions}
      />
      <CustomSelect
        name="saves"
        label="SV:"
        placeholder="Saves"
        handleChange={this.handleChange}
        selectOptions={saveOptions}
      />
      <button type="submit" className="submit">
        Submit
      </button>
    </form>
  );
}

组件/CustomSelect/CustomSelect.js

import React from "react";
import PropTypes from "prop-types";
import Select from "react-select";
import { labelStyles, selectStyles } from "./styles/styles";

const CustomSelect = ({
  handleChange,
  label,
  name,
  placeholder,
  selectOptions,
  value
}) => (
  <div className="select-container">
    <label htmlFor={name} style={labelStyles}>
      {label}
    </label>
    <Select
      name={name}
      placeholder={placeholder}
      isClearable={false}
      backspaceRemovesValue={false}
      isSearchable={true}
      value={value}
      onChange={value => handleChange(name, value)}
      options={selectOptions}
      styles={selectStyles}
    />
  </div>
);

CustomSelect.propTypes = {
  handleChange: PropTypes.func.isRequired,
  label: PropTypes.string.isRequired,
  name: PropTypes.string.isRequired,
  placeholder: PropTypes.string.isRequired,
  selectOptions: PropTypes.arrayOf(
    PropTypes.shape({
      label: PropTypes.string.isRequired,
      value: PropTypes.string.isRequired
    })
  ),
  value: PropTypes.objectOf({
    value: PropTypes.string,
    label: PropTypes.string
  })
};

export default CustomSelect;

components/CustomSelect/styles/styles.js(参见 style keys 的文档——如果添加更长的标签,则必须调整 labelStyles width 属性;否则,labelselect 的比例会有所不同)

export const selectStyles = {
  option: (provided, state) => ({
    ...provided,
    borderBottom: "1px dotted pink",
    color: state.isSelected ? "blue" : "",
    fontSize: 16,
    backgroundColor: state.isSelected ? "#eee" : "",
    textAlign: "left",
    cursor: "pointer"
  }),
  container: base => ({
    ...base,
    width: "100%"
  }),
  control: base => ({
    ...base,
    height: 32,
    minHeight: 32,
    fontSize: 16,
    borderRadius: 0,
    width: "100%",
    textAlign: "left",
    cursor: "pointer"
  }),
  dropdownIndicator: base => ({
    ...base,
    display: "none"
  }),
  indicatorSeparator: base => ({
    ...base,
    display: "none"
  }),
  valueContainer: base => ({
    ...base,
    padding: 0,
    paddingLeft: 2
  })
};

export const labelStyles = {
  fontSize: 16,
  paddingTop: 8,
  marginRight: 5,
  width: 50,
  textAlign: "right"
};

样式.css

.app-container {
  padding: 0px 20px;
  text-align: center;
  font-family: sans-serif;
}

.select-container {
  display: flex;
  margin: 0 auto;
  width: 100%;
  max-width: 500px;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -webkit-flex-direction: row;
  -ms-flex-direction: row;
  flex-direction: row;
  -webkit-box-flex: 1;
  margin-bottom: 10px;
}

.submit {
  cursor: pointer;
  background-color: #1e87f0;
  color: #fff;
  border: 1px solid transparent;
  box-sizing: border-box;
  padding: 0 30px;
  vertical-align: middle;
  font-size: 14px;
  line-height: 38px;
  text-transform: uppercase;
  transition: 0.1s ease-in-out;
  transition-property: color, background-color, border-color;
}

.submit:hover {
  background-color: #0f7ae5;
  color: #fff;
}

.submit:focus {
  background-color: #1e87f0;
  color: #fff;
  outline: none;
}

选项 2

或者,您可以在不使用 react-select 的情况下完成所有操作,我强烈推荐这样做,因为它排除了另一个依赖项!因此,您可以选择根据需要设置样式(完全通过 css,完全通过 css-in-js 或组合)。

工作示例:https://codesandbox.io/s/w72k49nn27 (本例仅使用css)

关于css - 使用 CSS-in-JS 对 ReactSelect 进行样式化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54611889/

相关文章:

reactjs - 如果在 react-select 中选择了相同的选项,则不要触发 onChange

css - 如何在 HTML 中制作跑道或自定义形状?

javascript - 为什么我的 dc-js 轴显示得这么粗,而刷子过滤器显示为不透明的黑色?

reactjs - React Router嵌套路由在父路由内部渲染,如何单独渲染?

javascript - 如何使用 react-select 呈现 "N items selected"而不是 N 个选定项目的列表

reactjs - React-Select 组件 closeMenuOnSelect={false} 仍在关闭

css - Bootstrap 无法在移动设备上正确缩放

html - 在 erb 文件中设置虚线内联样式

reactjs - Flux中HTTP请求应该从哪里发起?

reactjs - Yarn 可以列出所有可用的脚本吗?