Skip to content
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

Adds support for a accept list in the SortableContext #1293

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 10 additions & 1 deletion packages/sortable/src/components/SortableContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {getSortedRects, itemsEqual, normalizeDisabled} from '../utilities';
import {rectSortingStrategy} from '../strategies';

export interface Props {
accepts: string[];
children: React.ReactNode;
items: (UniqueIdentifier | {id: UniqueIdentifier})[];
strategy?: SortingStrategy;
Expand All @@ -17,6 +18,7 @@ export interface Props {
const ID_PREFIX = 'Sortable';

interface ContextDescriptor {
accepts: string[];
activeIndex: number;
containerId: string;
disabled: Disabled;
Expand All @@ -32,6 +34,7 @@ export const Context = React.createContext<ContextDescriptor>({
activeIndex: -1,
containerId: ID_PREFIX,
disableTransforms: false,
accepts: [],
items: [],
overIndex: -1,
useDragOverlay: false,
Expand All @@ -44,6 +47,7 @@ export const Context = React.createContext<ContextDescriptor>({
});

export function SortableContext({
accepts,
children,
id,
items: userDefinedItems,
Expand Down Expand Up @@ -72,7 +76,10 @@ export function SortableContext({
const previousItemsRef = useRef(items);
const itemsHaveChanged = !itemsEqual(items, previousItemsRef.current);
const disableTransforms =
(overIndex !== -1 && activeIndex === -1) || itemsHaveChanged;
(overIndex !== -1 &&
activeIndex === -1 &&
!accepts.includes(active?.data?.current?.type)) ||
itemsHaveChanged;
const disabled = normalizeDisabled(disabledProp);

useIsomorphicLayoutEffect(() => {
Expand All @@ -87,6 +94,7 @@ export function SortableContext({

const contextValue = useMemo(
(): ContextDescriptor => ({
accepts,
activeIndex,
containerId,
disabled,
Expand All @@ -99,6 +107,7 @@ export function SortableContext({
}),
// eslint-disable-next-line react-hooks/exhaustive-deps
[
accepts,
activeIndex,
containerId,
disabled.draggable,
Expand Down
4 changes: 3 additions & 1 deletion packages/sortable/src/hooks/useSortable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export function useSortable({
transition = defaultTransition,
}: Arguments) {
const {
accepts,
items,
containerId,
activeIndex,
Expand Down Expand Up @@ -110,7 +111,8 @@ export function useSortable({
const displaceItem =
isSorting &&
!disableTransforms &&
isValidIndex(activeIndex) &&
(isValidIndex(activeIndex) ||
accepts.includes(active?.data?.current?.type)) &&
isValidIndex(overIndex);
const shouldDisplaceDragSource = !useDragOverlay && isDragging;
const dragSourceDisplacement =
Expand Down