Skip to content

Commit 4aa2120

Browse files
authored
fix: Option not pass className & style (#457)
* fix missing option style passing * test case
1 parent 2622d1a commit 4aa2120

File tree

5 files changed

+53
-10
lines changed

5 files changed

+53
-10
lines changed

examples/single.less

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.test-option {
2+
font-weight: bolder;
3+
}

examples/single.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import React from 'react';
33
import Select, { Option } from '../src';
44
import '../assets/index.less';
5+
import './single.less';
56

67
class Test extends React.Component {
78
state = {
@@ -93,7 +94,12 @@ class Test extends React.Component {
9394
<Option value="21" disabled text="disabled">
9495
disabled
9596
</Option>
96-
<Option value="31" text="yiminghe">
97+
<Option
98+
value="31"
99+
text="yiminghe"
100+
className="test-option"
101+
style={{ background: 'yellow' }}
102+
>
97103
yiminghe
98104
</Option>
99105
{[0, 1, 2, 3, 4, 5, 6, 7, 8, 9].map(i => (

src/OptionList.tsx

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -270,19 +270,31 @@ const OptionList: React.RefForwardingComponent<
270270
);
271271
}
272272

273-
const { disabled, value, title, children } = data as OptionData;
273+
const {
274+
disabled,
275+
value,
276+
title,
277+
children,
278+
style,
279+
className,
280+
} = data as OptionData;
274281

275282
// Option
276283
const selected = values.has(value);
277284

278285
const optionPrefixCls = `${itemPrefixCls}-option`;
279-
const optionClassName = classNames(itemPrefixCls, optionPrefixCls, {
280-
[`${optionPrefixCls}-grouped`]: groupOption,
281-
[`${optionPrefixCls}-active`]:
282-
activeIndex === itemIndex && !disabled,
283-
[`${optionPrefixCls}-disabled`]: disabled,
284-
[`${optionPrefixCls}-selected`]: selected,
285-
});
286+
const optionClassName = classNames(
287+
itemPrefixCls,
288+
optionPrefixCls,
289+
className,
290+
{
291+
[`${optionPrefixCls}-grouped`]: groupOption,
292+
[`${optionPrefixCls}-active`]:
293+
activeIndex === itemIndex && !disabled,
294+
[`${optionPrefixCls}-disabled`]: disabled,
295+
[`${optionPrefixCls}-selected`]: selected,
296+
},
297+
);
286298

287299
const mergedLabel = childrenAsData ? children : label;
288300

@@ -308,6 +320,7 @@ const OptionList: React.RefForwardingComponent<
308320
onSelectValue(value);
309321
}
310322
}}
323+
style={style}
311324
>
312325
<div className={`${optionPrefixCls}-content`}>
313326
{mergedLabel || value}

tests/Select.test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,4 +1501,25 @@ describe('Select.Basic', () => {
15011501
expectOpen(wrapper, false);
15021502
});
15031503
});
1504+
1505+
it('should pass className & style to option', () => {
1506+
const wrapper = mount(
1507+
<Select
1508+
options={[
1509+
{
1510+
value: 'test',
1511+
className: 'test-class',
1512+
style: { background: 'yellow' },
1513+
},
1514+
]}
1515+
/>,
1516+
);
1517+
toggleOpen(wrapper);
1518+
expect(
1519+
wrapper.find('.rc-select-item-option').hasClass('test-class'),
1520+
).toBeTruthy();
1521+
expect(wrapper.find('.rc-select-item-option').props().style).toEqual({
1522+
background: 'yellow',
1523+
});
1524+
});
15041525
});

tests/__snapshots__/Select.test.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ exports[`Select.Basic render renders dropdown correctly 1`] = `
507507
</div>
508508
<div
509509
aria-selected="false"
510-
class="antd-item antd-item-option antd-item-option-grouped antd-item-option-active"
510+
class="antd-item antd-item-option option-test antd-item-option-grouped antd-item-option-active"
511511
>
512512
<div
513513
class="antd-item-option-content"

0 commit comments

Comments
 (0)