UNPKG

18.6 kBTypeScriptView Raw
1declare namespace FomanticUI {
2 interface Form {
3 settings: FormSettings;
4
5 /**
6 * Submits selected form.
7 */
8 (behavior: 'submit'): void;
9
10 /**
11 * Returns 'true'/'false' whether a form passes its validation rules.
12 */
13 (behavior: 'is valid'): boolean;
14
15 /**
16 * Adds rule to existing rules for field, also aliased as 'add field'.
17 */
18 (behavior: 'add rule', field: string, rules: object[]): void;
19
20 /**
21 * Adds fields object to existing fields.
22 */
23 (behavior: 'add fields', fields: object[]): void;
24
25 /**
26 * Removes specific rule from field leaving other rules.
27 */
28 (behavior: 'remove rule', field: string, rules: object[]): void;
29
30 /**
31 * Remove all validation for a field.
32 */
33 (behavior: 'remove field', field: string): void;
34
35 /**
36 * Returns 'true'/'false' whether a field passes its validation rules.
37 * If you add 'true' as the second parameter, any failed rule will update the UI.
38 */
39 (behavior: 'is valid', fieldName: string, showErrors: boolean): boolean;
40
41 /**
42 * Validates form, updates UI, and calls 'onSuccess' or 'onFailure'.
43 */
44 (behavior: 'validate form'): void;
45
46 /**
47 * Validates field, updates UI, and calls 'onSuccess' or 'onFailure'.
48 */
49 (behavior: 'validate field', fieldName: string): void;
50
51 /**
52 * Returns element with matching name, id, or data-validate metadata to identifier.
53 */
54 (behavior: 'get field', identifier: string): string;
55
56 /**
57 * Returns value of element with id.
58 */
59 (behavior: 'get value', identifier: string): string;
60
61 /**
62 * Returns object of element values that match array of identifiers.
63 * If no IDS are passed will return all fields.
64 */
65 (behavior: 'get values', identifiers?: string[]): object;
66
67 /**
68 * Sets value of element with id.
69 */
70 (behavior: 'set value', identifier: string, value: string): void;
71
72 /**
73 * Sets key/value pairs from passed values object to matching identifiers.
74 */
75 (behavior: 'set values', values: object[]): JQuery;
76
77 /**
78 * Returns validation rules for a given jQuery-referenced input field.
79 */
80 (behavior: 'get validation', element: JQuery): object;
81
82 /**
83 * Returns whether a field exists.
84 */
85 (behavior: 'has field', identifier: string): boolean;
86
87 /**
88 * Manually add errors to form, given an array errors.
89 */
90 (behavior: 'add errors', errors: object[]): void;
91
92 /**
93 * Removes all current errors from the error message box.
94 */
95 (behavior: 'remove errors'): void;
96
97 /**
98 * Adds a custom user prompt for a given element with identifier.
99 */
100 (behavior: 'add prompt', identifier: string, errors: object[]): void;
101
102 /**
103 * Empty all fields and remove possible errors.
104 */
105 (behavior: 'clear'): void;
106
107 /**
108 * Set all fields to their initial value and remove possible errors.
109 */
110 (behavior: 'reset'): void;
111
112 /**
113 * Set fields actual values as default values.
114 */
115 (behavior: 'set defaults'): void;
116
117 /**
118 * Return elements which have been modified since form state was changed to 'dirty'.
119 */
120 (behavior: 'get dirty fields'): string[];
121
122 /**
123 * Set the state of the form to 'clean' and set new values as default.
124 */
125 (behavior: 'set as clean'): void;
126
127 /**
128 * Automatically adds the "empty" rule or automatically checks a checkbox for all fields with classname or attribute 'required'.
129 */
130 (behavior: 'set auto check'): void;
131
132 /**
133 * Set or unset matching fields as optional.
134 */
135 (behavior: 'set optional', identifier: string, bool: boolean): void;
136
137 /**
138 * Destroys instance and removes all events.
139 */
140 (behavior: 'destroy'): JQuery;
141 <K extends keyof FormSettings>(behavior: 'setting', name: K, value?: undefined, ): Partial<Pick<FormSettings, keyof FormSettings>>;
142 <K extends keyof FormSettings>(behavior: 'setting', name: K, value: FormSettings[K]): JQuery;
143 (behavior: 'setting', value: Partial<Pick<FormSettings, keyof FormSettings>>): JQuery;
144 (settings?: Partial<Pick<FormSettings, keyof FormSettings>>): JQuery;
145 }
146
147 /**
148 * @see {@link https://fomantic-ui.com/behaviors/form.html#/settings}
149 */
150 interface FormSettings {
151 // region Form Settings
152
153 /**
154 * Adds keyboard shortcuts for enter and escape keys to submit form and blur fields respectively.
155 * @default true
156 */
157 keyboardShortcuts: boolean;
158
159 /**
160 * Event used to trigger validation.
161 * Can be either 'submit', 'blur' or 'change'.
162 * @default 'submit'
163 */
164 on: 'submit' | 'blur' | 'change';
165
166 /**
167 * If set to true will revalidate fields with errors on input change.
168 * @default true
169 */
170 revalidate: boolean;
171
172 /**
173 * Delay from last typed letter to validate a field when using 'on: change' or when revalidating a field.
174 * @default true
175 */
176 delay: boolean;
177
178 /**
179 * Adds inline error on field validation error.
180 * @default false
181 */
182 inline: boolean;
183
184 /**
185 * Whether or not the form should trim the value before validating.
186 * @default false
187 */
188 shouldTrim: boolean;
189
190 /**
191 * Define how calendar values will be returned.
192 * Can be either 'date', 'input' or 'formatter'.
193 * @default 'date'
194 */
195 dateHandling: 'date' | 'input' | 'formatter';
196
197 /**
198 * Named transition to use when animating validation errors.
199 * Fade and slide down are available without including 'ui transitions'.
200 * @see {@link https://fomantic-ui.com/modules/transition.html}
201 * @default 'scale'
202 */
203 transition: string;
204
205 /**
206 * Animation speed for inline prompt.
207 * @default 150
208 */
209 duration: number;
210
211 /**
212 * Prevent user from leaving the page if the form has a 'dirty' state by displaying a prompt.
213 * @default false
214 */
215 preventLeaving: boolean;
216
217 /**
218 * Whether fields with classname or attribute 'required' should automatically add the "empty" rule or automatically checks checkbox fields.
219 * @default false
220 */
221 autoCheckRequired: boolean;
222
223 /**
224 * Whether, on an invalid form validation, it should automatically focus either the first error field ('true') or a specific dom node (Use a unique selector string such as '.ui.error.message' or '#myelement') or nothing ('false').
225 * @default false
226 */
227 errorFocus: boolean | string;
228
229 /**
230 *
231 * @default 0
232 */
233 errorLimit: number;
234
235 // endregion
236
237 // region Form Prompts
238
239 text: Form.TextSettings;
240
241 prompt: Form.PromptSettings;
242
243 // endregion
244
245 // region Formatters
246
247 formatter: Form.FormatterSettings;
248
249 // endregion
250
251 // region Callbacks
252
253 /**
254 * Callback on each valid field.
255 */
256 onValid(this: JQuery): void;
257
258 /**
259 * Callback on each invalid field.
260 */
261 onInvalid(this: JQuery): void;
262
263 /**
264 * Callback if a form is all valid.
265 */
266 onSuccess(this: JQuery, event: Event, fields: object[]): void;
267
268 /**
269 * Callback if any form field is invalid.
270 */
271 onFailure(this: JQuery, formErrors: object[], fields: object[]): void;
272
273 /**
274 * Callback if form state is modified to 'dirty'.
275 * Triggered when at least on field has been modified.
276 */
277 onDirty(this: JQuery): void;
278
279 /**
280 * Callback if form state is modified to 'clean'.
281 * Triggered when all fields are set to default values.
282 */
283 onClean(this: JQuery): void;
284
285 // endregion
286
287 // region DOM Settings
288
289 /**
290 * DOM Selectors used internally.
291 * Selectors used to find parts of a module.
292 */
293 selector: Form.SelectorSettings;
294
295 /**
296 * Class names used to determine element state.
297 */
298 metadata: Form.MetadataSettings;
299
300 /**
301 * Class names used to determine element state.
302 */
303 className: Form.ClassNameSettings;
304
305 // endregion
306
307 // region Debug Settings
308
309 /**
310 * Name used in log statements
311 * @default 'Form'
312 */
313 name: string;
314
315 /**
316 * Event namespace. Makes sure module teardown does not effect other events attached to an element.
317 * @default 'form'
318 */
319 namespace: string;
320
321 /**
322 * Silences all console output including error messages, regardless of other debug settings.
323 * @default false
324 */
325 silent: boolean;
326
327 /**
328 * Debug output to console
329 * @default false
330 */
331 debug: boolean;
332
333 /**
334 * Show console.table output with performance metrics
335 * @default true
336 */
337 performance: boolean;
338
339 /**
340 * Debug output includes all internal behaviors
341 * @default false
342 */
343 verbose: boolean;
344
345 errors: Form.ErrorSettings;
346
347 // endregion
348 }
349
350 namespace Form {
351 type TextSettings = Partial<Pick<Settings.Texts, keyof Settings.Texts>>;
352 type PromptSettings = Partial<Pick<Settings.Prompts, keyof Settings.Prompts>>;
353 type FormatterSettings = Partial<Pick<Settings.Formatters, keyof Settings.Formatters>>;
354 type SelectorSettings = Partial<Pick<Settings.Selectors, keyof Settings.Selectors>>;
355 type MetadataSettings = Partial<Pick<Settings.Metadatas, keyof Settings.Metadatas>>;
356 type ClassNameSettings = Partial<Pick<Settings.ClassNames, keyof Settings.ClassNames>>;
357 type ErrorSettings = Partial<Pick<Settings.Errors, keyof Settings.Errors>>;
358
359 namespace Settings {
360 interface Texts {
361 /**
362 * @default 'Please enter a valid value'
363 */
364 unspecifiedRule: string;
365
366 /**
367 * @default 'This field'
368 */
369 unspecifiedField: string;
370
371 /**
372 * @default 'There are unsaved changes on this page which will be discarded if you continue.'
373 */
374 leavingMessage: string;
375 }
376
377 interface Prompts {
378 /**
379 * @default '{name} must have a value'
380 */
381 empty: string;
382
383 /**
384 * @default '{name} must be checked'
385 */
386 checked: string;
387
388 /**
389 * @default '{name} must be a valid e-mail'
390 */
391 email: string;
392
393 /**
394 * @default '{name} must be a valid url'
395 */
396 url: string;
397
398 /**
399 * @default '{name} is not formatted correctly'
400 */
401 regExp: string;
402
403 /**
404 * @default '{name} must be an integer'
405 */
406 integer: string;
407
408 /**
409 * @default '{name} must be a decimal number'
410 */
411 decimal: string;
412
413 /**
414 * @default '{name} must be set to a number'
415 */
416 number: string;
417
418 /**
419 * @default "{name} must be '{ruleValue}'"
420 */
421 is: string;
422
423 /**
424 * @default "{name} must be exactly '{ruleValue}'"
425 */
426 isExactly: string;
427
428 /**
429 * @default "{name} cannot be set to '{ruleValue}'"
430 */
431 not: string;
432
433 /**
434 * @default "{name} cannot be set to exactly '{ruleValue}'"
435 */
436 notExactly: string;
437
438 /**
439 * @default "{name} cannot contain '{ruleValue}'"
440 */
441 contain: string;
442
443 /**
444 * @default "{name} cannot contain exactly '{ruleValue}'"
445 */
446 containExactly: string;
447
448 /**
449 * @default "{name} must contain '{ruleValue}'"
450 */
451 doesntContain: string;
452
453 /**
454 * @default "{name} must contain exactly '{ruleValue}'"
455 */
456 doesntContainExactly: string;
457
458 /**
459 * @default '{name} must be at least {ruleValue} characters'
460 */
461 minLength: string;
462
463 /**
464 * @default '{name} must be exactly {ruleValue} characters'
465 */
466 exactLength: string;
467
468 /**
469 * @default '{name} cannot be longer than {ruleValue} characters'
470 */
471 maxLength: string;
472
473 /**
474 * @default '{name} must match {ruleValue} field'
475 */
476 match: string;
477
478 /**
479 * @default '{name} must have a different value than {ruleValue} field'
480 */
481 different: string;
482
483 /**
484 * @default '{name} must be a valid credit card number'
485 */
486 creditCard: string;
487
488 /**
489 * @default '{name} must have at least {ruleValue} choices'
490 */
491 minCount: string;
492
493 /**
494 * @default '{name} must have exactly {ruleValue} choices'
495 */
496 exactCount: string;
497
498 /**
499 * @default '{name} must have {ruleValue} or less choices'
500 */
501 maxCount: string;
502 }
503
504 interface Formatters {
505 date(date: string): string;
506 datetime(date: string): string;
507 time(date: string): string;
508 month(date: string): string;
509 year(date: string): string;
510 }
511
512 interface Selectors {
513 /**
514 * @default 'input[type="checkbox"], input[type="radio"]'
515 */
516 checkbox: string;
517
518 /**
519 * @default '.clear'
520 */
521 clear: string;
522
523 /**
524 * @default 'input, textarea, select'
525 */
526 field: string;
527
528 /**
529 * @default '.field'
530 */
531 group: string;
532
533 /**
534 * @default 'input'
535 */
536 input: string;
537
538 /**
539 * @default '.error.message'
540 */
541 message: string;
542
543 /**
544 * @default '.prompt.label'
545 */
546 prompt: string;
547
548 /**
549 * @default 'input[type="radio"]'
550 */
551 radio: string;
552
553 /**
554 * @default '.reset:not([type="reset"])'
555 */
556 reset: string;
557
558 /**
559 * @default '.submit:not([type="submit"])'
560 */
561 submit: string;
562
563 /**
564 * @default '.ui.checkbox'
565 */
566 uiCheckbox: string;
567
568 /**
569 * @default '.ui.dropdown'
570 */
571 uiDropdown: string;
572
573 /**
574 * @default '.ui.calendar'
575 */
576 uiCalendar: string;
577 }
578
579 interface Metadatas {
580 /**
581 * @default 'default'
582 */
583 defaultValue: string;
584
585 /**
586 * @default 'validate'
587 */
588 validate: string;
589
590 /**
591 * @default 'isDirty'
592 */
593 isDirty: string;
594 }
595
596 interface ClassNames {
597 /**
598 * @default 'error'
599 */
600 error: string;
601
602 /**
603 * @default 'ui basic red pointing prompt label'
604 */
605 label: string;
606
607 /**
608 * @default 'down'
609 */
610 pressed: string;
611
612 /**
613 * @default 'success'
614 */
615 success: string;
616 }
617
618 interface Errors {
619 /**
620 * @default 'You must specify a string identifier for each field'
621 */
622 identifier: string;
623
624 /**
625 * @default 'The method you called is not defined'
626 */
627 method: string;
628
629 /**
630 * @default 'There is no rule matching the one you specified'
631 */
632 noRule: string;
633
634 /**
635 * @default 'Starting in 2.0 forms now only take a single settings object. Validation settings converted to new syntax automatically.'
636 */
637 oldSyntax: string;
638
639 /**
640 * @default 'Field identifier {identifier} not found'
641 */
642 noField: string;
643
644 /**
645 * @default 'This module requires ui {element}'
646 */
647 noElement: string;
648 }
649 }
650 }
651}