Skip to content

Commit 6893f40

Browse files
authored
chore: removes duplicative loop over column state to determine linked cells (#7958)
1 parent 2a8bd4c commit 6893f40

File tree

3 files changed

+16
-33
lines changed

3 files changed

+16
-33
lines changed

packages/ui/src/elements/Table/index.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,26 @@ export const Table: React.FC<Props> = ({ columns: columnsFromProps, customCellCo
6060
data.map((row, rowIndex) => (
6161
<tr className={`row-${rowIndex + 1}`} key={rowIndex}>
6262
{activeColumns.map((col, colIndex) => {
63+
const isLink =
64+
(colIndex === 0 && col.accessor !== '_select') ||
65+
(colIndex === 1 && activeColumns[0]?.accessor === '_select')
66+
67+
const cellProps = {
68+
link: isLink,
69+
...(col.cellProps || {}),
70+
}
71+
6372
return (
6473
<td className={`cell-${col.accessor}`} key={colIndex}>
6574
<TableCellProvider
6675
cellData={row[col.accessor]}
67-
cellProps={col?.cellProps}
76+
cellProps={cellProps}
6877
columnIndex={colIndex}
6978
customCellContext={customCellContext}
7079
rowData={row}
7180
>
7281
<RenderComponent
73-
clientProps={{ ...col?.cellProps }}
82+
clientProps={cellProps}
7483
mappedComponent={col.components.Cell}
7584
/>
7685
</TableCellProvider>

packages/ui/src/elements/TableColumns/buildColumnState.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ export const buildColumnState = (args: Args): Column[] => {
9292
activeColumnsIndices.push(index)
9393
}
9494

95-
const isFirstActiveColumn = activeColumnsIndices[0] === index
96-
9795
const CustomLabelToRender =
9896
field &&
9997
'admin' in field &&
@@ -137,7 +135,6 @@ export const buildColumnState = (args: Args): Column[] => {
137135
...(cellProps?.[index]?.field || ({} as ClientField)),
138136
} as ClientField,
139137
...cellProps?.[index],
140-
link: isFirstActiveColumn,
141138
},
142139
components: {
143140
Cell: field.admin?.components?.Cell || {

packages/ui/src/elements/TableColumns/index.tsx

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -87,26 +87,6 @@ export const TableColumnsProvider: React.FC<Props> = ({
8787
[preferenceKey, setPreference],
8888
)
8989

90-
const reassignLinkColumn = (columns: Column[]): Column[] => {
91-
let foundFirstActive = false
92-
const newColumns = columns.map((col) => {
93-
const linkColumn = col.active && !foundFirstActive && col.accessor !== '_select'
94-
if (linkColumn) {
95-
foundFirstActive = true
96-
}
97-
98-
return {
99-
...col,
100-
cellProps: {
101-
...col.cellProps,
102-
link: linkColumn,
103-
},
104-
}
105-
})
106-
107-
return newColumns
108-
}
109-
11090
const moveColumn = useCallback(
11191
(args: { fromIndex: number; toIndex: number }) => {
11292
const { fromIndex, toIndex } = args
@@ -115,9 +95,8 @@ export const TableColumnsProvider: React.FC<Props> = ({
11595
const [columnToMove] = withMovedColumn.splice(fromIndex, 1)
11696
withMovedColumn.splice(toIndex, 0, columnToMove)
11797

118-
const newColumns = reassignLinkColumn(withMovedColumn)
119-
setTableColumns(newColumns)
120-
updateColumnPreferences(newColumns)
98+
setTableColumns(withMovedColumn)
99+
updateColumnPreferences(withMovedColumn)
121100
},
122101
[tableColumns, updateColumnPreferences],
123102
)
@@ -136,9 +115,8 @@ export const TableColumnsProvider: React.FC<Props> = ({
136115
}
137116
})
138117

139-
const newColumns = reassignLinkColumn(toggledColumns)
140-
setTableColumns(newColumns)
141-
updateColumnPreferences(newColumns)
118+
setTableColumns(toggledColumns)
119+
updateColumnPreferences(toggledColumns)
142120
},
143121
[tableColumns, updateColumnPreferences],
144122
)
@@ -152,8 +130,7 @@ export const TableColumnsProvider: React.FC<Props> = ({
152130
}
153131
})
154132

155-
const newColumns = reassignLinkColumn(activeColumns)
156-
updateColumnPreferences(newColumns)
133+
updateColumnPreferences(activeColumns)
157134
},
158135
[tableColumns, updateColumnPreferences],
159136
)

0 commit comments

Comments
 (0)