Skip to content

Commit 88980c7

Browse files
authored
refactor(DataTable): address todos following component conversion (#20326)
1 parent 48a1954 commit 88980c7

15 files changed

+585
-454
lines changed

packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2558,6 +2558,9 @@ Map {
25582558
},
25592559
},
25602560
"propTypes": {
2561+
"children": {
2562+
"type": "node",
2563+
},
25612564
"experimentalAutoAlign": {
25622565
"type": "bool",
25632566
},
@@ -2597,6 +2600,7 @@ Map {
25972600
"radio": {
25982601
"type": "bool",
25992602
},
2603+
"render": [Function],
26002604
"rows": {
26012605
"args": [
26022606
{

packages/react/src/components/AILabel/AILabelDatatable.mdx renamed to packages/react/src/components/AILabel/AILabelDataTable.mdx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ callout. To achieve this, you can pass in `AILabelActions` as a child of
7575

7676
### Using as a column header
7777

78-
To use an `AILabel` inside a column header, you'll need to add a `AILabel` to the
79-
appropriate column data.
78+
To use an `AILabel` inside a column header, you'll need to add a `AILabel` to
79+
the appropriate column data.
8080

8181
```jsx
8282
const columnAILabelHeaders = [
@@ -125,13 +125,11 @@ const columnAILabelHeaders = [
125125
];
126126
```
127127

128-
To ensure the table cells are styled appropriately, you'll need to use the new
128+
To ensure the table cells are styled appropriately, you'll need to use the
129129
`getCellProps` prop getter, like so:
130130

131131
```jsx
132-
<TableCell {...getCellProps({ cell })} key={cell.id}>
133-
{cell.value}
134-
</TableCell>
132+
<TableCell {...getCellProps({ cell })}>{cell.value}</TableCell>
135133
```
136134

137135
### Using in a row
@@ -141,12 +139,12 @@ To use an `AILabel` inside a `DataTable` row, you'll need to pass in your
141139

142140
```jsx
143141
<TableBody>
144-
{rows.map((row, i) => (
145-
<TableRow key={i} {...getRowProps({ row })}>
142+
{rows.map((row) => (
143+
<TableRow {...getRowProps({ row })}>
146144
<TableSlugRow slug={AILabel} />
147145
<TableSelectRow {...getSelectionProps({ row })} />
148146
{row.cells.map((cell) => (
149-
<TableCell key={cell.id}>{cell.value}</TableCell>
147+
<TableCell {...getCellProps({ cell })}>{cell.value}</TableCell>
150148
))}
151149
</TableRow>
152150
))}
@@ -157,4 +155,4 @@ To use an `AILabel` inside a `DataTable` row, you'll need to pass in your
157155

158156
Help us improve this component by providing feedback, asking questions on Slack,
159157
or updating this file on
160-
[GitHub](https://github.com/carbon-design-system/carbon/edit/main/packages/react/src/components/AILabel/AILabelDatatable.mdx).
158+
[GitHub](https://github.com/carbon-design-system/carbon/edit/main/packages/react/src/components/AILabel/AILabelDataTable.mdx).

packages/react/src/components/DataTable/DataTable.mdx

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import {
4343
- [Accessibility Considerations](#accessibility-considerations)
4444
- [Accessible Name](#accessible-name)
4545
- [Props](#props)
46-
- [Render props](#render-props)
46+
- [Render function](#render-function)
4747
- [Prop getters](#prop-getters)
4848
- [Actions](#actions)
4949
- [State](#state)
@@ -119,7 +119,14 @@ writing:
119119

120120
```jsx
121121
<DataTable rows={rows} headers={headers}>
122-
{({ rows, headers, getTableProps, getHeaderProps, getRowProps }) => (
122+
{({
123+
rows,
124+
headers,
125+
getTableProps,
126+
getHeaderProps,
127+
getRowProps,
128+
getCellProps,
129+
}) => (
123130
<Table {...getTableProps()}>
124131
<TableHead>
125132
<TableRow>
@@ -134,7 +141,7 @@ writing:
134141
{rows.map((row) => (
135142
<TableRow {...getRowProps({ row })}>
136143
{row.cells.map((cell) => (
137-
<TableCell key={cell.id}>{cell.value}</TableCell>
144+
<TableCell {...getCellProps({ cell })}>{cell.value}</TableCell>
138145
))}
139146
</TableRow>
140147
))}
@@ -173,9 +180,9 @@ some table column headers, but not all.
173180

174181
In addition to the prop getter specified in the previous section, you can also
175182
change the sort status of the table by using the `sortBy` action made available
176-
in your `render` prop function. This `sortBy` utility takes in the `key` of the
177-
header you want to sort by as an argument. After invoking this method with the
178-
given `key`, the table should be sorted by the header that you've specified.
183+
in your `children` prop. This `sortBy` utility takes in the `key` of the header
184+
you want to sort by as an argument. After invoking this method with the given
185+
`key`, the table should be sorted by the header that you've specified.
179186

180187
### Custom sorting
181188

@@ -205,7 +212,7 @@ function customSortRow(cellA, cellB, { sortDirection, sortStates, locale }) {
205212

206213
## Expansion
207214

208-
The `DataTable` components supports row-level expansion when combined with the
215+
The `DataTable` component supports row-level expansion when combined with the
209216
`TableExpandHeader`, `TableExpandRow`, and `TableExpandedRow` components.
210217

211218
<Canvas id="components-datatable-expansion--default" />
@@ -222,7 +229,7 @@ has the necessary props.
222229

223230
### Programmatic expansion
224231

225-
You can use the `expandRow` action made available through your `render` prop
232+
You can use the `expandRow` action made available through your `children` prop
226233
function to toggle the expansion state of a given row. This method takes in the
227234
row id as a single argument.
228235

@@ -244,12 +251,12 @@ cells in a row. It also uses `getSelectionProps` props, but also takes in the
244251
current `row` in order to retrieve selection state about the given row.
245252

246253
You can access all the selected rows through the `selectedRows` property passed
247-
into your `render` prop function.
254+
into your `children` prop function.
248255

249256
### Programmatic selection
250257

251-
You can use either of the following actions from your `render` prop function to
252-
update the selection status of a row:
258+
You can use either of the following actions from your `children` prop function
259+
to update the selection status of a row:
253260

254261
- `selectAll`: invoking this will toggle the selection of all rows, either by
255262
making all selected or de-selecting all rows
@@ -259,18 +266,19 @@ update the selection status of a row:
259266
## Filtering
260267

261268
Filtering in a `DataTable` is provided through usage of the `TableToolbar` and
262-
the `TableToolbarSearch` component. Any input entered through
263-
`TableToolbarSearch` will be used when the `filterRows` prop is applied
269+
the `TableToolbarSearch` component. By default, filtering is handled by a
270+
built-in `filterRows` implementation. You can customize filtering behavior by
271+
passing your own `filterRows` function as a prop.
264272

265273
By default `filterRows` is provided through our default implementation. However,
266274
you can provide your own method if needed.
267275

268276
<Canvas id="components-datatable-filtering--default" />
269277

270278
In order to integrate filtering into your data table, you will need to provide
271-
the `onInputChange` function provided to you from `DataTable`'s render prop and
272-
pass it to the `onChange` prop of `TableToolbarSearch` in your `TableToolbar`
273-
component.
279+
the `onInputChange` function provided to you from `DataTable`'s `children` prop
280+
and pass it to the `onChange` prop of `TableToolbarSearch` in your
281+
`TableToolbar` component.
274282

275283
### Multiple filters with batch updates
276284

@@ -333,9 +341,9 @@ const handleOnResetFilter = () => {
333341
Finally, pass the array of rows from the `useState` into the DataTable.
334342

335343
```jsx
336-
<DataTable rows={renderedRows} headers={headers}>
337-
...
338-
</Datatable>
344+
<DataTable rows={renderedRows} headers={headers}>
345+
// ...
346+
</DataTable>
339347
```
340348

341349
## Batch actions
@@ -370,9 +378,9 @@ cell.
370378
```jsx
371379
<TableBody>
372380
{rows.map((row) => (
373-
<TableRow key={row.id} {...getRowProps({ row })}>
381+
<TableRow {...getRowProps({ row })}>
374382
{row.cells.map((cell) => (
375-
<TableCell key={cell.id}>{cell.value}</TableCell>
383+
<TableCell {...getCellProps({ cell })}>{cell.value}</TableCell>
376384
))}
377385
<TableCell className="cds--table-column-menu">
378386
<OverflowMenu size="sm" flipped>
@@ -491,24 +499,23 @@ naming rule
491499

492500
<ArgTypes />
493501

494-
### Render props
502+
### Render function
495503

496-
The `DataTable` component uses a render props pattern to give you full control
497-
over rendering, while Carbon manages internal state like sorting, filtering,
498-
selection, and expansion.
504+
The `DataTable` component uses a render function passed via the `children` prop
505+
to give you full control over how your table is rendered, while Carbon manages
506+
internal state like sorting, filtering, selection, and expansion.
499507

500-
You can pass either a `render` or `children` function as a prop. This function
501-
receives an object with the following properties:
508+
The function you provide to the `children` prop receives a render props object
509+
with access to:
502510

503511
- [Prop getters](#prop-getters)
504512
- [Actions](#actions)
505513
- [State](#state)
506514

507515
#### Prop getters
508516

509-
These functions are provided via the `DataTable`'s render props and must be
510-
applied to the corresponding Carbon components to enable built-in functionality
511-
like sorting, selection, and expansion.
517+
These functions must be applied to the corresponding components to enable
518+
built-in functionality like sorting, selection, and expansion.
512519

513520
| Getter | Description |
514521
| ------------------------ | ----------------------------------------------------------------- |
@@ -529,7 +536,7 @@ the `DataTableRenderProps` type in the
529536

530537
#### Actions
531538

532-
These functions allow you to programmatically update the table's state.
539+
These functions allow you to programmatically update the table's internal state.
533540

534541
| Action | Description |
535542
| --------------- | ----------------------------------------- |
@@ -581,9 +588,7 @@ conditional rendering.
581588
<TableRow {...getRowProps({ row })}>
582589
<TableSelectRow {...getSelectionProps({ row })} />
583590
{row.cells.map((cell) => (
584-
<TableCell key={cell.id} {...getCellProps({ cell })}>
585-
{cell.value}
586-
</TableCell>
591+
<TableCell {...getCellProps({ cell })}>{cell.value}</TableCell>
587592
))}
588593
</TableRow>
589594
))}

0 commit comments

Comments
 (0)