Skip to content

Commit

Permalink
Merge pull request #93 from Kuechlin/features-filter-and-selection
Browse files Browse the repository at this point in the history
Features filter and selection
  • Loading branch information
Kuechlin authored Sep 6, 2022
2 parents 353a88a + bcf1d2a commit e7c2a48
Show file tree
Hide file tree
Showing 13 changed files with 369 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ function Demo() {
- [x] Fixed Header
- [ ] Column pinning
- [ ] Column Ordering
- [x] Row Selection
- [x] Pagination
- [x] Styles Api
- [x] Docs
Expand Down
11 changes: 11 additions & 0 deletions docs/pages/Properties.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,17 @@ export default function Properties() {
},
],
},
{
group: 'Row Selection',
children: [
{ name: 'withRowSelection', type: 'boolean', description: 'Enables row selection' },
{
name: 'onRowSelectionChange',
type: 'OnChangeCallback<RowSelectionState>',
description: 'Callback when row selection changed',
},
],
},
]}
/>
</Stack>
Expand Down
127 changes: 127 additions & 0 deletions docs/pages/examples/ExternalFilterExample.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { Stack, Card, Text, Group } from '@mantine/core';
import {
booleanFilterFn,
DataGrid,
dateFilterFn,
highlightFilterValue,
numberFilterFn,
stringFilterFn,
ExternalColumnFilter,
useDataGrid,
} from '../../../src';
import CodeDemo from '../../components/CodeDemo';
import { Data, demoData } from '../../demoData';

export default function ExternalFilterExample() {
const [table, setRef] = useDataGrid<Data>();

return (
<CodeDemo code={grid_usage}>
{table && (
<Group>
<Card>
<Stack>
<Text>Text Filter:</Text>
<ExternalColumnFilter column={table.getColumn('text')} />
</Stack>
</Card>
</Group>
)}
<DataGrid
data={demoData.splice(0, 25)}
tableRef={setRef}
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,
},
]}
/>
</CodeDemo>
);
}
const grid_usage = `
import {
DataGrid,
booleanFilterFn,
dateFilterFn,
highlightFilterValue,
numberFilterFn,
stringFilterFn,
} from 'mantine-data-grid';
function Demo() {
return (
<DataGrid
data={data}
striped
highlightOnHover
withGlobalFilter
withPagination
withColumnFilters
withSorting
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,
},
]}
/>
);
}
`;
132 changes: 132 additions & 0 deletions docs/pages/examples/RowSelectionExample.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { RowSelectionState } from '@tanstack/react-table';
import { useState } from 'react';
import {
booleanFilterFn,
DataGrid,
dateFilterFn,
highlightFilterValue,
numberFilterFn,
stringFilterFn,
} from '../../../src';
import CodeDemo from '../../components/CodeDemo';
import { demoData } from '../../demoData';

export default function RowSelectionExample() {
const [selected, setSelected] = useState<RowSelectionState>({});
return (
<CodeDemo code={grid_usage}>
<DataGrid
data={demoData}
striped
highlightOnHover
withGlobalFilter
withPagination
withColumnFilters
withSorting
withColumnResizing
withRowSelection
state={{
rowSelection: selected,
}}
onRowSelectionChange={setSelected}
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,
},
]}
/>
</CodeDemo>
);
}
const grid_usage = `
import {
DataGrid,
booleanFilterFn,
dateFilterFn,
highlightFilterValue,
numberFilterFn,
stringFilterFn,
} from 'mantine-data-grid';
function Demo() {
const [selected, setSelected] = useState<RowSelectionState>({});
return (
<DataGrid
data={demoData}
striped
highlightOnHover
withGlobalFilter
withPagination
withColumnFilters
withSorting
withColumnResizing
withRowSelection
state={{
rowSelection: selected,
}}
onRowSelectionChange={setSelected}
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,
},
]}
/>
);
}
`;
13 changes: 13 additions & 0 deletions docs/pages/examples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import EmptyExample from './EmptyExample';
import InitialStateExample from './InitialStateExample';
import LocaleExample from './LocaleExample';
import OnRowClickExample from './OnRowExample';
import OverrideExample from './ExternalFilterExample';
import RowSelectionExample from './RowSelectionExample';
import StateExample from './StateExample';
import StylesExample from './StylesExample';
import ExternalFilterExample from './ExternalFilterExample';

export type Example = {
label: string;
Expand Down Expand Up @@ -67,4 +70,14 @@ export const examples = {
path: '/example/locale',
element: LocaleExample,
}),
rowSelection: ex({
label: 'Row selection',
path: '/example/rowselection',
element: RowSelectionExample,
}),
override: ex({
label: 'External filter',
path: '/example/externalfilter',
element: ExternalFilterExample,
}),
};
22 changes: 21 additions & 1 deletion src/ColumnFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ActionIcon, Button, Group, Menu, Stack } from '@mantine/core';
import { Column } from '@tanstack/react-table';
import { useState } from 'react';
import { createElement, useState } from 'react';
import { Check, Filter, X } from 'tabler-icons-react';
import { isDataGridFilter } from './types';

Expand Down Expand Up @@ -76,3 +76,23 @@ export const ColumnFilter = ({ column, className, color }: ColumnFilterProps) =>
</Menu>
);
};

export interface ExternalColumnFilterProps {
column: Column<any, any>;
}

export const ExternalColumnFilter = ({ column }: ExternalColumnFilterProps) => {
const filterFn = column.columnDef.filterFn;
const [value, setValue] = useState(
column.getFilterValue() || (isDataGridFilter(filterFn) ? filterFn.init() : { value: null })
);

if (!isDataGridFilter(filterFn)) return null;

const handleChange = (value: any) => {
column.setFilterValue(value);
setValue(value);
};

return createElement(filterFn.element, { filter: value, onFilterChange: handleChange });
};
Loading

0 comments on commit e7c2a48

Please sign in to comment.