简体   繁体   English

使用 react-select,将 classNames 添加到“multiValue”和“option”元素

[英]With react-select, add classNames onto "multiValue" and "option" elements

We are looking to add classes to various elements in the react-select select component.我们希望将类添加到react-select select 组件中的各种元素。 We have the following code to create a multi-select:我们有以下代码来创建多选:

const CustomMultiLabel = (option) => {
    return (
        <span className={`value-${option.data.value}`}>
            {option.data.shortLabel}
        </span>
    );
};

const scopeMultiSelect =
    (<Select
        className='cbb-select cbb-scope-multi-select'
        styles={blackSelectStyles()}
        isDisabled={isDisabled}
        value={multiScope}
        isMulti
        onChange={handleChange}
        options={multiScopeOptions}
        components={{ MultiValueLabel: CustomMultiLabel }}
        getOptionProps={(option) => {
            return {
                className: option.data.value
            };
        }}
    />);

And the following HTML is rendered:并呈现以下 HTML:

在此处输入图像描述

We are struggling with classes in 2 ways:我们正在以两种方式与课程作斗争:

  • currently the class-name value-${option.data.value} (eg 'value-l5g') is being added to the element, but we need this to be added to 's parent, the with className -multiValue .目前类名value-${option.data.value} (例如'value-l5g')被添加到元素中,但我们需要将它添加到元素的父元素,即 with className -multiValue
  • no classes are being added onto the options in the menuList , however we would like classes added here as well.没有类被添加到menuList中的options中,但是我们也希望在此处添加类。

Is it possible to modify our code to add classes into the correct places?是否可以修改我们的代码以将类添加到正确的位置?

Edit: we are using react-select version 3.1.0, but it looks like a classNames prop was introduced in version 5.7.0... we are trying to upgrade to give this a try.编辑:我们正在使用 react-select 版本 3.1.0,但看起来classNames道具是在版本 5.7.0 中引入的......我们正在尝试升级以尝试一下。

After upgrading to v5.7.0 , adding the following to the main select did the trick:升级到v5.7.0后,将以下内容添加到主要的 select 中就可以了:

<Select
    classNames={{
        option: d => `our-option-${d.data.value}`,
         multiValue: d => `our-multivalue-${d.data.value}`
    }}
    ...
/>

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

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