Skip to content

docs(AnalyticalTable): add JSDoc comment, enhance story #934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 117 additions & 59 deletions packages/main/src/components/AnalyticalTable/AnalyticalTable.stories.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Meta, Story, Canvas, ArgsTable } from '@storybook/addon-docs/blocks';
import { ArgsTable, Canvas, Meta, Story } from '@storybook/addon-docs/blocks';
import { AnalyticalTable } from '@ui5/webcomponents-react/lib/AnalyticalTable';
import { createSelectArgTypes } from '../../../../../shared/stories/createSelectArgTypes';
import { TableScaleWidthMode } from '@ui5/webcomponents-react/lib/TableScaleWidthMode';
Expand All @@ -15,6 +15,7 @@ import { TextAlign } from '@ui5/webcomponents-react/lib/TextAlign';
import { DefaultLoadingComponent } from './defaults/LoadingComponent';
import { DefaultNoDataComponent } from './defaults/NoDataComponent';
import { DocsCommonProps } from '@shared/stories/DocsCommonProps';
import { useEffect, useRef, useState } from 'react';

<Meta
title="Components / AnalyticalTable"
Expand Down Expand Up @@ -125,42 +126,7 @@ import { DocsCommonProps } from '@shared/stories/DocsCommonProps';
<DocsHeader />

<Canvas>
<Story name="Default">
{(args) => (
<div style={{ display: 'flex', flexDirection: 'column' }}>
<AnalyticalTable
{...args}
title={args.title}
data={args.data}
columns={args.columns}
loading={args.loading}
alternateRowColor={args.alternateRowColor}
sortable={args.sortable}
filterable={args.filterable}
visibleRows={args.visibleRows}
minRows={args.minRows}
groupable={args.groupable}
selectionMode={args.selectionMode}
scaleWidthMode={args.scaleWidthMode}
onRowSelected={args.onRowSelected}
onSort={args.onSort}
onGroup={args.onGroup}
onRowExpandChange={args.onRowExpandChange}
groupBy={args.groupBy}
rowHeight={args.rowHeight}
selectedRowIds={args.selectedRowIds}
onColumnsReordered={args.onColumnsReordered}
withRowHighlight={args.withRowHighlight}
highlightField={args.highlightField}
infiniteScroll={args.infiniteScroll}
infiniteScrollThreshold={args.infiniteScrollThreshold}
onLoadMore={args.onLoadMore}
selectionBehavior={args.selectionBehavior}
overscanCountHorizontal={args.overscanCountHorizontal}
/>
</div>
)}
</Story>
<Story name="Default">{(args) => <AnalyticalTable {...args} />}</Story>
</Canvas>

<ArgsTable story="." />
Expand Down Expand Up @@ -194,13 +160,22 @@ import { DocsCommonProps } from '@shared/stories/DocsCommonProps';
| `disableGroupBy` | `boolean` | Disable groupBy for this column |
| `defaultCanSort` | `boolean` | If set to true, this column will be sortable, regardless if it has a valid `accessor` |
| `disableSortBy` | `boolean` | Disable sorting for this column |
| `sortDescFirst` | `boolean` | If set to `true`, the first sort direction for this column will be descending instead of ascending. |
| `sortInverted` | `boolean` | If set to `true`, the underlying sorting direction will be inverted, but the UI will not. |
| `sortType` | `string OR ((rowA, rowB, columnId: string, descending: boolean) => any)` | String or custom sort function.<br />Supported String Values: <ul><li>`basic`</li><li>`datetime`</li><li>`alphanumeric`</li></ul> |
| `disableResizing` | `boolean` | Disable resizing for this column |
| `hAlign` | `TextAlign` | Horizontal align of the cell |
| `vAlign` | `VerticalAlign` | Vertical align of the cell |

## Recipes

### How to select rows containing active elements?

By default, the `AnalyticalTable` will not select any rows after clicking on active elements inside a table cell like a `Button`, `Link`,
etc. <br />
In case you want to select the row anyways, you can "mark" the event to allow such a behaviour. <br />
Example: `<Link onClick={(e) => {e.markerAllowTableRowSelection = true;}>My Link Text</Link>`

### How do I stop my table state from automatically resetting when my data changes?

By default, the `AnalyticalTable` will reset the sorters, filters, grouping, selected rows, etc. when the table data changes.
Expand Down Expand Up @@ -270,32 +245,115 @@ export const ResponsiveTable = () => {
With the help of that effect, the table will now show either 2 columns on a mobile phone, 3 columns on a tablet device and all columns on Desktop devices.
This even works if you resize the browser window!

<br />

# Stories

<br />

## Tree Table

<Canvas>
<Story name="Tree Table" args={{ data: generateData(20, true) }}>
{(args) => (
<AnalyticalTable
{...args}
title={args.title}
data={args.data}
columns={args.columns}
loading={args.loading}
sortable={args.sortable}
filterable={args.filterable}
visibleRows={args.visibleRows}
minRows={args.minRows}
selectionMode={args.selectionMode}
onRowSelected={args.onRowSelected}
onSort={args.onSort}
onRowExpandChange={args.onRowExpandChange}
subRowsKey={args.subRowsKey}
selectedRowIds={args.selectedRowIds}
selectionBehavior={args.selectionBehavior}
isTreeTable
/>
)}
<Story name="Tree Table" args={{ data: generateData(20, true), isTreeTable: true }}>
{(args) => <AnalyticalTable {...args} />}
</Story>
</Canvas>

The `data` structure of the tree table is as follows:

```js
const data = {
name: "Greg Miller",
age: 35,
friend: {
name: "Rose Franco",
age: 32,
},
status: "None",
subRows: [
{
name: "Rick DeAngelo",
age: 25,
friend: {
name: "Susanne Franco",
age: 37,
},
status: "None",
subRows: [...],
},
],
...
};
```

In this example the default key for sub row detection is used (`subRows`), you can use any key you like by setting the `subRowsKey` prop.

<br />

## Infinite Scrolling

The table initially contains 50 rows, when the last 10 rows are reached the table will load more data.

<Canvas>
<Story name="Infinite Scrolling">
{(args) => {
const [data, setData] = useState(args.data.slice(0, 50));
const [loading, setLoading] = useState(false);
const offset = useRef(50);
const onLoadMore = () => {
setLoading(true);
};
useEffect(() => {
if (loading) {
setTimeout(() => {
setData((prev) => [...prev, ...args.data.slice(offset.current, offset.current + 50)]);
setLoading(false);
offset.current += 50;
}, 2000);
}
}, [loading, args.data, offset.current]);
return (
<AnalyticalTable
data={data}
columns={args.columns}
infiniteScroll={true}
infiniteScrollThreshold={10}
title="Scroll to load more data"
onLoadMore={onLoadMore}
loading={loading}
/>
);
}}
</Story>
</Canvas>

```jsx
const InfiniteScrollTable = (props) => {
const [data, setData] = useState(props.data.slice(0, 50));
const [loading, setLoading] = useState(false);
const offset = useRef(50);
const onLoadMore = () => {
setLoading(true);
};
useEffect(() => {
if (loading) {
setTimeout(() => {
setData((prev) => [...prev, ...props.data.slice(offset.current, offset.current + 50)]);
setLoading(false);
offset.current += 50;
}, 2000);
}
}, [loading, props.data, offset.current]);
return (
<AnalyticalTable
data={data}
columns={props.columns}
infiniteScroll={true}
infiniteScrollThreshold={10}
title="Scroll to load more data"
onLoadMore={onLoadMore}
loading={loading}
/>
);
};
```
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export const VirtualTableBodyContainer = (props) => {
onLoadMore,
rows,
internalRowHeight,
handleExternalScroll
handleExternalScroll,
visibleRows
} = props;
const [isMounted, setIsMounted] = useState(false);

Expand All @@ -35,8 +36,8 @@ export const VirtualTableBodyContainer = (props) => {
const isScrollingDown = lastScrollTop.current < scrollOffset;
if (isScrollingDown && infiniteScroll) {
lastScrollTop.current = scrollOffset;
const currentTopRow = Math.floor(scrollOffset / internalRowHeight);
if (rows.length - currentTopRow < infiniteScrollThreshold) {
const currentLastRow = Math.floor(scrollOffset / internalRowHeight) + visibleRows;
if (rows.length - currentLastRow < infiniteScrollThreshold) {
if (!firedInfiniteLoadEvents.current.has(rows.length)) {
onLoadMore({
detail: {
Expand Down
Loading