UNPKG

9.75 kBTypeScriptView Raw
1/// <reference types="jquery"/>
2
3export as namespace Select2;
4
5// --------------------------------------------------------------------------
6// For jQuery v1 and v2 backward compatibility
7// --------------------------------------------------------------------------
8
9export type Sub<O extends string, D extends string> = { [K in O]: (Record<D, never> & Record<string, K>)[K] }[O];
10
11/**
12 * Same as jQuery v3 `JQuery.AjaxSettingsBase`.
13 */
14export type JQueryAjaxSettingsBase = Pick<JQueryAjaxSettings, Sub<keyof JQueryAjaxSettings, "url">>;
15
16/**
17 * Same as jQuery v3 `JQuery.EventHandlerBase`.
18 */
19// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
20export type JQueryEventHandlerBase<TContext, T> = (this: TContext, t: T, ...args: any[]) => void | false;
21
22/**
23 * Same as jQuery v3 `JQuery.PlainObject`.
24 */
25export interface PlainObject<T = any> {
26 [key: string]: T;
27}
28
29// --------------------------------------------------------------------------
30// Some Interfaces
31// --------------------------------------------------------------------------
32
33export interface Select2 {
34 $container: JQuery;
35 $dropdown: JQuery;
36 $selection: JQuery;
37 $results: JQuery;
38 dropdown: any;
39 id: string;
40 options: { options: Options };
41 results: any;
42 selection: any;
43}
44
45export interface QueryOptions {
46 term?: string | undefined;
47 page?: number | undefined;
48}
49
50export interface SearchOptions {
51 term: string;
52}
53
54export interface DataFormat {
55 id: number | string;
56 text: string;
57 selected?: boolean | undefined;
58 disabled?: boolean | undefined;
59}
60
61export interface GroupedDataFormat {
62 text: string;
63 children?: DataFormat[] | undefined;
64
65 id?: undefined;
66}
67
68export interface ProcessedResult<Result = DataFormat | GroupedDataFormat> {
69 results: Result[];
70 pagination?: { more: boolean } | undefined;
71}
72
73export interface LoadingData {
74 loading: boolean;
75 text: string;
76
77 id?: undefined;
78 element?: undefined;
79}
80
81export interface OptGroupData {
82 children: OptionData[];
83 disabled: boolean;
84 element: HTMLOptGroupElement;
85 selected: boolean;
86 text: string;
87 title: string;
88
89 loading?: undefined;
90}
91
92export interface OptionData {
93 disabled: boolean;
94 element: HTMLOptionElement;
95 id: string;
96 selected: boolean;
97 text: string;
98 title: string;
99
100 loading?: undefined;
101 children?: undefined;
102}
103
104export interface IdTextPair {
105 id: string;
106 text: string;
107
108 loading?: undefined;
109 element?: undefined;
110}
111
112export interface TranslationArg {
113 input: string;
114 minimum: number;
115 maximum: number;
116}
117
118export interface Translation {
119 errorLoading?: (() => string) | undefined;
120 inputTooLong?: ((arg: TranslationArg) => string) | undefined;
121 inputTooShort?: ((arg: TranslationArg) => string) | undefined;
122 loadingMore?: (() => string) | undefined;
123 maximumSelected?: ((arg: TranslationArg) => string) | undefined;
124 noResults?: (() => string) | undefined;
125 searching?: (() => string) | undefined;
126}
127
128export interface DataParams {
129 data: OptionData; // TODO: must be data source
130 originalEvent: BaseJQueryEventObject;
131}
132
133export interface IngParams {
134 name: "select" | "open" | "close" | "unselect";
135 prevented: boolean;
136}
137
138export interface Event<TElement, T> extends BaseJQueryEventObject {
139 params: T;
140}
141
142export interface Trigger {
143 type: "select2:select";
144 params: {
145 data: IdTextPair;
146 };
147}
148
149// --------------------------------------------------------------------------
150// Ajax Option
151// --------------------------------------------------------------------------
152
153export interface AjaxOptions<Result = DataFormat | GroupedDataFormat, RemoteResult = any>
154 extends JQueryAjaxSettingsBase
155{
156 delay?: number | undefined;
157 url?: string | ((params: QueryOptions) => string) | undefined;
158 data?: ((params: QueryOptions) => PlainObject | string) | undefined;
159 transport?:
160 | ((settings: JQueryAjaxSettings, success: (data: RemoteResult) => undefined, failure: () => undefined) => void)
161 | undefined;
162 processResults?: ((data: RemoteResult, params: QueryOptions) => ProcessedResult<Result>) | undefined;
163}
164
165// --------------------------------------------------------------------------
166// Built-in AMD Loader
167// --------------------------------------------------------------------------
168
169export interface Select2Require {
170 config(config: Select2RequireConfig): Select2Require;
171 (module: string): any;
172 (modules: string[]): void;
173 (modules: string[], ready: (...modules: any[]) => void): void;
174 (modules: string[], ready: (...modules: any[]) => void, errback: (err: any) => void): void;
175}
176
177export interface Select2RequireConfig {
178 map?: {
179 [id: string]: {
180 [id: string]: string;
181 };
182 } | undefined;
183 config?: { [id: string]: {} } | undefined;
184 deps?: string[] | undefined;
185 callback?: ((...modules: any[]) => void) | undefined;
186}
187
188// --------------------------------------------------------------------------
189// Options
190// --------------------------------------------------------------------------
191
192export interface Options<Result = DataFormat | GroupedDataFormat, RemoteResult = any> {
193 ajax?: AjaxOptions<Result, RemoteResult> | undefined;
194 allowClear?: boolean | undefined;
195 amdBase?: string | undefined;
196 amdLanguageBase?: string | undefined;
197 closeOnSelect?: boolean | undefined;
198 containerCss?: any;
199 containerCssClass?: string | undefined;
200 data?: DataFormat[] | GroupedDataFormat[] | undefined;
201 dataAdapter?: any;
202 debug?: boolean | undefined;
203 dir?: "ltr" | "rtl" | undefined;
204 disabled?: boolean | undefined;
205 dropdownAdapter?: any;
206 dropdownAutoWidth?: boolean | undefined;
207 dropdownCss?: any;
208 dropdownCssClass?: string | undefined;
209 dropdownParent?: HTMLElement | JQuery | string | undefined;
210 escapeMarkup?: ((markup: string) => string) | undefined;
211 initSelection?: ((element: JQuery, callback: (data: any) => void) => void) | undefined;
212 language?: string | Translation | undefined;
213 matcher?:
214 | ((params: SearchOptions, data: OptGroupData | OptionData) => OptGroupData | OptionData | null)
215 | undefined;
216 maximumInputLength?: number | undefined;
217 maximumSelectionLength?: number | undefined;
218 minimumInputLength?: number | undefined;
219 minimumResultsForSearch?: number | undefined;
220 multiple?: boolean | undefined;
221 placeholder?: string | IdTextPair | undefined;
222 resultsAdapter?: any;
223 selectionAdapter?: any;
224 selectOnClose?: boolean | undefined;
225 sorter?:
226 | ((data: Array<OptGroupData | OptionData | IdTextPair>) => Array<OptGroupData | OptionData | IdTextPair>)
227 | undefined;
228 tags?: boolean | undefined;
229 templateResult?: ((result: LoadingData | Result) => string | JQuery | null) | undefined;
230 templateSelection?:
231 | ((selection: IdTextPair | LoadingData | Result, container: JQuery) => string | JQuery)
232 | undefined;
233 theme?: string | undefined;
234 tokenizer?: ((input: string, selection: any[], selectCallback: () => void, options: Options) => string) | undefined;
235 tokenSeparators?: string[] | undefined;
236 width?: string | undefined;
237
238 // Not in https://select2.org/configuration/options-api
239 createTag?: ((params: SearchOptions) => (IdTextPair & Record<string, any>) | null) | undefined;
240 insertTag?: ((data: Array<OptionData | IdTextPair>, tag: IdTextPair) => void) | undefined;
241}
242
243// --------------------------------------------------------------------------
244// jQuery And Select2 Plugin
245// --------------------------------------------------------------------------
246
247export interface Select2Plugin<TElement = HTMLElement> {
248 amd: { require: Select2Require };
249
250 defaults: {
251 set: (key: string, value: any) => void;
252 reset: () => void;
253 };
254
255 (): JQuery<TElement>;
256 // eslint-disable-next-line @definitelytyped/no-unnecessary-generics
257 <Result = DataFormat | GroupedDataFormat, RemoteResult = any>(
258 options: Options<Result, RemoteResult>,
259 ): JQuery<TElement>;
260
261 /**
262 * Get the data object of the current selection
263 */
264 (method: "data"): OptionData[];
265 /**
266 * Reverts changes to DOM done by Select2. Any selection done via Select2 will be preserved.
267 */
268 (method: "destroy"): JQuery<TElement>;
269 /**
270 * Opens the dropdown
271 */
272 (method: "open"): JQuery<TElement>;
273 /**
274 * Closes the dropdown
275 */
276 (method: "close"): JQuery<TElement>;
277}
278
279declare global {
280 interface JQuery<TElement = HTMLElement> {
281 select2: Select2Plugin<TElement>;
282 data(key: "select2"): Select2;
283
284 trigger(events: Trigger): void;
285
286 // TODO: events "change" and "change.select2"
287 on(events: "select2:closing", handler?: JQueryEventHandlerBase<TElement, Event<TElement, IngParams>>): this;
288 on(events: "select2:close", handler?: JQueryEventHandlerBase<TElement, Event<TElement, {}>>): this;
289 on(events: "select2:opening", handler?: JQueryEventHandlerBase<TElement, Event<TElement, IngParams>>): this;
290 on(events: "select2:open", handler?: JQueryEventHandlerBase<TElement, Event<TElement, {}>>): this;
291 on(events: "select2:selecting", handler?: JQueryEventHandlerBase<TElement, Event<TElement, IngParams>>): this;
292 on(events: "select2:select", handler?: JQueryEventHandlerBase<TElement, Event<TElement, DataParams>>): this;
293 on(events: "select2:unselecting", handler?: JQueryEventHandlerBase<TElement, Event<TElement, IngParams>>): this;
294 on(events: "select2:unselect", handler?: JQueryEventHandlerBase<TElement, Event<TElement, DataParams>>): this;
295 }
296}