UNPKG

5.57 kBTypeScriptView Raw
1import { CSSProperties, ForwardRefExoticComponent, ReactHTML, ReactNode, RefAttributes, Component } from "react";
2import Sortable, { MoveEvent, Options, SortableEvent } from "sortablejs";
3/**
4 * Construct a type with the properties of T except for those in type K.
5 * Including this allows for backwards compatibility with earlier versions of TS.
6 */
7type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
8export interface ItemInterface {
9 /** The unique id associated with your item. It's recommended this is the same as the key prop for your list item. */
10 id: string | number;
11 /** When true, the item is selected using MultiDrag */
12 selected?: boolean;
13 /** When true, the item is deemed "chosen", which basically just a mousedown event. */
14 chosen?: boolean;
15 /** When true, it will not be possible to pick this item up in the list. */
16 filtered?: boolean;
17 [property: string]: any;
18}
19export interface ReactSortableProps<T> extends ReactSortableOptions, Omit<Options, AllMethodNames> {
20 /**
21 * The list of items to use.
22 */
23 list: T[];
24 /**
25 * Sets the state for your list of items.
26 */
27 setList: (newState: T[], sortable: Sortable | null, store: Store) => void;
28 /**
29 * If parsing in a component WITHOUT a ref, an error will be thrown.
30 *
31 * To fix this, use the `forwardRef` component.
32 *
33 * @example
34 * forwardRef<HTMLElement, YOURPROPS>((props, ref) => <button {...props} ref={ref} />)
35 */
36 tag?: ForwardRefExoticComponent<RefAttributes<any>> | keyof ReactHTML;
37 /**
38 * If this is provided, the function will replace the clone in place.
39 *
40 * When an is moved from `A` to `B` with `pull: 'clone'`,
41 * the original element will be moved to `B`
42 * and the new clone will be placed in `A`
43 */
44 clone?: (currentItem: T, evt: SortableEvent) => T;
45 style?: CSSProperties;
46 className?: string;
47 id?: string;
48 children?: ReactNode;
49}
50/**
51 * Holds the react component as a reference so we can access it's store.
52 *
53 * Mainly used to access `props.list` within another components.
54 */
55export interface Store {
56 dragging: null | ReactSortable<any>;
57}
58/**
59 * Change the `on[...]` methods in Sortable.Options,
60 * so that they all have an extra arg that is `store: Store`
61 */
62export type ReactSortableOptions = Partial<Record<AllMethodsExceptMove, (evt: SortableEvent, sortable: Sortable | null, store: Store) => void>> & {
63 /**
64 * The default sortable behaviour has been changed.
65 *
66 * If the return value is void, then the defaults will kick in.
67 * it saves the user trying to figure it out.
68 * and they can just use onmove as a callback value
69 */
70 onMove?: (evt: MoveEvent, originalEvent: Event, sortable: Sortable | null, store: Store) => boolean | -1 | 1 | void;
71};
72/** All method names starting with `on` in `Sortable.Options` */
73export type AllMethodNames = "onAdd" | "onChange" | "onChoose" | "onClone" | "onEnd" | "onFilter" | "onMove" | "onRemove" | "onSort" | "onSpill" | "onStart" | "onUnchoose" | "onUpdate" | "onSelect" | "onDeselect";
74/** Method names that fire in `this`, when this is react-sortable */
75export type HandledMethodNames = "onAdd" | "onRemove" | "onUpdate" | "onStart" | "onEnd" | "onSpill" | "onSelect" | "onDeselect" | "onChoose" | "onUnchoose";
76export type UnHandledMethodNames = Exclude<AllMethodsExceptMove, HandledMethodNames | "onMove">;
77/**
78 * Same as `SortableMethodKeys` type but with out the string `onMove`.
79 */
80export type AllMethodsExceptMove = Exclude<AllMethodNames, "onMove">;
81export 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}
108interface MultiIndices {
109 multiDragElement: HTMLElement;
110 index: number;
111}
112interface MultiDragEvent extends SortableEvent {
113 clones: HTMLElement[];
114 oldIndicies: MultiIndices[];
115 newIndicies: MultiIndices[];
116 swapItem: HTMLElement | null;
117}
118export { default as Sortable, Direction, DOMRect, GroupOptions, MoveEvent, Options, PullResult, PutResult, SortableEvent, SortableOptions, Utils, } from "sortablejs";
119
120//# sourceMappingURL=index.d.ts.map