Skip to content

Commit bf6643d

Browse files
authored
fix: Group not provide title attr (#727)
* fix: group missing title * test: coverage
1 parent 8dd3d4d commit bf6643d

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

src/OptionList.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ export interface RefOptionListProps {
2323
scrollTo?: (index: number) => void;
2424
}
2525

26+
function isTitleType(content: any) {
27+
return typeof content === 'string' || typeof content === 'number';
28+
}
29+
2630
/**
2731
* Using virtual list of option display.
2832
* Will fallback to dom if use customize render.
@@ -272,8 +276,13 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, OptionListP
272276

273277
// Group
274278
if (group) {
279+
const groupTitle = data.title ?? (isTitleType(label) && label);
280+
275281
return (
276-
<div className={classNames(itemPrefixCls, `${itemPrefixCls}-group`)}>
282+
<div
283+
className={classNames(itemPrefixCls, `${itemPrefixCls}-group`)}
284+
title={groupTitle}
285+
>
277286
{label !== undefined ? label : key}
278287
</div>
279288
);
@@ -301,10 +310,7 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, OptionListP
301310
// https://github.com/ant-design/ant-design/issues/34145
302311
const content = typeof mergedLabel === 'number' ? mergedLabel : mergedLabel || value;
303312
// https://github.com/ant-design/ant-design/issues/26717
304-
let optionTitle =
305-
typeof content === 'string' || typeof content === 'number'
306-
? content.toString()
307-
: undefined;
313+
let optionTitle = isTitleType(content) ? content.toString() : undefined;
308314
if (title !== undefined) {
309315
optionTitle = title;
310316
}

tests/Group.test.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,26 @@ describe('Select.Group', () => {
3434
wrapper.find('input').simulate('change', { target: { value: '1' } });
3535
expect(wrapper.find('List').props().data).toHaveLength(2);
3636
});
37+
38+
describe('group title', () => {
39+
it('label as title', () => {
40+
const wrapper = mount(
41+
<Select open>
42+
<OptGroup label="zombiej" />
43+
</Select>,
44+
);
45+
46+
expect(wrapper.find('.rc-select-item-group').prop('title')).toEqual('zombiej');
47+
});
48+
49+
it('customize title', () => {
50+
const wrapper = mount(
51+
<Select open>
52+
<OptGroup label="zombiej" title="bamboo" />
53+
</Select>,
54+
);
55+
56+
expect(wrapper.find('.rc-select-item-group').prop('title')).toEqual('bamboo');
57+
});
58+
});
3759
});

tests/__snapshots__/Select.test.tsx.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@ exports[`Select.Basic render renders dropdown correctly 1`] = `
534534
>
535535
<div
536536
class="antd-item antd-item-group"
537+
title="manager"
537538
>
538539
manager
539540
</div>
@@ -584,6 +585,7 @@ exports[`Select.Basic render renders dropdown correctly 1`] = `
584585
</div>
585586
<div
586587
class="antd-item antd-item-group"
588+
title="engineer"
587589
>
588590
engineer
589591
</div>

tests/__snapshots__/Tags.test.tsx.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ exports[`Select.Tags OptGroup renders correctly 1`] = `
142142
>
143143
<div
144144
class="rc-select-item rc-select-item-group"
145+
title="Manager"
145146
>
146147
Manager
147148
</div>
@@ -170,6 +171,7 @@ exports[`Select.Tags OptGroup renders correctly 1`] = `
170171
</div>
171172
<div
172173
class="rc-select-item rc-select-item-group"
174+
title="Engineer"
173175
>
174176
Engineer
175177
</div>

0 commit comments

Comments
 (0)