Skip to content

Commit 7532d2e

Browse files
authored
feat: support nativeElement (#270)
* feat: support nativeElement * test: adjust
1 parent ff58fb8 commit 7532d2e

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/List.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export type ScrollConfig = ScrollTarget | ScrollPos;
3939
export type ScrollTo = (arg: number | ScrollConfig) => void;
4040

4141
export type ListRef = {
42+
nativeElement: HTMLDivElement;
4243
scrollTo: ScrollTo;
4344
getScrollInfo: () => ScrollInfo;
4445
};
@@ -147,6 +148,7 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
147148
const mergedData = data || EMPTY_DATA;
148149
const componentRef = useRef<HTMLDivElement>();
149150
const fillerInnerRef = useRef<HTMLDivElement>();
151+
const containerRef = useRef<HTMLDivElement>();
150152

151153
// =============================== Item Key ===============================
152154

@@ -455,6 +457,7 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
455457
);
456458

457459
React.useImperativeHandle(ref, () => ({
460+
nativeElement: containerRef.current,
458461
getScrollInfo: getVirtualScrollInfo,
459462
scrollTo: (config) => {
460463
function isPosScroll(arg: any): arg is ScrollPos {
@@ -534,6 +537,7 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
534537

535538
return (
536539
<div
540+
ref={containerRef}
537541
style={{
538542
...style,
539543
position: 'relative',

tests/list.test.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ describe('List.Basic', () => {
1717
</List>
1818
);
1919

20-
if (props.ref) {
21-
node = <div>{node}</div>;
22-
}
23-
2420
return mount(node);
2521
}
2622

@@ -226,4 +222,10 @@ describe('List.Basic', () => {
226222

227223
expect(wrapper.find('div#my_list').prop('role')).toEqual('listbox');
228224
});
225+
226+
it('nativeElement', () => {
227+
const ref = React.createRef();
228+
const wrapper = genList({ data: genData(1), ref });
229+
expect(ref.current.nativeElement).toBe(wrapper.getDOMNode());
230+
});
229231
});

0 commit comments

Comments
 (0)