generated from gravity-ui/package-example
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
189 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/components/__stories__/ColumnPinningWithReorderingDemo.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import React from 'react'; | ||
|
||
import type {ColumnDef, ColumnPinningState} from '@tanstack/react-table'; | ||
|
||
import {defaultDragHandleColumn} from '../../constants'; | ||
import {withTableReorder} from '../../hocs'; | ||
import {useTable} from '../../hooks'; | ||
import type {SortableListDragResult} from '../SortableList'; | ||
import {Table} from '../Table'; | ||
|
||
import {cnColumnPinningDemo} from './ColumnPinningDemo.classname'; | ||
import {columns} from './constants/columnPinning'; | ||
import {data as originalData} from './constants/data'; | ||
import type {Item} from './types'; | ||
|
||
const TableWithReordering = withTableReorder(Table); | ||
|
||
const columnsWithReordering: ColumnDef<Item>[] = [ | ||
{ | ||
...(defaultDragHandleColumn as ColumnDef<Item>), | ||
minSize: 16, | ||
}, | ||
...columns, | ||
]; | ||
|
||
export const ColumnPinningWithReorderingDemo = () => { | ||
const [data, setData] = React.useState(originalData); | ||
|
||
const [columnPinning, setColumnPinning] = React.useState<ColumnPinningState>({ | ||
left: [defaultDragHandleColumn.id ?? ''], | ||
right: [], | ||
}); | ||
|
||
const table = useTable({ | ||
data, | ||
columns: columnsWithReordering, | ||
getRowId: (item) => item.id, | ||
enableColumnPinning: true, | ||
onColumnPinningChange: setColumnPinning, | ||
state: { | ||
columnPinning, | ||
}, | ||
}); | ||
|
||
const handleReorder = React.useCallback( | ||
({draggedItemKey, baseItemKey}: SortableListDragResult) => { | ||
setData((prevData) => { | ||
const dataClone = prevData.slice(); | ||
|
||
const index = dataClone.findIndex((item) => item.id === draggedItemKey); | ||
|
||
if (index >= 0) { | ||
const dragged = dataClone.splice(index, 1)[0] as Item; | ||
const insertIndex = dataClone.findIndex((item) => item.id === baseItemKey); | ||
|
||
if (insertIndex >= 0) { | ||
dataClone.splice(insertIndex + 1, 0, dragged); | ||
} else { | ||
dataClone.unshift(dragged); | ||
} | ||
} | ||
|
||
return dataClone; | ||
}); | ||
}, | ||
[], | ||
); | ||
|
||
return ( | ||
<div className={cnColumnPinningDemo()}> | ||
<TableWithReordering | ||
className={cnColumnPinningDemo('table')} | ||
table={table} | ||
onReorder={handleReorder} | ||
/> | ||
</div> | ||
); | ||
}; |
49 changes: 49 additions & 0 deletions
49
src/components/__stories__/ColumnPinningWithSelectionDemo.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import React from 'react'; | ||
|
||
import type {ColumnDef, ColumnPinningState, RowSelectionState} from '@tanstack/react-table'; | ||
|
||
import {defaultSelectionColumn} from '../../constants'; | ||
import {useTable} from '../../hooks'; | ||
import {Table} from '../Table'; | ||
|
||
import {cnColumnPinningDemo} from './ColumnPinningDemo.classname'; | ||
import {columns} from './constants/columnPinning'; | ||
import {data} from './constants/data'; | ||
import type {Item} from './types'; | ||
|
||
const columnsWithSelection: ColumnDef<Item>[] = [ | ||
{ | ||
...(defaultSelectionColumn as ColumnDef<Item>), | ||
}, | ||
...columns, | ||
]; | ||
|
||
export const ColumnPinningWithSelectionDemo = () => { | ||
const [columnPinning, setColumnPinning] = React.useState<ColumnPinningState>({ | ||
left: [defaultSelectionColumn.id ?? ''], | ||
right: [], | ||
}); | ||
|
||
const [rowSelection, setRowSelection] = React.useState<RowSelectionState>({}); | ||
|
||
const table = useTable({ | ||
data, | ||
columns: columnsWithSelection, | ||
getRowId: (item) => item.id, | ||
enableColumnPinning: true, | ||
enableRowSelection: true, | ||
enableMultiRowSelection: true, | ||
onColumnPinningChange: setColumnPinning, | ||
onRowSelectionChange: setRowSelection, | ||
state: { | ||
columnPinning, | ||
rowSelection, | ||
}, | ||
}); | ||
|
||
return ( | ||
<div className={cnColumnPinningDemo()}> | ||
<Table className={cnColumnPinningDemo('table')} table={table} /> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.