|
| 1 | +# Grid Layout control |
| 2 | + |
| 3 | +This control renders a responsive grid layout for your web parts. The grid layout behaves according to the [SharePoint web part layouts design pattern](https://docs.microsoft.com/en-us/sharepoint/dev/design/layout-patterns#grid-layout). |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +The grid layout will automatically reflow grid items according to the space available for the control. On mobile devices and 1/3 column layouts, it will render a compact layout. |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +Although it is best used with the Fabric UI [DocumentCard control](https://developer.microsoft.com/en-us/fabric#/controls/web/documentcard), it will render any rectangular content you wish to display. |
| 12 | + |
| 13 | +## How to use this control in your solutions |
| 14 | + |
| 15 | +- Check that you installed the `@pnp/spfx-controls-react` dependency. Check out the [getting started](../#getting-started) page for more information about installing the dependency. |
| 16 | +- Import the following modules to your component: |
| 17 | + |
| 18 | +```TypeScript |
| 19 | +import { GridLayout } from "@pnp/spfx-controls-react/lib/GridLayout"; |
| 20 | +``` |
| 21 | + |
| 22 | +- Retrieve the items you wish to display in your grid control. For example, you can place them in your component's `state`: |
| 23 | + |
| 24 | +```TypeScript |
| 25 | +// This sample places loads items in the constructor. You may wish to load |
| 26 | +// your items in the componentDidUpdate |
| 27 | +constructor(props: IMyWebPartProps) { |
| 28 | + super(props); |
| 29 | + |
| 30 | + this.state = { |
| 31 | + items: [{ |
| 32 | + thumbnail: "https://pixabay.com/get/57e9dd474952a414f1dc8460825668204022dfe05555754d742e7bd6/hot-air-balloons-1984308_640.jpg", |
| 33 | + title: "Adventures in SPFx", |
| 34 | + name: "Perry Losselyong", |
| 35 | + profileImageSrc: "https://robohash.org/blanditiisadlabore.png?size=50x50&set=set1", |
| 36 | + location: "SharePoint", |
| 37 | + activity: "3/13/2019" |
| 38 | + }, { |
| 39 | + thumbnail: "https://pixabay.com/get/55e8d5474a52ad14f1dc8460825668204022dfe05555754d742d79d0/autumn-3804001_640.jpg", |
| 40 | + title: "The Wild, Untold Story of SharePoint!", |
| 41 | + name: "Ebonee Gallyhaock", |
| 42 | + profileImageSrc: "https://robohash.org/delectusetcorporis.bmp?size=50x50&set=set1", |
| 43 | + location: "SharePoint", |
| 44 | + activity: "6/29/2019" |
| 45 | + }, { |
| 46 | + thumbnail: "https://pixabay.com/get/57e8dd454c50ac14f1dc8460825668204022dfe05555754d742c72d7/log-cabin-1886620_640.jpg", |
| 47 | + title: "Low Code Solutions: PowerApps", |
| 48 | + name: "Seward Keith", |
| 49 | + profileImageSrc: "https://robohash.org/asperioresautquasi.jpg?size=50x50&set=set1", |
| 50 | + location: "PowerApps", |
| 51 | + activity: "12/31/2018" |
| 52 | + }, { |
| 53 | + thumbnail: "https://pixabay.com/get/55e3d445495aa514f1dc8460825668204022dfe05555754d742b7dd5/portrait-3316389_640.jpg", |
| 54 | + title: "Not Your Grandpa's SharePoint", |
| 55 | + name: "Sharona Selkirk", |
| 56 | + profileImageSrc: "https://robohash.org/velnammolestiae.png?size=50x50&set=set1", |
| 57 | + location: "SharePoint", |
| 58 | + activity: "11/20/2018" |
| 59 | + }, { |
| 60 | + thumbnail: "https://pixabay.com/get/57e6dd474352ae14f1dc8460825668204022dfe05555754d742a7ed1/faucet-1684902_640.jpg", |
| 61 | + title: "Get with the Flow", |
| 62 | + name: "Boyce Batstone", |
| 63 | + profileImageSrc: "https://robohash.org/nulladistinctiomollitia.jpg?size=50x50&set=set1", |
| 64 | + location: "Flow", |
| 65 | + activity: "5/26/2019" |
| 66 | + }] |
| 67 | + }; |
| 68 | + } |
| 69 | +``` |
| 70 | + |
| 71 | +- Because you will implement the method to render each item in your web part, your items can be anything you'd like. Our sample data defines a `thumbnail`, `title`, `name`, `profileImageSrc`, `location` and `activity` to coincide with the Fabric UI `DocumentCard` elements, but you can use any properties you need. |
| 72 | +- In the component that will call the `GridLayout` control, create callback function to render every item in the grid. You can return any rectangular element you want. For example, this code uses the Fabric UI `DocumentCard` control. |
| 73 | + |
| 74 | +```TypeScript |
| 75 | +import { |
| 76 | + DocumentCard, |
| 77 | + DocumentCardActivity, |
| 78 | + DocumentCardPreview, |
| 79 | + DocumentCardDetails, |
| 80 | + DocumentCardTitle, |
| 81 | + IDocumentCardPreviewProps, |
| 82 | + DocumentCardLocation, |
| 83 | + DocumentCardType |
| 84 | +} from 'office-ui-fabric-react/lib/DocumentCard'; |
| 85 | +import { ImageFit } from 'office-ui-fabric-react/lib/Image'; |
| 86 | +import { ISize } from 'office-ui-fabric-react/lib/Utilities'; |
| 87 | + |
| 88 | +... |
| 89 | + |
| 90 | + private _onRenderGridItem = (item: any, finalSize: ISize, isCompact: boolean): JSX.Element => { |
| 91 | + const previewProps: IDocumentCardPreviewProps = { |
| 92 | + previewImages: [ |
| 93 | + { |
| 94 | + previewImageSrc: item.thumbnail, |
| 95 | + imageFit: ImageFit.cover, |
| 96 | + height: 130 |
| 97 | + } |
| 98 | + ] |
| 99 | + }; |
| 100 | + |
| 101 | + return <div |
| 102 | + data-is-focusable={true} |
| 103 | + role="listitem" |
| 104 | + aria-label={item.title} |
| 105 | + > |
| 106 | + <DocumentCard |
| 107 | + type={isCompact ? DocumentCardType.compact : DocumentCardType.normal} |
| 108 | + onClick={(ev: React.SyntheticEvent<HTMLElement>) => alert("You clicked on a grid item")} |
| 109 | + |
| 110 | + > |
| 111 | + <DocumentCardPreview {...previewProps} /> |
| 112 | + {!isCompact && <DocumentCardLocation location={item.location} />} |
| 113 | + <DocumentCardDetails> |
| 114 | + <DocumentCardTitle |
| 115 | + title={item.title} |
| 116 | + shouldTruncate={true} |
| 117 | + /> |
| 118 | + <DocumentCardActivity |
| 119 | + activity={item.activity} |
| 120 | + people={[{ name: item.name, profileImageSrc: item.profileImageSrc }]} |
| 121 | + /> |
| 122 | + </DocumentCardDetails> |
| 123 | + </DocumentCard> |
| 124 | + </div>; |
| 125 | + } |
| 126 | +``` |
| 127 | + |
| 128 | + > Note that the sample code above uses the `isCompact` parameter to remove `DocumentCard` elements and to render a compact layout. You may choose to ignore the `isCompact` parameter if you do not wish to handle compact layouts. |
| 129 | + |
| 130 | +- Use the `GridLayout` control in your code as follows: |
| 131 | + |
| 132 | +```TypeScript |
| 133 | + <GridLayout |
| 134 | + ariaLabel="List of content, use right and left arrow keys to navigate, arrow down to access details." |
| 135 | + items={this.state.items} |
| 136 | + onRenderGridItem={(item: any, finalSize: ISize, isCompact: boolean) => this._onRenderGridItem(item, finalSize, isCompact)} |
| 137 | + /> |
| 138 | +``` |
| 139 | + |
| 140 | +## Implementation |
| 141 | + |
| 142 | +The grid layout control can be configured with the following properties: |
| 143 | + |
| 144 | +| Property | Type | Required | Description | |
| 145 | +| ---- | ---- | ---- | ---- | |
| 146 | +| ariaLabel | string | no | The accessible text you wish to display for the grid control. We recommend that you use `"List of content, use right and left arrow keys to navigate, arrow down to access details."`. | |
| 147 | +| items | any[] | yes | The array of items you wish to display. | |
| 148 | +| listProps | IListProps | no | Provides additional list properties to customize the underlaying list. | |
| 149 | +| onRenderGridItem | function | yes | onRenderGridItem handler for the grid layout. Use this handler to specify how you wish to render each grid item | |
| 150 | + |
| 151 | + |
0 commit comments