1- import { forwardRef , useCallback , useEffect , useImperativeHandle , useMemo , useRef , useState } from 'react'
1+ import { forwardRef , useCallback , useEffect , useImperativeHandle , useMemo , useRef , useState , type ComponentType } from 'react'
22import type { RLCrudProps , RLCrudRef , RLCrudActionType } from './types'
33import type { RLCrudFormFieldType } from '../RLCrudForm'
44import type { RLCrudFiltersRef } from '../RLCrudFilters'
5+ import type { RLCrudInputValueType } from '../RLCrudInput'
56import { RLCrudAction } from '../RLCrudAction'
67import { RLPaginator } from '../RLPaginator'
78import { RLDialog } from '../RLDialog'
@@ -43,7 +44,7 @@ export const RLCrud = forwardRef<RLCrudRef, RLCrudProps>(
4344 highlightLastEdited = true ,
4445 highlightLastEditedClass = '!bg-row-selected' ,
4546 persistActionDialog = true ,
46- rowClass : externalRowClass ,
47+ rowClassName : externalRowClassName ,
4748 getItems,
4849 addItem,
4950 editItem,
@@ -55,7 +56,7 @@ export const RLCrud = forwardRef<RLCrudRef, RLCrudProps>(
5556 } ,
5657 ref
5758 ) => {
58- const [ items , setItems ] = useState < unknown [ ] > ( [ ] )
59+ const [ items , setItems ] = useState < Record < string , unknown > [ ] > ( [ ] )
5960 const [ currentPage , setCurrentPage ] = useState ( 0 )
6061 const [ rowsPerPage , setRowsPerPage ] = useState (
6162 rowsPerPageOptions . includes ( initialRowsPerPage )
@@ -91,7 +92,10 @@ export const RLCrud = forwardRef<RLCrudRef, RLCrudProps>(
9192 sortable : header . sortable ,
9293 ...( header . columnProps ? { columnProps : header . columnProps } : { } ) ,
9394 ...( header . type
94- ? { component : components ?. [ header . type ] , componentProps : header . componentProps }
95+ ? {
96+ component : components ?. [ header . type ] as ComponentType < { data : unknown ; field : string ; [ key : string ] : unknown } > ,
97+ componentProps : header . componentProps as Record < string , unknown >
98+ }
9599 : { } )
96100 } ) ) ,
97101 [ headers , translationFn , components ]
@@ -115,10 +119,11 @@ export const RLCrud = forwardRef<RLCrudRef, RLCrudProps>(
115119 return
116120 }
117121
118- setItems ( response . result )
122+ setItems ( response . result as Record < string , unknown > [ ] )
119123 setCurrentPage ( response . page . currentPage )
120124 setTotalRows ( response . page . totalRows )
121125 } catch ( e ) {
126+ // eslint-disable-next-line no-console
122127 console . error ( e )
123128 onFetchError ?.( )
124129 }
@@ -171,29 +176,36 @@ export const RLCrud = forwardRef<RLCrudRef, RLCrudProps>(
171176 await fetchData ( )
172177 } , [ fetchData ] )
173178
174- const rowClass = useCallback (
179+ const rowClassName = useCallback (
175180 ( row : unknown ) => {
176- const externalClasses = externalRowClass ?.( row ) || [ ]
181+ const externalResult = externalRowClassName ?.( row )
182+ const externalClasses = Array . isArray ( externalResult )
183+ ? externalResult
184+ : externalResult
185+ ? [ externalResult ]
186+ : [ ]
187+
177188 if ( ( row as Record < string , unknown > ) [ primary_key ] === lastSelectedItem && highlightLastEdited ) {
178- return [ ...externalClasses , highlightLastEditedClass ]
189+ return [ ...externalClasses , highlightLastEditedClass ] . join ( ' ' )
179190 }
180- return externalClasses
191+ return externalClasses . join ( ' ' )
181192 } ,
182- [ externalRowClass , primary_key , lastSelectedItem , highlightLastEdited , highlightLastEditedClass ]
193+ [ externalRowClassName , primary_key , lastSelectedItem , highlightLastEdited , highlightLastEditedClass ]
183194 )
184195
185196 const onAdd = useCallback (
186197 async ( data : Record < string , unknown > ) => {
187198 const response = await addItem ?.( data )
199+ const responseRecord = response as Record < string , unknown >
188200 const newId =
189- ( response as Record < string , unknown > ) ?. result ?. [ primary_key ] ??
190- ( response as Record < string , unknown > ) ?. [ primary_key ]
201+ ( ( responseRecord ?. result as Record < string , unknown > ) ?. [ primary_key ] ) ??
202+ responseRecord ?. [ primary_key ]
191203 setLastSelectedItem ( newId )
192204
193205 if ( goToInsertedRow ) {
194206 skipWatchersRef . current = true
195- setFiltersApplied ( { [ primary_key ] : newId } )
196- filtersRef . current ?. setFilterModel ( { [ primary_key ] : newId } )
207+ setFiltersApplied ( { [ primary_key ] : newId as RLCrudInputValueType } )
208+ filtersRef . current ?. setFilterModel ( { [ primary_key ] : newId as RLCrudInputValueType } )
197209 filtersRef . current ?. setOpen ( true )
198210 setCurrentPage ( 1 )
199211 await Promise . resolve ( ) // Allow state to update
@@ -291,7 +303,7 @@ export const RLCrud = forwardRef<RLCrudRef, RLCrudProps>(
291303 actions = { [ ] }
292304 paginator = { false }
293305 actionHeaderLabel = { translationFn ( actionHeaderI18nKey ) }
294- rowClass = { rowClass }
306+ rowClassName = { rowClassName }
295307 actionsSlot = { renderActions }
296308 emptySlot = { < div className = "flex justify-center p-4" > Empty</ div > }
297309 />
@@ -348,7 +360,7 @@ export const RLCrud = forwardRef<RLCrudRef, RLCrudProps>(
348360 requiredRuleMessage = { translationFn ( requiredI18nKey ) }
349361 cancelLabel = { translationFn ( cancelI18nKey ) }
350362 confirmLabel = { translationFn ( editI18nKey ) }
351- value = { selectedItem ?? undefined }
363+ value = { ( selectedItem as { [ key : string ] : RLCrudInputValueType } ) ?? undefined }
352364 primaryKey = { primary_key }
353365 onClose = { closeDialog }
354366 onCancel = { closeDialog }
0 commit comments