Skip to content

Commit ef34813

Browse files
committed
test: batch update
1 parent c03a03e commit ef34813

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

src/SelectInput/Content/SingleContent.tsx

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import * as React from 'react';
2+
import clsx from 'clsx';
23
import Input from '../Input';
34
import { useSelectInputContext } from '../context';
45
import useBaseProps from '../../hooks/useBaseProps';
56
import Placeholder from './Placeholder';
67
import type { SharedContentProps } from '.';
8+
import SelectContext from '../../SelectContext';
79

810
export default React.forwardRef<HTMLInputElement, SharedContentProps>(function SingleContent(
911
{ inputProps },
@@ -12,6 +14,7 @@ export default React.forwardRef<HTMLInputElement, SharedContentProps>(function S
1214
const { prefixCls, searchValue, activeValue, displayValues, maxLength, mode } =
1315
useSelectInputContext();
1416
const { triggerOpen } = useBaseProps();
17+
const selectContext = React.useContext(SelectContext);
1518

1619
const [inputChanged, setInputChanged] = React.useState(false);
1720

@@ -24,6 +27,32 @@ export default React.forwardRef<HTMLInputElement, SharedContentProps>(function S
2427
mergedSearchValue = activeValue;
2528
}
2629

30+
// Extract option props, excluding label and value, and handle className/style merging
31+
const optionProps = React.useMemo(() => {
32+
let restProps = {
33+
className: `${prefixCls}-content-value`,
34+
style: {
35+
visibility: mergedSearchValue ? 'hidden' : 'visible',
36+
},
37+
};
38+
39+
if (displayValue && selectContext?.flattenOptions) {
40+
const option = selectContext.flattenOptions.find((opt) => opt.value === displayValue.value);
41+
if (option?.data) {
42+
const { label, value, className, style, ...rest } = option.data;
43+
44+
restProps = {
45+
...restProps,
46+
...rest,
47+
className: clsx(restProps.className, className),
48+
style: { ...restProps.style, ...style },
49+
};
50+
}
51+
}
52+
53+
return restProps;
54+
}, [displayValue, selectContext?.flattenOptions, prefixCls, mergedSearchValue]);
55+
2756
React.useEffect(() => {
2857
if (combobox) {
2958
setInputChanged(false);
@@ -33,14 +62,7 @@ export default React.forwardRef<HTMLInputElement, SharedContentProps>(function S
3362
return (
3463
<div className={`${prefixCls}-content`}>
3564
{displayValue ? (
36-
<div
37-
className={`${prefixCls}-content-value`}
38-
style={{
39-
visibility: mergedSearchValue ? 'hidden' : 'visible',
40-
}}
41-
>
42-
{displayValue.label}
43-
</div>
65+
<div {...optionProps}>{displayValue.label}</div>
4466
) : (
4567
<Placeholder hasSearchValue={!!mergedSearchValue} />
4668
)}

tests/Select.test.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2204,8 +2204,7 @@ describe('Select.Basic', () => {
22042204
<Select value="b" options={[{ label: 'bamboo', title: 'TitleBamboo', value: 'b' }]} />,
22052205
);
22062206

2207-
// expect(container.find('.rc-select-item').prop('title')).toEqual('TitleBamboo');
2208-
expect(container.querySelector('.rc-select-item').getAttribute('title')).toEqual(
2207+
expect(container.querySelector('.rc-select-content-value').getAttribute('title')).toEqual(
22092208
'TitleBamboo',
22102209
);
22112210
});

tests/utils/common.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ export function selectItem(wrapper: any, index: number = 0) {
3535
export function findSelection(wrapper: any, index: number = 0) {
3636
if (wrapper instanceof HTMLElement) {
3737
const itemNode = wrapper.querySelectorAll('.rc-select-item')[index];
38-
const contentNode = itemNode.querySelector('.rc-select-item-content');
38+
const contentNode =
39+
itemNode?.querySelector('.rc-select-item-content') ||
40+
wrapper.querySelector('.rc-select-content-value');
3941

4042
if (contentNode) {
4143
return contentNode;

0 commit comments

Comments
 (0)