diff --git a/docs/pages/Demo.tsx b/docs/pages/Demo.tsx index a228a72..e5e88af 100644 --- a/docs/pages/Demo.tsx +++ b/docs/pages/Demo.tsx @@ -136,7 +136,7 @@ export default function Demo() { debug - data={state.showEmpty ? [] : state.withPagination ? demoData : demoData.slice(0, 25)} + data={state.showEmpty ? [] : state.withPagination ? demoData : demoData.slice(0, 30)} horizontalSpacing={state.horizontalSpacing} verticalSpacing={state.verticalSpacing} fontSize={state.fontSize} diff --git a/docs/pages/Properties.tsx b/docs/pages/Properties.tsx index 2c267d2..3708884 100644 --- a/docs/pages/Properties.tsx +++ b/docs/pages/Properties.tsx @@ -1,4 +1,4 @@ -import { Stack, Title, Text } from '@mantine/core'; +import { Stack, Text, Title } from '@mantine/core'; import { PropertyTable } from '../components/PropertyTable'; import { See } from '../components/See'; @@ -109,11 +109,6 @@ export default function Properties() { type: 'boolean', description: 'Disable flex layout', }, - { - name: 'noEllipsis', - type: 'boolean', - description: 'Disable Text overflow ellipsis', - }, { name: 'debug', type: 'boolean', diff --git a/docs/pages/examples/AsyncExample.tsx b/docs/pages/examples/AsyncExample.tsx index e5ca11d..0f0311c 100644 --- a/docs/pages/examples/AsyncExample.tsx +++ b/docs/pages/examples/AsyncExample.tsx @@ -34,6 +34,7 @@ function fetchData(page: number, pageSize: number, search: string): Promise({ list: [], total: 0 }); + const [pagination, setPagination] = useState({ pageIndex: 0, pageSize: 10 }); const [searchValue, setSearchValue] = useState(''); const load: OnChangeCallback = async ({ pageIndex, pageSize }) => { @@ -41,20 +42,21 @@ export default function AsyncExample() { setLoading(true); const res = await fetchData(pageIndex, pageSize, searchValue); setData(res); + setPagination({ pageIndex, pageSize }); setLoading(false); }; const search: OnChangeCallback = async (val) => { console.log(`search`); setLoading(true); - const res = await fetchData(0, 10, val); + const res = await fetchData(0, pagination.pageSize, val); setData(res); setSearchValue(val); setLoading(false); }; useEffect(() => { - load({ pageIndex: 0, pageSize: 10 }); + load(pagination); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); @@ -67,6 +69,7 @@ export default function AsyncExample() { withPagination withGlobalFilter loading={loading} + state={{ pagination }} columns={[ { accessorKey: 'id', diff --git a/docs/pages/examples/EditableExample.tsx b/docs/pages/examples/EditableExample.tsx new file mode 100644 index 0000000..d74de75 --- /dev/null +++ b/docs/pages/examples/EditableExample.tsx @@ -0,0 +1,89 @@ +import { TextInput } from '@mantine/core'; +import { CellContext } from '@tanstack/react-table'; +import { useCallback, useEffect, useState } from 'react'; +import { booleanFilterFn, DataGrid, dateFilterFn, numberFilterFn, stringFilterFn } from '../../../src'; +import { Data, demoData } from '../../demoData'; + +export default function EditableExample() { + const [data, setData] = useState(demoData.slice(0, 25)); + + const handleChange = useCallback( + (index: number, key: keyof Data, value: string) => { + setData((state) => { + const next = [...state]; + next[index] = { + ...state[index], + [key]: value, + }; + return next; + }); + }, + [setData] + ); + + return ( + , + }, + { + 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()?.toLocaleDateString(), + filterFn: dateFilterFn, + }, + { + accessorKey: 'bool', + filterFn: booleanFilterFn, + }, + ]} + /> + ); +} + +// https://tanstack.com/table/v8/docs/examples/react/editable-data +function CellInput({ + getValue, + onChange, + row: { index }, + column: { id }, +}: CellContext & { onChange(rowIndex: number, columnId: string, value: unknown): void }) { + const initialValue = getValue(); + const [value, setValue] = useState(initialValue); + useEffect(() => { + setValue(initialValue); + }, [initialValue]); + + const hanldeBlur = () => { + onChange(index, id, value); + }; + + return setValue(e.target.value)} onBlur={hanldeBlur} />; +} diff --git a/docs/pages/examples/index.ts b/docs/pages/examples/index.ts index 6dbc851..16ce8e7 100644 --- a/docs/pages/examples/index.ts +++ b/docs/pages/examples/index.ts @@ -59,6 +59,10 @@ import CustomPaginationExample from './CustomPaginationExample'; // @ts-ignore import CustomPaginationExampleCode from './CustomPaginationExample.tsx?raw'; +import EditableExample from './EditableExample'; +// @ts-ignore +import EditableExampleCode from './EditableExample.tsx?raw'; + export type Example = { label: string; path: string; @@ -158,4 +162,10 @@ export const examples = { element: CustomPaginationExample, code: CustomPaginationExampleCode, }), + editable: ex({ + label: 'Editable Cell', + path: '/example/editable', + element: EditableExample, + code: EditableExampleCode, + }), }; diff --git a/package.json b/package.json index e5769cf..00b456c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mantine-data-grid", - "version": "0.0.21", + "version": "0.0.22", "homepage": "https://kuechlin.github.io/mantine-data-grid/", "repository": { "type": "git", diff --git a/src/ColumnFilter.tsx b/src/ColumnFilter.tsx index 41d5ac0..dc25fc3 100644 --- a/src/ColumnFilter.tsx +++ b/src/ColumnFilter.tsx @@ -67,8 +67,15 @@ export const ColumnFilter = ({ column, className, color }: ColumnFilterProps) => -