@@ -3,6 +3,7 @@ import { CurrentChildrenContext, CurrentContentContext } from '@sensenet/hooks-r
33import React , { useCallback , useContext , useEffect , useRef , useState } from 'react'
44// @ts -ignore
55import ReactDataGrid from 'react-data-grid'
6+ import { useSelectionService } from '../../hooks'
67
78import { DropFileArea } from '../DropFileArea'
89import { ActionFormatter } from './Formatters/ActionFormatter'
@@ -16,6 +17,7 @@ import { EmptyRowsView } from './Views/EmptyRowsView'
1617
1718export function Grid < T extends GenericContent = GenericContent > ( this : any , props : GridProps < T > ) {
1819 // @ts -ignore
20+ const selectionService = useSelectionService ( )
1921 const [ selectedIndexes , setSelectedIndexes ] = useState < any [ ] > ( [ ] )
2022 const [ rowItems , setRowItems ] = useState < any [ ] > ( [ ] )
2123 const [ sortColumn , setSortColumn ] = useState < string > ( 'DisplayName' )
@@ -38,12 +40,23 @@ export function Grid<T extends GenericContent = GenericContent>(this: any, props
3840 { key : 'check' , name : '#' , width : 0 , formatter : CheckBoxFormatter , flex : 1 } ,
3941 { key : 'icon' , name : '' , width : 35 , formatter : IconFormatter , flex : 1 } ,
4042 { key : 'id' , name : 'ID' , width : 55 , sortable : true , flex : 1 } ,
41- { key : 'index' , name : 'Index' , width : 45 , sortable : true , flex : 1 } ,
43+ { key : 'index' , name : 'Idx' , width : 35 , sortable : true , flex : 1 } ,
44+ {
45+ key : 'Name' ,
46+ name : 'Name' ,
47+ resizable : true ,
48+ width : 200 ,
49+ autofill : false ,
50+ sortable : true ,
51+ filterable : true ,
52+ formatter : DisplayNameFormatter ,
53+ flex : 1 ,
54+ } ,
4255 {
4356 key : 'DisplayName' ,
4457 name : 'Display Name' ,
4558 resizable : true ,
46- minWidth : 400 ,
59+ minWidth : 200 ,
4760 autofill : true ,
4861 sortable : true ,
4962 filterable : true ,
@@ -123,6 +136,11 @@ export function Grid<T extends GenericContent = GenericContent>(this: any, props
123136 columns [ needAutoFillIndex ] . width = Number ( currentContainer . getTotalWidth ( ) ) - sumofwidths - 100
124137 }
125138 }
139+ useEffect ( ( ) => {
140+ const selected = selectedIndexes . map ( ( i ) => rowItems [ i ] . Content )
141+ selectionService . selection . setValue ( selected )
142+ } , [ rowItems , selectedIndexes , selectionService . selection ] )
143+
126144 useEffect ( ( ) => {
127145 for ( let i = 0 ; i < selectedIndexes . length ; i ++ ) {
128146 const a = selectedIndexes [ i ]
@@ -162,6 +180,16 @@ export function Grid<T extends GenericContent = GenericContent>(this: any, props
162180 result = bCol . localeCompare ( aCol )
163181 }
164182 }
183+ if ( sortColumn === 'Name' ) {
184+ const aCol = ( a . Name ?? '' ) . trim ( )
185+ const bCol = ( b . Name ?? '' ) . trim ( )
186+ if ( sortDirection === 'ASC' ) {
187+ result = aCol . localeCompare ( bCol )
188+ }
189+ if ( sortDirection === 'DESC' ) {
190+ result = bCol . localeCompare ( aCol )
191+ }
192+ }
165193 if ( sortColumn === 'CreatedBy' ) {
166194 const aCol = `${ ( a . CreatedBy as User ) . Domain } \\${ ( a . CreatedBy as User ) . LoginName } `
167195 const bCol = `${ ( b . CreatedBy as User ) . Domain } \\${ ( b . CreatedBy as User ) . LoginName } `
@@ -213,6 +241,7 @@ export function Grid<T extends GenericContent = GenericContent>(this: any, props
213241 index : child . Index ,
214242 icon : child ,
215243 DisplayName : child . DisplayName ,
244+ Name : child . Name ,
216245 Locked : child . Locked ,
217246 CreatedBy : child . CreatedBy ,
218247 CreationDate : child . CreationDate ,
@@ -225,6 +254,10 @@ export function Grid<T extends GenericContent = GenericContent>(this: any, props
225254 }
226255 setRowItems ( items )
227256 } , [ children , sortColumn , sortDirection ] )
257+ useEffect ( ( ) => {
258+ console . log ( 'Grid: children changed' )
259+ setSelectedIndexes ( [ ] )
260+ } , [ children ] )
228261 //examples: https://github.com/adazzle/react-data-grid/blob/v6.0.0-alpha.0/packages/react-data-grid-examples/src/scripts
229262 return (
230263 < DropFileArea parentContent = { parentContent } style = { { height : '100%' , overflow : 'hidden' } } >
0 commit comments