Skip to content

Commit 566104f

Browse files
authored
fix: set data attributes (#463)
1 parent a1cf890 commit 566104f

File tree

2 files changed

+34
-46
lines changed

2 files changed

+34
-46
lines changed

src/OptionList.tsx

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,7 @@ const OptionList: React.RefForwardingComponent<
108108
return -1;
109109
};
110110

111-
const [activeIndex, setActiveIndex] = React.useState(() =>
112-
getEnabledActiveIndex(0),
113-
);
111+
const [activeIndex, setActiveIndex] = React.useState(() => getEnabledActiveIndex(0));
114112
const setActive = (index: number) => {
115113
setActiveIndex(index);
116114

@@ -126,9 +124,7 @@ const OptionList: React.RefForwardingComponent<
126124

127125
// Auto active first item when list length or searchValue changed
128126
React.useEffect(() => {
129-
setActive(
130-
defaultActiveFirstOption !== false ? getEnabledActiveIndex(0) : -1,
131-
);
127+
setActive(defaultActiveFirstOption !== false ? getEnabledActiveIndex(0) : -1);
132128
}, [memoFlattenOptions.length, searchValue]);
133129

134130
// Auto scroll to item position in single mode
@@ -171,10 +167,7 @@ const OptionList: React.RefForwardingComponent<
171167
}
172168

173169
if (offset !== 0) {
174-
const nextActiveIndex = getEnabledActiveIndex(
175-
activeIndex + offset,
176-
offset,
177-
);
170+
const nextActiveIndex = getEnabledActiveIndex(activeIndex + offset, offset);
178171
scrollIntoView(nextActiveIndex);
179172
setActive(nextActiveIndex);
180173
}
@@ -226,24 +219,15 @@ const OptionList: React.RefForwardingComponent<
226219
const item = memoFlattenOptions[index];
227220
const value = item && (item.data as OptionData).value;
228221
return item ? (
229-
<div
230-
key={index}
231-
role="option"
232-
id={`${id}_list_${index}`}
233-
aria-selected={values.has(value)}
234-
>
222+
<div key={index} role="option" id={`${id}_list_${index}`} aria-selected={values.has(value)}>
235223
{value}
236224
</div>
237225
) : null;
238226
}
239227

240228
return (
241229
<>
242-
<div
243-
role="listbox"
244-
id={`${id}_list`}
245-
style={{ height: 0, overflow: 'hidden' }}
246-
>
230+
<div role="listbox" id={`${id}_list`} style={{ height: 0, overflow: 'hidden' }}>
247231
{renderItem(activeIndex - 1)}
248232
{renderItem(activeIndex)}
249233
{renderItem(activeIndex + 1)}
@@ -265,9 +249,7 @@ const OptionList: React.RefForwardingComponent<
265249
// Group
266250
if (group) {
267251
return (
268-
<div
269-
className={classNames(itemPrefixCls, `${itemPrefixCls}-group`)}
270-
>
252+
<div className={classNames(itemPrefixCls, `${itemPrefixCls}-group`)}>
271253
{label !== undefined ? label : key}
272254
</div>
273255
);
@@ -280,34 +262,28 @@ const OptionList: React.RefForwardingComponent<
280262
children,
281263
style,
282264
className,
265+
...otherProps
283266
} = data as OptionData;
284267

285268
// Option
286269
const selected = values.has(value);
287270

288271
const optionPrefixCls = `${itemPrefixCls}-option`;
289-
const optionClassName = classNames(
290-
itemPrefixCls,
291-
optionPrefixCls,
292-
className,
293-
{
294-
[`${optionPrefixCls}-grouped`]: groupOption,
295-
[`${optionPrefixCls}-active`]:
296-
activeIndex === itemIndex && !disabled,
297-
[`${optionPrefixCls}-disabled`]: disabled,
298-
[`${optionPrefixCls}-selected`]: selected,
299-
},
300-
);
272+
const optionClassName = classNames(itemPrefixCls, optionPrefixCls, className, {
273+
[`${optionPrefixCls}-grouped`]: groupOption,
274+
[`${optionPrefixCls}-active`]: activeIndex === itemIndex && !disabled,
275+
[`${optionPrefixCls}-disabled`]: disabled,
276+
[`${optionPrefixCls}-selected`]: selected,
277+
});
301278

302279
const mergedLabel = childrenAsData ? children : label;
303280

304281
const iconVisible =
305-
!menuItemSelectedIcon ||
306-
typeof menuItemSelectedIcon === 'function' ||
307-
selected;
282+
!menuItemSelectedIcon || typeof menuItemSelectedIcon === 'function' || selected;
308283

309284
return (
310285
<div
286+
{...otherProps}
311287
aria-selected={selected}
312288
className={optionClassName}
313289
title={title}
@@ -325,9 +301,7 @@ const OptionList: React.RefForwardingComponent<
325301
}}
326302
style={style}
327303
>
328-
<div className={`${optionPrefixCls}-content`}>
329-
{mergedLabel || value}
330-
</div>
304+
<div className={`${optionPrefixCls}-content`}>{mergedLabel || value}</div>
331305
{React.isValidElement(menuItemSelectedIcon) || selected}
332306
{iconVisible && (
333307
<TransBtn
@@ -346,10 +320,9 @@ const OptionList: React.RefForwardingComponent<
346320
);
347321
};
348322

349-
const RefOptionList = React.forwardRef<
350-
RefOptionListProps,
351-
OptionListProps<SelectOptionsType>
352-
>(OptionList);
323+
const RefOptionList = React.forwardRef<RefOptionListProps, OptionListProps<SelectOptionsType>>(
324+
OptionList,
325+
);
353326
RefOptionList.displayName = 'OptionList';
354327

355328
export default RefOptionList;

tests/OptionList.test.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,19 @@ describe('OptionList', () => {
137137

138138
expect(preventDefault).toHaveBeenCalled();
139139
});
140+
141+
it('Data attributes should be set correct', () => {
142+
const wrapper = mount(
143+
generateList({
144+
options: [{ value: '1' }, { value: '2', 'data-num': '123' }],
145+
}),
146+
);
147+
148+
expect(
149+
wrapper
150+
.find('.rc-select-item-option')
151+
.last()
152+
.prop('data-num'),
153+
).toBe('123');
154+
});
140155
});

0 commit comments

Comments
 (0)