zidan-select 2.2.4 → 2.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,431 @@
1
+ /**
2
+ * Type definitions for ZidanForm / zidan-select.
3
+ * https://github.com/akhtar1985/zidan-select
4
+ */
5
+
6
+ export type SelectMode = 'select' | 'combobox' | 'tags';
7
+ export type SelectLayout =
8
+ | 'classic-clean'
9
+ | 'floating-label'
10
+ | 'soft-pill'
11
+ | 'bordered-brutalist'
12
+ | 'glass-elevated';
13
+ export type Framework = 'auto' | 'none' | 'vanilla' | 'bootstrap' | 'tailwind' | 'bulma' | 'foundation';
14
+ export type Theme = 'default' | 'modern' | 'minimal' | 'dark' | 'glass';
15
+ export type FieldLayout = 'stacked' | 'inline' | 'card' | 'compact' | 'floating' | 'outlined';
16
+ export type Scrollbar = 'default' | 'thin' | 'fancy' | 'hover' | 'hidden';
17
+ export type ValidationState = 'valid' | 'invalid' | null | undefined;
18
+ export type PhoneFormat = 'national' | 'international' | 'e164';
19
+
20
+ export interface SelectItem {
21
+ value: string | number;
22
+ label?: string;
23
+ group?: string | null;
24
+ disabled?: boolean;
25
+ data?: unknown;
26
+ }
27
+
28
+ export interface SelectGroup {
29
+ label?: string;
30
+ group?: string;
31
+ options: SelectItemInput[];
32
+ }
33
+
34
+ export type SelectItemInput = string | number | SelectItem | SelectGroup;
35
+
36
+ export interface FetchContext {
37
+ page: number;
38
+ pageSize: number;
39
+ append: boolean;
40
+ instance: ZidanSelect;
41
+ }
42
+
43
+ export interface FetchPage {
44
+ items?: SelectItemInput[];
45
+ options?: SelectItemInput[];
46
+ results?: SelectItemInput[];
47
+ hasMore?: boolean;
48
+ total?: number;
49
+ }
50
+
51
+ export type FetchResult = SelectItemInput[] | FetchPage;
52
+
53
+ export type FetchOptions = (
54
+ query: string,
55
+ signal?: AbortSignal,
56
+ context?: FetchContext
57
+ ) => FetchResult | Promise<FetchResult>;
58
+
59
+ export interface SelectCallbacks {
60
+ filterOption?(item: SelectItem, query: string, context: { instance: ZidanSelect; mode: SelectMode; caseSensitive: boolean; selected: boolean }): boolean;
61
+ renderOption?(item: SelectItem, context: { index: number; selected: boolean; active: boolean; query: string; instance: ZidanSelect }): string | Node | null | undefined;
62
+ renderSelected?(item: SelectItem, instance: ZidanSelect): string | Node | null | undefined;
63
+ renderTag?(item: SelectItem, instance: ZidanSelect): string | Node | null | undefined;
64
+ validateTag?(tag: string, tags: string[], context: { instance: ZidanSelect }): boolean | string | void;
65
+ formatTag?(tag: string, instance: ZidanSelect): string;
66
+ onInit?(detail: { instance: ZidanSelect; mode: SelectMode; value: unknown }): void;
67
+ onChange?(detail: { value: string | string[]; previousValue?: string | string[]; item?: SelectItem; instance: ZidanSelect }): void;
68
+ onSelect?(detail: { value: string | string[]; item: SelectItem; instance: ZidanSelect }): void;
69
+ onOpen?(detail: { instance: ZidanSelect }): void;
70
+ onClose?(detail: { instance: ZidanSelect }): void;
71
+ onTagAdd?(detail: { tag: string; tags: string[]; instance: ZidanSelect }): void;
72
+ onTagRemove?(detail: { tag: string; tags: string[]; instance: ZidanSelect }): void;
73
+ onLoadMore?(detail: { page: number; query: string; instance: ZidanSelect }): void;
74
+ onError?(detail: { error: unknown; message: string; instance: ZidanSelect }): void;
75
+ }
76
+
77
+ export interface SelectOptions extends SelectCallbacks {
78
+ mode?: SelectMode;
79
+ layout?: SelectLayout;
80
+ theme?: Theme | string;
81
+ themeFramework?: Framework;
82
+ id?: string;
83
+ name?: string;
84
+ disabled?: boolean;
85
+ required?: boolean;
86
+ options?: SelectItemInput[] | null;
87
+ value?: string | string[] | null;
88
+ label?: string | null;
89
+ placeholder?: string | null;
90
+ multiple?: boolean;
91
+ searchable?: boolean;
92
+ clearable?: boolean;
93
+ closeOnSelect?: boolean | null;
94
+ maxDropdownHeight?: number;
95
+ noResultsText?: string;
96
+ emptyText?: string;
97
+ loadingText?: string;
98
+ clearText?: string;
99
+ fetchOptions?: FetchOptions | null;
100
+ debounce?: number;
101
+ minChars?: number;
102
+ allowFreeText?: boolean;
103
+ searchValues?: boolean;
104
+ /** > 0 enables paged fetchOptions() results and a load-more footer */
105
+ fetchPageSize?: number;
106
+ autoLoadMore?: boolean;
107
+ cacheResults?: boolean;
108
+ loadMoreText?: string;
109
+ maxTags?: number;
110
+ minTagLength?: number;
111
+ maxTagLength?: number;
112
+ caseSensitive?: boolean;
113
+ delimiters?: string[];
114
+ commitOnTab?: boolean;
115
+ commitOnBlur?: boolean;
116
+ suggestions?: SelectItemInput[] | null;
117
+ valueDelimiter?: string;
118
+ customClasses?: { [key: string]: string };
119
+ validation?: ValidationState;
120
+ scrollbar?: Scrollbar;
121
+ }
122
+
123
+ export interface SelectAsyncState {
124
+ query: string;
125
+ page: number;
126
+ hasMore: boolean;
127
+ loading: boolean;
128
+ fetchingMore: boolean;
129
+ error: string | null;
130
+ }
131
+
132
+ export interface AsyncValidationContext {
133
+ signal?: AbortSignal;
134
+ field: ZidanFieldBase;
135
+ instance: ZidanFieldBase;
136
+ }
137
+
138
+ export type AsyncRuleResult = boolean | string | Error | void | null | undefined;
139
+ export type RuleResult = AsyncRuleResult | Promise<AsyncRuleResult>;
140
+ export type FieldRule = (value: unknown, field: ZidanFieldBase, context: AsyncValidationContext) => RuleResult;
141
+ export type DateRule = (value: string, date: Date, field: ZidanTextField, context: AsyncValidationContext) => RuleResult;
142
+
143
+ export interface FieldMessages {
144
+ required: string;
145
+ email: string;
146
+ url: string;
147
+ pattern: string;
148
+ min: string;
149
+ max: string;
150
+ step: string;
151
+ minlength: string;
152
+ maxlength: string;
153
+ badInput: string;
154
+ phone: string;
155
+ integer: string;
156
+ date: string;
157
+ weekend: string;
158
+ dateDisabled: string;
159
+ fileType: string;
160
+ fileSize: string;
161
+ fileCount: string;
162
+ pending: string;
163
+ generic: string;
164
+ [key: string]: string;
165
+ }
166
+
167
+ export interface FieldOptions {
168
+ theme?: Theme | string;
169
+ framework?: Framework;
170
+ layout?: FieldLayout;
171
+ validation?: ValidationState;
172
+ messages?: Partial<FieldMessages>;
173
+ /** sync rules return boolean | message; promise results get a pending state */
174
+ validate?: FieldRule;
175
+ alwaysValidate?: boolean;
176
+ showValidState?: boolean;
177
+ label?: string;
178
+ hint?: string;
179
+ uploadUrl?: string;
180
+ uploadHandler?: UploadHandler;
181
+ [key: string]: unknown;
182
+ }
183
+
184
+ export interface UploadContext {
185
+ file: File;
186
+ field: ZidanFileUpload;
187
+ formData: FormData;
188
+ signal: AbortSignal;
189
+ onProgress(percent: number): void;
190
+ complete(result?: unknown): void;
191
+ fail(error?: unknown): void;
192
+ }
193
+
194
+ export type UploadHandler = (file: File, context: UploadContext) => void | Promise<unknown>;
195
+
196
+ export interface FileFieldOptions extends FieldOptions {
197
+ variant?: string;
198
+ accept?: string;
199
+ multiple?: boolean;
200
+ maxFiles?: number;
201
+ maxSize?: number;
202
+ uploadUrl?: string;
203
+ uploadHandler?: UploadHandler;
204
+ simulateUpload?: boolean;
205
+ autoUpload?: boolean;
206
+ previews?: boolean;
207
+ }
208
+
209
+ export interface TextFieldOptions extends FieldOptions {
210
+ icon?: string;
211
+ iconPosition?: 'start' | 'end';
212
+ prefix?: string;
213
+ suffix?: string;
214
+ clearable?: boolean;
215
+ counter?: boolean;
216
+ counterMax?: number;
217
+ output?: HTMLOutputElement | string;
218
+ stepper?: boolean;
219
+ stepperMode?: string;
220
+ phone?: boolean;
221
+ phoneCountry?: string;
222
+ phoneFormat?: PhoneFormat;
223
+ rangeVariant?: 'default' | 'gradient' | 'stepped' | 'minimal';
224
+ rangeBubble?: boolean;
225
+ rangeBubbleMode?: 'always' | 'active' | 'never';
226
+ rangeBubblePosition?: 'top' | 'bottom';
227
+ rangeFill?: boolean;
228
+ rangeMinMax?: boolean;
229
+ rangeTicks?: number[] | number | string;
230
+ rangeTickLabels?: boolean;
231
+ rangeMinLabel?: string;
232
+ rangeMaxLabel?: string;
233
+ rangeFormat?: string | ((value: number, field: ZidanTextField) => string);
234
+ rangeLabels?: Record<string, string>;
235
+ disableWeekends?: boolean;
236
+ disabledDates?: string[] | string | ((value: string, date: Date) => boolean);
237
+ validateDate?: DateRule;
238
+ rows?: number;
239
+ }
240
+
241
+ export interface UploadState {
242
+ file: File;
243
+ name: string;
244
+ size: number;
245
+ status: 'idle' | 'uploading' | 'success' | 'error' | 'canceled' | string;
246
+ progress: number;
247
+ error: string;
248
+ result: unknown;
249
+ }
250
+
251
+ export class ZidanFieldBase {
252
+ constructor(target: HTMLElement, options?: FieldOptions, kind?: string);
253
+ readonly el: HTMLElement;
254
+ readonly root: HTMLElement;
255
+ readonly kind: string;
256
+ readonly opts: FieldOptions & { messages: FieldMessages };
257
+ validation: ValidationState;
258
+ errorMessage: string | null;
259
+ pending: boolean;
260
+ setTheme(theme: Theme | string): this;
261
+ setFramework(framework: Framework): this;
262
+ setLayout(layout: FieldLayout): this;
263
+ setError(message?: string | null): this;
264
+ clearError(): this;
265
+ setValidation(state: ValidationState): this;
266
+ getValidation(): ValidationState;
267
+ isPending(): boolean;
268
+ /** boolean for sync rules, Promise<boolean> when an async rule runs */
269
+ validate(): boolean | Promise<boolean>;
270
+ checkAsync(): Promise<boolean>;
271
+ getValue(): unknown;
272
+ setValue(value: unknown): this;
273
+ update(): this;
274
+ refresh(): this;
275
+ on<K extends EventTarget>(node: K | null, type: string, handler: EventListener, config?: boolean | AddEventListenerOptions): this;
276
+ emit(name: string, detail?: Record<string, unknown>): this;
277
+ destroy(): this;
278
+ }
279
+
280
+ export class ZidanSelect {
281
+ constructor(target: string | HTMLSelectElement | HTMLElement, options?: SelectOptions);
282
+ readonly el: HTMLElement;
283
+ readonly root: HTMLElement;
284
+ readonly mode: SelectMode;
285
+ readonly layout: SelectLayout;
286
+ open(): this;
287
+ close(): this;
288
+ toggle(): this;
289
+ getValue(): string | string[];
290
+ setValue(value: string | number | Array<string | number> | null, options?: { silent?: boolean }): this;
291
+ clear(): this;
292
+ setOptions(options: SelectItemInput[] | null): this;
293
+ addTag(label: string, data?: unknown): this;
294
+ removeTag(index: number): this;
295
+ getTags(): SelectItem[];
296
+ enable(): this;
297
+ disable(): this;
298
+ setLayout(layout: SelectLayout): this;
299
+ setFramework(framework: Framework): this;
300
+ setScrollbar(scrollbar: Scrollbar): this;
301
+ setValidation(state: ValidationState): this;
302
+ isOpen(): boolean;
303
+ refresh(): this;
304
+ on(eventName: string, handler: (event: CustomEvent<Record<string, unknown>>) => void): this;
305
+ off(eventName: string, handler?: (event: CustomEvent<Record<string, unknown>>) => void): this;
306
+ announce(message: string): this;
307
+ /** fetch the next page (remote pagination) */
308
+ loadMore(): this | Promise<SelectItem[]>;
309
+ hasMoreResults(): boolean;
310
+ getAsyncState(): SelectAsyncState;
311
+ clearCache(): this;
312
+ refreshOptions(): this | Promise<SelectItem[]>;
313
+ destroy(): this;
314
+
315
+ static readonly version: string;
316
+ static cacheLimit: number;
317
+ static defaults: SelectOptions;
318
+ static layouts: SelectLayout[];
319
+ static frameworks: Framework[];
320
+ static scrollbars: Scrollbar[];
321
+ static instances: ZidanSelect[];
322
+ static get(target: string | Element | null): ZidanSelect | null;
323
+ static destroyAll(): void;
324
+ }
325
+
326
+ export class ZidanCheckbox extends ZidanFieldBase {
327
+ constructor(target: HTMLInputElement, options?: FieldOptions);
328
+ isChecked(): boolean;
329
+ setChecked(checked: boolean): this;
330
+ setIndeterminate(value: boolean): this;
331
+ }
332
+
333
+ export class ZidanRadioGroup extends ZidanFieldBase {
334
+ constructor(target: HTMLElement | HTMLInputElement | RadioNodeList, options?: FieldOptions);
335
+ getValue(): string;
336
+ setValue(value: string): this;
337
+ }
338
+
339
+ export class ZidanFileUpload extends ZidanFieldBase {
340
+ constructor(target: HTMLInputElement, options?: FileFieldOptions);
341
+ readonly hasUploader: boolean;
342
+ getFiles(): File[];
343
+ getUploadState(): UploadState[];
344
+ addFiles(files: File[] | FileList): this;
345
+ replaceFiles(files: File[] | FileList): this;
346
+ removeFile(index: number): this;
347
+ clear(): this;
348
+ uploadAll(): this | Promise<boolean>;
349
+ uploadFile(file: File): this | Promise<unknown>;
350
+ cancelUpload(file: File): this;
351
+ cancelAll(): this;
352
+ retryUpload(file: File): this;
353
+ setVariant(variant: string): this;
354
+ setUploader(handler: UploadHandler | null): this;
355
+ }
356
+
357
+ export class ZidanTextField extends ZidanFieldBase {
358
+ constructor(target: HTMLInputElement | HTMLTextAreaElement, options?: TextFieldOptions);
359
+ readonly phone: { country: string; format: PhoneFormat } | null;
360
+ stepUp(times?: number): this;
361
+ stepDown(times?: number): this;
362
+ setStepper(enabled: boolean): this;
363
+ openPicker(): void;
364
+ showPicker(): void;
365
+ getPhoneCountry(): string;
366
+ setPhoneCountry(iso: string): this;
367
+ setPhoneFormat(format: PhoneFormat): this;
368
+ formatPhone(value?: string): string;
369
+ getValue(): string;
370
+ setValue(value: string | number | null): this;
371
+ }
372
+
373
+ export interface ZidanFormStatic {
374
+ version: string;
375
+ themes: string[];
376
+ frameworks: string[];
377
+ fieldLayouts: string[];
378
+ fields: ZidanFieldBase[];
379
+ messages: FieldMessages;
380
+ icons: Record<string, string>;
381
+ variants: {
382
+ checkbox?: string[];
383
+ switch?: string[];
384
+ radio?: string[];
385
+ file?: string[];
386
+ range?: string[];
387
+ };
388
+ stepperModes?: string[];
389
+ phoneCountries?: Record<string, { name: string; code: string; min: number; max: number }>;
390
+ phoneFormats?: PhoneFormat[];
391
+ api: Record<string, unknown> | null;
392
+ registerIcon(name: string, markup: string): typeof ZidanSelect | ZidanFormStatic | undefined;
393
+ get(target: string | Element | null): ZidanFieldBase | null;
394
+ init(scope?: Document | Element, options?: FieldOptions): ZidanFieldBase[];
395
+ field: typeof zidanField;
396
+ refresh(scope?: Document | Element): ZidanFieldBase[];
397
+ destroyAll(): ZidanFormStatic;
398
+ observe(scope?: Element, options?: FieldOptions): MutationObserver | null;
399
+ formatPhone?(value: string, country?: string, format?: PhoneFormat): string;
400
+ detectPhoneCountry?(value: string): string;
401
+ }
402
+
403
+ export const ZidanForm: ZidanFormStatic;
404
+
405
+ export function zidanSelect(target: string | Element | ArrayLike<Element>, options?: SelectOptions): ZidanSelect | ZidanSelect[];
406
+ export function initZidanSelects(container?: Document | Element, options?: SelectOptions): ZidanSelect[];
407
+ export function zidanField(target: string | Element, options?: FieldOptions): ZidanFieldBase | null;
408
+ export function zidanField(target: ArrayLike<Element>, options?: FieldOptions): ZidanFieldBase[];
409
+ export function initZidanForms(container?: Document | Element, options?: FieldOptions): ZidanFieldBase[];
410
+ export function registerIcon(name: string, markup: string): typeof ZidanSelect | ZidanFormStatic | undefined;
411
+ export function formatPhone(value: string, country?: string, format?: PhoneFormat): string;
412
+ export function detectPhoneCountry(value: string): string;
413
+
414
+ export interface AutoInitTarget {
415
+ selector: string;
416
+ options?: SelectOptions | FieldOptions;
417
+ }
418
+
419
+ export interface AutoInitConfig {
420
+ autoInit?: boolean;
421
+ selector?: string | AutoInitTarget | Array<string | AutoInitTarget>;
422
+ targets?: Array<string | AutoInitTarget>;
423
+ selectors?: Array<string | AutoInitTarget>;
424
+ fieldTargets?: Array<string | AutoInitTarget>;
425
+ formTargets?: Array<string | AutoInitTarget>;
426
+ options?: SelectOptions;
427
+ fieldOptions?: FieldOptions;
428
+ fields?: FieldOptions;
429
+ }
430
+
431
+ export default ZidanForm;