11/* eslint-disable complexity */
2- import React , { useState } from "react" ;
2+ import React , { CSSProperties , useState } from "react" ;
33import { DateHandler } from "@neolution-ch/react-pattern-ui" ;
4- import { DataTableColumnDescription , DataTableRoutedActions , RowStyleType } from "../DataTable/DataTableInterfaces" ;
4+ import { DataTableColumnDescription , DataTableRoutedActions , RowHighlightInterface , RowStyleType } from "../DataTable/DataTableInterfaces" ;
55import { getDeepValue } from "../Utils/DeepValue" ;
66import { ActionsCell } from "../DataTable/Actions/ActionsCell" ;
77import { ActionsPosition } from "../DataTable/DataTableTypes" ;
@@ -12,6 +12,7 @@ interface DataTableRowProps<T, TRouteNames> {
1212 columns : DataTableColumnDescription < T > [ ] ;
1313 actions ?: DataTableRoutedActions < T , TRouteNames > ;
1414 rowStyle ?: RowStyleType < T > ;
15+ rowHighlight ?: RowHighlightInterface < T > ;
1516 actionsPosition ?: ActionsPosition ;
1617}
1718
@@ -22,14 +23,62 @@ export function DataTableRow<T, TRouteNames>({
2223 columns,
2324 actions,
2425 rowStyle,
26+ rowHighlight,
2527 actionsPosition,
2628} : DataTableRowProps < T , TRouteNames > ) {
2729 const keyValue = getDeepValue ( record , keyField ) ;
2830 const [ collapsed , setCollapsed ] = useState ( true ) ;
2931
32+ const operator_table = {
33+ ">" : function ( a : number | Date , b : number | Date ) {
34+ return a > b ;
35+ } ,
36+ "<" : function ( a : number | Date , b : number | Date ) {
37+ return a < b ;
38+ } ,
39+ "==" : function ( a : number | Date , b : number | Date ) {
40+ return a == b ;
41+ } ,
42+ "!=" : function ( a : number | Date , b : number | Date ) {
43+ return a != b ;
44+ } ,
45+ } ;
46+
47+ function getStyle ( rowObjectT : T , rowHighlight ?: RowHighlightInterface < T > ) : CSSProperties | undefined {
48+ const defaultStyle : CSSProperties = {
49+ backgroundColor : "rgba(255,0,0,0.7)" ,
50+ color : "white" ,
51+ } ;
52+
53+ if ( ! rowHighlight ) {
54+ return undefined ;
55+ }
56+
57+ let selectedValue : number | Date | undefined ;
58+ if ( typeof rowObjectT [ rowHighlight . compareField ] == "number" ) {
59+ selectedValue = rowObjectT [ rowHighlight . compareField ] as unknown as number ;
60+ } else if ( typeof rowObjectT [ rowHighlight . compareField ] == "string" ) {
61+ selectedValue = new Date ( rowObjectT [ rowHighlight . compareField ] as unknown as string ) ;
62+ }
63+
64+ if ( selectedValue == null ) {
65+ return undefined ;
66+ }
67+
68+ if ( typeof selectedValue != "number" && isNaN ( selectedValue ?. getDate ( ) ) ) {
69+ return undefined ;
70+ }
71+
72+ if ( operator_table [ rowHighlight . operation ] ( selectedValue , rowHighlight . compareValue ) ) {
73+ return rowHighlight . customStyle ?? defaultStyle ;
74+ }
75+
76+ return undefined ;
77+ }
78+
3079 return (
3180 < React . Fragment >
32- < tr key = { `${ keyValue } _row` } style = { rowStyle ? rowStyle ( keyValue , record ) : undefined } >
81+ < tr key = { `${ keyValue } _row` } style = { { ... ( rowStyle ? rowStyle ( keyValue , record ) : undefined ) } } >
3382 { actionsPosition === ActionsPosition . Left && (
3483 < ActionsCell collapsed = { collapsed } setCollapsed = { setCollapsed } actions = { actions } keyValue = { keyValue } record = { record } />
3584 ) }
@@ -42,23 +91,23 @@ export function DataTableRow<T, TRouteNames>({
4291 column . cellStyle instanceof Function
4392 ? column . cellStyle ( { key : keyValue , row : record , value : deepValue } )
4493 : column . cellStyle ?? undefined ;
45-
94+ const cellStyle = { ... getStyle ( record , rowHighlight ) , ... style } ;
4695 if ( column . enumValues && ! Number . isNaN ( deepValueInt ) && column . enumValues . filter ( ( c ) => c . value === deepValueInt ) . length > 0 )
4796 return (
48- < td key = { key } style = { style } >
97+ < td key = { key } style = { cellStyle } >
4998 { column . enumValues . filter ( ( c ) => c . value === deepValueInt ) [ 0 ] . text }
5099 </ td >
51100 ) ;
52101
53102 if ( column . formatter )
54103 return (
55- < td key = { key } style = { style } >
104+ < td key = { key } style = { cellStyle } >
56105 { column . formatter ( { key : keyValue , row : record , value : deepValue } ) }
57106 </ td >
58107 ) ;
59108
60109 return (
61- < td key = { key } style = { style } >
110+ < td key = { key } style = { cellStyle } >
62111 { column . dateTimeFormat ? DateHandler . getDateFormattedWithDefault ( deepValue , column . dateTimeFormat , "-" ) : deepValue }
63112 </ td >
64113 ) ;
@@ -74,6 +123,7 @@ export function DataTableRow<T, TRouteNames>({
74123 < DataTableRow
75124 key = { `${ keyValue } _subrow_${ getDeepValue ( subRow , keyField ) } ` }
76125 keyField = { keyField }
126+ rowHighlight = { rowHighlight }
77127 columns = { actions ?. collapse ?. columns || columns }
78128 record = { subRow }
79129 actionsPosition = { actionsPosition }
0 commit comments