Skip to content

Commit 14824f5

Browse files
EmilyyyLiu刘欢
and
刘欢
authored
feat:Using the showScrollBar property of the List component (#1122)
* feat:Using the showScrollBar property of the List component * feat:调整单测 --------- Co-authored-by: 刘欢 <[email protected]>
1 parent 3ff731d commit 14824f5

File tree

3 files changed

+89
-3
lines changed

3 files changed

+89
-3
lines changed

src/BaseSelect/index.tsx

+14-2
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export interface BaseSelectProps extends BaseSelectPrivateProps, React.AriaAttri
136136
tagRender?: (props: CustomTagProps) => React.ReactElement;
137137
direction?: 'ltr' | 'rtl';
138138
maxLength?: number;
139-
139+
showScrollBar?: boolean | 'optional';
140140
// MISC
141141
tabIndex?: number;
142142
autoFocus?: boolean;
@@ -223,6 +223,7 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
223223
className,
224224
showSearch,
225225
tagRender,
226+
showScrollBar = 'optional',
226227
direction,
227228
omitDomProps,
228229

@@ -693,8 +694,19 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)
693694
showSearch: mergedShowSearch,
694695
multiple,
695696
toggleOpen: onToggleOpen,
697+
showScrollBar,
696698
}),
697-
[props, notFoundContent, triggerOpen, mergedOpen, id, mergedShowSearch, multiple, onToggleOpen],
699+
[
700+
props,
701+
notFoundContent,
702+
triggerOpen,
703+
mergedOpen,
704+
id,
705+
mergedShowSearch,
706+
multiple,
707+
onToggleOpen,
708+
showScrollBar,
709+
],
698710
);
699711

700712
// ==================================================================

src/OptionList.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, {}> = (_, r
4444
toggleOpen,
4545
notFoundContent,
4646
onPopupScroll,
47+
showScrollBar,
4748
} = useBaseProps();
4849
const {
4950
maxCount,
@@ -325,6 +326,7 @@ const OptionList: React.ForwardRefRenderFunction<RefOptionListProps, {}> = (_, r
325326
virtual={virtual}
326327
direction={direction}
327328
innerProps={virtual ? null : a11yProps}
329+
showScrollBar={showScrollBar}
328330
>
329331
{(item, itemIndex) => {
330332
const { group, groupOption, data, label, value } = item;

tests/OptionList.test.tsx

+73-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import OptionList from '../src/OptionList';
66
import SelectContext from '../src/SelectContext';
77
import { fillFieldNames, flattenOptions } from '../src/utils/valueUtil';
88
import { injectRunAllTimers } from './utils/common';
9-
import { createEvent, fireEvent, render } from '@testing-library/react';
9+
import { createEvent, fireEvent, render, waitFor } from '@testing-library/react';
10+
import Select from '../src';
11+
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
1012

1113
jest.mock('../src/utils/platformUtil');
1214

@@ -459,4 +461,74 @@ describe('OptionList', () => {
459461
});
460462
expect(onActiveValue).not.toHaveBeenCalledWith('3', expect.anything(), expect.anything());
461463
});
464+
465+
describe('List.ScrollBar', () => {
466+
let mockElement;
467+
let boundingRect = {
468+
top: 0,
469+
bottom: 0,
470+
width: 100,
471+
height: 50,
472+
};
473+
474+
beforeAll(() => {
475+
mockElement = spyElementPrototypes(HTMLElement, {
476+
offsetHeight: {
477+
get: () => 100,
478+
},
479+
clientHeight: {
480+
get: () => 50,
481+
},
482+
getBoundingClientRect: () => boundingRect,
483+
offsetParent: {
484+
get: () => document.body,
485+
},
486+
});
487+
});
488+
489+
afterAll(() => {
490+
mockElement.mockRestore();
491+
});
492+
493+
beforeEach(() => {
494+
boundingRect = {
495+
top: 0,
496+
bottom: 0,
497+
width: 100,
498+
height: 50,
499+
};
500+
jest.useFakeTimers();
501+
});
502+
503+
afterEach(() => {
504+
jest.useRealTimers();
505+
});
506+
507+
it('should show scrollbar when showScrollBar is true', async () => {
508+
const options = Array.from({ length: 10 }, (_, index) => ({
509+
label: `${index + 1}`,
510+
value: `${index + 1}`,
511+
}));
512+
513+
const { container } = render(<Select open showScrollBar options={options} />);
514+
515+
await waitFor(() => {
516+
const scrollbarElement = container.querySelector('.rc-virtual-list-scrollbar-visible');
517+
expect(scrollbarElement).not.toBeNull();
518+
});
519+
});
520+
it('not have scrollbar when showScrollBar is false', async () => {
521+
const options = Array.from({ length: 10 }, (_, index) => ({
522+
label: `${index + 1}`,
523+
value: `${index + 1}`,
524+
}));
525+
526+
const { container } = render(<Select open showScrollBar={false} options={options} />);
527+
528+
await waitFor(() => {
529+
const scrollbarElement = container.querySelector('.rc-virtual-list-scrollbar-visible');
530+
expect(scrollbarElement).toBeNull();
531+
});
532+
});
533+
});
462534
});

0 commit comments

Comments
 (0)