Skip to content

Commit 59fb521

Browse files
authored
Merge pull request #1976 from Ateina/1852-Grid-layout-settings-as-parameters
1852 - Grid layout settings as parameters
2 parents e26443d + af6c1ba commit 59fb521

File tree

4 files changed

+72
-27
lines changed

4 files changed

+72
-27
lines changed

docs/documentation/docs/controls/GridLayout.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,10 @@ The grid layout control can be configured with the following properties:
147147
| items | any[] | yes | The array of items you wish to display. |
148148
| listProps | IListProps | no | Provides additional list properties to customize the underlying list. |
149149
| onRenderGridItem | function | yes | onRenderGridItem handler for the grid layout. Use this handler to specify how you wish to render each grid item |
150+
| itemPadding | number | no | Gap between items. |
151+
| itemMinWidth | number | no | Minimum width for each item. |
152+
| itemMaxWidth | number | no | Maximum width for each item. |
153+
| compactThreshold | number | no | Threshold width below which the compact layout is activated. |
154+
| rowsPerPage | number | no | Number of rows displayed per page. |
150155

151156
![](https://telemetry.sharepointpnp.com/sp-dev-fx-controls-react/wiki/controls/gridlayout)

src/controls/gridLayout/GridLayout.tsx

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@ import { IGridLayoutProps, IGridLayoutState } from './GridLayout.types';
1010

1111
import * as telemetry from '../../common/telemetry';
1212

13-
// Get the constants from the SCSS so that we don't hard-code look and feel elements
14-
const ROWS_PER_PAGE: number = +styles.rowsPerPage;
15-
const MAX_ROW_HEIGHT: number = +styles.maxWidth;
16-
const PADDING: number = +styles.padding;
17-
const MIN_WIDTH: number = +styles.minWidth;
18-
const COMPACT_THRESHOLD: number = +styles.compactThreshold;
19-
2013
/**
2114
* Grid layout component
2215
*/
@@ -31,6 +24,13 @@ export class GridLayout extends React.Component<IGridLayoutProps, IGridLayoutSta
3124
telemetry.track('ReactGridLayout');
3225
}
3326

27+
//Get constants from SCSS if they are not passed in props
28+
private ROWS_PER_PAGE: number = this.props.rowsPerPage ?? +styles.rowsPerPage;
29+
private MAX_WIDTH: number = this.props.itemMaxWidth ?? +styles.maxWidth;
30+
private MIN_WIDTH: number = this.props.itemMinWidth ?? +styles.minWidth;
31+
private PADDING: number = this.props.itemPadding ?? +styles.padding;
32+
private COMPACT_THRESHOLD: number = this.props.compactThreshold ?? +styles.compactThreshold;
33+
3434
// Number of columns in a row
3535
private _columnCount: number;
3636

@@ -49,17 +49,17 @@ export class GridLayout extends React.Component<IGridLayoutProps, IGridLayoutSta
4949
public render(): React.ReactElement<IGridLayoutProps> {
5050
return (
5151
<div className={styles.gridLayout} role="group" aria-label={this.props.ariaLabel}>
52-
<FocusZone>
53-
<List
54-
role="presentation"
55-
className={styles.gridLayoutList}
56-
items={this.props.items}
57-
getItemCountForPage={this._getItemCountForPage}
58-
getPageHeight={this._getPageHeight}
59-
onRenderCell={this._onRenderCell}
60-
{...this.props.listProps}
61-
/>
62-
</FocusZone>
52+
<FocusZone>
53+
<List
54+
role="presentation"
55+
className={styles.gridLayoutList}
56+
items={this.props.items}
57+
getItemCountForPage={this._getItemCountForPage}
58+
getPageHeight={this._getPageHeight}
59+
onRenderCell={this._onRenderCell}
60+
{...this.props.listProps}
61+
/>
62+
</FocusZone>
6363
</div>
6464
);
6565
}
@@ -69,19 +69,19 @@ export class GridLayout extends React.Component<IGridLayoutProps, IGridLayoutSta
6969
*/
7070
private _getItemCountForPage = (itemIndex: number, surfaceRect: IRectangle): number => {
7171
if (itemIndex === 0) {
72-
this._isCompact = surfaceRect.width < COMPACT_THRESHOLD;
72+
this._isCompact = surfaceRect.width < this.COMPACT_THRESHOLD;
7373
if (this._isCompact) {
7474
this._columnCount = 1;
7575
this._columnWidth = surfaceRect.width;
7676
return this.props.items.length;
7777
} else {
78-
this._columnCount = Math.ceil(surfaceRect.width / (MAX_ROW_HEIGHT));
79-
this._columnWidth = Math.max(MIN_WIDTH, Math.floor(surfaceRect.width / this._columnCount) + Math.floor(PADDING / this._columnCount));
78+
this._columnCount = Math.ceil(surfaceRect.width / (this.MAX_WIDTH));
79+
this._columnWidth = Math.max(this.MIN_WIDTH, Math.floor(surfaceRect.width / this._columnCount) + Math.floor(this.PADDING / this._columnCount));
8080
this._rowHeight = this._columnWidth;
8181
}
8282
}
8383

84-
return this._columnCount * ROWS_PER_PAGE;
84+
return this._columnCount * this.ROWS_PER_PAGE;
8585
}
8686

8787
/**
@@ -91,17 +91,17 @@ export class GridLayout extends React.Component<IGridLayoutProps, IGridLayoutSta
9191
if (this._isCompact) {
9292
return this.props.items.length * this._rowHeight;
9393
}
94-
return this._rowHeight * ROWS_PER_PAGE;
94+
return this._rowHeight * this.ROWS_PER_PAGE;
9595
}
9696

9797
/**
9898
* Calls the passed onRenderCell
9999
*/
100100
private _onRenderCell = (item: any, index: number | undefined): JSX.Element => { // eslint-disable-line @typescript-eslint/no-explicit-any
101101
const isCompact: boolean = this._isCompact;
102-
const cellPadding: number = index % this._columnCount !== this._columnCount - 1 && !isCompact ? PADDING : 0;
102+
const cellPadding: number = index % this._columnCount !== this._columnCount - 1 && !isCompact ? this.PADDING : 0;
103103
const finalSize: ISize = { width: this._columnWidth, height: this._rowHeight };
104-
const cellWidth: number = isCompact ? this._columnWidth + PADDING : this._columnWidth - PADDING;
104+
const cellWidth: number = isCompact ? this._columnWidth + this.PADDING : this._columnWidth - this.PADDING;
105105
return (
106106
<div
107107
style={{

src/controls/gridLayout/GridLayout.types.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,41 @@ export interface IGridLayoutProps {
1717
* The method to render each cell item
1818
*/
1919
onRenderGridItem: (item: any, finalSize: ISize, isCompact: boolean) => JSX.Element; // eslint-disable-line @typescript-eslint/no-explicit-any
20+
21+
/**
22+
* Layout configuration props.
23+
* All properties are optional; defaults are provided via styles (SCSS module).
24+
*/
25+
26+
/**
27+
* Gap between items.
28+
* Default: 20.
29+
*/
30+
itemPadding?: number;
31+
32+
/**
33+
* Minimum width for each item.
34+
* Default: 210.
35+
*/
36+
itemMinWidth?: number;
37+
38+
/**
39+
* Maximum width for each item.
40+
* Default: 320
41+
*/
42+
itemMaxWidth?: number;
43+
44+
/**
45+
* Threshold width below which the compact layout is activated.
46+
* Default: 480.
47+
*/
48+
compactThreshold?: number;
49+
50+
/**
51+
* Number of rows displayed per page.
52+
* Default: 3.
53+
*/
54+
rowsPerPage?: number;
2055
}
2156

2257
export interface IGridLayoutState {}

src/webparts/controlsTest/components/ControlsTest.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ export default class ControlsTest extends React.Component<IControlsTestProps, IC
955955
<DocumentCard
956956
type={isCompact ? DocumentCardType.compact : DocumentCardType.normal}
957957
onClick={(ev: React.SyntheticEvent<HTMLElement>) => alert("You clicked on a grid item")}
958-
958+
style={{maxWidth: _finalSize.width}}
959959
>
960960
<DocumentCardPreview {...previewProps} />
961961
{!isCompact && <DocumentCardLocation location={item.location} />}
@@ -2176,7 +2176,12 @@ export default class ControlsTest extends React.Component<IControlsTestProps, IC
21762176
ariaLabel={"List of content, use right and left arrow keys to navigate, arrow down to access details."}
21772177
items={sampleGridData}
21782178
onRenderGridItem={(item: any, finalSize: ISize, isCompact: boolean) => this._onRenderGridItem(item, finalSize, isCompact)}
2179-
/>
2179+
itemMinWidth={250}
2180+
itemMaxWidth={600}
2181+
itemPadding={60}
2182+
//compactThreshold={220}
2183+
//rowsPerPage={1}
2184+
/>
21802185
</div>
21812186
}
21822187
{controlVisibility.HoverReactionsBar &&

0 commit comments

Comments
 (0)