1+ import React , { PureComponent } from 'react' ;
2+ import { GridPanel , InjectedQueryModels , withQueryModels } from '@labkey/components' ;
3+
4+ // First define the properties of the component you're going to wrap with withQueryModels.
5+ interface OwnProps {
6+ title : string ;
7+ asPanel : boolean ;
8+ }
9+
10+ // Here we create a type that includes InjectedQueryModels because
11+ // we're wrapping the component with withQueryModels which will inject
12+ // queryModels and actions objects.
13+ type Props = OwnProps & InjectedQueryModels ;
14+
15+ // Here we use the name ExampleGridPanelImpl, users will not use this
16+ // component directly, only the wrapped version below which we expose
17+ // to users as ExampleGridPanel.
18+ class ExampleGridPanelImpl extends PureComponent < Props > {
19+ onRefreshGrid = ( ) => {
20+ const { queryModels, actions } = this . props ;
21+ const { containersModel } = queryModels ;
22+
23+ actions . loadModel ( containersModel . id ) ;
24+ } ;
25+
26+ // This is an example of a custom button bar element in your GridPanel that can interact with the QueryModel.
27+ renderGridButtons = ( ) => {
28+ return (
29+ < button className = { 'labkey-button' } onClick = { this . onRefreshGrid } >
30+ Refresh Grid
31+ </ button >
32+ )
33+ } ;
34+
35+ render ( ) {
36+ // Note that queryModels and actions come from InjectedQueryModels via withQueryModels
37+ const { queryModels, actions, title, asPanel } = this . props ;
38+ const { containersModel } = queryModels ;
39+
40+ return (
41+ < GridPanel
42+ model = { containersModel }
43+ ButtonsComponent = { this . renderGridButtons }
44+ actions = { actions }
45+ title = { title }
46+ asPanel = { asPanel }
47+ hideEmptyChartMenu = { true }
48+ hideEmptyViewMenu = { true }
49+ />
50+ ) ;
51+ }
52+ }
53+
54+ // Next wrap your component with withQueryModels, here we set the type
55+ // to OwnProps so the returned component, ExampleGridPanel, can
56+ // be used in a type-safe manner. In this case, if the user forgets to
57+ // pass in a title or the asPanel property, we'll get a compiler error as intended.
58+ export const ExampleGridPanel = withQueryModels < OwnProps > ( ExampleGridPanelImpl ) ;
0 commit comments