Skip to content

Commit e3cda4a

Browse files
committed
feat: support prefixCls
1 parent 5f03df9 commit e3cda4a

File tree

5 files changed

+58
-8
lines changed

5 files changed

+58
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
"@types/react": "^16.8.19",
4343
"@types/react-dom": "^16.8.4",
4444
"@types/warning": "^3.0.0",
45-
"classnames": "^2.2.6",
4645
"cross-env": "^5.2.0",
4746
"enzyme": "^3.1.0",
4847
"enzyme-adapter-react-16": "^1.0.2",
@@ -55,6 +54,7 @@
5554
"typescript": "^3.5.2"
5655
},
5756
"dependencies": {
57+
"classnames": "^2.2.6",
5858
"rc-util": "^4.8.0"
5959
}
6060
}

src/Filler.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as React from 'react';
2+
import classNames from 'classnames';
23

34
interface FillerProps {
5+
prefixCls?: string;
46
/** Virtual filler height. Should be `count * itemMinHeight` */
57
height: number;
68
/** Set offset of visible items. Should be the top of start item position */
@@ -12,7 +14,12 @@ interface FillerProps {
1214
/**
1315
* Fill component to provided the scroll content real height.
1416
*/
15-
const Filler: React.FC<FillerProps> = ({ height, offset, children }): React.ReactElement => {
17+
const Filler: React.FC<FillerProps> = ({
18+
height,
19+
offset,
20+
children,
21+
prefixCls,
22+
}): React.ReactElement => {
1623
let outerStyle: React.CSSProperties = {};
1724

1825
let innerStyle: React.CSSProperties = {
@@ -36,7 +43,14 @@ const Filler: React.FC<FillerProps> = ({ height, offset, children }): React.Reac
3643

3744
return (
3845
<div style={outerStyle}>
39-
<div style={innerStyle}>{children}</div>
46+
<div
47+
style={innerStyle}
48+
className={classNames({
49+
[`${prefixCls}-holder-inner`]: prefixCls,
50+
})}
51+
>
52+
{children}
53+
</div>
4054
</div>
4155
);
4256
};

src/List.tsx

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as React from 'react';
2+
import classNames from 'classnames';
23
import Filler from './Filler';
34
import {
45
getElementScrollPercentage,
@@ -39,6 +40,7 @@ export interface ScrollInfo {
3940
}
4041

4142
export interface ListProps<T> extends React.HTMLAttributes<any> {
43+
prefixCls?: string;
4244
children: RenderFunc<T>;
4345
data: T[];
4446
height?: number;
@@ -548,7 +550,9 @@ class List<T> extends React.Component<ListProps<T>, ListState<T>> {
548550
public render() {
549551
const { isVirtual, itemCount } = this.state;
550552
const {
553+
prefixCls,
551554
style,
555+
className,
552556
component: Component = 'div',
553557
height,
554558
itemHeight,
@@ -559,16 +563,21 @@ class List<T> extends React.Component<ListProps<T>, ListState<T>> {
559563
...restProps
560564
} = this.props;
561565

566+
const mergedClassName = classNames(prefixCls, className);
567+
562568
// Render pure list if not set height or height is enough for all items
563569
if (!isVirtual) {
564570
return (
565571
<Component
566572
style={height ? { ...style, height, ...ScrollStyle } : style}
573+
className={mergedClassName}
567574
{...restProps}
568575
onScroll={this.onRawScroll}
569576
ref={this.listRef}
570577
>
571-
<Filler height={height}>{this.renderChildren(data, 0, children)}</Filler>
578+
<Filler prefixCls={prefixCls} height={height}>
579+
{this.renderChildren(data, 0, children)}
580+
</Filler>
572581
</Component>
573582
);
574583
}
@@ -584,8 +593,18 @@ class List<T> extends React.Component<ListProps<T>, ListState<T>> {
584593
const contentHeight = itemCount * itemHeight * ITEM_SCALE_RATE;
585594

586595
return (
587-
<Component style={mergedStyle} {...restProps} onScroll={this.onScroll} ref={this.listRef}>
588-
<Filler height={contentHeight} offset={status === 'MEASURE_DONE' ? startItemTop : 0}>
596+
<Component
597+
style={mergedStyle}
598+
className={mergedClassName}
599+
{...restProps}
600+
onScroll={this.onScroll}
601+
ref={this.listRef}
602+
>
603+
<Filler
604+
prefixCls={prefixCls}
605+
height={contentHeight}
606+
offset={status === 'MEASURE_DONE' ? startItemTop : 0}
607+
>
589608
{this.renderChildren(data.slice(startIndex, endIndex + 1), startIndex, children)}
590609
</Filler>
591610
</Component>

src/mock.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as React from 'react';
2+
import classNames from 'classnames';
23
import OriginList from './List';
34
import Filler from './Filler';
45

@@ -9,7 +10,9 @@ class List<T> extends OriginList<T> {
910

1011
render() {
1112
const {
13+
prefixCls,
1214
style,
15+
className,
1316
component: Component = 'div',
1417
height,
1518
itemHeight,
@@ -20,9 +23,13 @@ class List<T> extends OriginList<T> {
2023
...restProps
2124
} = this.props;
2225

26+
const mergedClassName = classNames(prefixCls, className);
27+
2328
return (
24-
<Component style={{ ...style, height }} {...restProps}>
25-
<Filler height={height}>{this.renderChildren(data, 0, children)}</Filler>
29+
<Component style={{ ...style, height }} className={mergedClassName} {...restProps}>
30+
<Filler prefixCls={prefixCls} height={height}>
31+
{this.renderChildren(data, 0, children)}
32+
</Filler>
2633
</Component>
2734
);
2835
}

tests/props.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,14 @@ describe('Props', () => {
3030
.key(),
3131
).toBe('1128');
3232
});
33+
34+
it('prefixCls', () => {
35+
const wrapper = mount(
36+
<List data={[0]} itemKey={id => id} prefixCls="prefix">
37+
{id => <div>{id}</div>}
38+
</List>,
39+
);
40+
41+
expect(wrapper.find('.prefix-holder-inner').length).toBeTruthy();
42+
});
3343
});

0 commit comments

Comments
 (0)