Skip to content

Commit

Permalink
Take NoResult component out of table, update stories
Browse files Browse the repository at this point in the history
  • Loading branch information
JeanMarcMilletScality committed Jun 26, 2024
1 parent ea811ef commit 93dc1e3
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 70 deletions.
51 changes: 51 additions & 0 deletions src/lib/components/UnsuccessfulResult.component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import styled from 'styled-components';
import { Icon } from './icon/Icon.component';
import { Loader } from './loader/Loader.component';
import {
TableHeightKeyType,
TableLocalType,
tableRowHeight,
translatedMessages,
} from './tablev2/TableUtils';
import { Text } from './text/Text.component';
import { Box } from '../next';
import { spacing } from '../spacing';

export const NoResult = styled(Box)<{ height?: number | string }>`
display: flex;
justify-content: center;
align-items: center;
color: ${(props) => props.theme.textSecondary};
height: ${(props) => props.height}rem;
gap: ${spacing.r8};
`;

export type UnsuccessfulResultProps = {
name?: {
en: { singular: string; plural: string };
fr?: { singular: string; plural: string };
};
locale?: TableLocalType;
status: 'error' | 'loading' | 'idle' | 'noResult';
heightInRem?: number | string;
} & (
| { rowHeight?: TableHeightKeyType; heightInRem?: never }
| { rowHeight?: never; heightInRem?: number | string }
);

export const UnsuccessfulResult = (props: UnsuccessfulResultProps) => {
const { heightInRem = 5, name, locale, status, rowHeight } = props;
const height = rowHeight ? tableRowHeight[rowHeight] : heightInRem;
return (
<NoResult height={height}>
{(status === 'loading' || status === 'idle') && <Loader />}
{status === 'error' && (
<Icon name="Exclamation-circle" color="statusWarning" />
)}

<Text color="textSecondary">
{translatedMessages(status, name, locale)}
</Text>
</NoResult>
);
};
37 changes: 10 additions & 27 deletions src/lib/components/tablev2/TableCommon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,14 @@ import {
} from 'react-window';
import {
convertRemToPixels,
translatedMessages,
TableHeightKeyType,
TableLocalType,
tableRowHeight,
} from './TableUtils';
import { useTableContext } from './Tablev2.component';
import { NoResult } from './Tablestyle';
import { Loader } from '../loader/Loader.component';
import { Text } from '../text/Text.component';
import { Icon } from '../icon/Icon.component';
import useSyncedScroll from './useSyncedScroll';
import { CSSProperties } from 'styled-components';
import { UnsuccessfulResult } from '../UnsuccessfulResult.component';

type VirtualizedRowsType<
DATA_ROW extends Record<string, unknown> = Record<string, unknown>,
Expand Down Expand Up @@ -141,26 +137,17 @@ export function TableRows<
return index;
}

if (status === 'idle' || status === 'loading') {
if (status === 'idle' || status === 'loading' || status === 'error') {
return (
<NoResult rowHeight={rowHeight}>
<Loader />
<Text color="textSecondary">
{translatedMessages('loading', entityName, locale)}
</Text>
</NoResult>
);
}
if (status === 'error') {
return (
<NoResult rowHeight={rowHeight}>
<Icon name="Exclamation-circle" color="statusWarning" />
<Text color="textSecondary">
{translatedMessages('error', entityName, locale)}
</Text>
</NoResult>
<UnsuccessfulResult
name={entityName}
status={status}
locale={locale}
rowHeight={rowHeight}
/>
);
}

if (status === 'success' || status === undefined) {
if (typeof children === 'function') {
return children(
Expand Down Expand Up @@ -189,11 +176,7 @@ export function TableRows<
/>
);
} else {
return (
<NoResult rowHeight={rowHeight}>
{translatedMessages('noResult', entityName, locale)}
</NoResult>
);
return <UnsuccessfulResult name={entityName} status="noResult" />;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib/components/tablev2/TableUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const tableRowHeight = {
h64: '4.572', //3 line
};

type TableMessagesType = 'error' | 'loading' | 'noResult';
type TableMessagesType = 'error' | 'loading' | 'idle' | 'noResult';

export const translatedMessages = (
type: TableMessagesType,
Expand All @@ -80,7 +80,7 @@ export const translatedMessages = (
}, please refresh the
page.`;
}
if (type === 'loading') {
if (type === 'loading' || type === 'idle') {
if (locale === 'fr') {
return `Chargement des ${entityName?.fr?.plural || 'données'}...`;
}
Expand Down
9 changes: 0 additions & 9 deletions src/lib/components/tablev2/Tablestyle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,6 @@ export const TooltipContent = styled.div`
min-width: 60px;
`;

export const NoResult = styled(Box)<{ rowHeight: TableHeightKeyType }>`
display: flex;
justify-content: center;
align-items: center;
color: ${(props) => props.theme.textSecondary};
height: ${(props) => tableRowHeight[props.rowHeight]}rem;
gap: ${spacing.r8};
`;

export const SortCaret = <
DATA_ROW extends Record<string, unknown> = Record<string, unknown>,
>({
Expand Down
33 changes: 33 additions & 0 deletions stories/noresult.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from 'react';
import { Meta, StoryObj } from '@storybook/react';
import {
UnsuccessfulResult,
UnsuccessfulResultProps,
} from '../src/lib/components/UnsuccessfulResult.component.tsx';

type Story = StoryObj<UnsuccessfulResultProps>;

const meta: Meta<UnsuccessfulResultProps> = {
title: 'Components/NoResult',
component: UnsuccessfulResult,
};

export default meta;

export const Loading: Story = {
render: () => {
return <UnsuccessfulResult status="loading" />;
},
};

export const Error: Story = {
render: () => {
return <UnsuccessfulResult status="error" />;
},
};

export const NoResult: Story = {
render: () => {
return <UnsuccessfulResult status="noResult" />;
},
};
70 changes: 38 additions & 32 deletions stories/tablev2.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ const columns: Column<Entry>[] = [
Header: 'First Name',
accessor: 'firstName',
cellStyle: {
width: 'unset',
flex: 2,
textAlign: 'left',
},
Cell: ({ value }) => {
Expand All @@ -81,6 +83,8 @@ const columns: Column<Entry>[] = [
Header: 'Last Name',
accessor: 'lastName',
cellStyle: {
width: 'unset',
flex: 2,
textAlign: 'left',
},
// disable the sorting on this column
Expand All @@ -90,7 +94,8 @@ const columns: Column<Entry>[] = [
Header: 'Age',
accessor: 'age',
cellStyle: {
width: '50px',
width: 'unset',
flex: 1,
textAlign: 'left',
},
},
Expand All @@ -99,6 +104,8 @@ const columns: Column<Entry>[] = [
accessor: 'health',
sortType: 'health',
cellStyle: {
width: 'unset',
flex: 1,
textAlign: 'left',
},
},
Expand All @@ -119,26 +126,27 @@ export const SimpleContentTable = {
>
{location.search}
</span>
<Table columns={columns} data={data} defaultSortingKey={'health'}>
<Table
columns={columns}
data={data}
defaultSortingKey={'health'}
entityName={{
en: {
singular: 'user',
plural: 'users',
},
}}
>
<div
style={{
margin: '16px 0',
}}
>
<Table.SearchWithQueryParams
displayedName={{
singular: 'person',
plural: 'persons',
}}
/>
<Table.SearchWithQueryParams />
</div>
<Table.SingleSelectableContent
rowHeight="h40"
separationLineVariant="backgroundLevel3"
backgroundVariant="backgroundLevel1"
children={(Rows) => {
return <>{Rows}</>;
}}
></Table.SingleSelectableContent>
</Table>
</>
Expand All @@ -158,7 +166,6 @@ export const SimpleContentTable = {
<Table.SingleSelectableContent
rowHeight="h32"
separationLineVariant="backgroundLevel3"
backgroundVariant="backgroundLevel1"
/>
</Table>
</div>
Expand All @@ -178,7 +185,6 @@ export const SimpleContentTable = {
<Table.SingleSelectableContent
rowHeight="h40"
separationLineVariant="backgroundLevel3"
backgroundVariant="backgroundLevel1"
selectedId={'Rodolph Yohann'}
onRowSelected={action('Table Row Clicked')}
/>
Expand Down Expand Up @@ -211,7 +217,6 @@ export const SimpleContentTable = {
<Table.SingleSelectableContent
rowHeight="h40"
separationLineVariant="backgroundLevel3"
backgroundVariant="backgroundLevel1"
onRowSelected={action('Table Row Clicked')}
/>
</Table>
Expand All @@ -233,7 +238,6 @@ export const SimpleContentTable = {
<Table.MultiSelectableContent
rowHeight="h40"
separationLineVariant="backgroundLevel3"
backgroundVariant="backgroundLevel1"
onMultiSelectionChanged={(rows) => {
console.log('Table.MultiSelectableContent selected row', rows);
}}
Expand Down Expand Up @@ -290,6 +294,8 @@ export const asyncTable = {
accessor: 'firstName',
cellStyle: {
textAlign: 'left',
width: 'unset',
flex: 1,
},
Cell: renderRowSubComponent,
},
Expand All @@ -298,14 +304,17 @@ export const asyncTable = {
accessor: 'lastName',
cellStyle: {
textAlign: 'left',
width: 'unset',
flex: 1,
},
},
{
Header: 'Age',
accessor: 'age',
cellStyle: {
width: '50px',
textAlign: 'left',
width: 'unset',
flex: 0.5,
},
},
{
Expand All @@ -314,6 +323,8 @@ export const asyncTable = {
sortType: 'health',
cellStyle: {
textAlign: 'left',
width: 'unset',
flex: 1,
},
},
];
Expand All @@ -333,13 +344,10 @@ export const asyncTable = {
data={data}
defaultSortingKey={'health'}
>
<Table.SearchWithQueryParams
displayedName={{ singular: 'user', plural: 'users' }}
></Table.SearchWithQueryParams>
<Table.SearchWithQueryParams></Table.SearchWithQueryParams>
<Table.SingleSelectableContent
rowHeight="h40"
separationLineVariant="backgroundLevel3"
backgroundVariant="backgroundLevel1"
selectedId={'Rodolph Yohann'}
onRowSelected={action('Table Row Clicked')}
/>
Expand Down Expand Up @@ -401,7 +409,6 @@ export const OnBottomCallback = {
<Table.SingleSelectableContent
rowHeight="h40"
separationLineVariant="backgroundLevel3"
backgroundVariant="backgroundLevel1"
/>
</Table>
</div>
Expand Down Expand Up @@ -554,14 +561,15 @@ export const EmptyTable = {
render: (args) => {
const { background } = args;
return (
<Table columns={columns} data={[]} defaultSortingKey={'firstName'}>
<Table.SingleSelectableContent
rowHeight="h40"
separationLineVariant={background}
backgroundVariant="backgroundLevel1"
onRowSelected={action('Table Row Clicked')}
/>
</Table>
<Box width="500px" height="200px">
<Table columns={columns} data={[]} defaultSortingKey={'firstName'}>
<Table.SingleSelectableContent
rowHeight="h40"
separationLineVariant={background}
onRowSelected={action('Table Row Clicked')}
/>
</Table>
</Box>
);
},
argTypes: {
Expand Down Expand Up @@ -595,7 +603,6 @@ export const LoadingTable = {
<Table.SingleSelectableContent
rowHeight="h40"
separationLineVariant="backgroundLevel3"
backgroundVariant="backgroundLevel1"
/>
</Table>
</Box>
Expand All @@ -621,7 +628,6 @@ export const ErrorTable = {
<Table.SingleSelectableContent
rowHeight="h40"
separationLineVariant="backgroundLevel4"
backgroundVariant="backgroundLevel1"
locale="en"
/>
</Table>
Expand Down

0 comments on commit 93dc1e3

Please sign in to comment.