Skip to content

Commit 2878f2c

Browse files
committed
feat: DynamicDataTable - update support for button type cell wrapper component
1 parent 853f164 commit 2878f2c

File tree

2 files changed

+25
-37
lines changed

2 files changed

+25
-37
lines changed

src/Shared/Components/DynamicDataTable/DynamicDataTableRow.tsx

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createRef, Fragment, RefObject, useEffect, useRef } from 'react'
1+
import { createElement, createRef, Fragment, ReactElement, RefObject, useEffect, useRef } from 'react'
22
// eslint-disable-next-line import/no-extraneous-dependencies
33
import { followCursor } from 'tippy.js'
44

@@ -19,12 +19,15 @@ import { getActionButtonPosition, getRowGridTemplateColumn, rowTypeHasInputField
1919
import { DynamicDataTableRowType, DynamicDataTableRowProps, DynamicDataTableRowDataType } from './types'
2020

2121
const conditionalWrap =
22-
(
23-
renderer: (props: { customState?: Record<string, any>; children: JSX.Element }) => JSX.Element,
24-
customState: Record<string, any>,
25-
) =>
26-
(children: JSX.Element) =>
27-
renderer({ children, customState })
22+
<K extends string>(renderer: (row: DynamicDataTableRowType<K>) => ReactElement, row: DynamicDataTableRowType<K>) =>
23+
(children: JSX.Element) => {
24+
if (renderer) {
25+
const { props, type } = renderer(row)
26+
return createElement(type, props, children)
27+
}
28+
29+
return null
30+
}
2831

2932
export const DynamicDataTableRow = <K extends string>({
3033
rows = [],
@@ -40,6 +43,7 @@ export const DynamicDataTableRow = <K extends string>({
4043
onRowDelete,
4144
leadingCellIcon,
4245
trailingCellIcon,
46+
buttonCellWrapComponent,
4347
}: DynamicDataTableRowProps<K>) => {
4448
// CONSTANTS
4549
const isFirstRowEmpty = headers.every(({ key }) => !rows[0]?.data[key].value)
@@ -151,18 +155,16 @@ export const DynamicDataTableRow = <K extends string>({
151155
return (
152156
<div className="w-100 h-100 flex top">
153157
<ConditionalWrap
154-
condition={row.data[key].wrap}
155-
wrap={conditionalWrap(row.data[key].wrapRenderer, row.customState)}
158+
condition={!!buttonCellWrapComponent}
159+
wrap={conditionalWrap(buttonCellWrapComponent, row)}
156160
>
157-
<div>
158-
<button
159-
type="button"
160-
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' : ''}`}
161-
>
162-
{row.data[key].props?.icon || null}
163-
{row.data[key].props.text}
164-
</button>
165-
</div>
161+
<button
162+
type="button"
163+
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' : ''}`}
164+
>
165+
{row.data[key].props?.icon || null}
166+
{row.data[key].props.text}
167+
</button>
166168
</ConditionalWrap>
167169
</div>
168170
)

src/Shared/Components/DynamicDataTable/types.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { DetailedHTMLProps, ReactNode } from 'react'
17+
import { DetailedHTMLProps, ReactElement, ReactNode } from 'react'
1818

1919
import { SortingOrder } from '@Common/Constants'
2020

@@ -81,25 +81,8 @@ export type DynamicDataTableCellPropsMap = {
8181
}
8282
}
8383

84-
type DynamicDataTableCellExtendedPropsMap = {
85-
[DynamicDataTableRowDataType.TEXT]: {}
86-
[DynamicDataTableRowDataType.DROPDOWN]: {}
87-
[DynamicDataTableRowDataType.SELECT_TEXT]: {}
88-
[DynamicDataTableRowDataType.BUTTON]:
89-
| {
90-
wrap: true
91-
wrapRenderer: (props: { children: JSX.Element; customState?: Record<string, any> }) => JSX.Element
92-
}
93-
| {
94-
wrap?: false
95-
wrapRenderer?: never
96-
}
97-
}
98-
9984
type DynamicDataTableCellData<T extends keyof DynamicDataTableCellPropsMap = keyof DynamicDataTableCellPropsMap> =
100-
T extends keyof DynamicDataTableCellPropsMap
101-
? { type: T; props: DynamicDataTableCellPropsMap[T] } & DynamicDataTableCellExtendedPropsMap[T]
102-
: never
85+
T extends keyof DynamicDataTableCellPropsMap ? { type: T; props: DynamicDataTableCellPropsMap[T] } : never
10386

10487
/**
10588
* Type representing a key-value row.
@@ -153,6 +136,8 @@ export type DynamicDataTableProps<K extends string> = {
153136
leadingCellIcon?: DynamicDataTableCellIcon<K>
154137
/** Optional configuration for displaying an icon in the trailing position of a cell. */
155138
trailingCellIcon?: DynamicDataTableCellIcon<K>
139+
/** An optional function to render a custom wrapper component for the type `DynamicDataTableRowDataType.BUTTON`. */
140+
buttonCellWrapComponent?: (row: DynamicDataTableRowType<K>) => ReactElement
156141
/** An optional React node for a custom header component. */
157142
headerComponent?: ReactNode
158143
/** When true, data addition field will not be shown. */
@@ -257,4 +242,5 @@ export interface DynamicDataTableRowProps<K extends string>
257242
| 'validationSchema'
258243
| 'leadingCellIcon'
259244
| 'trailingCellIcon'
245+
| 'buttonCellWrapComponent'
260246
> {}

0 commit comments

Comments
 (0)