@@ -3,13 +3,14 @@ import ExampleContainer from "../../.storybook/components/ExampleContainer";
33import DxcDataGrid from "./DataGrid" ;
44import DxcContainer from "../container/Container" ;
55import { GridColumn , HierarchyGridRow } from "./types" ;
6- import { useState } from "react" ;
6+ import { isValidElement , useState } from "react" ;
77import { disabledRules } from "../../test/accessibility/rules/specific/data-grid/disabledRules" ;
88import preview from "../../.storybook/preview" ;
99import { userEvent , within } from "@storybook/test" ;
1010import DxcBadge from "../badge/Badge" ;
1111import { ActionsPropsType } from "../table/types" ;
1212import { Meta , StoryObj } from "@storybook/react" ;
13+ import { isKeyOfRow } from "./utils" ;
1314
1415export default {
1516 title : "Data Grid" ,
@@ -588,8 +589,12 @@ const customSortColumns: GridColumn[] = [
588589 alignment : "center" ,
589590 summaryKey : "total" ,
590591 sortable : true ,
591- sortFn : ( a : JSX . Element , b : JSX . Element ) =>
592- a . props . label < b . props . label ? - 1 : a . props . label > b . props . label ? 1 : 0 ,
592+ sortFn : ( a , b ) => {
593+ if ( isValidElement ( a ) && isValidElement ( b ) ) {
594+ return a . props . label < b . props . label ? - 1 : a . props . label > b . props . label ? 1 : 0 ;
595+ }
596+ return 0 ;
597+ } ,
593598 } ,
594599] ;
595600
@@ -729,10 +734,20 @@ const DataGrid = () => {
729734 console . log ( `Sorting the column '${ columnKey } ' by '${ direction } ' direction` ) ;
730735 setRowsControlled ( ( currentRows ) => {
731736 return currentRows . sort ( ( a , b ) => {
732- if ( direction === "ASC" ) {
733- return a [ columnKey ] < b [ columnKey ] ? - 1 : a [ columnKey ] > b [ columnKey ] ? 1 : 0 ;
737+ if ( isKeyOfRow ( columnKey , a ) && isKeyOfRow ( columnKey , b ) ) {
738+ const valueA = a [ columnKey ] ;
739+ const valueB = b [ columnKey ] ;
740+ if ( valueA != null && valueB != null ) {
741+ if ( direction === "ASC" ) {
742+ return valueA < valueB ? - 1 : valueA > valueB ? 1 : 0 ;
743+ } else {
744+ return valueA < valueB ? 1 : valueA > valueB ? - 1 : 0 ;
745+ }
746+ } else {
747+ return 0 ;
748+ }
734749 } else {
735- return a [ columnKey ] < b [ columnKey ] ? 1 : a [ columnKey ] > b [ columnKey ] ? - 1 : 0 ;
750+ return 0 ;
736751 }
737752 } ) ;
738753 } ) ;
@@ -925,39 +940,43 @@ export const DataGridSortedWithChildren: Story = {
925940 render : DataGridSortedChildren ,
926941 play : async ( { canvasElement } ) => {
927942 const canvas = within ( canvasElement ) ;
943+ const checkboxes = canvas . getAllByRole ( "checkbox" ) ;
944+ const columnheaders = canvas . getAllByRole ( "columnheader" ) ;
928945
929- await userEvent . click ( canvas . getAllByRole ( "checkbox" ) [ 0 ] ) ;
946+ checkboxes [ 0 ] && ( await userEvent . click ( checkboxes [ 0 ] ) ) ;
930947 await userEvent . click ( canvas . getByText ( "Root Node 1" ) ) ;
931948 await userEvent . click ( canvas . getByText ( "Root Node 2" ) ) ;
932949 await userEvent . click ( canvas . getByText ( "Child Node 1.1" ) ) ;
933950 await userEvent . click ( canvas . getByText ( "Child Node 2.1" ) ) ;
934- await userEvent . click ( canvas . getAllByRole ( "columnheader" ) [ 1 ] ) ;
935- await userEvent . click ( canvas . getAllByRole ( "columnheader" ) [ 1 ] ) ;
936- await userEvent . click ( canvas . getAllByRole ( "checkbox" ) [ 5 ] ) ;
951+ columnheaders [ 1 ] && ( await userEvent . click ( columnheaders [ 1 ] ) ) ;
952+ columnheaders [ 1 ] && ( await userEvent . click ( columnheaders [ 1 ] ) ) ;
953+ checkboxes [ 5 ] && ( await userEvent . click ( checkboxes [ 5 ] ) ) ;
937954
938- await userEvent . click ( canvas . getAllByRole ( "checkbox" ) [ 13 ] ) ;
955+ checkboxes [ 13 ] && ( await userEvent . click ( checkboxes [ 13 ] ) ) ;
939956 await userEvent . click ( canvas . getByText ( "Paginated Node 1" ) ) ;
940957 await userEvent . click ( canvas . getByText ( "Paginated Node 2" ) ) ;
941958 await userEvent . click ( canvas . getByText ( "Paginated Node 1.1" ) ) ;
942959 await userEvent . click ( canvas . getByText ( "Paginated Node 2.1" ) ) ;
943- await userEvent . click ( canvas . getAllByRole ( "columnheader" ) [ 4 ] ) ;
944- await userEvent . click ( canvas . getAllByRole ( "checkbox" ) [ 18 ] ) ;
960+ columnheaders [ 4 ] && ( await userEvent . click ( columnheaders [ 4 ] ) ) ;
961+ checkboxes [ 18 ] && ( await userEvent . click ( checkboxes [ 18 ] ) ) ;
945962 } ,
946963} ;
947964
948965export const DataGridSortedExpanded : Story = {
949966 render : DataGridSortedExpandable ,
950967 play : async ( { canvasElement } ) => {
951968 const canvas = within ( canvasElement ) ;
952- await userEvent . click ( canvas . getAllByRole ( "button" ) [ 0 ] ) ;
953- await userEvent . click ( canvas . getAllByRole ( "button" ) [ 1 ] ) ;
954- await userEvent . click ( canvas . getAllByRole ( "columnheader" ) [ 4 ] ) ;
955- await userEvent . click ( canvas . getAllByRole ( "button" ) [ 9 ] ) ;
956- await userEvent . click ( canvas . getAllByRole ( "button" ) [ 10 ] ) ;
957- await userEvent . click ( canvas . getAllByRole ( "columnheader" ) [ 10 ] ) ;
958- await userEvent . click ( canvas . getAllByRole ( "button" ) [ 16 ] ) ;
959- await userEvent . click ( canvas . getAllByRole ( "button" ) [ 43 ] ) ;
960- await userEvent . click ( canvas . getAllByRole ( "button" ) [ 36 ] ) ;
961- await userEvent . click ( canvas . getAllByRole ( "button" ) [ 37 ] ) ;
969+ const buttons = canvas . getAllByRole ( "button" ) ;
970+ const columnHeaders = canvas . getAllByRole ( "columnheader" ) ;
971+ buttons [ 0 ] && ( await userEvent . click ( buttons [ 0 ] ) ) ;
972+ buttons [ 1 ] && ( await userEvent . click ( buttons [ 1 ] ) ) ;
973+ columnHeaders [ 4 ] && ( await userEvent . click ( columnHeaders [ 4 ] ) ) ;
974+ buttons [ 9 ] && ( await userEvent . click ( buttons [ 9 ] ) ) ;
975+ buttons [ 10 ] && ( await userEvent . click ( buttons [ 10 ] ) ) ;
976+ columnHeaders [ 10 ] && ( await userEvent . click ( columnHeaders [ 10 ] ) ) ;
977+ buttons [ 16 ] && ( await userEvent . click ( buttons [ 16 ] ) ) ;
978+ buttons [ 43 ] && ( await userEvent . click ( buttons [ 43 ] ) ) ;
979+ buttons [ 36 ] && ( await userEvent . click ( buttons [ 36 ] ) ) ;
980+ buttons [ 37 ] && ( await userEvent . click ( buttons [ 37 ] ) ) ;
962981 } ,
963982} ;
0 commit comments