-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from Kuechlin/v0.0.21
V0.0.21
- Loading branch information
Showing
14 changed files
with
1,052 additions
and
498 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { ActionIcon, Box, Button, Card, Group } from '@mantine/core'; | ||
import { ColumnOrderState, TableOptions } from '@tanstack/react-table'; | ||
import { useState } from 'react'; | ||
|
||
import { | ||
closestCenter, | ||
DndContext, | ||
DragOverlay, | ||
KeyboardSensor, | ||
MouseSensor, | ||
TouchSensor, | ||
UniqueIdentifier, | ||
useSensor, | ||
useSensors, | ||
} from '@dnd-kit/core'; | ||
import { restrictToHorizontalAxis } from '@dnd-kit/modifiers'; | ||
import { arrayMove, horizontalListSortingStrategy, SortableContext, useSortable } from '@dnd-kit/sortable'; | ||
import { CSS } from '@dnd-kit/utilities'; | ||
import { GridDots } from 'tabler-icons-react'; | ||
import { DataGrid, DataGridHeaderCellProps } from '../../../src'; | ||
import { Data, demoData } from '../../demoData'; | ||
|
||
export default function ColumnDragDropExample() { | ||
const columns: TableOptions<Data>['columns'] = [ | ||
{ id: 'cat', accessorFn: (row) => row.cat }, | ||
{ id: 'fish', accessorFn: (row) => row.fish }, | ||
{ id: 'city', accessorFn: (row) => row.city }, | ||
{ id: 'value', accessorFn: (row) => row.value }, | ||
]; | ||
const [columnOrder, setColumnOrder] = useState<ColumnOrderState>( | ||
columns.map((column) => column.id as string) //must start out with populated columnOrder so we can splice | ||
); | ||
|
||
const resetOrder = () => setColumnOrder(columns.map((column) => column.id as string)); | ||
const [activeId, setActiveId] = useState<UniqueIdentifier | null>(null); | ||
const sensors = useSensors(useSensor(MouseSensor, {}), useSensor(TouchSensor, {}), useSensor(KeyboardSensor, {})); | ||
console.log(columnOrder); | ||
|
||
return ( | ||
<> | ||
<Button onClick={resetOrder}>Reset column order</Button> | ||
<DndContext | ||
sensors={sensors} | ||
onDragEnd={(event) => { | ||
const { active, over } = event; | ||
if (over && active.id !== over.id) { | ||
setColumnOrder((data) => { | ||
const oldIndex = columnOrder.indexOf(String(active.id)); | ||
const newIndex = columnOrder.indexOf(String(over.id)); | ||
return arrayMove(data, oldIndex, newIndex); | ||
}); | ||
} | ||
|
||
setActiveId(null); | ||
}} | ||
onDragStart={(e) => setActiveId(String(e.active.id))} | ||
onDragCancel={() => setActiveId(null)} | ||
collisionDetection={closestCenter} | ||
modifiers={[restrictToHorizontalAxis]} | ||
> | ||
<DragOverlay> | ||
<Card p="xs"> | ||
<Group position="apart"> | ||
{activeId} | ||
<ActionIcon ml="auto"> | ||
<GridDots /> | ||
</ActionIcon> | ||
</Group> | ||
</Card> | ||
</DragOverlay> | ||
<DataGrid | ||
data={demoData} | ||
columns={columns} | ||
withPagination | ||
state={{ columnOrder }} | ||
components={{ | ||
headerWrapper: ({ children, ...props }) => ( | ||
<thead {...props}> | ||
<SortableContext items={columnOrder} strategy={horizontalListSortingStrategy}> | ||
{children} | ||
</SortableContext> | ||
</thead> | ||
), | ||
headerCell: ({ children, header, ...props }) => ( | ||
<DraggableColumnHeader {...props} header={header}> | ||
{children} | ||
</DraggableColumnHeader> | ||
), | ||
}} | ||
/> | ||
</DndContext> | ||
</> | ||
); | ||
} | ||
const DraggableColumnHeader = ({ children, header, ...props }: DataGridHeaderCellProps<Data>) => { | ||
const { attributes, listeners, transform, transition, setNodeRef } = useSortable({ | ||
id: header.column.id, | ||
}); | ||
|
||
return ( | ||
<th | ||
{...props} | ||
ref={setNodeRef} | ||
style={{ | ||
...props?.style, | ||
transform: CSS.Transform.toString(transform), | ||
transition: transition, | ||
}} | ||
> | ||
<Box sx={{ display: 'flex' }}> | ||
{children} | ||
<ActionIcon ml="auto"> | ||
<GridDots {...attributes} {...listeners} /> | ||
</ActionIcon> | ||
</Box> | ||
</th> | ||
); | ||
}; |
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,72 @@ | ||
import { Center, Pagination } from '@mantine/core'; | ||
import { | ||
booleanFilterFn, | ||
DataGrid, | ||
dateFilterFn, | ||
highlightFilterValue, | ||
numberFilterFn, | ||
stringFilterFn, | ||
} from '../../../src'; | ||
import { demoData } from '../../demoData'; | ||
|
||
export default function CustomPaginationExample() { | ||
return ( | ||
<DataGrid | ||
data={demoData} | ||
striped | ||
highlightOnHover | ||
withGlobalFilter | ||
withPagination | ||
withColumnFilters | ||
withSorting | ||
withColumnResizing | ||
components={{ | ||
pagination: ({ table }) => ( | ||
<Center> | ||
<Pagination | ||
size="xl" | ||
page={table.getState().pagination.pageIndex + 1} | ||
total={table.getPageCount()} | ||
onChange={(pageNum) => table.setPageIndex(Number(pageNum) - 1)} | ||
siblings={1} | ||
color="red" | ||
/> | ||
</Center> | ||
), | ||
}} | ||
columns={[ | ||
{ | ||
accessorKey: 'text', | ||
header: 'Text that is too long for a Header', | ||
filterFn: stringFilterFn, | ||
size: 300, | ||
cell: highlightFilterValue, | ||
}, | ||
{ | ||
header: 'Animal', | ||
columns: [ | ||
{ accessorKey: 'cat', filterFn: stringFilterFn }, | ||
{ | ||
accessorKey: 'fish', | ||
filterFn: stringFilterFn, | ||
}, | ||
], | ||
}, | ||
{ | ||
accessorKey: 'city', | ||
filterFn: stringFilterFn, | ||
}, | ||
{ accessorKey: 'value', filterFn: numberFilterFn }, | ||
{ | ||
accessorKey: 'date', | ||
cell: (cell) => cell.getValue<Date>()?.toLocaleDateString(), | ||
filterFn: dateFilterFn, | ||
}, | ||
{ | ||
accessorKey: 'bool', | ||
filterFn: booleanFilterFn, | ||
}, | ||
]} | ||
/> | ||
); | ||
} |
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.