Skip to content

Commit

Permalink
use filters as functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Yannick Küchlin authored and Yannick Küchlin committed Jul 21, 2022
1 parent ca85542 commit 9220ccc
Show file tree
Hide file tree
Showing 17 changed files with 152 additions and 200 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ dist-docs
stats.html

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
Expand Down
15 changes: 15 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "tsc-watch",
"command": "tsc",
"args": ["-w", "-p", ".", "--noEmit"],
"isBackground": true,
"problemMatcher": "$tsc-watch"
}
]
}
30 changes: 19 additions & 11 deletions docs/components/Demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@ import {
} from '@mantine/core';
import { useState } from 'react';

import { DataGrid, DataGridFilterFn, PaginationArg } from '../../src';
import { highlightFilterValue } from '../../src/ColumnFilter/stringFilter';
import {
booleanFilterFn,
DataGrid,
DataGridFilterFn,
dateFilterFn,
highlightFilterValue,
numberFilterFn,
PaginationArg,
stringFilterFn,
} from '../../src';

type Data = {
text: string;
Expand Down Expand Up @@ -86,12 +94,12 @@ const useStyles = createStyles((theme) => ({
gridWrapper: {
display: 'flex',
alignItems: 'stretch',
width: "100%",
marginTop: theme.spacing.lg
width: '100%',
marginTop: theme.spacing.lg,
},
gridProps: {
borderLeftWidth: 1,
borderLeftStyle: "solid",
borderLeftStyle: 'solid',
borderLeftColor: theme.colors.gray[6],
},
}));
Expand Down Expand Up @@ -151,7 +159,7 @@ export default function Demo() {
{
accessorKey: 'text',
header: 'Text that is too long for a Header',
filterFn: 'stringFilterFn',
filterFn: stringFilterFn,
size: 300,
cell: highlightFilterValue,
},
Expand All @@ -161,20 +169,20 @@ export default function Demo() {
{ accessorKey: 'cat', filterFn: catFilter },
{
accessorKey: 'fish',
filterFn: 'stringFilterFn',
filterFn: stringFilterFn,
},
],
},
{
accessorKey: 'city',
filterFn: 'stringFilterFn',
filterFn: stringFilterFn,
},
{ accessorKey: 'value', filterFn: 'numberFilterFn' },
{ accessorKey: 'value', filterFn: numberFilterFn },
{
accessorKey: 'date',
cell: (cell) =>
cell.getValue()?.toLocaleDateString(),
filterFn: 'dateFilterFn',
filterFn: dateFilterFn,
},
{
accessorKey: 'bool',
Expand All @@ -184,7 +192,7 @@ export default function Demo() {
}
return <Badge color="red">false</Badge>;
},
filterFn: 'booleanFilterFn',
filterFn: booleanFilterFn,
},
]}
/>
Expand Down
21 changes: 14 additions & 7 deletions docs/components/Documentation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ var data: Data[] = new Array(100).fill({}).map((i) => ({
`;

const grid_usage = `
import { DataGrid, highlightFilterValue } from 'mantine-data-grid';
import {
DataGrid,
highlightFilterValue,
stringFilterFn,
numberFilterFn,
dateFilterFn,
booleanFilterFn
} from 'mantine-data-grid';
function Demo() {
return (
Expand All @@ -61,7 +68,7 @@ function Demo() {
{
accessorKey: 'text',
header: 'Text that is too long for a Header',
filterFn: 'stringFilterFn',
filterFn: stringFilterFn,
size: 300,
cell: highlightFilterValue,
},
Expand All @@ -71,24 +78,24 @@ function Demo() {
{ accessorKey: 'cat', filterFn: catFilter },
{
accessorKey: 'fish',
filterFn: 'stringFilterFn',
filterFn: stringFilterFn,
},
],
},
{
accessorKey: 'city',
filterFn: 'stringFilterFn',
},
{ accessorKey: 'value', filterFn: 'numberFilterFn' },
{ accessorKey: 'value', filterFn: numberFilterFn },
{
accessorKey: 'date',
cell: (cell) =>
cell.getValue()?.toLocaleDateString(),
filterFn: 'dateFilterFn',
filterFn: dateFilterFn,
},
{
accessorKey: 'bool',
filterFn: 'booleanFilterFn',
filterFn: booleanFilterFn,
},
]}
/>
Expand Down Expand Up @@ -129,7 +136,7 @@ catFilter.element = function ({ filter, onFilterChange }) {
function Demo() {
return (
<DataGrid
data={/* data source */}
data={data}
columns={[
{
accessorKey: 'cat',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"scripts": {
"dev": "vite -c vite.config.docs.ts",
"build": "tsc && vite build",
"build": "vite build && tsc --emitDeclarationOnly",
"build:docs": "vite build -c vite.config.docs.ts"
},
"peerDependencies": {
Expand Down
25 changes: 3 additions & 22 deletions src/ColumnFilter/ColumnFilter.tsx → src/ColumnFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,7 @@ import { ActionIcon, Button, Group, Popover, Stack } from '@mantine/core';
import { Column, FilterFn, RowData } from '@tanstack/react-table';
import { ComponentType, useState } from 'react';
import { Check, Filter, X } from 'tabler-icons-react';
import { dataGridfilterFns } from '.';

export type DataGridFilterFn<TData extends RowData> = FilterFn<TData> & {
element: ComponentType<DataGridFilterProps>;
init(): any;
};
export function isDataGridFilter(val: any): val is DataGridFilterFn<any> {
return typeof val === 'function' && 'element' in val && 'init' in val;
}

export type DataGridFilterProps = {
filter: any;
onFilterChange(value: any): void;
};
import { isDataGridFilter } from './types';

export interface ColumnFilterProps {
column: Column<any>;
Expand All @@ -27,15 +14,9 @@ export const ColumnFilter = ({ column, className }: ColumnFilterProps) => {

const filterFn = column.columnDef.filterFn;

const filter = isDataGridFilter(filterFn)
? filterFn
: typeof filterFn === 'string' && filterFn in dataGridfilterFns
? dataGridfilterFns[filterFn as keyof typeof dataGridfilterFns]
: null;

if (filter === null) return null;
if (!isDataGridFilter(filterFn)) return null;

const { element: Element, init } = filter;
const { element: Element, init } = filterFn;

const open = () =>
setState({
Expand Down
37 changes: 0 additions & 37 deletions src/ColumnFilter/index.ts

This file was deleted.

Loading

0 comments on commit 9220ccc

Please sign in to comment.