-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathTableManageViewsModal.jsx
255 lines (243 loc) · 9.03 KB
/
TableManageViewsModal.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
import React, { useState } from 'react';
import PropTypes from 'prop-types';
import { Checkbox, Modal, Search } from '@carbon/react';
import ComposedModal from '../../ComposedModal/ComposedModal';
import { settings } from '../../../constants/Settings';
import { OverridePropTypes } from '../../../constants/SharedPropTypes';
import { SimplePaginationPropTypes } from '../../SimplePagination/SimplePagination';
import deprecate from '../../../internal/deprecate';
import useMerged from '../../../hooks/useMerged';
import TableManageViewsList from './TableManageViewsList';
import { ViewsPropType } from './SharedTableManageViewsModalPropTypes';
const { iotPrefix } = settings;
const propTypes = {
/**
* Callbacks for the actions of the modal
* onEdit : Called when the user clicks edit on a view row. The ID of the view is passed as argument.
* onDelete : Called when the user clicks delete on a view row. The ID of the view is passed as argument.
* onClearError : Called when the dialog error msg is cleared
* onSearchChange : Called when the search input value is changed, the search term is passed as argument.
* onDisplayPublicChange : Called when view title input value is changed, a bool value is passed as argument.
*/
actions: PropTypes.shape({
onClose: PropTypes.func,
onEdit: PropTypes.func,
onDelete: PropTypes.func,
onClearError: PropTypes.func,
onSearchChange: PropTypes.func.isRequired,
onDisplayPublicChange: PropTypes.func.isRequired,
}).isRequired,
/** The ID of the view that should be marked as default if any */
defaultViewId: PropTypes.string,
/** True means that the 'display public' checkbox is checked by default */
displayPublicDefaultChecked: PropTypes.bool,
/** Shows this string as a general modal error when present */
error: PropTypes.string,
/** Internationalization strings object */
i18n: PropTypes.shape({
closeIconDescription: PropTypes.string,
defaultLabelText: PropTypes.string,
deleteIconText: PropTypes.string,
deleteWarningTextTemplate: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
deleteWarningCancel: PropTypes.string,
deleteWarningConfirm: PropTypes.string,
editIconText: PropTypes.string,
listTitle: PropTypes.string,
modalTitle: PropTypes.string,
privateLabelText: PropTypes.string,
publicLabelText: PropTypes.string,
publicCheckboxLabelText: PropTypes.string,
searchPlaceholderText: PropTypes.string,
searchClearButtonLabelText: PropTypes.string,
searchIconLabelText: PropTypes.string,
}),
/** Shows loading skeleton and disables pagination when true */
isLoading: PropTypes.bool,
/** The default value of the search field value */
searchValueDefault: PropTypes.string,
/** Determines if the modal is open or closed (i.e. visible or not to the user) */
open: PropTypes.bool.isRequired,
/** Used to override the internal components and props for advanced customization */
overrides: PropTypes.shape({
mainModal: OverridePropTypes,
publicCheckbox: OverridePropTypes,
search: OverridePropTypes,
tableManageViewsList: OverridePropTypes,
warningModal: OverridePropTypes,
}),
/** pagination at the bottom of list, see SimplePaginationPropTypes */
pagination: PropTypes.shape(SimplePaginationPropTypes),
// TODO: remove deprecated 'testID' in v3
// eslint-disable-next-line react/require-default-props
testID: deprecate(
PropTypes.string,
`The 'testID' prop has been deprecated. Please use 'testId' instead.`
),
/** Id that can be used for testing */
testId: PropTypes.string,
/** The views to be displayed in the list, see ViewsPropType */
views: ViewsPropType.isRequired,
};
const defaultProps = {
defaultViewId: undefined,
displayPublicDefaultChecked: true,
error: undefined,
i18n: {
closeIconDescription: 'Close',
defaultLabelText: 'default',
deleteIconText: 'delete',
deleteWarningTextTemplate: (label) => `You are about to delete view ${label}.`,
deleteWarningCancel: 'Cancel',
deleteWarningConfirm: 'Delete',
editIconText: 'edit',
listTitle: 'Available Views',
modalTitle: 'Manage views',
privateLabelText: 'Private',
publicLabelText: 'Public',
publicCheckboxLabelText: 'Display public views',
searchPlaceholderText: 'Search views',
searchClearButtonLabelText: 'Clear search input',
searchIconLabelText: 'Search',
},
isLoading: false,
overrides: undefined,
searchValueDefault: '',
testId: 'TableManageViewsModal',
pagination: undefined,
};
const defaultFunc = () => {};
const TableManageViewsModal = ({
actions: {
onEdit = defaultFunc,
onDelete = defaultFunc,
onClearError = defaultFunc,
onClose,
onSearchChange,
onDisplayPublicChange,
},
defaultViewId,
displayPublicDefaultChecked,
error,
i18n,
isLoading,
open,
overrides,
pagination,
searchValueDefault,
// TODO: remove deprecated 'testID' in v3.
testID,
testId,
views,
}) => {
const {
closeIconDescription,
deleteWarningTextTemplate,
deleteWarningCancel,
deleteWarningConfirm,
modalTitle,
publicCheckboxLabelText,
searchPlaceholderText,
searchClearButtonLabelText,
searchIconLabelText,
...mergedI18n
} = useMerged(defaultProps.i18n, i18n);
const primaryInputId = 'manage-views-modal-search';
const MyMainModal = overrides?.mainModal?.component || ComposedModal;
const MySearch = overrides?.search?.component || Search;
const MyPublicCheckbox = overrides?.publicCheckbox?.component || Checkbox;
const MyTableManageViewsList = overrides?.tableManageViewsList?.component || TableManageViewsList;
const MyWarningModal = overrides?.warningModal?.component || Modal;
const [showDeleteWarning, setShowDeleteWarning] = useState(false);
const [viewIdToDelete, setViewIdToDelete] = useState(null);
const getDeleteWarningText = () => {
const viewTitle = views.find((view) => view.id === viewIdToDelete).title;
return typeof deleteWarningTextTemplate === 'function'
? deleteWarningTextTemplate(viewTitle)
: deleteWarningTextTemplate.replace('{0}', viewTitle);
};
const onShowWarning = (id) => {
setViewIdToDelete(id);
setShowDeleteWarning(true);
};
return (
<>
<MyMainModal
className={`${iotPrefix}--manage-views-modal`}
// TODO: remove deprecated 'testID' in v3.
testID={testID || testId}
error={error}
header={{
title: modalTitle,
}}
iconDescription={closeIconDescription}
onClearError={onClearError}
onClose={onClose}
open={open}
passiveModal
selectorPrimaryFocus={`#${primaryInputId}`}
{...overrides?.mainModal?.props}
>
<div className={`${iotPrefix}--manage-views-modal__filter-container`}>
<MySearch
className={`${iotPrefix}--manage-views-modal__search`}
defaultValue={searchValueDefault}
closeButtonLabelText={searchClearButtonLabelText}
id={primaryInputId}
name="searchValue"
labelText={searchIconLabelText}
onChange={(evt) => onSearchChange(evt?.target?.value)}
placeholder={searchPlaceholderText}
size="lg"
type="text"
{...overrides?.search?.props}
/>
<MyPublicCheckbox
name="isPublic"
// TODO: remove deprecated 'testID' in v3.
data-testid={`${testID || testId}-public-checkbox`}
defaultChecked={displayPublicDefaultChecked}
id="manage-views-modal-public-checkbox-label"
labelText={publicCheckboxLabelText}
wrapperClassName={`${iotPrefix}--manage-views-modal__public-checkbox`}
onChange={(event, { checked: isPublic }) => onDisplayPublicChange(isPublic)}
{...overrides?.publicCheckbox?.props}
/>
</div>
<MyTableManageViewsList
defaultViewId={defaultViewId}
i18n={mergedI18n}
isLoading={isLoading}
onEdit={onEdit}
onDelete={onShowWarning}
pagination={pagination}
// TODO: remove deprecated 'testID' in v3.
testID={`${testID || testId}-views-list`}
views={views}
{...overrides?.tableManageViewsList?.props}
/>
</MyMainModal>
{showDeleteWarning && (
<MyWarningModal
className={`${iotPrefix}--manage-views-modal-warning`}
closeButtonLabel={closeIconDescription}
modalHeading={getDeleteWarningText(viewIdToDelete, views)}
onRequestClose={() => {
setShowDeleteWarning(false);
}}
onRequestSubmit={() => {
onDelete(viewIdToDelete);
setShowDeleteWarning(false);
}}
open={showDeleteWarning}
primaryButtonText={deleteWarningConfirm}
secondaryButtonText={deleteWarningCancel}
size="sm"
{...overrides?.warningModal?.props}
/>
)}
</>
);
};
TableManageViewsModal.propTypes = propTypes;
TableManageViewsModal.defaultProps = defaultProps;
export default TableManageViewsModal;