-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathCardEditForm.jsx
206 lines (193 loc) · 6.9 KB
/
CardEditForm.jsx
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import React, { useMemo } from 'react';
import PropTypes from 'prop-types';
import { Tabs, Tab, TabList, TabPanels, TabPanel } from '@carbon/react';
import { CARD_DIMENSIONS, CARD_TYPES } from '../../../constants/LayoutConstants';
import { settings } from '../../../constants/Settings';
import {
DashboardEditorActionsPropTypes,
DataItemsPropTypes,
defaultDashboardEditorActionsProps,
} from '../../DashboardEditor/editorUtils';
import CardEditFormContent from './CardEditFormContent';
import CardEditFormSettings from './CardEditFormSettings';
const { iotPrefix } = settings;
const propTypes = {
/** card data value */
cardConfig: PropTypes.object, // eslint-disable-line react/forbid-prop-types
/** Callback function when form data changes */
onChange: PropTypes.func.isRequired,
i18n: PropTypes.shape({
openEditorButton: PropTypes.string,
contentTabLabel: PropTypes.string,
settingsTabLabel: PropTypes.string,
cardSize_SMALL: PropTypes.string,
cardSize_SMALLWIDE: PropTypes.string,
cardSize_SMALLFULL: PropTypes.string,
cardSize_MEDIUM: PropTypes.string,
cardSize_MEDIUMTHIN: PropTypes.string,
cardSize_MEDIUMWIDE: PropTypes.string,
cardSize_LARGE: PropTypes.string,
cardSize_LARGETHIN: PropTypes.string,
cardSize_LARGEWIDE: PropTypes.string,
chartType_BAR: PropTypes.string,
chartType_LINE: PropTypes.string,
barChartType_SIMPLE: PropTypes.string,
barChartType_GROUPED: PropTypes.string,
barChartType_STACKED: PropTypes.string,
barChartLayout_HORIZONTAL: PropTypes.string,
barChartLayout_VERTICAL: PropTypes.string,
errorTitle: PropTypes.string,
modalTitle: PropTypes.string,
modalLabel: PropTypes.string,
modalHelpText: PropTypes.string,
modalIconDescription: PropTypes.string,
}),
/** if provided, returns an array of strings which are the dataItems to be allowed
* on each card
* getValidDataItems(card, selectedTimeRange)
*/
getValidDataItems: PropTypes.func,
/** if provided, returns an array of strings which are the timeRanges to be allowed
* on each card
* getValidTimeRanges(card, selectedDataItems)
*/
getValidTimeRanges: PropTypes.func,
/** an array of dataItems to be included on each card
* this prop will be ignored if getValidDataItems is defined
*/
dataItems: DataItemsPropTypes,
/** an object where the keys are available dimensions and the values are the values available for those dimensions
* ex: { manufacturer: ['Rentech', 'GHI Industries'], deviceid: ['73000', '73001', '73002'] }
*/
availableDimensions: PropTypes.shape({}),
currentBreakpoint: PropTypes.string,
isSummaryDashboard: PropTypes.bool,
/** optional link href's for each card type that will appear in a tooltip */
dataSeriesItemLinks: PropTypes.shape({
simpleBar: PropTypes.string,
groupedBar: PropTypes.string,
stackedBar: PropTypes.string,
timeSeries: PropTypes.string,
value: PropTypes.string,
custom: PropTypes.string,
table: PropTypes.string,
image: PropTypes.string,
}),
actions: DashboardEditorActionsPropTypes,
};
const defaultProps = {
cardConfig: {},
i18n: {
openEditorButton: 'Open JSON editor',
contentTabLabel: 'Content',
settingsTabLabel: 'Settings',
cardSize_SMALL: 'Small',
cardSize_SMALLWIDE: 'Small wide',
cardSize_SMALLFULL: 'Small full',
cardSize_MEDIUM: 'Medium',
cardSize_MEDIUMTHIN: 'Medium thin',
cardSize_MEDIUMWIDE: 'Medium wide',
cardSize_LARGE: 'Large',
cardSize_LARGETHIN: 'Large thin',
cardSize_LARGEWIDE: 'Large wide',
chartType_BAR: 'Bar',
chartType_LINE: 'Line',
barChartType_SIMPLE: 'Simple',
barChartType_GROUPED: 'Grouped',
barChartType_STACKED: 'Stacked',
barChartLayout_HORIZONTAL: 'Horizontal',
barChartLayout_VERTICAL: 'Vertical',
errorTitle: 'Error:',
modalTitle: 'Edit card JSON configuration',
modalLabel: 'Card editor',
modalHelpText:
'The JSON definition for this card is provided below. You can modify this data directly to update the card configuration.',
modalIconDescription: 'Close',
},
getValidDataItems: null,
getValidTimeRanges: null,
dataItems: [],
availableDimensions: {},
currentBreakpoint: 'xl',
isSummaryDashboard: false,
dataSeriesItemLinks: null,
actions: defaultDashboardEditorActionsProps,
};
/**
* Returns card size and dimensions labels
* @param {string} size
* @param {Object<string>} i18n
* @returns {string}
*/
export const getCardSizeText = (size, i18n) => {
const sizeName = i18n[`cardSize_${size}`];
const sizeDimensions = `(${CARD_DIMENSIONS[size].lg.w}x${CARD_DIMENSIONS[size].lg.h})`;
return `${sizeName} ${sizeDimensions}`;
};
const CardEditForm = ({
cardConfig,
isSummaryDashboard,
onChange,
i18n,
dataItems,
getValidDataItems,
getValidTimeRanges,
currentBreakpoint,
availableDimensions,
dataSeriesItemLinks,
// eslint-disable-next-line react/prop-types
onFetchDynamicDemoHotspots,
actions,
}) => {
const mergedI18n = useMemo(() => ({ ...defaultProps.i18n, ...i18n }), [i18n]);
const baseClassName = `${iotPrefix}--card-edit-form`;
const isCustomCardWithNoSettings =
(cardConfig.type === CARD_TYPES.CUSTOM || !CARD_TYPES.hasOwnProperty(cardConfig.type)) &&
!cardConfig.renderEditSettings;
return (
<div className={baseClassName}>
<Tabs>
<TabList contained fullWidth>
<Tab>{mergedI18n.contentTabLabel}</Tab>
{!isCustomCardWithNoSettings ? <Tab>{mergedI18n.settingsTabLabel}</Tab> : null}
</TabList>
<TabPanels>
<TabPanel>
<CardEditFormContent
cardConfig={cardConfig}
onChange={onChange}
isSummaryDashboard={isSummaryDashboard}
i18n={mergedI18n}
dataItems={dataItems}
availableDimensions={availableDimensions}
getValidDataItems={getValidDataItems}
getValidTimeRanges={getValidTimeRanges}
currentBreakpoint={currentBreakpoint}
dataSeriesItemLinks={dataSeriesItemLinks}
onFetchDynamicDemoHotspots={onFetchDynamicDemoHotspots}
actions={actions}
/>
</TabPanel>
{!isCustomCardWithNoSettings ? (
<TabPanel>
<CardEditFormSettings
availableDimensions={availableDimensions}
cardConfig={cardConfig}
onChange={onChange}
i18n={mergedI18n}
getValidDataItems={getValidDataItems}
getValidTimeRanges={getValidTimeRanges}
currentBreakpoint={currentBreakpoint}
dataSeriesItemLinks={dataSeriesItemLinks}
onFetchDynamicDemoHotspots={onFetchDynamicDemoHotspots}
/>
</TabPanel>
) : null}
</TabPanels>
</Tabs>
</div>
);
};
CardEditForm.propTypes = propTypes;
CardEditForm.defaultProps = defaultProps;
export default CardEditForm;