Skip to content
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

Upgrade Sortable example #20

Merged
merged 1 commit into from
Dec 4, 2023
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
219 changes: 85 additions & 134 deletions packages/module/patternfly-docs/content/examples/Sortable.tsx
Original file line number Diff line number Diff line change
@@ -1,150 +1,101 @@
import React from 'react';
import { debounce } from '@patternfly/react-core';
import { sortable, SortByDirection, TableGridBreakpoint } from '@patternfly/react-table';
import { Table as TableDeprecated, TableHeader as TableHeaderDeprecated } from '@patternfly/react-table/deprecated';
import { Caption, Table, Td, Th, Thead, ThProps, Tr } from '@patternfly/react-table';
import { CellMeasurerCache, CellMeasurer } from 'react-virtualized';
import { AutoSizer, VirtualTableBody } from '@patternfly/react-virtualized-extension';

export class SortableExample extends React.Component {
constructor(props) {
super(props);
const rows = [];
for (let i = 0; i < 100; i++) {
rows.push({
id: `sortable-row-${i}`,
cells: [`one-${i}`, `two-${i}`, `three-${i}`, `four-${i}`, `five-${i}`]
});
}

this.sortableVirtualBody = null;
export const SortableExample: React.FunctionComponent = () => {
const rows: { id: string; cells: string[] }[] = [];
for (let i = 0; i < 100; i++) {
rows.push({
id: `sortable-row-${i}`,
cells: [`one-${i}`, `two-${i}`, `three-${i}`, `four-${i}`, `five-${i}`]
});
}

this.state = {
columns: [
{
title: 'Repositories',
transforms: [sortable],
props: { className: 'pf-m-6-col-on-sm pf-m-4-col-on-md pf-m-3-col-on-lg pf-m-2-col-on-xl' }
},
{
title: 'Branches',
props: { className: 'pf-m-6-col-on-sm pf-m-4-col-on-md pf-m-3-col-on-lg pf-m-2-col-on-xl' }
},
{
title: 'Pull requests',
transforms: [sortable],
props: { className: 'pf-m-4-col-on-md pf-m-4-col-on-lg pf-m-3-col-on-xl pf-m-hidden pf-m-visible-on-md' }
},
{
title: 'Workspaces',
props: { className: 'pf-m-2-col-on-lg pf-m-2-col-on-xl pf-m-hidden pf-m-visible-on-lg' }
},
{ title: 'Last Commit', props: { className: 'pf-m-3-col-on-xl pf-m-hidden pf-m-visible-on-xl' } }
],
rows,
sortBy: {}
};
const columns = ['Repositories', 'Branches', 'Pull requests', 'Workspaces', 'Last Commit'];

this.onSort = this.onSort.bind(this);
this._handleResize = debounce(this._handleResize.bind(this), 100);
}
const [activeSortIndex, setActiveSortIndex] = React.useState<number>(-1);

componentDidMount() {
// re-render after resize
window.addEventListener('resize', this._handleResize);
}
// Sort direction of the currently sorted column
const [activeSortDirection, setActiveSortDirection] = React.useState<'asc' | 'desc' | undefined>();

componentWillUnmount() {
window.removeEventListener('resize', this._handleResize);
}
const getRowIndex = (str: string) => Number(str?.split('-')[1]);

_handleResize() {
this.forceUpdate();
}
const getSortParams = (columnIndex: number): ThProps['sort'] => ({
sortBy: {
index: activeSortIndex,
direction: activeSortDirection
},
onSort: (_event, index, direction) => {
setActiveSortIndex(index);
setActiveSortDirection(direction as 'desc' | 'asc');
},
columnIndex
});

onSort(_event, index, direction) {
const sortedRows = this.state.rows.sort((a, b) =>
// eslint-disable-next-line no-nested-ternary
a.cells[index] < b.cells[index] ? -1 : a.cells[index] > b.cells[index] ? 1 : 0
);
this.setState({
sortBy: {
index,
direction
},
rows: direction === SortByDirection.asc ? sortedRows : sortedRows.reverse()
});
if (activeSortIndex !== null) {
rows.sort((a, b) => {
const aValue = a.cells[activeSortIndex];
const bValue = b.cells[activeSortIndex];

this.sortableVirtualBody.forceUpdateVirtualGrid();
}
const aValueIndex = getRowIndex(aValue);
const bValueIndex = getRowIndex(bValue);

render() {
const { sortBy, columns, rows } = this.state;
if (activeSortDirection === 'asc') {
return aValueIndex - bValueIndex;
}

const measurementCache = new CellMeasurerCache({
fixedWidth: true,
minHeight: 44,
keyMapper: (rowIndex) => rowIndex
return bValueIndex - aValueIndex;
});
}

const rowRenderer = ({ index, _isScrolling, key, style, parent }) => {
const { rows, columns } = this.state;

return (
<CellMeasurer cache={measurementCache} columnIndex={0} key={key} parent={parent} rowIndex={index}>
<tr style={style} role="row">
<td className={columns[0].props.className} role="gridcell">
{rows[index].cells[0]}
</td>
<td className={columns[1].props.className} role="gridcell">
{rows[index].cells[1]}
</td>
<td className={columns[2].props.className} role="gridcell">
{rows[index].cells[2]}
</td>
<td className={columns[3].props.className} role="gridcell">
{rows[index].cells[3]}
</td>
<td className={columns[4].props.className} role="gridcell">
{rows[index].cells[4]}
</td>
</tr>
</CellMeasurer>
);
};
const measurementCache = new CellMeasurerCache({
fixedWidth: true,
minHeight: 44,
keyMapper: (rowIndex) => rowIndex
});

return (
<div aria-label="Scrollable Table" className="pf-v5-c-scrollablegrid">
<TableDeprecated
caption="Sortable Virtualized Table"
cells={columns}
rows={rows}
gridBreakPoint={TableGridBreakpoint.none}
sortBy={sortBy}
onSort={this.onSort}
role="grid"
aria-rowcount={rows.length}
>
<TableHeaderDeprecated />
</TableDeprecated>
<AutoSizer disableHeight>
{({ width }) => (
<VirtualTableBody
ref={(ref) => (this.sortableVirtualBody = ref)}
className="pf-v5-c-table pf-v5-c-virtualized pf-v5-c-window-scroller"
deferredMeasurementCache={measurementCache}
rowHeight={measurementCache.rowHeight}
height={400}
overscanRowCount={2}
columnCount={1}
rows={rows}
rowCount={rows.length}
rowRenderer={rowRenderer}
width={width}
role="grid"
/>
)}
</AutoSizer>
</div>
);
}
}
const rowRenderer = ({ index: rowIndex, _isScrolling, key, style, parent }) => (
<CellMeasurer cache={measurementCache} columnIndex={0} key={key} parent={parent} rowIndex={rowIndex}>
<Tr style={style}>
{columns.map((col, index) => (
<Td key={`${rowIndex}-${index + 1}`}>{rows[rowIndex].cells[index]}</Td>
))}
</Tr>
</CellMeasurer>
);
return (
<div aria-label="Scrollable Table" className="pf-v5-c-scrollablegrid">
<Table aria-label="Sortable table" ouiaId="SortableTable">
<Caption>Sortable Virtualized Table</Caption>
<Thead>
<Tr>
<Th sort={getSortParams(0)}>{columns[0]}</Th>
<Th>{columns[1]}</Th>
<Th sort={getSortParams(2)}>{columns[2]}</Th>
<Th>{columns[3]}</Th>
<Th>{columns[4]}</Th>
</Tr>
</Thead>
</Table>
<AutoSizer disableHeight>
{({ width }) => (
<VirtualTableBody
ref={(ref) => ref}
className="pf-v5-c-table pf-v5-c-virtualized pf-v5-c-window-scroller"
deferredMeasurementCache={measurementCache}
rowHeight={measurementCache.rowHeight}
height={400}
overscanRowCount={2}
columnCount={1}
rows={rows}
rowCount={rows.length}
rowRenderer={rowRenderer}
width={width}
role="grid"
/>
)}
</AutoSizer>
</div>
);
};
Loading
Loading