Skip to content

Commit 672c3a0

Browse files
committed
🌱 Add data attributes to table rows
To enable easier and safer selection of table rows by testing code, add some data attributes to the tr: - `data-item-id` - Holds the `idProperty` value of a table's items. This is typically the item's id number but may be a frontend generated ID in some cases. - `data-item-name` - If the table property `dataNameProperty` is provided, hold the name property of a table's item. Signed-off-by: Scott J Dickerson <[email protected]>
1 parent 646d3d6 commit 672c3a0

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

client/src/app/hooks/table-controls/types.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { TableProps, TdProps, ThProps, TrProps } from "@patternfly/react-table";
22
import { ISelectionStateArgs, useSelectionState } from "@migtools/lib-ui";
3-
import { DisallowCharacters, DiscriminatedArgs } from "@app/utils/type-utils";
3+
import {
4+
DisallowCharacters,
5+
DiscriminatedArgs,
6+
KeyWithValueType,
7+
} from "@app/utils/type-utils";
48
import {
59
IFilterStateArgs,
610
ILocalFilterDerivedStateArgs,
@@ -314,6 +318,11 @@ export type IUseTableControlPropsArgs<
314318
* The state for the columns feature. Returned by useColumnState.
315319
*/
316320
columnState: IColumnState<TColumnKey>;
321+
/**
322+
* Name of a field in TItem to use as the table row's `data-item-name` value. Without
323+
* this property provided, the `data-item-name` is not added to the table row.
324+
*/
325+
dataNameProperty?: KeyWithValueType<TItem, string>;
317326
};
318327

319328
/**

client/src/app/hooks/table-controls/useTableControlProps.ts

+6
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ export const useTableControlProps = <
6767
isItemSelected,
6868
},
6969
columnNames,
70+
idProperty,
71+
dataNameProperty,
72+
tableName,
7073
hasActionsColumn = false,
7174
variant,
7275
isFilterEnabled,
@@ -134,6 +137,9 @@ export const useTableControlProps = <
134137
const getTrProps: PropHelpers["getTrProps"] = ({ item, onRowClick }) => {
135138
const activeItemTrProps = getActiveItemTrProps({ item });
136139
return {
140+
id: `${tableName}-row-item-${item[idProperty]}`,
141+
"data-item-id": item[idProperty],
142+
"data-item-name": dataNameProperty && item[dataNameProperty],
137143
...(isActiveItemEnabled && activeItemTrProps),
138144
onRowClick: (event) =>
139145
handlePropagatedRowClick(event, () => {

client/src/app/pages/applications/applications-table/applications-table.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ export const ApplicationsTable: React.FC = () => {
321321
const tableControls = useLocalTableControls({
322322
tableName: "applications",
323323
idProperty: "id",
324+
dataNameProperty: "name",
324325
items: applications || [],
325326
columnNames: {
326327
name: "Name",

0 commit comments

Comments
 (0)