1 | import { CSSProperties, ForwardRefExoticComponent, ReactHTML, ReactNode, RefAttributes, Component } from "react";
|
2 | import Sortable, { MoveEvent, Options, SortableEvent } from "sortablejs";
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
|
8 | export interface ItemInterface {
|
9 |
|
10 | id: string | number;
|
11 |
|
12 | selected?: boolean;
|
13 |
|
14 | chosen?: boolean;
|
15 |
|
16 | filtered?: boolean;
|
17 | [property: string]: any;
|
18 | }
|
19 | export interface ReactSortableProps<T> extends ReactSortableOptions, Omit<Options, AllMethodNames> {
|
20 | |
21 |
|
22 |
|
23 | list: T[];
|
24 | |
25 |
|
26 |
|
27 | setList: (newState: T[], sortable: Sortable | null, store: Store) => void;
|
28 | |
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | tag?: ForwardRefExoticComponent<RefAttributes<any>> | keyof ReactHTML;
|
37 | |
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | clone?: (currentItem: T, evt: SortableEvent) => T;
|
45 | style?: CSSProperties;
|
46 | className?: string;
|
47 | id?: string;
|
48 | children?: ReactNode;
|
49 | }
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | export interface Store {
|
56 | dragging: null | ReactSortable<any>;
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | export type ReactSortableOptions = Partial<Record<AllMethodsExceptMove, (evt: SortableEvent, sortable: Sortable | null, store: Store) => void>> & {
|
63 | |
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | onMove?: (evt: MoveEvent, originalEvent: Event, sortable: Sortable | null, store: Store) => boolean | -1 | 1 | void;
|
71 | };
|
72 |
|
73 | export type AllMethodNames = "onAdd" | "onChange" | "onChoose" | "onClone" | "onEnd" | "onFilter" | "onMove" | "onRemove" | "onSort" | "onSpill" | "onStart" | "onUnchoose" | "onUpdate" | "onSelect" | "onDeselect";
|
74 |
|
75 | export type HandledMethodNames = "onAdd" | "onRemove" | "onUpdate" | "onStart" | "onEnd" | "onSpill" | "onSelect" | "onDeselect" | "onChoose" | "onUnchoose";
|
76 | export type UnHandledMethodNames = Exclude<AllMethodsExceptMove, HandledMethodNames | "onMove">;
|
77 |
|
78 |
|
79 |
|
80 | export type AllMethodsExceptMove = Exclude<AllMethodNames, "onMove">;
|
81 | export class ReactSortable<T extends ItemInterface> extends Component<ReactSortableProps<T>> {
|
82 | static defaultProps: Partial<ReactSortableProps<any>>;
|
83 | constructor(props: ReactSortableProps<T>);
|
84 | componentDidMount(): void;
|
85 | componentDidUpdate(prevProps: ReactSortableProps<T>): void;
|
86 | render(): JSX.Element;
|
87 | /** Appends the `sortable` property to this component */
|
88 | private get sortable();
|
89 | /** Converts all the props from `ReactSortable` into the `options` object that `Sortable.create(el, [options])` can use. */
|
90 | makeOptions(): Options;
|
91 | /** Prepares a method that will be used in the sortable options to call an `on[Handler]` prop & an `on[Handler]` ReactSortable method. */
|
92 | prepareOnHandlerPropAndDOM(evtName: HandledMethodNames): (evt: SortableEvent) => void;
|
93 | /** Prepares a method that will be used in the sortable options to call an `on[Handler]` prop */
|
94 | prepareOnHandlerProp(evtName: Exclude<AllMethodsExceptMove, HandledMethodNames>): (evt: SortableEvent) => void;
|
95 | /** Calls the `props.on[Handler]` function */
|
96 | callOnHandlerProp(evt: SortableEvent, evtName: AllMethodsExceptMove): void;
|
97 | onAdd(evt: MultiDragEvent): void;
|
98 | onRemove(evt: MultiDragEvent): void;
|
99 | onUpdate(evt: MultiDragEvent): void;
|
100 | onStart(): void;
|
101 | onEnd(): void;
|
102 | onChoose(evt: SortableEvent): void;
|
103 | onUnchoose(evt: SortableEvent): void;
|
104 | onSpill(evt: SortableEvent): void;
|
105 | onSelect(evt: MultiDragEvent): void;
|
106 | onDeselect(evt: MultiDragEvent): void;
|
107 | }
|
108 | interface MultiIndices {
|
109 | multiDragElement: HTMLElement;
|
110 | index: number;
|
111 | }
|
112 | interface MultiDragEvent extends SortableEvent {
|
113 | clones: HTMLElement[];
|
114 | oldIndicies: MultiIndices[];
|
115 | newIndicies: MultiIndices[];
|
116 | swapItem: HTMLElement | null;
|
117 | }
|
118 | export { default as Sortable, Direction, DOMRect, GroupOptions, MoveEvent, Options, PullResult, PutResult, SortableEvent, SortableOptions, Utils, } from "sortablejs";
|
119 |
|
120 |
|