Table View
Create a cell plugin
Add an Effort property that stores and edits a task's size.
Define the data plugin
In effort-plugin.tsx, define the stored value and how Table View converts it.
Use the plugin ID as the property's type.
import type { CellPlugin } from "@notion-kit/table-hook/plugins";
type Effort = "Small" | "Large";
type EffortPlugin = CellPlugin<"effort", Effort, undefined>;
export const effort: EffortPlugin = {
id: "effort",
default: { data: "Small", config: undefined },
fromValue: (value) => (value === "Large" ? "Large" : "Small"),
toValue: (data) => data,
toTextValue: (data) => data,
isEmpty: () => false,
canGroup: false,
};Add a UI adapter
In the same file, add an adapter with the same ID. Read the value with
cell.getData() and commit edits with cell.update(). Show the value directly
when the view is locked.
import { Icon } from "@notion-kit/icons";
import type { CellProps, TableUiPlugin } from "@notion-kit/table-view";
import { Button } from "@notion-kit/ui/primitives";
function EffortCell({ cell }: CellProps) {
const value = cell.getData<EffortPlugin>();
if (cell.getContext().table.getTableGlobalState().locked) return value;
return (
<Button
size="sm"
aria-label="Toggle effort"
aria-pressed={value === "Large"}
onClick={() =>
cell.update<EffortPlugin>(value === "Small" ? "Large" : "Small")
}
>
{value}
</Button>
);
}
const icon = <Icon.Star />;
export const effortUi: TableUiPlugin<EffortPlugin> = {
id: "effort",
meta: { name: "Effort", desc: "Task size", icon },
default: { name: "Effort", icon },
renderCell: (props) => <EffortCell {...props} />,
renderGroupingValue: () => null,
};Register the plugin
Keep the built-in plugins and append the new pair. Add an "effort" property
to make it available in the table.
import { DEFAULT_PLUGINS, TableView } from "@notion-kit/table-view";
import { effort, effortUi } from "./effort-plugin";
const plugins = {
data: [...DEFAULT_PLUGINS.data, effort],
ui: [...DEFAULT_PLUGINS.ui, effortUi],
};
const now = Date.now();
export function Tasks() {
return (
<TableView
plugins={plugins}
defaultProperties={[
{ id: "name", name: "Task", type: "title" },
{ id: "size", name: "Effort", type: "effort" },
]}
defaultData={[
{
id: "task-1",
createdAt: now,
lastEditedAt: now,
properties: {
name: { id: "task-1-name", value: "Review the design" },
size: { id: "task-1-size", value: "Small" },
},
},
]}
/>
);
}See Cell plugins for filtering, bulk editing, and other optional hooks.