material-react-table 1.0.0-beta.10 → 1.0.0-beta.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -130,6 +130,10 @@ export declare const MRT_TableRoot: <TData extends Record<string, any> = {}>(pro
130
130
  table: MRT_TableInstance<TData>;
131
131
  column: MRT_Column<TData>;
132
132
  }) => import("@mui/material").IconButtonProps<"button", {}>) | undefined;
133
+ muiTableHeadCellFilterCheckboxProps?: import("@mui/material").CheckboxProps | (({ column, table, }: {
134
+ column: MRT_Column<TData>;
135
+ table: MRT_TableInstance<TData>;
136
+ }) => import("@mui/material").CheckboxProps) | undefined;
133
137
  muiTableHeadCellFilterTextFieldProps?: import("@mui/material").TextFieldProps | (({ table, column, rangeFilterIndex, }: {
134
138
  table: MRT_TableInstance<TData>;
135
139
  column: MRT_Column<TData>;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { MutableRefObject, Dispatch, SetStateAction, ReactNode } from 'react';
2
- import { ButtonProps, TextFieldProps, TableCellProps, IconButtonProps, ToolbarProps, LinearProgressProps, CheckboxProps, SkeletonProps, TableBodyProps, TableRowProps, TableContainerProps, TableFooterProps, TableHeadProps, TablePaginationProps, PaperProps, TableProps, ChipProps, AlertProps } from '@mui/material';
2
+ import { ButtonProps, TextFieldProps, TableCellProps, IconButtonProps, CheckboxProps, ToolbarProps, LinearProgressProps, SkeletonProps, TableBodyProps, TableRowProps, TableContainerProps, TableFooterProps, TableHeadProps, TablePaginationProps, PaperProps, TableProps, ChipProps, AlertProps } from '@mui/material';
3
3
  import { Row, Table, TableState, ColumnDef, DeepKeys, Column, Header, HeaderGroup, Cell, SortingFn, FilterFn, TableOptions, OnChangeFn } from '@tanstack/react-table';
4
4
  import { Options, VirtualItem } from 'react-virtual';
5
5
  import * as _tanstack_table_core from '@tanstack/table-core';
@@ -296,9 +296,10 @@ declare type MRT_ColumnDef<TData extends Record<string, any> = {}> = Omit<Column
296
296
  row: MRT_Row<TData>;
297
297
  table: MRT_TableInstance<TData>;
298
298
  }) => ReactNode;
299
- Filter?: ({ column, header, table, }: {
299
+ Filter?: ({ column, header, rangeFilterIndex, table, }: {
300
300
  column: MRT_Column<TData>;
301
301
  header: MRT_Header<TData>;
302
+ rangeFilterIndex?: number;
302
303
  table: MRT_TableInstance<TData>;
303
304
  }) => ReactNode;
304
305
  Footer?: ReactNode | (({ column, footer, table, }: {
@@ -355,7 +356,7 @@ declare type MRT_ColumnDef<TData extends Record<string, any> = {}> = Omit<Column
355
356
  text: string;
356
357
  value: any;
357
358
  })[];
358
- filterVariant?: 'text' | 'select' | 'multi-select' | 'range';
359
+ filterVariant?: 'text' | 'select' | 'multi-select' | 'range' | 'checkbox';
359
360
  /**
360
361
  * footer must be a string. If you want custom JSX to render the footer, you can also specify a `Footer` option. (Capital F)
361
362
  */
@@ -386,9 +387,11 @@ declare type MRT_ColumnDef<TData extends Record<string, any> = {}> = Omit<Column
386
387
  row: MRT_Row<TData>;
387
388
  table: MRT_TableInstance<TData>;
388
389
  }) => TextFieldProps);
389
- muiTableBodyCellProps?: TableCellProps | (({ cell, table, }: {
390
- table: MRT_TableInstance<TData>;
390
+ muiTableBodyCellProps?: TableCellProps | (({ cell, column, row, table, }: {
391
391
  cell: MRT_Cell<TData>;
392
+ column: MRT_Column<TData>;
393
+ row: MRT_Row<TData>;
394
+ table: MRT_TableInstance<TData>;
392
395
  }) => TableCellProps);
393
396
  muiTableFooterCellProps?: TableCellProps | (({ table, column, }: {
394
397
  table: MRT_TableInstance<TData>;
@@ -402,6 +405,10 @@ declare type MRT_ColumnDef<TData extends Record<string, any> = {}> = Omit<Column
402
405
  table: MRT_TableInstance<TData>;
403
406
  column: MRT_Column<TData>;
404
407
  }) => IconButtonProps);
408
+ muiTableHeadCellFilterCheckboxProps?: CheckboxProps | (({ column, table, }: {
409
+ column: MRT_Column<TData>;
410
+ table: MRT_TableInstance<TData>;
411
+ }) => CheckboxProps);
405
412
  muiTableHeadCellFilterTextFieldProps?: TextFieldProps | (({ table, column, rangeFilterIndex, }: {
406
413
  table: MRT_TableInstance<TData>;
407
414
  column: MRT_Column<TData>;
@@ -598,6 +605,10 @@ declare type MaterialReactTableProps<TData extends Record<string, any> = {}> = O
598
605
  table: MRT_TableInstance<TData>;
599
606
  column: MRT_Column<TData>;
600
607
  }) => IconButtonProps);
608
+ muiTableHeadCellFilterCheckboxProps?: CheckboxProps | (({ column, table, }: {
609
+ column: MRT_Column<TData>;
610
+ table: MRT_TableInstance<TData>;
611
+ }) => CheckboxProps);
601
612
  muiTableHeadCellFilterTextFieldProps?: TextFieldProps | (({ table, column, rangeFilterIndex, }: {
602
613
  table: MRT_TableInstance<TData>;
603
614
  column: MRT_Column<TData>;
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.0.0-beta.10",
2
+ "version": "1.0.0-beta.13",
3
3
  "license": "MIT",
4
4
  "name": "material-react-table",
5
5
  "description": "A fully featured Material UI V5 implementation of TanStack React Table V8, written from the ground up in TypeScript.",
@@ -275,10 +275,12 @@ export type MRT_ColumnDef<TData extends Record<string, any> = {}> = Omit<
275
275
  Filter?: ({
276
276
  column,
277
277
  header,
278
+ rangeFilterIndex,
278
279
  table,
279
280
  }: {
280
281
  column: MRT_Column<TData>;
281
282
  header: MRT_Header<TData>;
283
+ rangeFilterIndex?: number;
282
284
  table: MRT_TableInstance<TData>;
283
285
  }) => ReactNode;
284
286
  Footer?:
@@ -349,7 +351,7 @@ export type MRT_ColumnDef<TData extends Record<string, any> = {}> = Omit<
349
351
  enableEditing?: boolean;
350
352
  filterFn?: MRT_FilterFn<TData>;
351
353
  filterSelectOptions?: (string | { text: string; value: any })[];
352
- filterVariant?: 'text' | 'select' | 'multi-select' | 'range';
354
+ filterVariant?: 'text' | 'select' | 'multi-select' | 'range' | 'checkbox';
353
355
  /**
354
356
  * footer must be a string. If you want custom JSX to render the footer, you can also specify a `Footer` option. (Capital F)
355
357
  */
@@ -398,10 +400,14 @@ export type MRT_ColumnDef<TData extends Record<string, any> = {}> = Omit<
398
400
  | TableCellProps
399
401
  | (({
400
402
  cell,
403
+ column,
404
+ row,
401
405
  table,
402
406
  }: {
403
- table: MRT_TableInstance<TData>;
404
407
  cell: MRT_Cell<TData>;
408
+ column: MRT_Column<TData>;
409
+ row: MRT_Row<TData>;
410
+ table: MRT_TableInstance<TData>;
405
411
  }) => TableCellProps);
406
412
  muiTableFooterCellProps?:
407
413
  | TableCellProps
@@ -430,6 +436,15 @@ export type MRT_ColumnDef<TData extends Record<string, any> = {}> = Omit<
430
436
  table: MRT_TableInstance<TData>;
431
437
  column: MRT_Column<TData>;
432
438
  }) => IconButtonProps);
439
+ muiTableHeadCellFilterCheckboxProps?:
440
+ | CheckboxProps
441
+ | (({
442
+ column,
443
+ table,
444
+ }: {
445
+ column: MRT_Column<TData>;
446
+ table: MRT_TableInstance<TData>;
447
+ }) => CheckboxProps);
433
448
  muiTableHeadCellFilterTextFieldProps?:
434
449
  | TextFieldProps
435
450
  | (({
@@ -783,6 +798,15 @@ export type MaterialReactTableProps<TData extends Record<string, any> = {}> =
783
798
  table: MRT_TableInstance<TData>;
784
799
  column: MRT_Column<TData>;
785
800
  }) => IconButtonProps);
801
+ muiTableHeadCellFilterCheckboxProps?:
802
+ | CheckboxProps
803
+ | (({
804
+ column,
805
+ table,
806
+ }: {
807
+ column: MRT_Column<TData>;
808
+ table: MRT_TableInstance<TData>;
809
+ }) => CheckboxProps);
786
810
  muiTableHeadCellFilterTextFieldProps?:
787
811
  | TextFieldProps
788
812
  | (({
@@ -68,7 +68,7 @@ export const MRT_TableBodyCell: FC<Props> = ({
68
68
 
69
69
  const mcTableCellBodyProps =
70
70
  columnDef.muiTableBodyCellProps instanceof Function
71
- ? columnDef.muiTableBodyCellProps({ cell, table })
71
+ ? columnDef.muiTableBodyCellProps({ cell, column, row, table })
72
72
  : columnDef.muiTableBodyCellProps;
73
73
 
74
74
  const tableCellProps = {
@@ -164,8 +164,12 @@ export const getDefaultColumnFilterFn = <
164
164
  columnDef: MRT_ColumnDef<TData>,
165
165
  ): MRT_FilterOption => {
166
166
  if (columnDef.filterVariant === 'multi-select') return 'arrIncludesSome';
167
- if (columnDef.filterVariant === 'select') return 'equals';
168
167
  if (columnDef.filterVariant === 'range') return 'betweenInclusive';
168
+ if (
169
+ columnDef.filterVariant === 'select' ||
170
+ columnDef.filterVariant === 'checkbox'
171
+ )
172
+ return 'equals';
169
173
  return 'fuzzy';
170
174
  };
171
175
 
@@ -3,6 +3,7 @@ import { Collapse } from '@mui/material';
3
3
  import { MRT_FilterRangeFields } from '../inputs/MRT_FilterRangeFields';
4
4
  import { MRT_FilterTextField } from '../inputs/MRT_FilterTextField';
5
5
  import { MRT_Header, MRT_TableInstance } from '..';
6
+ import { MRT_FilterCheckbox } from '../inputs/MRT_FilterCheckbox';
6
7
 
7
8
  interface Props {
8
9
  header: MRT_Header;
@@ -20,9 +21,12 @@ export const MRT_TableHeadCellFilterContainer: FC<Props> = ({
20
21
 
21
22
  return (
22
23
  <Collapse in={showColumnFilters} mountOnEnter unmountOnExit>
23
- {['between', 'betweenInclusive', 'inNumberRange'].includes(
24
- columnDef._filterFn,
25
- ) ? (
24
+ {columnDef.filterVariant === 'checkbox' ? (
25
+ <MRT_FilterCheckbox column={column} table={table} />
26
+ ) : columnDef.filterVariant === 'range' ||
27
+ ['between', 'betweenInclusive', 'inNumberRange'].includes(
28
+ columnDef._filterFn,
29
+ ) ? (
26
30
  <MRT_FilterRangeFields header={header} table={table} />
27
31
  ) : (
28
32
  <MRT_FilterTextField header={header} table={table} />
@@ -0,0 +1,96 @@
1
+ import React, { FC } from 'react';
2
+ import {
3
+ Checkbox,
4
+ CheckboxProps,
5
+ FormControlLabel,
6
+ Tooltip,
7
+ } from '@mui/material';
8
+ import type { MRT_Column, MRT_TableInstance } from '..';
9
+
10
+ interface Props {
11
+ column: MRT_Column;
12
+ table: MRT_TableInstance;
13
+ }
14
+
15
+ export const MRT_FilterCheckbox: FC<Props> = ({ column, table }) => {
16
+ const {
17
+ getState,
18
+ options: { localization, muiTableHeadCellFilterCheckboxProps },
19
+ } = table;
20
+ const { density } = getState();
21
+ const { columnDef } = column;
22
+
23
+ const mTableHeadCellFilterCheckboxProps =
24
+ muiTableHeadCellFilterCheckboxProps instanceof Function
25
+ ? muiTableHeadCellFilterCheckboxProps({
26
+ column,
27
+ table,
28
+ })
29
+ : muiTableHeadCellFilterCheckboxProps;
30
+
31
+ const mcTableHeadCellFilterCheckboxProps =
32
+ columnDef.muiTableHeadCellFilterCheckboxProps instanceof Function
33
+ ? columnDef.muiTableHeadCellFilterCheckboxProps({
34
+ column,
35
+ table,
36
+ })
37
+ : columnDef.muiTableHeadCellFilterCheckboxProps;
38
+
39
+ const checkboxProps = {
40
+ ...mTableHeadCellFilterCheckboxProps,
41
+ ...mcTableHeadCellFilterCheckboxProps,
42
+ } as CheckboxProps;
43
+
44
+ const filterLabel = localization.filterByColumn?.replace(
45
+ '{column}',
46
+ columnDef.header,
47
+ );
48
+
49
+ return (
50
+ <Tooltip
51
+ arrow
52
+ enterDelay={1000}
53
+ enterNextDelay={1000}
54
+ title={checkboxProps?.title ?? filterLabel}
55
+ >
56
+ <FormControlLabel
57
+ control={
58
+ <Checkbox
59
+ checked={column.getFilterValue() === 'true'}
60
+ indeterminate={column.getFilterValue() === undefined}
61
+ color={
62
+ column.getFilterValue() === undefined ? 'default' : 'primary'
63
+ }
64
+ size={density === 'compact' ? 'small' : 'medium'}
65
+ {...checkboxProps}
66
+ onClick={(e) => {
67
+ e.stopPropagation();
68
+ checkboxProps?.onClick?.(e);
69
+ }}
70
+ onChange={(e, checked) => {
71
+ column.setFilterValue(
72
+ column.getFilterValue() === undefined
73
+ ? 'true'
74
+ : column.getFilterValue() === 'true'
75
+ ? 'false'
76
+ : undefined,
77
+ );
78
+ checkboxProps?.onChange?.(e, checked);
79
+ }}
80
+ sx={(theme) => ({
81
+ height: '2.5rem',
82
+ width: '2.5rem',
83
+ ...(checkboxProps?.sx instanceof Function
84
+ ? checkboxProps.sx(theme)
85
+ : (checkboxProps?.sx as any)),
86
+ })}
87
+ />
88
+ }
89
+ disableTypography
90
+ label={checkboxProps.title ?? filterLabel}
91
+ sx={{ color: 'text.secondary', mt: '-4px', fontWeight: 'normal' }}
92
+ title={undefined}
93
+ />
94
+ </Tooltip>
95
+ );
96
+ };
@@ -3,6 +3,7 @@ import React, {
3
3
  FC,
4
4
  MouseEvent,
5
5
  useCallback,
6
+ useEffect,
6
7
  useState,
7
8
  } from 'react';
8
9
  import {
@@ -117,10 +118,12 @@ export const MRT_FilterTextField: FC<Props> = ({
117
118
  (event: ChangeEvent<HTMLInputElement>) => {
118
119
  let value =
119
120
  textFieldProps.type === 'date'
120
- ? new Date(event.target.value)
121
+ ? event.target.valueAsDate
122
+ : textFieldProps.type === 'number'
123
+ ? event.target.valueAsNumber
121
124
  : event.target.value;
122
125
  if (isRangeFilter) {
123
- column.setFilterValue((old: [string, string | Date]) => {
126
+ column.setFilterValue((old: Array<string | Date | number | null>) => {
124
127
  const newFilterValues = old ?? ['', ''];
125
128
  newFilterValues[rangeFilterIndex as number] = value;
126
129
  return newFilterValues;
@@ -169,8 +172,16 @@ export const MRT_FilterTextField: FC<Props> = ({
169
172
  setAnchorEl(event.currentTarget);
170
173
  };
171
174
 
175
+ useEffect(() => {
176
+ if (column.getFilterValue() === undefined) {
177
+ handleClear();
178
+ }
179
+ }, [column.getFilterValue()]);
180
+
172
181
  if (columnDef.Filter) {
173
- return <>{columnDef.Filter?.({ column, header, table })}</>;
182
+ return (
183
+ <>{columnDef.Filter?.({ column, header, rangeFilterIndex, table })}</>
184
+ );
174
185
  }
175
186
 
176
187
  return (
@@ -203,7 +214,7 @@ export const MRT_FilterTextField: FC<Props> = ({
203
214
  }
204
215
  FormHelperTextProps={{
205
216
  sx: {
206
- fontSize: '0.6rem',
217
+ fontSize: '0.75rem',
207
218
  lineHeight: '0.8rem',
208
219
  whiteSpace: 'nowrap',
209
220
  },
@@ -308,14 +319,13 @@ export const MRT_FilterTextField: FC<Props> = ({
308
319
  }}
309
320
  sx={(theme) => ({
310
321
  p: 0,
311
- minWidth:
312
- columnDef.filterVariant === 'range'
313
- ? '90px'
314
- : !filterChipLabel
315
- ? '120px'
316
- : 'auto',
322
+ minWidth: isRangeFilter
323
+ ? '100px'
324
+ : !filterChipLabel
325
+ ? '120px'
326
+ : 'auto',
317
327
  width: '100%',
318
- '& .MuiSelect-icon': {
328
+ '& .MuiSelect-icon': {
319
329
  mr: '1.5rem',
320
330
  },
321
331
  ...(textFieldProps?.sx instanceof Function
@@ -1,4 +1,10 @@
1
- import React, { ChangeEvent, MouseEvent, useCallback, useState } from 'react';
1
+ import React, {
2
+ ChangeEvent,
3
+ MouseEvent,
4
+ useCallback,
5
+ useEffect,
6
+ useState,
7
+ } from 'react';
2
8
  import {
3
9
  Collapse,
4
10
  debounce,
@@ -32,6 +38,11 @@ export const MRT_GlobalFilterTextField = <
32
38
  } = table;
33
39
  const { globalFilter, showGlobalFilter } = getState();
34
40
 
41
+ const textFieldProps =
42
+ muiSearchTextFieldProps instanceof Function
43
+ ? muiSearchTextFieldProps({ table })
44
+ : muiSearchTextFieldProps;
45
+
35
46
  const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
36
47
  const [searchValue, setSearchValue] = useState(globalFilter ?? '');
37
48
 
@@ -56,10 +67,11 @@ export const MRT_GlobalFilterTextField = <
56
67
  setGlobalFilter(undefined);
57
68
  };
58
69
 
59
- const textFieldProps =
60
- muiSearchTextFieldProps instanceof Function
61
- ? muiSearchTextFieldProps({ table })
62
- : muiSearchTextFieldProps;
70
+ useEffect(() => {
71
+ if (globalFilter === undefined) {
72
+ handleClear();
73
+ }
74
+ }, [globalFilter]);
63
75
 
64
76
  return (
65
77
  <Collapse