Re: [PR] refactor(Table): Use Table instead of html in CollectionTable [superset]

Tue, 22 Apr 2025 06:09:57 -0700


korbit-ai[bot] commented on code in PR #33159:
URL: https://github.com/apache/superset/pull/33159#discussion_r2054083433


##########
superset-frontend/src/components/Datasource/CollectionTable.tsx:
##########
@@ -331,109 +276,114 @@ export default class CRUDCollection extends 
PureComponent<
     );
   }
 
-  getCellProps(record: any, col: any) {
-    const cellPropsFn = this.props.itemCellProps?.[col];
-    const val = record[col];
-    return cellPropsFn ? cellPropsFn(val, this.getLabel(col), record) : {};
-  }
-
-  renderCell(record: any, col: any) {
+  renderCell(record: any, col: any): ReactNode {
     const renderer = this.props.itemRenderers?.[col];
     const val = record[col];
     const onChange = this.onCellChange.bind(this, record.id, col);
     return renderer ? renderer(val, onChange, this.getLabel(col), record) : 
val;
   }
 
-  renderItem(record: any) {
-    const { allowAddItem, allowDeletes, expandFieldset, tableColumns } =
-      this.props;
-    /* eslint-disable no-underscore-dangle */
-    const isExpanded =
-      !!this.state.expandedColumns[record.id] || record.__expanded;
-    let tds = [];
-    if (expandFieldset) {
-      tds.push(
-        <td key="__expand" className="expand">
-          <i
-            role="button"
-            aria-label="Toggle expand"
-            tabIndex={0}
-            // TODO: Remove fa-icon
-            // eslint-disable-next-line icons/no-fa-icons-usage
-            className={`fa fa-caret-${
-              isExpanded ? 'down' : 'right'
-            } text-primary pointer`}
-            onClick={this.toggleExpand.bind(this, record.id)}
-          />
-        </td>,
-      );
-    }
-    tds = tds.concat(
-      tableColumns.map(col => (
-        <td {...this.getCellProps(record, col)} key={col}>
-          {this.renderCell(record, col)}
-        </td>
-      )),
-    );
-    if (allowAddItem) {
-      tds.push(<td key="add" aria-label="Add" />);
-    }
+  buildTableColumns() {
+    const { tableColumns, allowDeletes, sortColumns = [] } = this.props;
+
+    const antdColumns = tableColumns.map(col => {
+      const label = this.getLabel(col);
+      const tooltip = this.getTooltip(col);
+      const isSortable = sortColumns.includes(col);
+      const currentSortOrder: SortOrder | null | undefined =
+        this.state.sortColumn === col
+          ? this.state.sort === 1
+            ? 'ascend'
+            : this.state.sort === 2
+              ? 'descend'
+              : null
+          : null;
+
+      return {
+        key: col,
+        dataIndex: col,
+        title: (
+          <>
+            {label}
+            {tooltip && (
+              <>
+                {' '}
+                <InfoTooltipWithTrigger
+                  label={t('description')}
+                  tooltip={tooltip}
+                  placement="top"
+                />
+              </>
+            )}
+          </>
+        ),
+        render: (text: any, record: CollectionItem) =>
+          this.renderCell(record, col),
+        onCell: (record: CollectionItem) => {
+          const cellPropsFn = this.props.itemCellProps?.[col];
+          const val = record[col];
+          return cellPropsFn ? cellPropsFn(val, label, record) : {};
+        },
+        sorter: isSortable,
+        sortOrder: currentSortOrder,
+      };
+    });
+
     if (allowDeletes) {
-      tds.push(
-        <td
-          key="__actions"
-          data-test="crud-delete-option"
-          className="text-primary"
-        >
-          <Icons.DeleteOutlined
-            aria-label="Delete item"
-            className="pointer"
-            data-test="crud-delete-icon"
-            role="button"
-            tabIndex={0}
-            onClick={this.deleteItem.bind(this, record.id)}
-            iconSize="l"
-          />
-        </td>,
-      );
-    }
-    const trs = [
-      <tr {...{ 'data-test': 'table-row' }} className="row" key={record.id}>
-        {tds}
-      </tr>,
-    ];
-    if (isExpanded) {
-      trs.push(
-        <tr className="exp" key={`exp__${record.id}`}>
-          <td
-            colSpan={this.effectiveTableColumns().length}
-            className="expanded"
+      antdColumns.push({
+        key: '__actions',
+        dataIndex: '__actions',
+        sorter: false,
+        title: <></>,
+        onCell: () => ({}),
+        sortOrder: null,
+        render: (_, record: CollectionItem) => (
+          <span
+            data-test="crud-delete-option"
+            className="text-primary"
+            css={{ display: 'flex', justifyContent: 'center' }}
           >
-            <div>{this.renderExpandableSection(record)}</div>
-          </td>
-        </tr>,
-      );
+            <Icons.DeleteOutlined
+              aria-label="Delete item"
+              className="pointer"
+              data-test="crud-delete-icon"
+              role="button"
+              tabIndex={0}
+              onClick={() => this.deleteItem(record.id)}
+              iconSize="l"
+            />
+          </span>
+        ),
+      });
     }
-    return trs;
+
+    return antdColumns as ColumnsType<CollectionItem>;
   }
 
-  renderEmptyCell() {
-    return (
-      <tr>
-        <td className="empty-collection">{this.props.emptyMessage}</td>
-      </tr>
+  render() {
+    const {
+      stickyHeader,
+      emptyMessage = t('No items'),
+      expandFieldset,
+    } = this.props;
+
+    const tableColumns = this.buildTableColumns();
+    const expandedRowKeys = Object.keys(this.state.expandedColumns).filter(
+      id => this.state.expandedColumns[id],
     );

Review Comment:
   ### Inefficient Expanded Rows Calculation <sub>![category 
Performance](https://img.shields.io/badge/Performance-4f46e5)</sub>
   
   <details>
     <summary>Tell me more</summary>
   
   ###### What is the issue?
   The expanded row keys are recalculated on every render by filtering all 
keys, which is inefficient for large collections.
   
   ###### Why this matters
   Filtering object keys on every render creates unnecessary computation 
overhead when the expanded state hasn't changed.
   
   ###### Suggested change ∙ *Feature Preview*
   Maintain a separate array of expanded keys in state and update it when 
toggling expansion:
   ```typescript
   state = {
     expandedKeys: [],
     // ... other state
   }
   
   toggleExpand(id: any) {
     this.setState(prevState => ({
       expandedKeys: prevState.expandedColumns[id] 
         ? prevState.expandedKeys.filter(k => k !== id)
         : [...prevState.expandedKeys, id],
       expandedColumns: {
         ...prevState.expandedColumns,
         [id]: !prevState.expandedColumns[id],
       },
     }));
   }
   ```
   
   
   ###### Provide feedback to improve future suggestions
   [![Nice 
Catch](https://img.shields.io/badge/👍%20Nice%20Catch-71BC78)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/2b600604-90ad-488a-8dd7-7533797969d5/upvote)
 
[![Incorrect](https://img.shields.io/badge/👎%20Incorrect-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/2b600604-90ad-488a-8dd7-7533797969d5?what_not_true=true)
  [![Not in 
Scope](https://img.shields.io/badge/👎%20Out%20of%20PR%20scope-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/2b600604-90ad-488a-8dd7-7533797969d5?what_out_of_scope=true)
 [![Not in coding 
standard](https://img.shields.io/badge/👎%20Not%20in%20our%20standards-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/2b600604-90ad-488a-8dd7-7533797969d5?what_not_in_standard=true)
 
[![Other](https://img.shields.io/badge/👎%20Other-white)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/2b600604-90ad-488a-8dd7-7533797969d5)
   </details>
   
   <sub>
   
   💬 Looking for more details? Reply to this comment to chat with Korbit.
   </sub>
   
   <!--- korbi internal id:d8ab88a4-5c0f-48c1-8e3a-2331adfa7718 -->
   
   
   [](d8ab88a4-5c0f-48c1-8e3a-2331adfa7718)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to