Skip to content

Commit 6a1a0bf

Browse files
author
Marosvölgyi Zoltán
committed
Add selectionService to Grid
1 parent 4d89609 commit 6a1a0bf

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

apps/sensenet/src/components/ContentBreadcrumbs.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ export const ContentBreadcrumbs = <T extends GenericContent = GenericContent>(pr
6363
selectedComponentsObserve.dispose()
6464
}
6565
}, [selectionService.selection])
66-
6766
return (
6867
<div className={classes.buttonsWrapper}>
6968
<Breadcrumbs<T>

apps/sensenet/src/components/IconFromPath.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ const IconFromPath = ({
4949
case '/Root/System/Images/Icons/GroupIcon.svg':
5050
svgPath = '/Root/System/Images/Icons/colors/users.svg'
5151
break
52-
5352
case '/Root/System/Images/Icons/site.svg':
5453
svgPath = '/Root/System/Images/Icons/colors/site.svg'
5554
break
@@ -87,7 +86,7 @@ const IconFromPath = ({
8786
const resizedsvg = svg
8887
.replace('width=', 'width="100%" oldwidth=')
8988
.replace('height=', 'height="100%" oldheight=')
90-
options.repo.iconCache.set(svgPath, resizedsvg)
89+
options.repo.iconCache.set(path, resizedsvg)
9190
setIcon(resizedsvg)
9291
return
9392
}

apps/sensenet/src/components/grid/Grid.tsx

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { CurrentChildrenContext, CurrentContentContext } from '@sensenet/hooks-r
33
import React, { useCallback, useContext, useEffect, useRef, useState } from 'react'
44
// @ts-ignore
55
import ReactDataGrid from 'react-data-grid'
6+
import { useSelectionService } from '../../hooks'
67

78
import { DropFileArea } from '../DropFileArea'
89
import { ActionFormatter } from './Formatters/ActionFormatter'
@@ -16,6 +17,7 @@ import { EmptyRowsView } from './Views/EmptyRowsView'
1617

1718
export 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

Comments
 (0)