MCPcopy Index your code
hub / github.com/adobe/react-spectrum / Table

Function Table

packages/react-aria/stories/table/example.tsx:32–98  ·  view source on GitHub ↗
(
  props: TableProps<T> & {
    selectionStyle?: 'highlight' | 'checkbox';
    selectionBehavior?: SelectionBehavior;
    onAction?: (key: Key) => void;
  }
)

Source from the content-addressed store, hash-verified

30import {VisuallyHidden} from '../../src/visually-hidden/VisuallyHidden';
31
32export function Table<T extends object>(
33 props: TableProps<T> & {
34 selectionStyle?: 'highlight' | 'checkbox';
35 selectionBehavior?: SelectionBehavior;
36 onAction?: (key: Key) => void;
37 }
38): JSX.Element {
39 let [showSelectionCheckboxes, setShowSelectionCheckboxes] = useState(
40 props.selectionStyle !== 'highlight'
41 );
42 let state = useTableState({
43 ...props,
44 showSelectionCheckboxes,
45 selectionBehavior: props.selectionStyle === 'highlight' ? 'replace' : 'toggle'
46 });
47 // If the selection behavior changes in state, we need to update showSelectionCheckboxes here due to the circular dependency...
48 let shouldShowCheckboxes = state.selectionManager.selectionBehavior !== 'replace';
49 if (shouldShowCheckboxes !== showSelectionCheckboxes) {
50 setShowSelectionCheckboxes(shouldShowCheckboxes);
51 }
52 let ref = useRef<HTMLTableElement | null>(null);
53 let bodyRef = useRef<HTMLElement | null>(null);
54 let {collection} = state;
55 let {gridProps} = useTable(
56 {
57 ...props,
58 onRowAction: props.onAction,
59 scrollRef: bodyRef
60 },
61 state,
62 ref
63 );
64
65 return (
66 <table {...gridProps} ref={ref} style={{borderCollapse: 'collapse'}}>
67 <TableRowGroup type="thead" style={{borderBottom: '2px solid gray', display: 'block'}}>
68 {collection.headerRows.map(headerRow => (
69 <TableHeaderRow key={headerRow.key} item={headerRow} state={state}>
70 {[...state.collection.getChildren!(headerRow.key)].map(column =>
71 column.props.isSelectionCell ? (
72 <TableSelectAllCell key={column.key} column={column} state={state} />
73 ) : (
74 <TableColumnHeader key={column.key} column={column} state={state} />
75 )
76 )}
77 </TableHeaderRow>
78 ))}
79 </TableRowGroup>
80 <TableRowGroup
81 ref={bodyRef}
82 type="tbody"
83 style={{display: 'block', overflow: 'auto', maxHeight: '200px'}}>
84 {[...collection].map(row => (
85 <TableRow key={row.key} item={row} state={state}>
86 {[...state.collection.getChildren!(row.key)].map(cell =>
87 cell.props.isSelectionCell ? (
88 <TableCheckboxCell key={cell.key} cell={cell} state={state} />
89 ) : (

Callers

nothing calls this directly

Calls 2

useTableStateFunction · 0.90
useTableFunction · 0.90

Tested by

no test coverage detected