Skip to content

Improve typings for DragTarget #108

@softmarshmallow

Description

@softmarshmallow

currently, the second argument, target: DragTarget from onDraop is typed as below.

export type DragTarget<T> = {
    item: ItemInstance<T>;
    childIndex: number;
    insertionIndex: number;
    dragLineIndex: number;
    dragLineLevel: number;
} | {
    item: ItemInstance<T>;
};

This introduces laibility over "insertionIndex" in target syntax for property check, which can break without error in future updates (as the package updates)

    onDrop(items, target) {
      const b_index = "insertionIndex" in target ? target.insertionIndex : 0;
      console.log("drop", items, target.item.getItemName());
    },

Introduce a flag or type, and make the properties always available 0 or null. (or even -1)

export type DragTarget<T> = {
    item: ItemInstance<T>;
    childIndex: number;
    insertionIndex: number;
    dragLineIndex: number | null;
    dragLineLevel: number | null;
};

I don't know the details behind this design, but Having childIndex insertionIndex as always available, fallback to 0 or -1 makes good sense.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions