This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Widget.tsx
187 lines (169 loc) · 6.8 KB
/
Widget.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import React, { FunctionComponent } from 'react'
import { connect } from 'react-redux'
import { Skeleton, Spin } from 'antd'
import { Store } from '../../interfaces/store'
import { WidgetTypes, CustomWidgetDescriptor, isCustomWidget, WidgetMetaAny, WidgetMeta, PopupWidgetTypes } from '../../interfaces/widget'
import TableWidget from '../widgets/TableWidget/TableWidget'
import TextWidget from '../widgets/TextWidget/TextWidget'
import FormWidget from '../widgets/FormWidget/FormWidget'
import InfoWidget from '../widgets/InfoWidget/InfoWidget'
import styles from './Widget.less'
import AssocListPopup from '../widgets/AssocListPopup/AssocListPopup'
import PickListPopup from '../widgets/PickListPopup/PickListPopup'
import { buildBcUrl } from '../../utils/strings'
import FlatTreePopup from '../widgets/FlatTree/FlatTreePopup'
import FlatTree from '../widgets/FlatTree/FlatTree'
import DebugPanel from '../DebugPanel/DebugPanel'
import { checkShowCondition } from '../../utils/bc'
import NavigationTabsWidget from '../widgets/NavigationTabsWidget/NavigationTabsWidget'
interface WidgetOwnProps {
meta: WidgetMeta | WidgetMetaAny
card?: (props: any) => React.ReactElement<any>
customSpinner?: (props: any) => React.ReactElement<any>
children?: React.ReactNode
}
interface WidgetProps extends WidgetOwnProps {
debugMode?: boolean
loading?: boolean
parentCursor?: string
customWidgets?: Record<string, CustomWidgetDescriptor>
showWidget: boolean
rowMetaExists: boolean
dataExists: boolean
}
const skeletonParams = { rows: 5 }
/**
*
* @param props
* @category Components
*/
export const Widget: FunctionComponent<WidgetProps> = props => {
if (!props.showWidget) {
return null
}
const customWidget = props.customWidgets?.[props.meta.type]
const skipCardWrapping = PopupWidgetTypes.includes(props.meta.type)
if (skipCardWrapping) {
return <> {chooseWidgetType(props.meta, props.customWidgets, props.children)} </>
}
const showSpinner = !!(props.loading && (props.rowMetaExists || props.dataExists))
const showSkeleton = props.loading && !showSpinner
const spinnerElement = props.customSpinner ? (
<props.customSpinner spinning={showSpinner}>
{chooseWidgetType(props.meta, props.customWidgets, props.children)}
</props.customSpinner>
) : (
<Spin spinning={showSpinner}>{chooseWidgetType(props.meta, props.customWidgets, props.children)}</Spin>
)
// TODO 2.0.0 delete spinner and skeleton. Spinner and skeleton should be overridden by props.card component
const WidgetParts = (
<>
{showSkeleton && <Skeleton loading paragraph={skeletonParams} />}
{!showSkeleton && spinnerElement}
{props.debugMode && <DebugPanel widgetMeta={props.meta} />}
</>
)
if (customWidget && !isCustomWidget(customWidget)) {
if (customWidget.card === null) {
return WidgetParts
}
if (customWidget.card) {
const CustomCard = customWidget.card
return <CustomCard meta={props.meta}>{WidgetParts}</CustomCard>
}
}
if (props.card) {
const Card = props.card
return <Card meta={props.meta}>{WidgetParts}</Card>
}
return (
<div className={styles.container} data-widget-type={props.meta.type}>
<h2 className={styles.title}>{props.meta.title}</h2>
{WidgetParts}
</div>
)
}
/**
* Return component instance based on type specified in widget meta
*
* `customWidgets` dictionary can be used to extend this function with new widget types,
* with custom declaration having a priority when it is specified for core widget type
* `children` is returned in case of unknown widget type
*
* @param widgetMeta Meta configuration for widget
* @param customWidgets Dictionary where key is a widget type and value is a component that should be rendered
* @param children Widgets children component, returned in case type is unknown
*/
function chooseWidgetType(
widgetMeta: WidgetMeta | WidgetMetaAny,
customWidgets?: Record<string, CustomWidgetDescriptor>,
children?: React.ReactNode
) {
const customWidget = customWidgets?.[widgetMeta.type]
if (customWidget) {
if (isCustomWidget(customWidget)) {
const CustomWidgetComponent = customWidget
return <CustomWidgetComponent meta={widgetMeta} />
}
const DescriptorComponent = customWidget.component
return <DescriptorComponent meta={widgetMeta} />
}
const knownWidgetMeta = widgetMeta as WidgetMetaAny
switch (knownWidgetMeta.type) {
case WidgetTypes.List:
case WidgetTypes.DataGrid:
return <TableWidget meta={knownWidgetMeta} showRowActions />
case WidgetTypes.Form:
return <FormWidget meta={knownWidgetMeta} />
case WidgetTypes.Text:
return <TextWidget meta={knownWidgetMeta} />
case WidgetTypes.AssocListPopup:
return <AssocListPopup widget={knownWidgetMeta} />
case WidgetTypes.PickListPopup:
return <PickListPopup widget={knownWidgetMeta} />
case WidgetTypes.Info:
return <InfoWidget meta={knownWidgetMeta} />
case WidgetTypes.FlatTree:
return <FlatTree meta={knownWidgetMeta} />
case WidgetTypes.FlatTreePopup:
return <FlatTreePopup meta={knownWidgetMeta} />
case WidgetTypes.NavigationTabs:
return <NavigationTabsWidget meta={knownWidgetMeta} />
default:
return children
}
}
function mapStateToProps(store: Store, ownProps: WidgetOwnProps) {
const bcName = ownProps.meta.bcName
const bc = store.screen.bo.bc[bcName]
const parent = store.screen.bo.bc[bc?.parentName]
const hasParent = !!parent
const legacyPopupCheck = store.view.popupData.bcName === bcName
const newPopupCheck = store.view.popupData.widgetName ? store.view.popupData.widgetName === ownProps.meta.name : legacyPopupCheck
let showWidget = PopupWidgetTypes.includes(ownProps.meta.type) ? newPopupCheck : true
if (
!checkShowCondition(
ownProps.meta.showCondition,
store.screen.bo.bc[ownProps.meta.showCondition?.bcName]?.cursor,
store.data[ownProps.meta.showCondition?.bcName],
store.view.pendingDataChanges
)
) {
showWidget = false
}
const bcUrl = buildBcUrl(bcName, true)
const rowMeta = bcUrl && store.view.rowMeta[bcName]?.[bcUrl]
return {
debugMode: store.session.debugMode,
loading: bc?.loading,
parentCursor: hasParent && parent.cursor,
showWidget,
rowMetaExists: !!rowMeta,
dataExists: !!store.data[bcName]
}
}
/**
* @category Components
*/
const ConnectedWidget = connect(mapStateToProps)(Widget)
export default ConnectedWidget