Skip to content
Draft
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@
{
"path": "lib/components/internal/widget-exports.js",
"brotli": false,
"limit": "1470 kB",
"limit": "1480 kB",
"ignore": "react-dom"
}
],
Expand Down
188 changes: 134 additions & 54 deletions pages/table-root/sorting.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import React, { useMemo, useState } from 'react';

import Button from '~components/button';
import Header from '~components/header';
import Icon from '~components/icon';
import SpaceBetween from '~components/space-between';
Expand All @@ -12,46 +13,33 @@ import TableHeaderCell from '~components/table-header-cell';
import TableRoot from '~components/table-root';
import TableRow from '~components/table-row';

import { useAppContext } from '../app/app-context';
import { SimplePage } from '../app/templates';
import { Item, makeItems } from './common';

import styles from './styles.scss';

type SortKey = 'name' | 'type' | 'size' | 'status';
type SortDirection = 'ascending' | 'descending';

const COLUMNS: ReadonlyArray<{ key: SortKey; label: string }> = [
interface SortEntry {
key: SortKey;
descending: boolean;
}

// Status can't be sorted, to show the disabled state.
const COLUMNS: ReadonlyArray<{ key: SortKey; label: string; disabled?: boolean }> = [
{ key: 'name', label: 'Name' },
{ key: 'type', label: 'Type' },
{ key: 'size', label: 'Size' },
{ key: 'status', label: 'Status' },
{ key: 'status', label: 'Status', disabled: true },
];

function compare(key: SortKey, a: Item, b: Item): number {
if (key === 'size') {
return parseInt(a.size, 10) - parseInt(b.size, 10);
}
return a[key].localeCompare(b[key]);
}

export default function TableSortingPage() {
const items = makeItems(12);
const [sortKey, setSortKey] = useState<SortKey>('name');
const [direction, setDirection] = useState<SortDirection>('ascending');

const rows = useMemo(() => {
const sorted = [...items].sort((a, b) => compare(sortKey, a, b));
return direction === 'ascending' ? sorted : sorted.reverse();
}, [items, sortKey, direction]);
const { urlParams, setUrlParams } = useAppContext<'sortKey' | 'sortDescending'>();
const sortKey = (urlParams.sortKey as SortKey | undefined) ?? 'name';
const descending = !!urlParams.sortDescending;

const handleSort = (key: SortKey) => {
if (key === sortKey) {
setDirection(prev => (prev === 'ascending' ? 'descending' : 'ascending'));
} else {
setSortKey(key);
setDirection('ascending');
}
};
const items = useMemo(() => makeItems(12), []);
const rows = useMemo(() => sortItems(items, [{ key: sortKey, descending }]), [items, sortKey, descending]);

return (
<SimplePage title="Table atomics — sorting (auto layout)" screenshotArea={{}}>
Expand All @@ -60,41 +48,133 @@ export default function TableSortingPage() {
<TableRoot ariaLabel="Resources">
<TableHead>
<TableRow>
{COLUMNS.map(({ key, label }) => {
const active = key === sortKey;
{COLUMNS.map(({ key, label, disabled }) => {
const direction = key === sortKey ? toDirection(descending) : undefined;
return (
<TableHeaderCell key={key} ariaSort={active ? direction : 'none'}>
<button type="button" className={styles['sort-button']} onClick={() => handleSort(key)}>
<span>{label}</span>
<span className={styles['sort-indicator']}>
<Icon
name={
active
? direction === 'ascending'
? 'caret-up-filled'
: 'caret-down-filled'
: 'caret-down'
}
/>
</span>
</button>
<TableHeaderCell key={key} disablePaddings="headerButton" ariaSort={direction}>
<Button
variant="table-header"
formAction="none"
wrapText={false}
disabled={disabled}
iconEnd={sortIcon(direction, disabled)}
onClick={() => setUrlParams({ sortKey: key, sortDescending: direction === 'ascending' })}
>
{label}
</Button>
</TableHeaderCell>
);
})}
</TableRow>
</TableHead>
<TableBody>
{rows.map(item => (
<TableRow key={item.id}>
<TableBodyCell>{item.name}</TableBodyCell>
<TableBodyCell>{item.type}</TableBodyCell>
<TableBodyCell>{item.size}</TableBodyCell>
<TableBodyCell>{item.status}</TableBodyCell>
</TableRow>
))}
</TableBody>
<ResourceRows rows={rows} />
</TableRoot>
<Header description="Shift+click adds a column to the sort.">Multi-column sort</Header>
<MultiSortTable items={items} />
</SpaceBetween>
</SimplePage>
);
}

function MultiSortTable({ items }: { items: Item[] }) {
const [sorting, setSorting] = useState<SortEntry[]>([
{ key: 'type', descending: false },
{ key: 'size', descending: true },
]);
const rows = useMemo(() => sortItems(items, sorting), [items, sorting]);

return (
<TableRoot ariaLabel="Resources, sorted by multiple columns">
<TableHead>
<TableRow>
{COLUMNS.map(({ key, label, disabled }) => {
const index = sorting.findIndex(entry => entry.key === key);
const entry = sorting[index];
const direction = entry ? toDirection(entry.descending) : undefined;
const priority = sorting.length > 1 && entry ? index + 1 : undefined;
return (
<TableHeaderCell key={key} disablePaddings="headerButton" ariaSort={direction}>
<Button
variant="table-header"
formAction="none"
wrapText={false}
disabled={disabled}
ariaLabel={priority ? `${label}, sort priority ${priority}` : undefined}
iconEnd={
priority ? (
<SpaceBetween direction="horizontal" size="xxs" alignItems="center">
<span aria-hidden="true">{priority}</span>
{sortIcon(direction, disabled)}
</SpaceBetween>
) : (
sortIcon(direction, disabled)
)
}
onClick={({ detail }) => {
const next = { key, descending: direction === 'ascending' };
setSorting(current =>
!detail.shiftKey
? [next]
: entry
? current.map(e => (e.key === key ? next : e))
: [...current, next]
);
}}
>
{label}
</Button>
</TableHeaderCell>
);
})}
</TableRow>
</TableHead>
<ResourceRows rows={rows} />
</TableRoot>
);
}

// A disabled column that isn't sorted has nothing to indicate, as in the existing Table.
function sortIcon(direction: SortDirection | undefined, disabled = false) {
if (!direction) {
return disabled ? null : <Icon name="caret-down" />;
}
return <Icon name={direction === 'ascending' ? 'caret-up-filled' : 'caret-down-filled'} />;
}

function ResourceRows({ rows }: { rows: Item[] }) {
return (
<TableBody>
{rows.map(item => (
<TableRow key={item.id}>
<TableBodyCell>{item.name}</TableBodyCell>
<TableBodyCell>{item.type}</TableBodyCell>
<TableBodyCell>{item.size}</TableBodyCell>
<TableBodyCell>{item.status}</TableBodyCell>
</TableRow>
))}
</TableBody>
);
}

function toDirection(descending: boolean): SortDirection {
return descending ? 'descending' : 'ascending';
}

function sortItems(items: Item[], sorting: SortEntry[]): Item[] {
return [...items].sort((a, b) => {
for (const { key, descending } of sorting) {
const result = compare(key, a, b);
if (result !== 0) {
return descending ? -result : result;
}
}
return 0;
});
}

function compare(key: SortKey, a: Item, b: Item): number {
if (key === 'size') {
return parseInt(a.size, 10) - parseInt(b.size, 10);
}
return a[key].localeCompare(b[key]);
}
18 changes: 10 additions & 8 deletions pages/table-root/sticky-header.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// SPDX-License-Identifier: Apache-2.0
import React, { useMemo } from 'react';

import Button from '~components/button';
import Header from '~components/header';
import Icon from '~components/icon';
import SegmentedControl from '~components/segmented-control';
import SpaceBetween from '~components/space-between';
import TableBody from '~components/table-body';
Expand All @@ -15,8 +17,6 @@ import TableRow from '~components/table-row';
import { useAppContext } from '../app/app-context';
import { SimplePage } from '../app/templates';

import styles from './styles.scss';

// Wide enough to scroll sideways, and long enough to scroll the page, so the pinned header can be checked
// against both. The Name header's sort button exercises pointer interaction with the pinned copy.

Expand Down Expand Up @@ -85,14 +85,16 @@ export default function TableStickyHeaderPage() {
stickyHeader={
<TableHead>
<TableRow>
<TableHeaderCell ariaSort={descending ? 'descending' : 'ascending'}>
<button
type="button"
className={styles['sort-button']}
<TableHeaderCell disablePaddings="headerButton" ariaSort={descending ? 'descending' : 'ascending'}>
<Button
variant="table-header"
formAction="none"
wrapText={false}
iconEnd={<Icon name={descending ? 'caret-down-filled' : 'caret-up-filled'} />}
onClick={() => setUrlParams({ sortDescending: !descending })}
>
Name {descending ? '▼' : '▲'}
</button>
Name
</Button>
</TableHeaderCell>
<TableHeaderCell>Type</TableHeaderCell>
<TableHeaderCell>Size</TableHeaderCell>
Expand Down
41 changes: 0 additions & 41 deletions pages/table-root/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,6 @@
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
@use '~design-tokens' as tokens;

// A minimal sort control for the demo: a native button that inherits the header cell's colour and
// typography (so the label stays the column-header colour, not link blue). It fills the cell and
// pushes the caret to the end, so the label stays left-aligned with the column while the sort
// indicator sits at the right.
.sort-button {
display: flex;
align-items: center;
justify-content: space-between;
inline-size: 100%;
gap: tokens.$space-static-xxs;
padding-block: 0;
padding-inline: 0;
border-block: none;
border-inline: none;
background: none;
color: inherit;
font: inherit;
cursor: pointer;
}

// Centres the selection control within a disablePaddings control cell, matching classic Table.
// The Cloudscape checkbox/radio control carries an intrinsic 2px top margin (it aligns the box with
// the first line of a label); with no visible label in a centred control column that margin biases
// the control ~1px below centre. Classic's own SelectionControl absorbs it with a compensating
// block-end padding on the control label; mirror that here so the control lands on the row centre.
.selection-cell {
display: flex;
justify-content: center;
align-items: center;
padding-block-end: 2px;
}

// Sort affordance at the right of a sortable header (the caret icon).
.sort-indicator {
display: inline-flex;
align-items: center;
gap: tokens.$space-static-xxs;
}

// A fixed-height region that scrolls its table in both directions.
.scroll-container {
block-size: 400px;
Expand Down
Loading
Loading