|
| 1 | +<p align="center"> <h1 align="center">material-react-table-v1-autocomplete</h1></p> |
| 2 | + |
| 3 | +<p align="center"> |
| 4 | +<a href="https://github.com/felipehimself" rel="nofollow"><img src="https://img.shields.io/badge/created%20by-@felipehimself-4BBAAB.svg" alt="Created by felipehimself"></a> |
| 5 | +<a href="https://opensource.org/licenses/MIT" rel="nofollow"><img src="https://img.shields.io/github/license/felipehimself/clean-accents" alt="License"></a> |
| 6 | +</p> |
| 7 | + |
| 8 | +## Introduction |
| 9 | + |
| 10 | +A custom multi-select filter component designed for [Material React Table Version 1](https://v1.material-react-table.com/) and [Material UI Version 5](https://v5.mui.com/material-ui/getting-started/installation/) . |
| 11 | + |
| 12 | +## Motivation |
| 13 | + |
| 14 | +Material React Table is awesome (it really is!), but version 1 lacks a built-in multi-select filter. If you're stuck using v1—perhaps due to migration limitations—you might still need this feature. I faced this exact situation, so I created a plug-and-play component that adds multi-select filtering with minimal setup. |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +```bash |
| 21 | +npm material-react-table-v1-autocomplete |
| 22 | +# or |
| 23 | +yarn material-react-table-v1-autocomplete |
| 24 | +``` |
| 25 | + |
| 26 | +## Usage |
| 27 | + |
| 28 | +Define your columns as usual, but pass a custom `Filter` using the `AutocompleteCheckbox` component and its companion `autocompleteFilterFn` for exact-match filtering. |
| 29 | + |
| 30 | +``` |
| 31 | +import MaterialReactTable from 'material-react-table'; |
| 32 | +import { AutocompleteCheckbox, autocompleteFilterFn } from 'material-react-table-v1-autocomplete'; |
| 33 | +
|
| 34 | +const App = () => { |
| 35 | + const data: TData[] = []; // Your data here or coming from anywhere |
| 36 | +
|
| 37 | + const columns = useMemo<MRT_ColumnDef<TData>[]>(() => [ |
| 38 | + { |
| 39 | + header: 'First Name', |
| 40 | + accessorKey: 'firstName', |
| 41 | + filterFn: autocompleteFilterFn, |
| 42 | + Filter: ({ header }) => ( |
| 43 | + <AutocompleteCheckbox |
| 44 | + key="firstName" |
| 45 | + header={header} |
| 46 | + options={[...new Set(data.map((i) => i.firstName))]} |
| 47 | + /> |
| 48 | + ), |
| 49 | + }, |
| 50 | + { |
| 51 | + header: 'Automatic', |
| 52 | + accessorKey: 'automatic', |
| 53 | + accessorFn: (row) => (row.automatic ? 'Yes' : 'No'), |
| 54 | + filterFn: autocompleteFilterFn, |
| 55 | + Filter: ({ header }) => ( |
| 56 | + <AutocompleteCheckbox |
| 57 | + key="automatic" |
| 58 | + header={header} |
| 59 | + options={[...new Set(data.map((i) => (i.automatic ? 'Yes' : 'No')))]} |
| 60 | + /> |
| 61 | + ), |
| 62 | + }, |
| 63 | + { |
| 64 | + header: 'Release Date', |
| 65 | + accessorKey: 'releaseDate', |
| 66 | + accessorFn: (row) => (row.releaseDate ? formatDate(row.releaseDate) : ''), |
| 67 | + filterFn: autocompleteFilterFn, |
| 68 | + Filter: ({ header }) => ( |
| 69 | + <AutocompleteCheckbox |
| 70 | + key="releaseDate" |
| 71 | + header={header} |
| 72 | + options={[...new Set(data.map((i) => formatDate(i.releaseDate)))]} |
| 73 | + /> |
| 74 | + ), |
| 75 | + }, |
| 76 | + { |
| 77 | + header: 'State', |
| 78 | + accessorKey: 'state', // Uses the default Material React Table filtering |
| 79 | + }, |
| 80 | + ], [data]); |
| 81 | +
|
| 82 | + return ( |
| 83 | + <MaterialReactTable |
| 84 | + key="mrt-example" |
| 85 | + columns={columns} |
| 86 | + data={data} |
| 87 | + /> |
| 88 | + ); |
| 89 | +}; |
| 90 | +``` |
| 91 | + |
| 92 | +## ⚠️Important Notes |
| 93 | + |
| 94 | +* `accessorKey` is required. It’s how the component links to the correct column. |
| 95 | +* If you're using `accessorFn` to transform data (e.g., converting booleans to "Yes"/"No"), you must apply the same logic when defining the filter options. |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +## Accepted Props |
| 100 | + |
| 101 | +### ✅ MUI |
| 102 | + |
| 103 | +Since Material React Table is built on Material UI, the internal elements (like `TextField` and `Checkbox`) accept the same MUI props. Avoid overriding props such as `onChange` or `onClick`, as these are handled internally by the component. |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +### ✨ Custom Props |
| 108 | + |
| 109 | +Other props that the component accepts can be seen in `TAutocompleteCheckboxProps` which is exported by the component. Some of them are `placeholder`, `filterButtonText`, `cleanButtonText` and others. |
| 110 | + |
| 111 | +## 🧼 Cleaning all Filters |
| 112 | + |
| 113 | +Material React Table v1 does not offer a built-in method to clear all filters. You’ll need to manually reset filters and also clear the internal state of `AutocompleteCheckbox` components by calling the `dispatchCleanAutocomplete()` function provided by this package. |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | +``` |
| 118 | +import { dispatchCleanAutocomplete } from 'material-react-table-v1-autocomplete'; |
| 119 | +import { Button } from "@mui/material"; |
| 120 | +
|
| 121 | +const App = () => { |
| 122 | + const tableInstanceRef = useRef<MRT_TableInstance<TData>>(null); |
| 123 | +
|
| 124 | + const handleCleanFilters = () => { |
| 125 | + tableInstanceRef.current?.resetColumnFilters(); |
| 126 | + dispatchCleanAutocomplete(); |
| 127 | + }; |
| 128 | +
|
| 129 | + return ( |
| 130 | + <> |
| 131 | + <Button onClick={handleCleanFilters}>Clean All Filters</Button> |
| 132 | + <MaterialReactTable |
| 133 | + key="mrt-example" |
| 134 | + tableInstanceRef={tableInstanceRef} |
| 135 | + columns={columns} |
| 136 | + data={data} |
| 137 | + /> |
| 138 | + </> |
| 139 | + ); |
| 140 | +}; |
| 141 | +``` |
| 142 | + |
| 143 | + |
| 144 | +## ⚡Performance Tips |
| 145 | + |
| 146 | +The `AutocompleteCheckbox` component is memoized using React.memo. Although it's not mandatory, to benefit from this, make sure to wrap the `options` prop in a useMemo to prevent unnecessary re-renders. |
| 147 | + |
| 148 | + |
| 149 | +## ♻️ Reusability |
| 150 | + |
| 151 | +Some props are commonly reused every time you use the `AutocompleteCheckbox` component. These include: |
| 152 | + |
| 153 | +* placeHolder — defaults to an empty string |
| 154 | +* filterButtonText — defaults to "Filter" |
| 155 | +* cleanButtonText — defaults to "Clear" |
| 156 | +* noOptionsText — defaults to "No options" |
| 157 | +* sortAscending — enables ascending sort for the dropdown (default behavior) |
| 158 | +* sortDescending — enables descending sort for the dropdown |
| 159 | + |
| 160 | +<br/> |
| 161 | + |
| 162 | +> ⚠️ **Important:** `sortAscending` and `sortDescending` are mutually exclusive. If neither is passed, it defaults to ascending. Passing **both** will result in a **TypeScript error**, so only pass the one you need. |
| 163 | +
|
| 164 | + |
| 165 | +## 🧱 Example: Creating a Reusable Wrapper Component |
| 166 | + |
| 167 | +You can wrap `AutocompleteCheckbox` in a custom component to centralize common props and avoid repetition: |
| 168 | +> 💡 **Tip:** Wrapping the component like this helps you localize text and avoid repetition across column definitions. |
| 169 | +
|
| 170 | +``` |
| 171 | +import { AutocompleteCheckbox, TAutocompleteCheckboxProps } from 'material-react-table-v1-autocomplete'; |
| 172 | +
|
| 173 | +const MyBaseAutocomplete = (props: TAutocompleteCheckboxProps<TData>) => { |
| 174 | + const { sortAscending, ...rest } = props; |
| 175 | +
|
| 176 | + // We destructure `sortAscending` to prevent a TypeScript conflict in case both sort props are accidentally included |
| 177 | +
|
| 178 | + return ( |
| 179 | + <AutocompleteCheckbox |
| 180 | + {...rest} |
| 181 | + filterButtonText="Filtrar" |
| 182 | + cleanButtonText="Limpar" |
| 183 | + noOptionsText="Sem opções" |
| 184 | + /> |
| 185 | + ); |
| 186 | +}; |
| 187 | +
|
| 188 | +``` |
0 commit comments