Skip to content

Commit

Permalink
add custom action to data table
Browse files Browse the repository at this point in the history
  • Loading branch information
PixeledCode committed May 8, 2024
1 parent 5cb337a commit 88d8c0d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
51 changes: 51 additions & 0 deletions packages/opub-ui/src/components/DataTable/DataTable.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Meta, StoryObj } from '@storybook/react';
import { IconCopy, IconPencil, IconTrash } from '@tabler/icons-react';
import { createColumnHelper } from '@tanstack/react-table';

import { Button } from '../Button';
import { makeTableData, Person } from '../Table/utils';
import { DataTable } from './DataTable';

Expand Down Expand Up @@ -195,6 +196,7 @@ const truncateData: Person[] = [
visits: 20,
status: 'Complicated',
progress: 10,
action: 'Action',
},
];

Expand All @@ -206,3 +208,52 @@ export const Truncate: Story = {
truncate: true,
},
};

const actionColumn = [
columnHelper.accessor('firstName', {
cell: (info) => info.getValue(),
header: () => 'First Name',
}),
columnHelper.accessor((row) => row.lastName, {
id: 'lastName',
header: 'Last Name',
}),
columnHelper.accessor('age', {
header: () => 'Age',
cell: (info) => info.renderValue(),
}),
columnHelper.accessor('visits', {
header: 'Visits',
}),
columnHelper.accessor('progress', {
header: 'Profile Progress',
}),
columnHelper.accessor('status', {
header: 'Status',
filterFn: 'columnFilter',
}),
columnHelper.accessor('action', {
header: 'Action',
cell: ({ row }) => (
<Button
size="slim"
kind="secondary"
onClick={(e) => {
e.stopPropagation();
console.log(row.original);
}}
>
Delete
</Button>
),
}),
];

export const customAction: Story = {
args: {
columnContentTypes: columnContentTypes,
rows: truncateData,
columns: actionColumn,
truncate: true,
},
};
4 changes: 3 additions & 1 deletion packages/opub-ui/src/components/Table/utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { range } from '../../utils';
import { faker } from '@faker-js/faker';

import { range } from '../../utils';

export type Person = {
firstName: any;
lastName: string;
age: number;
visits: number;
status: string;
progress: number;
action?: any;
};

const newPerson = (): Person => {
Expand Down
5 changes: 3 additions & 2 deletions packages/opub-ui/src/types/button.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ActionListItemDescriptor } from './actionlist';
import React, { FocusEvent } from 'react';

import { ActionListItemDescriptor } from './actionlist';

export interface BaseButtonProps {
/** A unique identifier for the button */
id?: string;
Expand Down Expand Up @@ -31,7 +32,7 @@ export interface BaseButtonProps {
/** Indicates the current checked state of the button when acting as a toggle or switch */
ariaChecked?: 'false' | 'true';
/** Callback when clicked */
onClick?(e?: React.MouseEvent<any>): void;
onClick?(e: React.MouseEvent<any>): void;
/** Callback when button becomes focussed */
onFocus?(e: FocusEvent<Element, Element>): void;
/** Callback when focus leaves button */
Expand Down

0 comments on commit 88d8c0d

Please sign in to comment.