-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
ShimmeredDetailsList.base.tsx
160 lines (136 loc) · 5.46 KB
/
ShimmeredDetailsList.base.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import * as React from 'react';
import { BaseComponent, css, classNamesFunction } from '../../Utilities';
import { SelectionMode } from '../../utilities/selection/interfaces';
import { DetailsList } from './DetailsList';
import { IDetailsRowProps } from './DetailsRow';
import { Shimmer, ShimmerElementsGroup, ShimmerElementType, IShimmerElement } from '../../Shimmer';
import {
IShimmeredDetailsListProps,
IShimmeredDetailsListStyleProps,
IShimmeredDetailsListStyles
} from './ShimmeredDetailsList.types';
import { CheckboxVisibility } from './DetailsList.types';
import { IDetailsRowStyleProps, IDetailsRowStyles } from './DetailsRow.types';
import { DEFAULT_CELL_STYLE_PROPS, getStyles as getRowStyles } from './DetailsRow.styles';
const getRowClassNames = classNamesFunction<IDetailsRowStyleProps, IDetailsRowStyles>();
const getClassNames = classNamesFunction<IShimmeredDetailsListStyleProps, IShimmeredDetailsListStyles>();
const SHIMMER_INITIAL_ITEMS = 10;
const DEFAULT_SHIMMER_HEIGHT = 7;
const SHIMMER_LINE_VS_CELL_WIDTH_RATIO = 0.95;
// This values are matching values from ./DetailsRow.css
const DEFAULT_ROW_HEIGHT = 42;
const COMPACT_ROW_HEIGHT = 32;
export class ShimmeredDetailsListBase extends BaseComponent<IShimmeredDetailsListProps, {}> {
private _shimmerItems: null[];
constructor(props: IShimmeredDetailsListProps) {
super(props);
this._shimmerItems = props.shimmerLines ? new Array(props.shimmerLines) : new Array(SHIMMER_INITIAL_ITEMS);
}
public render(): JSX.Element {
const {
items,
listProps,
styles,
theme,
shimmerLines,
onRenderCustomPlaceholder,
enableShimmer,
...detailsListProps
} = this.props;
// Adds to the optional listProp classname a fading out overlay classname only when shimmer enabled.
const classNames = getClassNames(styles, {
theme: theme!,
className: listProps && listProps.className,
enableShimmer
});
const newListProps = { ...listProps, className: classNames.root };
return (
<DetailsList
{...detailsListProps}
enableShimmer={enableShimmer}
items={enableShimmer ? this._shimmerItems : items}
onRenderMissingItem={this._onRenderShimmerPlaceholder}
listProps={newListProps}
/>
);
}
private _onRenderShimmerPlaceholder = (index: number, rowProps: IDetailsRowProps): React.ReactNode => {
const { onRenderCustomPlaceholder, compact } = this.props;
const { selectionMode, checkboxVisibility } = rowProps;
const theme = this.props.theme!;
const showCheckbox = selectionMode !== SelectionMode.none && checkboxVisibility !== CheckboxVisibility.hidden;
const rowStyleProps = {
...rowProps,
theme: theme
};
const rowClassNames = getRowClassNames(getRowStyles(rowStyleProps), {
theme: theme
});
const placeholderElements: React.ReactNode = onRenderCustomPlaceholder
? onRenderCustomPlaceholder()
: this._renderDefaultShimmerPlaceholder(rowProps);
return (
<div
className={css(showCheckbox && rowClassNames.shimmerLeftBorder, !compact && rowClassNames.shimmerBottomBorder)}
>
<Shimmer customElementsGroup={placeholderElements} />
</div>
);
};
private _renderDefaultShimmerPlaceholder = (rowProps: IDetailsRowProps): React.ReactNode => {
const { columns, compact, cellStyleProps = DEFAULT_CELL_STYLE_PROPS } = rowProps;
const shimmerElementsRow: JSX.Element[] = [];
const gapHeight: number = compact ? COMPACT_ROW_HEIGHT : DEFAULT_ROW_HEIGHT;
columns.map((column, columnIdx) => {
const shimmerElements: IShimmerElement[] = [];
const groupWidth: number =
cellStyleProps.cellLeftPadding +
cellStyleProps.cellRightPadding +
column.calculatedWidth! +
(column.isPadded ? cellStyleProps.cellExtraRightPadding : 0);
shimmerElements.push({
type: ShimmerElementType.gap,
width: cellStyleProps.cellLeftPadding,
height: gapHeight
});
if (column.isIconOnly) {
shimmerElements.push({
type: ShimmerElementType.line,
width: column.calculatedWidth!,
height: column.calculatedWidth!
});
shimmerElements.push({
type: ShimmerElementType.gap,
width: cellStyleProps.cellRightPadding,
height: gapHeight
});
} else {
shimmerElements.push({
type: ShimmerElementType.line,
width: column.calculatedWidth! * SHIMMER_LINE_VS_CELL_WIDTH_RATIO,
height: DEFAULT_SHIMMER_HEIGHT
});
shimmerElements.push({
type: ShimmerElementType.gap,
width:
cellStyleProps.cellRightPadding +
(column.calculatedWidth! - column.calculatedWidth! * SHIMMER_LINE_VS_CELL_WIDTH_RATIO) +
(column.isPadded ? cellStyleProps.cellExtraRightPadding : 0),
height: gapHeight
});
}
shimmerElementsRow.push(
<ShimmerElementsGroup key={columnIdx} width={`${groupWidth}px`} shimmerElements={shimmerElements} />
);
});
// When resizing the window from narrow to wider, we need to cover the exposed Shimmer wave until the column resizing logic is done.
shimmerElementsRow.push(
<ShimmerElementsGroup
key={'endGap'}
width={'100%'}
shimmerElements={[{ type: ShimmerElementType.gap, width: '100%', height: gapHeight }]}
/>
);
return <div style={{ display: 'flex' }}>{shimmerElementsRow}</div>;
};
}