Skip to content

Commit 440ea93

Browse files
committed
Rough draft of TableView resizing docs
1 parent 62dfe3d commit 440ea93

File tree

1 file changed

+60
-36
lines changed

1 file changed

+60
-36
lines changed

packages/@react-spectrum/table/docs/TableView.mdx

Lines changed: 60 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default Layout;
1212

1313
import docs from 'docs:@react-spectrum/table';
1414
import tableTypes from 'docs:@react-types/table/src/index.d.ts';
15-
import {HeaderInfo, PropTable, PageDescription} from '@react-spectrum/docs';
15+
import {HeaderInfo, PropTable, PageDescription, VersionBadge} from '@react-spectrum/docs';
1616
import {Keyboard} from '@react-spectrum/text';
1717
import packageData from '@react-spectrum/table/package.json';
1818

@@ -560,6 +560,65 @@ function AsyncSortTable() {
560560
}
561561
```
562562

563+
## Column Resizing <VersionBadge version="beta" style={{marginLeft: 4, verticalAlign: 'bottom'}} />
564+
565+
TableView supports resizable columns, allowing users to dynamically adjust the width of a column. To designate that a Column is resizable, provide it with the `allowsResizing` prop. This will render a draggable
566+
resizer handle for each resizable column that becomes visible on hover. Keyboard, touch, and screen reader users can enter "resize" mode by interacting with the target column's header and selecting the "Resize column" option
567+
from the dropdown.
568+
569+
### Width values
570+
571+
Columns support four different width props: `defaultWidth`, `width`, `minWidth`, and `maxWidth`. Each of these props accepts fixed values or percentages, with `width` and `defaultWidth` accepting fractional (`fr`) units as well.
572+
Providing a `width` to a Column sets its width to the value provided to it, making it unaffected by any changes to other columns' widths unless a fractional value is provided. Providing a `defaultWidth` allows you
573+
to specify a Column's initial width, but allows the column width to freely shrink and grow in response to changes in other columns' widths. `minWidth` and `maxWidth` allows you to restrict a Column's width to a minimum and
574+
maximum value respectively. In general, TableView prioritizes columns with defined widths and divides the remaining space evenly amongst the other columns.
575+
576+
### Resize events
577+
578+
TableView accepts an `onResize` prop which is triggered whenever a column resizer is moved by the user. This can be used in combination with the `width` prop to update a Column's in a controlled fashion. TableView also accepts
579+
an `onResizeEnd` prop as well, triggered when the user finishes a column resize operation. Both events receive a Map object containing the widths of every column.
580+
581+
The example below illustrates how each of the applied column width properties affects their resize behavior. It also demonstrates the difference between when `onResize` and `onResizeEnd` fires and the values
582+
provided to both.
583+
584+
TODO: fix types for width props
585+
```tsx example
586+
function ResizableTable() {
587+
let [widths, setWidths] = React.useState(new Map());
588+
let [finalWidths, setFinalWidths] = React.useState(new Map());
589+
// TODO: fix bug where null is returned when just clicking the column header
590+
591+
return (
592+
<Flex direction="column">
593+
<TableView aria-label="TableView with resizable columns" width={600} height={200} onResize={setWidths} onResizeEnd={setFinalWidths}>
594+
<TableHeader>
595+
<Column key="file" allowsResizing maxWidth={500}>File Name</Column>
596+
<Column key="type" allowsResizing defaultWidth="1fr">Type</Column>
597+
<Column key="size" allowsResizing width={80}>Size</Column>
598+
<Column key="date" allowsResizing minWidth={100}>Date Modified</Column>
599+
</TableHeader>
600+
<TableBody>
601+
<Row>
602+
<Cell>2022-Roadmap-Proposal-Revision-012822-Copy(2)</Cell>
603+
<Cell>PDF</Cell>
604+
<Cell>214 KB</Cell>
605+
<Cell>November 27, 2022 at 4:56PM</Cell>
606+
</Row>
607+
<Row>
608+
<Cell>62259692_p0_master1200</Cell>
609+
<Cell>PNG</Cell>
610+
<Cell>120 KB</Cell>
611+
<Cell>January 27, 2021 at 1:56AM</Cell>
612+
</Row>
613+
</TableBody>
614+
</TableView>
615+
<pre>Column widths during resize: {widths ? JSON.stringify(Object.fromEntries(widths)) : widths}</pre>
616+
<pre>Final column widths: {finalWidths ? JSON.stringify(Object.fromEntries(finalWidths)) : finalWidths}</pre>
617+
</Flex>
618+
);
619+
}
620+
```
621+
563622
## Props
564623

565624
### TableView props
@@ -642,41 +701,6 @@ function AsyncSortTable() {
642701
</TableView>
643702
```
644703

645-
### Column widths
646-
Columns support three different width props: `minWidth`, `width`, and `maxWidth`. Each of these props accepts fixed values or percentages. TableView prioritizes columns with defined widths and divides the remaining space evenly amongst the other columns.
647-
648-
```tsx example
649-
<TableView aria-label="Example table for column widths">
650-
<TableHeader>
651-
<Column maxWidth={300} align="start">Name</Column>
652-
<Column width={80}>Type</Column>
653-
<Column minWidth={100} align="end">Size</Column>
654-
</TableHeader>
655-
<TableBody>
656-
<Row>
657-
<Cell>2021406_Proposal</Cell>
658-
<Cell>PDF</Cell>
659-
<Cell>86 KB</Cell>
660-
</Row>
661-
<Row>
662-
<Cell>Budget Template</Cell>
663-
<Cell>XLS</Cell>
664-
<Cell>120 KB</Cell>
665-
</Row>
666-
<Row>
667-
<Cell>Onboarding</Cell>
668-
<Cell>PPT</Cell>
669-
<Cell>472 KB</Cell>
670-
</Row>
671-
<Row>
672-
<Cell>Welcome</Cell>
673-
<Cell>TXT</Cell>
674-
<Cell>24 KB</Cell>
675-
</Row>
676-
</TableBody>
677-
</TableView>
678-
```
679-
680704
### Column dividers
681705
[View guidelines](https://spectrum.adobe.com/page/table/#Column-dividers)
682706

0 commit comments

Comments
 (0)