Skip to content

Commit

Permalink
Only persist drop target OR focused key in Spectrum
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Jun 28, 2024
1 parent 0ccad33 commit 1802c1c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
9 changes: 3 additions & 6 deletions packages/@react-spectrum/list/src/ListView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,15 @@ function ListView<T extends object>(props: SpectrumListViewProps<T>, ref: DOMRef
}, state, domRef);

let focusedKey = selectionManager.focusedKey;
let dropTargetKey: Key | null = null;
if (dropState?.target?.type === 'item') {
dropTargetKey = dropState.target.key;
focusedKey = dropState.target.key;
if (dropState.target.dropPosition === 'after') {
// Normalize to the "before" drop position since we only render those in the DOM.
dropTargetKey = state.collection.getKeyAfter(dropTargetKey) ?? dropTargetKey;
focusedKey = state.collection.getKeyAfter(focusedKey) ?? focusedKey;
}
}

let persistedKeys = useMemo(() => {
return new Set([focusedKey, dropTargetKey].filter(k => k !== null));
}, [focusedKey, dropTargetKey]);
let persistedKeys = useMemo(() => focusedKey != null ? new Set([focusedKey]) : null, [focusedKey]);

// wait for layout to get accurate measurements
let [isVerticalScrollbarVisible, setVerticalScollbarVisible] = useState(false);
Expand Down
4 changes: 2 additions & 2 deletions packages/@react-spectrum/list/stories/ListViewDnDExamples.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ let itemList2 = [
];

export function ReorderExample(props) {
let {onDrop, onDragStart, onDragEnd, disabledKeys = ['2'], ...otherprops} = props;
let {items, onDrop, onDragStart, onDragEnd, disabledKeys = ['2'], ...otherprops} = props;
let list = useListData({
initialItems: props.items || itemList1
initialItems: items || itemList1
});

// Use a random drag type so the items can only be reordered within this list and not dragged elsewhere.
Expand Down
11 changes: 4 additions & 7 deletions packages/@react-spectrum/table/src/TableViewBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -404,19 +404,16 @@ function TableViewBase<T extends object>(props: TableBaseProps<T>, ref: DOMRef<H
}, [propsOnResizeEnd, setIsInResizeMode, setIsResizing]);

let focusedKey = state.selectionManager.focusedKey;
let dropTargetKey: Key | null = null;
if (dropState?.target?.type === 'item') {
dropTargetKey = dropState.target.key;
if (dropState.target.dropPosition === 'before' && dropTargetKey !== state.collection.getFirstKey()) {
focusedKey = dropState.target.key;
if (dropState.target.dropPosition === 'before' && focusedKey !== state.collection.getFirstKey()) {
// Normalize to the "after" drop position since we only render those in the DOM.
// The exception to this is for the first row in the table, where we also render the "before" position.
dropTargetKey = state.collection.getKeyBefore(dropTargetKey);
focusedKey = state.collection.getKeyBefore(focusedKey);
}
}

let persistedKeys = useMemo(() => {
return new Set([focusedKey, dropTargetKey].filter(k => k !== null));
}, [focusedKey, dropTargetKey]);
let persistedKeys = useMemo(() => focusedKey != null ? new Set([focusedKey]) : null, [focusedKey]);

let mergedProps = mergeProps(
isTableDroppable && droppableCollection?.collectionProps,
Expand Down

0 comments on commit 1802c1c

Please sign in to comment.