11import * as React from 'react' ;
2+ import { useNavigate } from 'react-router' ;
3+ import type { RaRecord } from '../types' ;
4+ import { useResourceContext } from '../core/useResourceContext' ;
5+ import { useRecordContext } from '../controller/record/useRecordContext' ;
6+ import { RecordContextProvider } from '../controller/record/RecordContext' ;
7+ import { useListContext } from '../controller/list/useListContext' ;
8+ import { useFieldValue } from '../util/useFieldValue' ;
9+ import { useEvent } from '../util/useEvent' ;
10+ import { useGetPathForRecordCallback } from '../routing/useGetPathForRecordCallback' ;
11+ import { useDataTableSelectedIdsContext } from '../dataTable/DataTableSelectedIdsContext' ;
212import {
3- DataTableBase ,
4- DataTableBaseProps ,
5- DataTableRenderContext ,
6- RaRecord ,
7- RecordContextProvider ,
8- useDataTableCallbacksContext ,
913 useDataTableRenderContext ,
10- useEvent ,
11- useFieldValue ,
12- useGetPathForRecordCallback ,
13- useListContext ,
14- useRecordContext ,
15- useResourceContext ,
16- } from '../' ;
17- import { useNavigate } from 'react-router' ;
14+ DataTableRenderContext ,
15+ } from '../dataTable/DataTableRenderContext' ;
16+ import { useDataTableConfigContext } from '../dataTable/DataTableConfigContext' ;
17+ import { useDataTableCallbacksContext } from '../dataTable/DataTableCallbacksContext' ;
18+ import { DataTableBase , DataTableBaseProps } from '../dataTable/DataTableBase' ;
1819
1920const DataTableCol = ( props : {
2021 children ?: React . ReactNode ;
@@ -79,7 +80,7 @@ const DataTableCell = (props: {
7980
8081const DataTableCellValue = ( props : { source : string } ) => {
8182 const value = useFieldValue ( props ) ;
82- return < > { value } </ > ;
83+ return < > { value ?. toString ( ) } </ > ;
8384} ;
8485
8586const DataTableRow = ( props : {
@@ -101,7 +102,10 @@ const DataTableRow = (props: {
101102 'DataTableRow can only be used within a ResourceContext or be passed a resource prop'
102103 ) ;
103104 }
104- const { rowClick } = useDataTableCallbacksContext ( ) ;
105+
106+ const { hasBulkActions = false } = useDataTableConfigContext ( ) ;
107+ const { handleToggleItem, rowClick } = useDataTableCallbacksContext ( ) ;
108+ const selectedIds = useDataTableSelectedIdsContext ( ) ;
105109
106110 const handleClick = useEvent ( async ( event : React . MouseEvent ) => {
107111 event . persist ( ) ;
@@ -127,21 +131,37 @@ const DataTableRow = (props: {
127131 } ) ;
128132 } ) ;
129133
130- return < tr onClick = { handleClick } > { props . children } </ tr > ;
134+ return (
135+ < tr onClick = { handleClick } >
136+ { hasBulkActions && (
137+ < DataTableCol >
138+ < input
139+ aria-label = "Select this row"
140+ type = "checkbox"
141+ checked = { selectedIds ?. includes ( record . id ) }
142+ onChange = { event => handleToggleItem ! ( record . id , event ) }
143+ />
144+ </ DataTableCol >
145+ ) }
146+ { props . children }
147+ </ tr >
148+ ) ;
131149} ;
132150
133151const isPromise = ( value : any ) : value is Promise < any > =>
134152 value && typeof value . then === 'function' ;
135153
136154export const DataTable = (
137- props : Omit < DataTableBaseProps , 'hasBulkActions' | 'empty' | 'loading' >
155+ props : Omit < DataTableBaseProps , 'hasBulkActions' | 'empty' | 'loading' > & {
156+ hasBulkActions ?: boolean ;
157+ }
138158) => {
139159 const { data } = useListContext ( ) ;
140160
141161 return (
142162 < DataTableBase
143- { ...props }
144163 hasBulkActions = { false }
164+ { ...props }
145165 empty = { null }
146166 loading = { null }
147167 >
@@ -151,7 +171,10 @@ export const DataTable = (
151171 >
152172 < DataTableRenderContext . Provider value = "header" >
153173 < thead >
154- < tr > { props . children } </ tr >
174+ < tr >
175+ { props . hasBulkActions ? < td > </ td > : null }
176+ { props . children }
177+ </ tr >
155178 </ thead >
156179 </ DataTableRenderContext . Provider >
157180 < DataTableRenderContext . Provider value = "data" >
0 commit comments