如何将自定义样式设置为antd Select?

蓝海豹

我要自定义antd Select当用户单击Selectantd时,Option应显示在antd上方,Select而不是显示在antd下方Select

antd Selecthttps//ant.design/components/select/

预期行为:我要这个1

实际行为:我不要这个2

JSX

import { FaPlane, FaWater } from "react-icons/fa";

//outside of class
const shipmentType = {
  sea: [
    { name: "FCL", desc: "FULL CONTAINER LOAD" },
    { name: "LCL", desc: "LESS CONTAINER LOAD" }
  ],
  air: [{ name: "AIR", desc: "AIR DELIVERY" }]
};


//inside of class

render(){
       return(
              <Select
              className="container-dropdown"
              onChange={this.onSelectChange}
              defaultValue={
                  <DisplayContainer data={shipmentType.sea[0]} />
              }
              key={ shipmentType.sea[0]}
            >
              <Option value={shipmentType.sea[0].name}>
                <DisplayContainer data={shipmentType.sea[0]} />
              </Option>
              <Option value={shipmentType.sea[1].name}>
                <DisplayContainer data={shipmentType.sea[1]} />
              </Option>
            </Select>
          );
}

DisplayContainer.js组件

const DisplayContainer = ({ data }) => {
  return (
    <div
      style={{
        width: "120px",
        height: "45px",
        display: "flex",
        flexFlow: "column",
        justifyContent: "center",
        alignItems: "center"
      }}
    >
      <span
        style={{
          display: "block",
          fontSize: "8px",
          padding: "5px 0px 0px 10px"
        }}
      >
        {data.desc}
      </span>

      <span style={{ padding: "2px 0px 0px 14px" }}>
        {data.name === "AIR" ? <FaPlane /> : <FaWater />}
        <span
          style={{ display: "inline", marginLeft: "14px", fontSize: "16px" }}
        >
          {data.name}
        </span>
      </span>
    </div>
  );
};

App.css

.container-dropdown {
    height: 53px;
    width: 140px;
    border: 0px solid white;
    border-radius: 0px;
    cursor: pointer;
    font-size: 18px;
    margin: 0px;
    padding: 0px;
}

custom-antd.css

.ant-select-selection.ant-select-selection--single {
    border-radius: 0px 8px 8px 0px;
    height: 53px;
}

.ant-select-selection-selected-value {
    height: 53px;
    padding: 0px;
    margin: 0px;
}

.ant-select-selection__rendered {
    padding: 0px;
    margin: 0px;
}

.ant-select-dropdown-menu.ant-select-dropdown-menu-root.ant-select-dropdown-menu-vertical {
    padding: 0px;
    margin: 0px;
}

.ant-select-dropdown-menu-item {
    padding: 0px;
    margin: 0px;
}

我该如何实现?我已经花了几个小时了。但我无法成功。我会感激你的 谢谢

编辑01:

用户单击Select框时

我希望顶部Option(即FCL)升起并盖上这样的Select盒子:

我要这个

我不希望Options(即FCLLCL)都显示在下面的Select框中:

我不要这个

马特·克鲁克

我相信我已经能够接近您想要实现的目标。以下是更新的custom-antd.css文件。

.ant-select-selection-selected-value {
  border-radius: 0px 8px 8px 0px;
  height: 53px;
}

.ant-select-selection.ant-select-selection--single {
  height: 53px;
}

.ant-select-selection.ant-select-selection--single
  > div
  > div
  > div
  > div
  + div {
  margin-top: -5px;
  padding: 4px 5px 5px 14px !important;
}

.ant-select-selection.ant-select-selection--single > div > div > div > div {
  margin-top: -20px;
}

.ant-select-selection.ant-select-selection--single[aria-expanded="true"]
  > div
  > div
  > div
  > div {
    margin-top: -10px;
}

 /*style for when the menu is expanded: show shipment description above icon and name*/ 
.ant-select-selection.ant-select-selection--single[aria-expanded="true"]
    > div
    > div
    > div
    > div
    + div {
       margin-top: -15px;
}

完整的代码沙箱可以在这里找到

本质上,您想要做的是使用组合器div为名称,描述等选择特定的。蚂蚁的设计嵌套在它们的结构中。

编辑

为了获得下拉菜单以根据当前选择的内容显示不同的数据(仅在选择FCL时显示LCL,反之亦然),您可以利用handleChange函数来过滤原始货运数据,以便返回当前未显示的所有内容选中(即选择FCL时显示不带FCL的LCL)。通过将原始装运数据与第二个阵列(已过滤的菜单数据)一起存储在状态中,您可以使用/更新第二个阵列作为选择选项。

这是你的状态。

  this.state = {
     shipmentArr: [],
     shipmentType: {
        sea: [
          { name: "FCL", desc: "FULL CONTAINER LOAD" },
          { name: "LCL", desc: "LESS CONTAINER LOAD" }
        ],
        air: [{ name: "AIR", desc: "AIR DELIVERY" }]
     }
  };

这是新的handleChange

handleChange = value => {
   var newShipmentType = this.state.shipmentType.sea.filter(x => {
     return x.name !== value;
   });
   this.setState({
     shipmentArr: newShipmentType
   });
};

这是componentDidMount(利用中handleChange)。

componentDidMount = () => {
    this.handleChange(this.state.shipmentType.sea[0].name);
};

以下是更新的Select组件。

<Select
    className="container-dropdown"
    onChange={this.handleChange}
    open={true} // USE THIS FOR DEBUGGING.
    defaultValue={
      <DisplayContainer data={this.state.shipmentType.sea[0]} />
    }
    key={this.state.shipmentArr[0]}
  >
    {this.state.shipmentArr.map(x => {
      return (
        <Option value={x.name}>
          <DisplayContainer data={x} />
        </Option>
      );
    })}
  </Select>

请参阅完整的更新Codepen

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Select2:如何将自定义样式添加到下拉菜单中不存在的选项?

来自分类Dev

如何将自定义样式设置为Anted Rate Component

来自分类Dev

如何使用react-select为选择使用自定义样式的打字稿

来自分类Dev

我无法在reactjs中将自定义样式设置为antd Date Picker

来自分类Dev

如何将自定义颜色设置为WindowBackground

来自分类Dev

如何将自定义标记设置为自定义视图?

来自分类Dev

将自定义颜色的按钮设置为禁用的样式

来自分类Dev

定义自定义选项组件时,如何为 react-select 应用默认样式?

来自分类Dev

Fabric.js:如何将自定义大小设置为Text或IText?

来自分类Dev

如何将自定义TaskScheduler设置为默认任务

来自分类Dev

如何将自定义Controltemplate设置为整个应用程序的默认值?

来自分类Dev

Swift:如何将自定义UICollectionViewCell设置为圆?

来自分类Dev

如何将自定义搜寻栏的ProgressDrawable大小设置为小于ProgressBackground

来自分类Dev

如何将自定义抽屉项目动态设置为“聚焦”?

来自分类Dev

如何将自定义视图设置为查找的唯一视图?

来自分类Dev

如何将自定义IJobStore设置为Quartz(.Net Core)

来自分类Dev

Fabric.js:如何将自定义大小设置为Text或IText?

来自分类Dev

如何将自定义templateBase设置为基本模板

来自分类Dev

GTM 如何将自定义维度设置为 GA 页面视图?

来自分类Dev

Angular UI - ui-select 自定义样式

来自分类Dev

自定义g.select taglib,默认设置为grails

来自分类Dev

如何将<ul>内部的<li>样式设置为具有自定义高度的表格?

来自分类Dev

如何将自定义样式添加到样式化的组件?

来自分类Dev

jQuery mobile-如何将select图标更改为自定义图标?

来自分类Dev

如何使用selectize.js jquery插件设置select的自定义高度和宽度?

来自分类Dev

上传器:如何将自定义文件夹名称设置为与用户ID相同?

来自分类Dev

#ember-power-select,如何在ember-power-select自定义搜索操作中设置预选值

来自分类Dev

为自定义控件设置样式

来自分类Dev

为自定义控件设置样式

Related 相关文章

  1. 1

    Select2:如何将自定义样式添加到下拉菜单中不存在的选项?

  2. 2

    如何将自定义样式设置为Anted Rate Component

  3. 3

    如何使用react-select为选择使用自定义样式的打字稿

  4. 4

    我无法在reactjs中将自定义样式设置为antd Date Picker

  5. 5

    如何将自定义颜色设置为WindowBackground

  6. 6

    如何将自定义标记设置为自定义视图?

  7. 7

    将自定义颜色的按钮设置为禁用的样式

  8. 8

    定义自定义选项组件时,如何为 react-select 应用默认样式?

  9. 9

    Fabric.js:如何将自定义大小设置为Text或IText?

  10. 10

    如何将自定义TaskScheduler设置为默认任务

  11. 11

    如何将自定义Controltemplate设置为整个应用程序的默认值?

  12. 12

    Swift:如何将自定义UICollectionViewCell设置为圆?

  13. 13

    如何将自定义搜寻栏的ProgressDrawable大小设置为小于ProgressBackground

  14. 14

    如何将自定义抽屉项目动态设置为“聚焦”?

  15. 15

    如何将自定义视图设置为查找的唯一视图?

  16. 16

    如何将自定义IJobStore设置为Quartz(.Net Core)

  17. 17

    Fabric.js:如何将自定义大小设置为Text或IText?

  18. 18

    如何将自定义templateBase设置为基本模板

  19. 19

    GTM 如何将自定义维度设置为 GA 页面视图?

  20. 20

    Angular UI - ui-select 自定义样式

  21. 21

    自定义g.select taglib,默认设置为grails

  22. 22

    如何将<ul>内部的<li>样式设置为具有自定义高度的表格?

  23. 23

    如何将自定义样式添加到样式化的组件?

  24. 24

    jQuery mobile-如何将select图标更改为自定义图标?

  25. 25

    如何使用selectize.js jquery插件设置select的自定义高度和宽度?

  26. 26

    上传器:如何将自定义文件夹名称设置为与用户ID相同?

  27. 27

    #ember-power-select,如何在ember-power-select自定义搜索操作中设置预选值

  28. 28

    为自定义控件设置样式

  29. 29

    为自定义控件设置样式

热门标签

归档