-
Notifications
You must be signed in to change notification settings - Fork 76
/
CardToolbar.jsx
331 lines (320 loc) · 11.1 KB
/
CardToolbar.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import React, { useMemo, useCallback } from 'react';
import PropTypes from 'prop-types';
import { omit, keyBy } from 'lodash-es';
import { Close16, Popup16, Settings16 } from '@carbon/icons-react';
import { OverflowMenu, OverflowMenuItem } from 'carbon-components-react';
import classnames from 'classnames';
import { useLangDirection } from 'use-lang-direction';
import { settings } from '../../constants/Settings';
import { TimeRangeOptionsPropTypes } from '../../constants/CardPropTypes';
import { CARD_ACTIONS } from '../../constants/LayoutConstants';
import DateTimePicker, {
DateTimePickerDefaultValuePropTypes,
} from '../DateTimePicker/DateTimePickerV2WithoutTimeSpinner';
import Button from '../Button';
import { PRESET_VALUES } from '../../constants/DateConstants';
import CardRangePicker, { CardRangePickerPropTypes } from './CardRangePicker';
const { iotPrefix, prefix } = settings;
export const ToolbarSVGWrapper = (props) => {
return (
<Button
kind="ghost"
className={classnames(
`${iotPrefix}--card--toolbar-action`,
`${iotPrefix}--card--toolbar-svg-wrapper`,
`${prefix}--btn--icon-only` // can't actually use the hasIconOnly prop because we don't want the tooltip
)}
{...props}
size="sm"
/>
);
};
const propTypes = {
/** set of available actions for the card */
availableActions: PropTypes.objectOf(PropTypes.oneOfType([PropTypes.bool, PropTypes.string]))
.isRequired,
/** is the card editable */
isEditable: PropTypes.bool,
/** is the card expanded */
isExpanded: PropTypes.bool,
/**
* Define the icon render to be rendered.
* Can be a React component class
*/
renderExpandIcon: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
className: PropTypes.string,
...omit(CardRangePickerPropTypes, 'onClose'),
/** Generates the available time range selection options. Each option should include 'this' or 'last'.
* i.e. { thisWeek: 'This week', lastWeek: 'Last week'}
*/
timeRange: PropTypes.oneOfType([
CardRangePickerPropTypes.timeRange,
DateTimePickerDefaultValuePropTypes,
]),
timeRangeOptions: TimeRangeOptionsPropTypes, // eslint-disable-line react/require-default-props
i18n: PropTypes.shape({
last24Hours: PropTypes.string,
last7Days: PropTypes.string,
lastMonth: PropTypes.string,
lastQuarter: PropTypes.string,
lastYear: PropTypes.string,
thisWeek: PropTypes.string,
thisMonth: PropTypes.string,
thisQuarter: PropTypes.string,
thisYear: PropTypes.string,
overflowMenuDescription: PropTypes.string,
cloneCardLabel: PropTypes.string,
deleteCardLabel: PropTypes.string,
closeLabel: PropTypes.string,
expandLabel: PropTypes.string,
settingsLabel: PropTypes.string,
extraActionLabel: PropTypes.string,
}),
testId: PropTypes.string,
locale: PropTypes.string,
dateTimeMask: PropTypes.string,
/** If set to true it will render outside of the current DOM in a portal, otherwise render as a child */
renderDateDropdownInPortal: PropTypes.bool,
};
const defaultProps = {
isEditable: false,
isExpanded: false,
renderExpandIcon: Popup16,
className: null,
locale: 'en',
timeRangeOptions: null,
timeRange: null,
i18n: {
last24Hours: 'Last 24 hours',
last7Days: 'Last 7 days',
lastMonth: 'Last month',
lastQuarter: 'Last quarter',
lastYear: 'Last year',
thisWeek: 'This week',
thisMonth: 'This month',
thisQuarter: 'This quarter',
thisYear: 'This year',
overflowMenuDescription: 'Card actions menu',
cloneCardLabel: 'Clone card',
deleteCardLabel: 'Delete card',
closeLabel: 'Close',
expandLabel: 'Expand',
settingsLabel: 'Settings',
extraActionLabel: 'Action Label',
},
testId: 'card-toolbar',
dateTimeMask: 'YYYY-MM-DD HH:mm',
renderDateDropdownInPortal: true,
};
const CardToolbar = ({
i18n,
width,
isEditable,
isExpanded,
renderExpandIcon,
availableActions,
timeRange,
timeRangeOptions: timeRangeOptionsProp,
onCardAction,
className,
testId,
locale,
dateTimeMask,
extraActions,
renderDateDropdownInPortal,
}) => {
const mergedI18n = { ...defaultProps.i18n, ...i18n };
const langDir = useLangDirection();
const overflowMenuPosition = React.useMemo(() => langDir === 'ltr', [langDir]);
// maps the timebox internal label to a translated string
// Need the default here in case that the CardToolbar is used by multiple different components
// Also needs to reassign itself if i18n changes
const timeRangeOptions = useMemo(
() =>
timeRangeOptionsProp ||
(typeof availableActions?.range === 'string' // if we're using date time picker default to those options
? keyBy(PRESET_VALUES, 'id')
: {
last24Hours: mergedI18n.last24HoursLabel,
last7Days: mergedI18n.last7DaysLabel,
lastMonth: mergedI18n.lastMonthLabel,
lastQuarter: mergedI18n.lastQuarterLabel,
lastYear: mergedI18n.lastYearLabel,
thisWeek: mergedI18n.thisWeekLabel,
thisMonth: mergedI18n.thisMonthLabel,
thisQuarter: mergedI18n.thisQuarterLabel,
thisYear: mergedI18n.thisYearLabel,
}),
[
availableActions,
mergedI18n.last24HoursLabel,
mergedI18n.last7DaysLabel,
mergedI18n.lastMonthLabel,
mergedI18n.lastQuarterLabel,
mergedI18n.lastYearLabel,
mergedI18n.thisMonthLabel,
mergedI18n.thisQuarterLabel,
mergedI18n.thisWeekLabel,
mergedI18n.thisYearLabel,
timeRangeOptionsProp,
]
);
const handleDateTimePickerChange = useCallback(
(selectedValue) => {
onCardAction('CHANGE_TIME_RANGE', selectedValue);
},
[onCardAction]
);
const extraActionSlot =
extraActions && Object.keys(extraActions).length !== 0 ? (
extraActions.children && extraActions.children.length ? (
<OverflowMenu
flipped={overflowMenuPosition}
title={extraActions.iconDescription || mergedI18n.overflowMenuDescription}
iconDescription={extraActions.iconDescription || mergedI18n.overflowMenuDescription}
{...(extraActions.icon ? { renderIcon: extraActions.icon } : {})}
>
{extraActions.children.map((child, i) =>
!child.hidden ? (
<OverflowMenuItem
data-testid={`${testId}-extra-overflow-menu-item-${i}`}
key={`${child.id}-${i}`}
itemText={child.itemText}
disabled={child.disabled}
onClick={() => (child.callback ? child.callback(child) : null)}
requireTitle
/>
) : null
)}
</OverflowMenu>
) : extraActions.icon ? (
<ToolbarSVGWrapper
title={extraActions.iconDescription || mergedI18n.extraActionLabel}
onClick={() => (extraActions.callback ? extraActions.callback(extraActions) : null)}
renderIcon={extraActions.icon}
testId={`${testId}-extra-single-action`}
disabled={extraActions.disabled}
/>
) : extraActions.content ? (
<div
className={`${iotPrefix}--card--toolbar-action--custom-actions`}
data-testid={`${testId}-extra-actions`}
>
{extraActions.content}
</div>
) : null
) : null;
return isEditable ? (
<div data-testid={testId} className={classnames(className, `${iotPrefix}--card--toolbar`)}>
{(availableActions.clone || availableActions.delete) && (
<OverflowMenu
flipped={overflowMenuPosition}
title={mergedI18n.overflowMenuDescription}
iconDescription={mergedI18n.overflowMenuDescription}
>
{availableActions.clone && (
<OverflowMenuItem
onClick={() => {
onCardAction(CARD_ACTIONS.CLONE_CARD);
}}
itemText={mergedI18n.cloneCardLabel}
title={mergedI18n.cloneCardLabel}
requireTitle
data-testid={`${testId}-clone-button`}
/>
)}
{availableActions.delete && (
<OverflowMenuItem
isDelete
onClick={() => {
onCardAction(CARD_ACTIONS.DELETE_CARD);
}}
itemText={mergedI18n.deleteCardLabel}
title={mergedI18n.deleteCardLabel}
requireTitle
data-testid={`${testId}-delete-button`}
/>
)}
</OverflowMenu>
)}
</div>
) : (
<div data-testid={testId} className={classnames(className, `${iotPrefix}--card--toolbar`)}>
{availableActions.range ? (
typeof availableActions.range === 'boolean' ? ( // boolean is the old range picker
<CardRangePicker
width={width}
i18n={mergedI18n}
timeRange={timeRange}
timeRangeOptions={timeRangeOptions}
onCardAction={onCardAction}
cardWidth={width}
testId={`${testId}-range-picker`}
/>
) : (
// string values mean use the new picker
<DateTimePicker
id={testId}
i18n={mergedI18n}
dateTimeMask={dateTimeMask}
locale={locale}
hasIconOnly
presets={Object.entries(timeRangeOptions).reduce(
(acc, [timeRangeOptionKey, timeRangeOption]) => {
acc.push({
id: timeRangeOptionKey,
label: timeRangeOption.label || mergedI18n.timeRange,
offset: timeRangeOption.offset,
});
return acc;
},
[]
)}
defaultValue={timeRange}
onApply={handleDateTimePickerChange}
renderInPortal={renderDateDropdownInPortal}
/>
)
) : null}
{availableActions.settings ? (
<ToolbarSVGWrapper
title={mergedI18n.settingsLabel}
onClick={() => onCardAction(CARD_ACTIONS.ON_SETTINGS_CLICK)}
iconDescription={mergedI18n.settingsLabel}
renderIcon={Settings16}
testId={`${testId}-settings-button`}
/>
) : null}
{availableActions.expand ? (
<>
{isExpanded ? (
<>
{availableActions.extra ? extraActionSlot : null}
<ToolbarSVGWrapper
title={mergedI18n.closeLabel}
onClick={() => onCardAction(CARD_ACTIONS.CLOSE_EXPANDED_CARD)}
iconDescription={mergedI18n.closeLabel}
renderIcon={Close16}
testId={`${testId}-close-button`}
/>
</>
) : (
<ToolbarSVGWrapper
title={mergedI18n.expandLabel}
onClick={() => {
onCardAction(CARD_ACTIONS.OPEN_EXPANDED_CARD);
}}
iconDescription={mergedI18n.expandLabel}
renderIcon={renderExpandIcon}
testId={`${testId}-expand-button`}
/>
)}
</>
) : null}
{!isExpanded && availableActions.extra ? extraActionSlot : null}
</div>
);
};
CardToolbar.propTypes = propTypes;
CardToolbar.defaultProps = defaultProps;
export default CardToolbar;