Skip to content

Commit

Permalink
Fix InteractiveTable: React, less hooks rendered than previous render (
Browse files Browse the repository at this point in the history
…#85043)

fix: react-hooks error if data length switches from above or below pageSize
  • Loading branch information
abannachGrafana committed Mar 25, 2024
1 parent 40e73f3 commit d7f739c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
@@ -1,8 +1,11 @@
import { Meta, StoryFn, StoryObj } from '@storybook/react';
import React, { useCallback, useState } from 'react';
import React, { useCallback, useMemo, useState } from 'react';

import { InteractiveTable, CellProps, LinkButton } from '@grafana/ui';

import { Field } from '../Forms/Field';
import { Input } from '../Input/Input';

import { FetchDataArgs, InteractiveTableHeaderTooltip } from './InteractiveTable';
import mdx from './InteractiveTable.mdx';

Expand Down Expand Up @@ -125,9 +128,9 @@ const meta: Meta<typeof InteractiveTable<CarData>> = {
argTypes: {},
};

type TableStory = StoryObj<typeof InteractiveTable<CarData>>;
type TableStoryObj = StoryObj<typeof InteractiveTable<CarData>>;

export const Basic: TableStory = {
export const Basic: TableStoryObj = {
args: {
columns: [
{
Expand Down Expand Up @@ -169,7 +172,7 @@ const ExpandedCell = ({ car }: CarData) => {
return <p>{car}</p>;
};

export const WithRowExpansion: TableStory = {
export const WithRowExpansion: TableStoryObj = {
args: {
renderExpandedRow: ExpandedCell,
},
Expand Down Expand Up @@ -216,11 +219,33 @@ export const WithCustomCell: StoryObj<typeof InteractiveTable<WithCustomCellData
},
};

export const WithPagination: TableStory = {
args: {
pageSize: 15,
data: pageableData,
},
export const WithPagination: StoryFn<typeof InteractiveTable> = (args) => {
const [filter, setFilter] = useState('');

const data = useMemo(() => {
if (filter) {
return pageableData.filter((d) => d.firstName.toLowerCase().includes(filter.toLowerCase()));
}
return pageableData;
}, [filter]);

return (
<>
<Field label={'Filter data'}>
<Input
placeholder={'Filter by first name'}
onChange={(event) => {
setFilter(event.currentTarget.value);
}}
/>
</Field>
<InteractiveTable {...args} data={data} />
</>
);
};

WithPagination.args = {
pageSize: 15,
};

const headerTooltips: Record<string, InteractiveTableHeaderTooltip> = {
Expand All @@ -238,7 +263,7 @@ const headerTooltips: Record<string, InteractiveTableHeaderTooltip> = {
iconName: 'plus-square',
},
};
export const WithHeaderTooltips: TableStory = {
export const WithHeaderTooltips: TableStoryObj = {
args: {
headerTooltips,
},
Expand Down
Expand Up @@ -141,6 +141,8 @@ interface BaseProps<TableData extends object> {
headerTooltips?: Record<string, InteractiveTableHeaderTooltip>;
/**
* Number of rows per page. A value of zero disables pagination. Defaults to 0.
* A React hooks error will be thrown if pageSize goes from greater than 0 to 0 or vice versa. If enabling pagination,
* make sure pageSize remains a non-zero value.
*/
pageSize?: number;
/**
Expand Down Expand Up @@ -196,7 +198,7 @@ export function InteractiveTable<TableData extends object>({
const tableHooks: Array<PluginHook<TableData>> = [useSortBy, useExpanded];

const multiplePages = data.length > pageSize;
const paginationEnabled = pageSize > 0 && multiplePages;
const paginationEnabled = pageSize > 0;

if (paginationEnabled) {
tableHooks.push(usePagination);
Expand Down Expand Up @@ -304,7 +306,7 @@ export function InteractiveTable<TableData extends object>({
})}
</tbody>
</table>
{paginationEnabled && (
{paginationEnabled && multiplePages && (
<span>
<Pagination
currentPage={tableInstance.state.pageIndex + 1}
Expand Down

0 comments on commit d7f739c

Please sign in to comment.