Skip to content

Commit 43ae9d3

Browse files
authored
feat: support disable virtual (#12)
* add virtual prop * add test case
1 parent 5b16af6 commit 43ae9d3

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

.prettierrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"endOfLine": "lf",
3+
"semi": true,
4+
"singleQuote": true,
5+
"tabWidth": 2,
6+
"trailingComma": "all",
7+
"printWidth": 100
8+
}

src/List.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ export interface ListProps<T> extends React.HTMLAttributes<any> {
6161
fullHeight?: boolean;
6262
itemKey: Key | ((item: T) => Key);
6363
component?: string | React.FC<any> | React.ComponentClass<any>;
64+
/** Disable scroll check. Usually used on animation control */
6465
disabled?: boolean;
66+
/** Set `false` will always use real scroll instead of virtual one */
67+
virtual?: boolean;
6568

6669
/** When `disabled`, trigger if changed item not render. */
6770
onSkipRender?: () => void;
@@ -158,7 +161,7 @@ class List<T = any> extends React.Component<ListProps<T>, ListState<T>> {
158161
startIndex: 0,
159162
endIndex: 0,
160163
startItemTop: 0,
161-
isVirtual: requireVirtual(props.height, props.itemHeight, props.data.length),
164+
isVirtual: requireVirtual(props.height, props.itemHeight, props.data.length, props.virtual),
162165
itemCount: props.data.length,
163166
};
164167
}
@@ -189,7 +192,7 @@ class List<T = any> extends React.Component<ListProps<T>, ListState<T>> {
189192
*/
190193
public componentDidUpdate() {
191194
const { status } = this.state;
192-
const { data, height, itemHeight, disabled, onSkipRender } = this.props;
195+
const { data, height, itemHeight, disabled, onSkipRender, virtual } = this.props;
193196
const prevData: T[] = this.cachedProps.data || [];
194197

195198
let changedItemIndex: number = null;
@@ -214,7 +217,7 @@ class List<T = any> extends React.Component<ListProps<T>, ListState<T>> {
214217
return;
215218
}
216219

217-
const isVirtual = requireVirtual(height, itemHeight, data.length);
220+
const isVirtual = requireVirtual(height, itemHeight, data.length, virtual);
218221
let nextStatus = status;
219222
if (this.state.isVirtual !== isVirtual) {
220223
nextStatus = isVirtual ? 'SWITCH_TO_VIRTUAL' : 'SWITCH_TO_RAW';
@@ -734,6 +737,7 @@ class List<T = any> extends React.Component<ListProps<T>, ListState<T>> {
734737
itemKey,
735738
onSkipRender,
736739
disabled,
740+
virtual,
737741
...restProps
738742
} = this.props;
739743

@@ -745,7 +749,7 @@ class List<T = any> extends React.Component<ListProps<T>, ListState<T>> {
745749
* Virtual list switch is works on component updated.
746750
* We should double check here if need cut the content.
747751
*/
748-
const shouldVirtual = requireVirtual(height, itemHeight, data.length);
752+
const shouldVirtual = requireVirtual(height, itemHeight, data.length, virtual);
749753

750754
return (
751755
<Component

src/utils/itemUtil.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ export function getCompareItemRelativeTop({
184184
return originCompareItemTop;
185185
}
186186

187-
export function requireVirtual(height: number, itemHeight: number, count: number) {
188-
return typeof height === 'number' && count * itemHeight > height;
187+
export function requireVirtual(
188+
height: number,
189+
itemHeight: number,
190+
count: number,
191+
virtual: boolean,
192+
) {
193+
return virtual !== false && typeof height === 'number' && count * itemHeight > height;
189194
}

tests/list.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,9 @@ describe('List', () => {
186186
setTimeout(done, 50);
187187
});
188188
});
189+
190+
it('`virtual` is false', () => {
191+
const wrapper = genList({ itemHeight: 20, height: 100, data: genData(100), virtual: false });
192+
expect(wrapper.find('li')).toHaveLength(100);
193+
});
189194
});

0 commit comments

Comments
 (0)