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.
feat(Table): drag column added (#43)
- Loading branch information
Showing
25 changed files
with
187 additions
and
110 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
3 changes: 0 additions & 3 deletions
3
src/components/BaseDraggableRowMarker/BaseDraggableRowMarker.classname.ts
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
src/components/BaseDraggableRowMarker/BaseDraggableRowMarker.scss
This file was deleted.
Oops, something went wrong.
13 changes: 0 additions & 13 deletions
13
src/components/BaseDraggableRowMarker/BaseDraggableRowMarker.tsx
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React from 'react'; | ||
|
||
import {useSortable} from '@dnd-kit/sortable'; | ||
import {Grip as GripIcon} from '@gravity-ui/icons'; | ||
import {Icon} from '@gravity-ui/uikit'; | ||
import type {Row} from '@tanstack/react-table'; | ||
|
||
import {b} from './DragHandle.classname'; | ||
|
||
import './DragHandle.scss'; | ||
|
||
export interface DragHandleProps<TData> { | ||
row: Row<TData>; | ||
} | ||
|
||
export const DragHandle = <TData,>({row}: DragHandleProps<TData>) => { | ||
const {attributes, listeners} = useSortable({ | ||
id: row.id, | ||
}); | ||
|
||
return ( | ||
<span {...attributes} {...listeners} className={b()} data-role="drag-handle"> | ||
<Icon data={GripIcon} size={16} /> | ||
</span> | ||
); | ||
}; | ||
|
||
DragHandle.displayName = 'DragHandle'; |
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 @@ | ||
export * from './DragHandle'; |
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
53 changes: 53 additions & 0 deletions
53
src/components/Table/__stories__/stories/ReorderingStory.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,53 @@ | ||
import React from 'react'; | ||
|
||
import type {ColumnDef} from '@tanstack/react-table'; | ||
|
||
import {dragHandleColumn} from '../../../../constants'; | ||
import {useTable} from '../../../../hooks'; | ||
import {columns as originalColumns} from '../../../BaseTable/__stories__/constants/columns'; | ||
import {data as originalData} from '../../../BaseTable/__stories__/constants/data'; | ||
import type {Item} from '../../../BaseTable/__stories__/types'; | ||
import type {ReorderingProviderProps} from '../../../ReorderingProvider'; | ||
import {ReorderingProvider} from '../../../ReorderingProvider'; | ||
import {Table} from '../../Table'; | ||
|
||
const columns: ColumnDef<Item>[] = [dragHandleColumn as ColumnDef<Item>, ...originalColumns]; | ||
|
||
export const ReorderingStory = () => { | ||
const [data, setData] = React.useState(originalData); | ||
|
||
const table = useTable({ | ||
columns, | ||
data, | ||
getRowId: (item) => item.id, | ||
}); | ||
|
||
const handleReorder = React.useCallback< | ||
NonNullable<ReorderingProviderProps<Item>['onReorder']> | ||
>(({draggedItemKey, baseItemKey}) => { | ||
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 ( | ||
<ReorderingProvider table={table} onReorder={handleReorder}> | ||
<Table table={table} /> | ||
</ReorderingProvider> | ||
); | ||
}; |
Oops, something went wrong.