-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDynamicDataTableRow.tsx
More file actions
395 lines (363 loc) · 16.1 KB
/
DynamicDataTableRow.tsx
File metadata and controls
395 lines (363 loc) · 16.1 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
* Copyright (c) 2024. Devtron Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
createElement,
createRef,
Fragment,
ReactElement,
RefObject,
useEffect,
useMemo,
useRef,
useState,
} from 'react'
// eslint-disable-next-line import/no-extraneous-dependencies
import { followCursor } from 'tippy.js'
import { ReactComponent as ICClose } from '@Icons/ic-close.svg'
import { ResizableTagTextArea } from '@Common/CustomTagSelector'
import { ConditionalWrap } from '@Common/Helper'
import { Tooltip } from '@Common/Tooltip'
import { ComponentSizeType } from '@Shared/constants'
import { Button, ButtonStyleType, ButtonVariantType } from '../Button'
import { FileUpload } from '../FileUpload'
import {
getSelectPickerOptionByValue,
SelectPicker,
SelectPickerOptionType,
SelectPickerTextArea,
SelectPickerVariantType,
} from '../SelectPicker'
import { DynamicDataTableRowDataType, DynamicDataTableRowProps, DynamicDataTableRowType } from './types'
import { getActionButtonPosition, getRowGridTemplateColumn, rowTypeHasInputField } from './utils'
const getWrapperForButtonCell =
<K extends string, CustomStateType = Record<string, unknown>>(
renderer: (row: DynamicDataTableRowType<K, CustomStateType>) => ReactElement,
row: DynamicDataTableRowType<K, CustomStateType>,
) =>
(children: JSX.Element) => {
if (renderer) {
const { props, type } = renderer(row)
return createElement(type, props, children)
}
return null
}
export const DynamicDataTableRow = <K extends string, CustomStateType = Record<string, unknown>>({
rows = [],
headers,
readOnly,
isDeletionNotAllowed,
cellError = {},
actionButtonConfig = null,
onRowEdit,
onRowDelete,
leadingCellIcon,
trailingCellIcon,
buttonCellWrapComponent,
focusableFieldKey,
}: DynamicDataTableRowProps<K, CustomStateType>) => {
// CONSTANTS
const isFirstRowEmpty = headers.every(({ key }) => !rows[0]?.data[key].value)
/** Boolean determining if table has rows. */
const hasRows = !!rows.length
const disableDeleteRow = rows.length === 1 && isFirstRowEmpty
/** style: grid-template-columns */
const rowGridTemplateColumn = getRowGridTemplateColumn(
headers,
actionButtonConfig,
isDeletionNotAllowed || readOnly,
)
// STATES
const [isRowAdded, setIsRowAdded] = useState(false)
// CELL REFS
const cellRef = useRef<Record<string | number, Record<K, RefObject<HTMLTextAreaElement>>>>()
if (!cellRef.current) {
cellRef.current = rows.reduce(
(acc, curr) => ({
...acc,
[curr.id]: headers.reduce((headerAcc, { key }) => ({ ...headerAcc, [key]: createRef() }), {}),
}),
{},
)
}
const rowIds = useMemo(() => rows.map(({ id }) => id), [rows])
useEffect(() => {
setIsRowAdded(rows.length > 0 && Object.keys(cellRef.current).length < rows.length)
const updatedCellRef = rowIds.reduce((acc, curr) => {
if (cellRef.current[curr]) {
acc[curr] = cellRef.current[curr]
} else {
acc[curr] = headers.reduce((headerAcc, { key }) => ({ ...headerAcc, [key]: createRef() }), {})
}
return acc
}, {})
cellRef.current = updatedCellRef
}, [JSON.stringify(rowIds)])
useEffect(() => {
if (isRowAdded) {
// Using the below logic to ensure the cell is focused after row addition.
const cell = cellRef.current[rows[0].id][focusableFieldKey || headers[0].key].current
if (cell) {
cell.focus()
setIsRowAdded(false)
}
}
}, [isRowAdded])
// METHODS
const onChange =
(row: DynamicDataTableRowType<K, CustomStateType>, key: K) =>
(e: React.ChangeEvent<HTMLTextAreaElement> | SelectPickerOptionType<string> | File[]) => {
let value = ''
const extraData = { selectedValue: null, files: [] }
switch (row.data[key].type) {
case DynamicDataTableRowDataType.DROPDOWN:
case DynamicDataTableRowDataType.SELECT_TEXT:
value = (e as SelectPickerOptionType<string>)?.value || ''
extraData.selectedValue = e
break
case DynamicDataTableRowDataType.FILE_UPLOAD:
value = (e as File[])[0]?.name || ''
extraData.files = e as File[]
break
case DynamicDataTableRowDataType.TEXT:
default:
value = (e as React.ChangeEvent<HTMLTextAreaElement>).target.value
break
}
onRowEdit(row, key, value, extraData)
}
const onDelete = (row: DynamicDataTableRowType<K, CustomStateType>) => () => {
onRowDelete(row)
}
// RENDERERS
const renderCellContent = (row: DynamicDataTableRowType<K, CustomStateType>, key: K) => {
const isDisabled = readOnly || row.data[key].disabled
switch (row.data[key].type) {
case DynamicDataTableRowDataType.DROPDOWN:
return (
<div className="w-100 h-100 flex top dc__align-self-start">
<SelectPicker<string, false>
{...row.data[key].props}
inputId={`data-table-${row.id}-${key}-cell`}
classNamePrefix="dynamic-data-table__cell__select-picker"
variant={SelectPickerVariantType.COMPACT}
value={getSelectPickerOptionByValue(
row.data[key].props?.options,
row.data[key].value,
null,
)}
onChange={onChange(row, key)}
isDisabled={isDisabled}
fullWidth
/>
</div>
)
case DynamicDataTableRowDataType.SELECT_TEXT: {
const { value, props } = row.data[key]
const { isCreatable = true } = props
return (
<div className="w-100 h-100 flex top dc__align-self-start">
<SelectPickerTextArea
isCreatable={isCreatable}
isClearable
{...props}
variant={SelectPickerVariantType.COMPACT}
classNamePrefix="dynamic-data-table__cell__select-picker-text-area"
inputId={`data-table-${row.id}-${key}-cell`}
minHeight={20}
maxHeight={160}
value={getSelectPickerOptionByValue(
props?.options,
value,
isCreatable && value ? { label: value, value } : null,
)}
onChange={onChange(row, key)}
isDisabled={isDisabled}
formatCreateLabel={(input) => `Use ${input}`}
refVar={cellRef?.current?.[row.id]?.[key]}
dependentRefs={cellRef?.current?.[row.id]}
fullWidth
/>
</div>
)
}
case DynamicDataTableRowDataType.BUTTON:
return (
<div className="w-100 h-100 flex top">
<ConditionalWrap
condition={!!buttonCellWrapComponent}
wrap={getWrapperForButtonCell(buttonCellWrapComponent, row)}
>
<button
type="button"
className={`dc__transparent w-100 p-8 flex left dc__gap-8 dc__hover-n50 cn-9 fs-13 lh-20 ${row.data[key].disabled ? 'dc__disabled' : ''}`}
>
{row.data[key].props?.icon || null}
{row.data[key].props.text}
</button>
</ConditionalWrap>
</div>
)
case DynamicDataTableRowDataType.FILE_UPLOAD:
return (
<div
className={`mw-none w-100 h-100 flex top left px-8 ${row.data[key].props?.isLoading || row.data[key].value ? 'py-3' : 'py-8'}`}
>
<FileUpload
{...row.data[key].props}
fileName={row.data[key].value}
onUpload={onChange(row, key)}
/>
</div>
)
default:
return (
<ResizableTagTextArea
{...row.data[key].props}
id={`data-table-${row.id}-${key}-cell`}
className={`dynamic-data-table__cell-input placeholder-cn5 p-8 cn-9 fs-13 lh-20 dc__align-self-start dc__no-border-radius ${isDisabled ? 'cursor-not-allowed' : ''}`}
minHeight={20}
maxHeight={160}
value={row.data[key].value}
onChange={onChange(row, key)}
disabled={isDisabled}
refVar={cellRef?.current?.[row.id]?.[key]}
dependentRefs={cellRef?.current?.[row.id]}
disableOnBlurResizeToMinHeight
/>
)
}
}
const renderAsterisk = (row: DynamicDataTableRowType<K, CustomStateType>, key: K) =>
row.data[key].required && <span className="mt-10 px-6 w-20 cr-5 fs-16 lh-20 dc__align-self-start">*</span>
const renderCellIcon = (row: DynamicDataTableRowType<K, CustomStateType>, key: K, isLeadingIcon?: boolean) => {
const iconConfig = isLeadingIcon ? leadingCellIcon : trailingCellIcon
if (!iconConfig?.[key]?.(row)) {
return null
}
return (
<div
className={`flex h-100 dc__align-self-start ${row.data[key].type !== DynamicDataTableRowDataType.TEXT ? `py-8 ${isLeadingIcon ? 'pl-8' : 'pr-8'}` : ''}`}
>
{iconConfig[key](row)}
</div>
)
}
const renderErrorMessage = (errorMessage: string) => (
<div key={errorMessage} className="flexbox align-items-center dc__gap-4">
<ICClose className="icon-dim-16 fcr-5 dc__align-self-start dc__no-shrink" />
<p className="fs-12 lh-16 cn-7 m-0">{errorMessage}</p>
</div>
)
const renderErrorMessages = (row: DynamicDataTableRowType<K, CustomStateType>, key: K) => {
const { isValid, errorMessages } =
!row.data[key].disabled && cellError[row.id]?.[key]
? cellError[row.id][key]
: { isValid: true, errorMessages: [] }
const isDropdown =
row.data[key].type === DynamicDataTableRowDataType.SELECT_TEXT ||
row.data[key].type === DynamicDataTableRowDataType.DROPDOWN
if (isValid) {
return null
}
// Adding 'no-error' class to hide error when SelectPicker or SelectPickerTextArea is focused.
return (
<div
className={`dynamic-data-table__error bg__primary dc__border br-4 py-7 px-8 flexbox-col dc__gap-4 ${isDropdown ? 'no-error' : ''}`}
>
{errorMessages.map((error) => renderErrorMessage(error))}
</div>
)
}
const renderCell = (row: DynamicDataTableRowType<K, CustomStateType>, key: K, index: number) => {
const isDisabled = readOnly || row.data[key].disabled || false
const hasError = !(cellError[row.id]?.[key]?.isValid ?? true)
const cellNode = (
<Tooltip
alwaysShowTippyOnHover={!!row.data[key].tooltip?.content || isDisabled}
className={row.data[key].tooltip?.className}
content={row.data[key].tooltip?.content || (isDisabled ? 'Cannot edit in read-only mode' : '')}
followCursor="horizontal"
plugins={[followCursor]}
>
<div
className={`dynamic-data-table__cell bg__primary flexbox dc__align-items-center dc__gap-4 dc__position-rel ${isDisabled ? 'cursor-not-allowed no-hover' : ''} ${!isDisabled && hasError ? 'dynamic-data-table__cell--error no-hover' : ''} ${!rowTypeHasInputField(row.data[key].type) ? 'no-hover no-focus' : ''}`}
>
{renderCellIcon(row, key, true)}
{renderCellContent(row, key)}
{renderAsterisk(row, key)}
{renderCellIcon(row, key)}
{renderErrorMessages(row, key)}
</div>
</Tooltip>
)
const actionButtonIndex = getActionButtonPosition({ headers, actionButtonConfig })
if (actionButtonIndex === index) {
const { renderer, position = 'start' } = actionButtonConfig
const actionButtonNode = (
<div
className={`dc__overflow-hidden flex top bg__primary ${(position === 'start' && key === headers[0].key) || (isDeletionNotAllowed && position === 'end' && key === headers[headers.length - 1].key) ? 'dynamic-data-table__cell' : ''}`}
>
{renderer(row)}
</div>
)
return position === 'start' ? (
<>
{actionButtonNode}
{cellNode}
</>
) : (
<>
{cellNode}
{actionButtonNode}
</>
)
}
return cellNode
}
return hasRows ? (
<div className="bcn-2 px-1 pb-1 dc__bottom-radius-4">
<div
className={`dynamic-data-table w-100 bcn-1 dc__bottom-radius-4 ${!readOnly ? 'row-column' : 'header-column'}`}
style={{
gridTemplateColumns: rowGridTemplateColumn,
}}
>
{rows.map((row) => (
<div key={row.id} className="dynamic-data-table__row">
{headers.map(({ key }, index) => (
<Fragment key={key}>{renderCell(row, key, index)}</Fragment>
))}
{!isDeletionNotAllowed && !readOnly && (
<div className="dynamic-data-table__row-delete-btn bg__primary">
<Button
dataTestId="dynamic-data-table-row-delete-btn"
ariaLabel="Delete Row"
showAriaLabelInTippy={false}
icon={<ICClose />}
disabled={disableDeleteRow || row.disableDelete}
onClick={onDelete(row)}
variant={ButtonVariantType.borderLess}
style={ButtonStyleType.negativeGrey}
size={ComponentSizeType.small}
/>
</div>
)}
</div>
))}
</div>
</div>
) : null
}