Skip to content

Commit

Permalink
progress on cell action docs, but not shipping yet. I need sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinVandy committed Jan 30, 2024
1 parent b77c69c commit 3a6fb00
Show file tree
Hide file tree
Showing 17 changed files with 383 additions and 192 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Expand Up @@ -17,7 +17,7 @@ This project uses PNPM and a TurboRepo with 2 projects.

### 1. Fork and Clone the project

Create your own fork, clone, and then make a feature/bugfix branch off of `main`. Branch name does not really matter.
Create your own fork, clone, and then make a feature/bugfix branch off of `v2`. Branch name does not really matter.

### 2. Install Dependencies

Expand Down Expand Up @@ -51,4 +51,4 @@ The Docs site will open on `port 3000` by default.
pnpm lib:build
```

> Note: After building the library, if you are running the docs site locally, it will use the compiled output of the dist folder. This can be annoying if you are trying to test changes to the library in the docs site itself. Just delete the `/dist` folder to test lib changes in the docs site.
> Note: After building the library, if you are running the docs site locally, it will use the compiled output of the dist folder. This can be annoying if you are trying to test changes to the library in the docs site itself. Just delete the `/dist` folder and restart the docs app to test lib changes in the docs site.
22 changes: 0 additions & 22 deletions apps/material-react-table-docs/components/navigation/Footer.tsx
Expand Up @@ -54,28 +54,6 @@ export const Footer = () => {
>
Suggest an Edit for this page on GitHub
</Button>
<Typography
sx={{
justifyContent: 'center',
display: 'flex',
alignContent: 'center',
}}
variant="subtitle2"
>
You can help make these docs better!{' '}
<a
aria-label="Learn how to contribute"
href="http://makeapullrequest.com"
target="_blank"
rel="noopener"
style={{ marginLeft: '0.5rem' }}
>
<img
alt="PRs are Welcome"
src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square"
/>
</a>
</Typography>
<Typography
variant="subtitle1"
sx={{
Expand Down
Expand Up @@ -383,6 +383,10 @@ export const routes: Array<RouteItem> = [
href: '/docs/guides/click-to-copy',
label: 'Click to Copy',
},
{
href: '/docs/guides/cell-actions',
label: 'Cell Actions (Context Menu)',
},
{
href: '/docs/guides/column-actions',
label: 'Column Actions (Menu)',
Expand Down
Expand Up @@ -162,9 +162,9 @@ export const columnOptions: ColumnOption[] = [
description: 'Enable the click to copy feature for this column.',
link: '/docs/guides/click-to-copy#enable-click-to-copy-per-column',
linkText: 'MRT Click to Copy Docs',
source: 'MRT',
required: false,
type: 'boolean',
source: 'MRT',
type: "boolean | 'context-menu' | ((cell: MRT_Cell<TData>) => 'context-menu' | boolean)",
},
{
columnOption: 'enableColumnActions',
Expand Down Expand Up @@ -610,6 +610,16 @@ export const columnOptions: ColumnOption[] = [
required: false,
type: 'false | 1 | -1',
},
{
columnOption: 'renderCellActionMenuItems',
defaultValue: '',
description: '',
link: '',
linkText: '',
required: false,
source: '',
type: '{ cell, closeMenu, column, internalMenuItems, row, staticColumnIndex, staticRowIndex, table }) => ReactNode[]',
},
{
columnOption: 'renderColumnActionsMenuItems',
defaultValue: '',
Expand Down
Expand Up @@ -245,7 +245,7 @@ export const tableOptions: TableOption[] = [
linkText: 'MRT Click to Copy Docs',
required: false,
source: 'MRT',
type: 'boolean',
type: "boolean | 'context-menu' | ((cell: MRT_Cell<TData>) => 'context-menu' | boolean)",
},
{
tableOption: 'enableColumnActions',
Expand Down Expand Up @@ -501,6 +501,16 @@ export const tableOptions: TableOption[] = [
source: '',
type: 'boolean',
},
{
tableOption: 'enableCellActions',
defaultValue: '',
description: '',
link: '',
linkText: '',
required: false,
source: '',
type: '((cell: MRT_Cell<TData>) => boolean) | boolean',
},
{
tableOption: 'enableRowActions',
defaultValue: '',
Expand Down Expand Up @@ -1899,6 +1909,16 @@ export const tableOptions: TableOption[] = [
source: '',
type: 'ReactNode | ({ table }) => ReactNode',
},
{
tableOption: 'renderCellActionMenuItems',
defaultValue: '',
description: '',
link: '',
linkText: '',
required: false,
source: '',
type: '{ cell, closeMenu, column, internalMenuItems, row, staticColumnIndex, staticRowIndex, table }) => ReactNode[]',
},
{
tableOption: 'renderColumnActionsMenuItems',
defaultValue: '',
Expand Down
Expand Up @@ -166,9 +166,11 @@ const Example = () => {
data: fetchedUsers,
createDisplayMode: 'row', // ('modal', and 'custom' are also available)
editDisplayMode: 'cell', // ('modal', 'row', 'table', and 'custom' are also available)
enableCellActions: true,
enableClickToCopy: 'context-menu',
enableColumnPinning: true,
enableEditing: true,
enableRowActions: true,
positionActionsColumn: 'last',
getRowId: (row) => row.id,
muiToolbarAlertBannerProps: isLoadingUsersError
? {
Expand Down
Expand Up @@ -175,9 +175,11 @@ const Example = () => {
data: fetchedUsers,
createDisplayMode: 'row', // ('modal', and 'custom' are also available)
editDisplayMode: 'cell', // ('modal', 'row', 'table', and 'custom' are also available)
enableCellActions: true,
enableClickToCopy: 'context-menu',
enableColumnPinning: true,
enableEditing: true,
enableRowActions: true,
positionActionsColumn: 'last',
getRowId: (row) => row.id,
muiToolbarAlertBannerProps: isLoadingUsersError
? {
Expand Down Expand Up @@ -235,6 +237,11 @@ const Example = () => {
Create New User
</Button>
),
initialState: {
columnPinning: {
right: ['mrt-row-actions'],
},
},
state: {
isLoading: isLoadingUsers,
isSaving: isCreatingUser || isUpdatingUsers || isDeletingUser,
Expand Down
8 changes: 4 additions & 4 deletions apps/material-react-table-docs/package.json
Expand Up @@ -43,12 +43,12 @@
"zod": "^3.22.4"
},
"devDependencies": {
"@tanstack/eslint-plugin-query": "^5.17.20",
"@types/node": "^20.11.7",
"@tanstack/eslint-plugin-query": "^5.17.22",
"@types/node": "^20.11.10",
"@types/react": "^18.2.48",
"@types/react-dom": "^18.2.18",
"@typescript-eslint/eslint-plugin": "^6.19.1",
"@typescript-eslint/parser": "^6.19.1",
"@typescript-eslint/eslint-plugin": "^6.20.0",
"@typescript-eslint/parser": "^6.20.0",
"eslint": "8.56.0",
"eslint-config-next": "14.1.0",
"next-plausible": "^3.12.0",
Expand Down
156 changes: 156 additions & 0 deletions apps/material-react-table-docs/pages/docs/guides/cell-actions.mdx
@@ -0,0 +1,156 @@
import Head from 'next/head';
import TableOptionsTable from '../../../components/prop-tables/TableOptionsTable';
import ColumnOptionsTable from '../../../components/prop-tables/ColumnOptionsTable';

<Head>
<title>{'Cell Actions Guide - Material React Table V2 Docs'}</title>
<meta
name="description"
content="How to add cell action context menus to each cell in Material React Table"
/>
</Head>

## Cell Actions Feature Guide

> New in v2.10.0
Material React Table provides you an easy shell to render a context menu for when a table cell/row is right clicked or otherwise activated. This is useful for providing additional actions that can be performed on a cell or row.

### Relevant Table Options

<TableOptionsTable
onlyOptions={
new Set([
'enableCellActions',
'enableClickToCopy',
'renderCellActionMenuItems',
])
}
/>

### Relevant Column Options

<ColumnOptionsTable
onlyOptions={
new Set([
'enableCellActions',
'enableClickToCopy',
'renderCellActionMenuItems',
])
}
/>

### Enable Cell Actions

To enable cell actions, you need to set the `enableCellActions` option to `true` for the cells that you want to have access to the context menu. This can be done at the table level or at the column level, and accepts a boolean or a function that returns a boolean.

```jsx
const table = useMaterialReactTable({
columns,
data,
enableCellActions: true,
//or
enableCellActions: (cell) => (cell.row.original.someCondition ? true : false),
});
```

### Render Cell Action Menu Items

The cell actions context menu will only appear if there are items to display. You can provide the `renderCellActionMenuItems` table option or column option to render the appropriate items in the context menu for each cell.

MRT also provides a `MRT_ActionMenuItem` component that you can use to render the menu items. This just a wrapper for the MUI MenuItem component that also provides consistent CSS for styling the icons, spacing, and optional sub-menu items. Use it if you want to have a consistent look and feel with all of the other built-in MRT Menus.

```jsx
const table = useMaterialReactTable({
columns,
data,
enableCellActions: true,
renderCellActionMenuItems: ({ closeMenu, cell, row, table }) => [
//array required
<MRT_ActionMenuItem //or just use the normal MUI MenuItem
icon={<Email />}
key={1}
label="Item 1"
onClick={() => {
//your logic here
closeMenu(); //close the menu after the action is performed
}}
table={table}
/>,
<MRT_ActionMenuItem
icon={<PersonOffOutlined />}
key={2}
label="Item 2"
onClick={async () => {
//await your logic here
closeMenu(); //close the menu after the async action is performed
}}
table={table}
/>,
],
});
```

### Include Automatic Cell Actions

A few cell actions are included by default when certain other features are enabled.

- A "Copy" action will be included when the `enableClickToCopy` option is set to `"context-menu"` (instead of `true`) for the table or column.

- An "Edit" action will be included when the `enableEditing` option is set to `true` and the `editDisplayMode` option is set to `"cell"`. This will not disable the default double-click to edit behavior, but will provide an additional way to edit the cell.

More built-in cell actions may be added in the future.

```jsx
const table = useMaterialReactTable({
columns,
data,
enableCellActions: true,
enableClickToCopy: 'context-menu',
enableEditing: true,
editDisplayMode: 'cell',
});
```

If you want to render these actions alongside your custom actions, you will just need to include the `internalMenuItems` parameter in your `renderCellActionMenuItems` function.

```jsx
const table = useMaterialReactTable({
columns,
data,
enableCellActions: true,
enableClickToCopy: 'context-menu',
enableEditing: true,
editDisplayMode: 'cell',
renderCellActionMenuItems: ({
closeMenu,
cell,
row,
table,
internalMenuItems,
}) => [
...internalMenuItems, //render the copy and edit actions wherever you want in the list
<Divider />, //optionally place a Menu Divider to separate groups of actions
<MRT_ActionMenuItem
icon={<Email />}
key={1}
label="Item 1"
onClick={() => {
//your logic here
closeMenu(); //close the menu after the action is performed
}}
table={table}
/>,
<MRT_ActionMenuItem
icon={<PersonOffOutlined />}
key={2}
label="Item 2"
onClick={async () => {
//await your logic here
closeMenu(); //close the menu after the async action is performed
}}
table={table}
/>,
],
});
```
Expand Up @@ -22,7 +22,7 @@ Material React Table has full support for localization (i18n). Some locales are

The following locales are included and can be imported from `'material-react-table/locales/'`:

`am`, `ar`, `az`, `bg`, `cs`, `da`, `de`, `en`, `es`, `et`, `fa`, `fi`, `fr`, `hu`, `he`, `hy`, `id`, `it`, `ja`, `ko`, `nl`, `no`, `np`, `pl`, `pt`, `pt-BR`, `ro`, `ru`, `sk`, `sr-Cyrl-RS`, `sr-Latn-RS`, `sv`, `tr`, `uk`, `vi`, `zh-Hans`, `zh-Hant`
`ar`, `az`, `bg`, `cs`, `da`, `de`, `en`, `es`, `et`, `fa`, `fi`, `fr`, `hu`, `he`, `hy`, `id`, `it`, `ja`, `ko`, `nl`, `no`, `np`, `pl`, `pt`, `pt-BR`, `ro`, `ru`, `sk`, `sr-Cyrl-RS`, `sr-Latn-RS`, `sv`, `tr`, `uk`, `vi`, `zh-Hans`, `zh-Hant`

> If your language is not yet supported, please consider making a PR to add it to the library! See [here on GitHub](https://github.com/KevinVandy/material-react-table/tree/v2/packages/material-react-table/src/locales).
Expand Down
4 changes: 2 additions & 2 deletions apps/test-vite/package.json
Expand Up @@ -22,8 +22,8 @@
"devDependencies": {
"@types/react": "^18.2.48",
"@types/react-dom": "^18.2.18",
"@typescript-eslint/eslint-plugin": "^6.19.1",
"@typescript-eslint/parser": "^6.19.1",
"@typescript-eslint/eslint-plugin": "^6.20.0",
"@typescript-eslint/parser": "^6.20.0",
"@vitejs/plugin-react": "^4.2.1",
"eslint": "^8.56.0",
"eslint-plugin-react-hooks": "^4.6.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/material-react-table/package.json
Expand Up @@ -80,11 +80,11 @@
"@storybook/react": "^7.6.10",
"@storybook/react-vite": "^7.6.10",
"@storybook/testing-library": "^0.2.2",
"@types/node": "^20.11.7",
"@types/node": "^20.11.10",
"@types/react": "^18.2.48",
"@types/react-dom": "^18.2.18",
"@typescript-eslint/eslint-plugin": "^6.19.1",
"@typescript-eslint/parser": "^6.19.1",
"@typescript-eslint/eslint-plugin": "^6.20.0",
"@typescript-eslint/parser": "^6.20.0",
"@vitejs/plugin-react": "^4.2.1",
"eslint": "^8.56.0",
"eslint-plugin-mui-path-imports": "^0.0.15",
Expand Down

0 comments on commit 3a6fb00

Please sign in to comment.