Skip to content

Clarifying the signature of the onMove callback #1861

@stof

Description

@stof

Custom

The doc for options in the readme describes 3 return values for onMove corresponding to the special behaviors (false, 1 and -1): https://github.com/SortableJS/Sortable/tree/88838bfa5647e278f4cf87fdd4c3e34cd24fb33c#options
Reading the source code at

Sortable/src/Sortable.js

Lines 1247 to 1254 in 88838bf

after = direction === 1;
let moveVector = onMove(rootEl, el, dragEl, dragRect, target, targetRect, evt, after);
if (moveVector !== false) {
if (moveVector === 1 || moveVector === -1) {
after = (moveVector === 1);
}
tells me that there is a 4th behavior: for any other returned value (for instance when you don't return anything), the library manages the insertion position automatically based on the direction (exactly as done when you don't specify a onMove callback).

I opened a PR at DefinitelyTyped/DefinitelyTyped#45684 to update the type definitions to change the return type to false | 1 | -1 | void, to account for this behavior (so that the typechecker does not complain when returning nothing). but the maintainers answered saying they prefer to stick to the documented API.
Is this behavior when returning nothing an official one ? Suggested answers for preventing the move to certain positions without forcing the insertion side are relying on this, either returning nothing or returning true depending on the snippets (see #1019 and #1500 for such examples).

What should be the official signature for this callback ?

  1. false | 1 | -1 (current type definition, preventing this behavior)
  2. false | 1 | -1 | void allowing to return nothing to keep the automatic insertion position based on the direction
  3. boolean | 1 | -1 | void allowing to return either true or nothing to keep the automatic insertion position

Based on the decision, it would be great to update the readme to reflect that in the doc (I can submit a PR once I know which variant the maintainers want)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions