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

Function Table

packages/react-aria/stories/table/example-resizing.tsx:43–156  ·  view source on GitHub ↗
(
  props: TableProps<T> & {
    selectionStyle?: 'highlight' | 'checkbox' | 'none';
    onAction?: (key: Key) => void;
    onResizeStart?: (widths: Map<Key, ColumnSize>) => void;
    onResize?: (widths: Map<Key, ColumnSize>) => void;
    onResizeEnd?: (widths: Map<Key, ColumnSize>) => void;
  }
)

Source from the content-addressed store, hash-verified

41import {VisuallyHidden} from '../../src/visually-hidden/VisuallyHidden';
42
43export function Table<T extends object>(
44 props: TableProps<T> & {
45 selectionStyle?: 'highlight' | 'checkbox' | 'none';
46 onAction?: (key: Key) => void;
47 onResizeStart?: (widths: Map<Key, ColumnSize>) => void;
48 onResize?: (widths: Map<Key, ColumnSize>) => void;
49 onResizeEnd?: (widths: Map<Key, ColumnSize>) => void;
50 }
51): JSX.Element {
52 let [showSelectionCheckboxes, setShowSelectionCheckboxes] = useState(
53 props.selectionStyle !== 'highlight'
54 );
55 let state = useTableState({
56 ...props,
57 showSelectionCheckboxes,
58 selectionBehavior: props.selectionStyle === 'highlight' ? 'replace' : 'toggle'
59 });
60
61 let [tableWidth, setTableWidth] = useState(0);
62 let getDefaultWidth = useCallback(() => undefined, []);
63 let getDefaultMinWidth = useCallback(() => 75, []);
64 let layoutState = useTableColumnResizeState(
65 {
66 getDefaultWidth,
67 getDefaultMinWidth,
68 tableWidth
69 },
70 state
71 );
72
73 // If the selection behavior changes in state, we need to update showSelectionCheckboxes here due to the circular dependency...
74 let shouldShowCheckboxes = state.selectionManager.selectionBehavior !== 'replace';
75 if (shouldShowCheckboxes !== showSelectionCheckboxes) {
76 setShowSelectionCheckboxes(shouldShowCheckboxes);
77 }
78 let ref = useRef<HTMLTableElement | null>(null);
79 let bodyRef = useRef<HTMLElement | null>(null);
80 let {collection} = state;
81 let {gridProps} = useTable(
82 {
83 ...props,
84 onRowAction: props.onAction,
85 scrollRef: bodyRef,
86 'aria-label': 'example table'
87 },
88 state,
89 ref
90 );
91 let layout = useMemo(() => ({...layoutState, tableState: state}), [layoutState, state]);
92
93 useLayoutEffect(() => {
94 if (bodyRef && bodyRef.current) {
95 setTableWidth(bodyRef.current.clientWidth);
96 }
97 }, []);
98 useResizeObserver({ref, onResize: () => setTableWidth(bodyRef.current!.clientWidth)});
99
100 return (

Callers

nothing calls this directly

Calls 5

useTableStateFunction · 0.90
useTableFunction · 0.90
useResizeObserverFunction · 0.90
classNamesFunction · 0.90

Tested by

no test coverage detected