Skip to content

Explain Proxy in TableView #4072

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

Merged
merged 5 commits into from
Feb 16, 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
16 changes: 8 additions & 8 deletions packages/@react-aria/table/stories/example-resizing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
import {classNames} from '@react-spectrum/utils';
import {FocusRing, useFocusRing} from '@react-aria/focus';
import {mergeProps, useLayoutEffect, useResizeObserver} from '@react-aria/utils';
import React, {useCallback, useState} from 'react';
import React, {useCallback, useMemo, useState} from 'react';
import styles from '@adobe/spectrum-css-temp/components/table/vars.css';
import {useCheckbox} from '@react-aria/checkbox';
import {useRef} from 'react';
Expand Down Expand Up @@ -69,6 +69,7 @@ export function Table(props) {
state,
ref
);
let layout = useMemo(() => ({...layoutState, tableState: state}), [layoutState, state]);

useLayoutEffect(() => {
if (bodyRef && bodyRef.current) {
Expand All @@ -85,7 +86,7 @@ export function Table(props) {
{[...headerRow.childNodes].map(column =>
column.props.isSelectionCell
? <TableSelectAllCell key={column.key} column={column} state={state} widths={widths} />
: <TableColumnHeader key={column.key} column={column} state={state} widths={widths} layoutState={layoutState} onResizeStart={props.onResizeStart} onResize={props.onResize} onResizeEnd={props.onResizeEnd} />
: <TableColumnHeader key={column.key} column={column} state={state} widths={widths} layout={layout} onResizeStart={props.onResizeStart} onResize={props.onResize} onResizeEnd={props.onResizeEnd} />
)}
</TableHeaderRow>
))}
Expand Down Expand Up @@ -125,16 +126,15 @@ export function TableHeaderRow({item, state, children, className}) {
</tr>
);
}
function Resizer({column, state, layoutState, onResizeStart, onResize, onResizeEnd}) {
function Resizer({column, layout, onResizeStart, onResize, onResizeEnd}) {
let ref = useRef(null);
let {resizerProps, inputProps} = useTableColumnResize({
column,
label: 'Resizer',
onResizeStart,
onResize,
onResizeEnd,
tableState: state
} as AriaTableColumnResizeProps<any>, layoutState, ref);
onResizeEnd
} as AriaTableColumnResizeProps<any>, layout, ref);

return (
<>
Expand Down Expand Up @@ -163,7 +163,7 @@ function Resizer({column, state, layoutState, onResizeStart, onResize, onResizeE
</>
);
}
export function TableColumnHeader({column, state, widths, layoutState, onResizeStart, onResize, onResizeEnd}) {
export function TableColumnHeader({column, state, widths, layout, onResizeStart, onResize, onResizeEnd}) {
let ref = useRef();
let {columnHeaderProps} = useTableColumnHeader({node: column}, state, ref);
let {isFocusVisible, focusProps} = useFocusRing();
Expand Down Expand Up @@ -197,7 +197,7 @@ export function TableColumnHeader({column, state, widths, layoutState, onResizeS
</div>
{
column.props.allowsResizing &&
<Resizer column={column} state={state} layoutState={layoutState} onResizeStart={onResizeStart} onResize={onResize} onResizeEnd={onResizeEnd} />
<Resizer column={column} layout={layout} onResizeStart={onResizeStart} onResize={onResize} onResizeEnd={onResizeEnd} />
}
</div>
</th>
Expand Down
3 changes: 3 additions & 0 deletions packages/@react-spectrum/table/src/TableView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ function TableView<T extends object>(props: SpectrumTableProps<T>, ref: DOMRef<H
// eslint-disable-next-line react-hooks/exhaustive-deps
[props.overflowMode, scale, density, columnLayout]
);

// Use a proxy so that a new object is created for each render so that alternate instances aren't affected by mutation.
// This can be thought of as equivalent to `{…tableLayout, tableState: state}`, but works with classes as well.
let layout = useMemo(() => {
let proxy = new Proxy(tableLayout, {
get(target, prop, receiver) {
Expand Down