mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-12-03 11:41:44 +00:00
added typings for jQuery Mask plugin (#13706)
* added typings for jQuery Mask plugin * turned on null strict checking * type fix * added missing semicolon
This commit is contained in:
Vendored
+75
@@ -0,0 +1,75 @@
|
||||
// Type definitions for jQuery Mask Plugin 1.14
|
||||
// Project: https://igorescobar.github.io/jQuery-Mask-Plugin/
|
||||
// Definitions by: Anže Videnič <https://github.com/avidenic>, Igor Escobar <http://www.igorescobar.com/>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
|
||||
/// <reference types="jquery" />
|
||||
|
||||
declare namespace jQueryMask {
|
||||
|
||||
interface Invalid {
|
||||
p: number;
|
||||
v: string;
|
||||
e: string;
|
||||
}
|
||||
|
||||
interface Options {
|
||||
maskElements?: string;
|
||||
dataMaskAttr?: string;
|
||||
dataMask?: boolean;
|
||||
watchInterval?: number;
|
||||
watchInputs?: boolean;
|
||||
watchDataMask?: boolean;
|
||||
byPassKeys?: number[];
|
||||
translation?: Translation;
|
||||
selectOnFocus?: boolean;
|
||||
reverse?: boolean;
|
||||
clearIfNotMatch?: boolean;
|
||||
onComplete?(value: string, e: Event, $element: JQuery, options: Options): void;
|
||||
onKeyPress?(value: string, e: Event, $element: JQuery, options: Options): void;
|
||||
onChange?(value: string, e: Event, $element: JQuery, options: Options): void;
|
||||
onInvalid?(value: string, e: Event, $element: JQuery, invalid: Invalid[], options: Options): void;
|
||||
}
|
||||
|
||||
interface Pattern {
|
||||
pattern?: RegExp;
|
||||
recursive?: boolean;
|
||||
optional?: boolean;
|
||||
fallback?: string;
|
||||
}
|
||||
|
||||
interface Translation {
|
||||
[key: string]: Pattern | {} | undefined;
|
||||
placeholder?: string;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
interface JQuery {
|
||||
|
||||
/**
|
||||
* Applies the mask to the matching selector elements.
|
||||
* @param mask should be a string or a function.
|
||||
* @param options should be an options object.
|
||||
* @returns The element.
|
||||
*/
|
||||
mask(mask: ((value: string) => string) | string, options?: jQueryMask.Options): JQuery;
|
||||
|
||||
/**
|
||||
* Seek and destroy.
|
||||
* @returns The element.
|
||||
*/
|
||||
unmask(): JQuery;
|
||||
|
||||
/**
|
||||
* Gets the value of the field without the mask.
|
||||
* @returns Value without the mask.
|
||||
*/
|
||||
cleanVal(): string;
|
||||
|
||||
/**
|
||||
* Gets masked value programmatically
|
||||
* @returns Masked value.
|
||||
*/
|
||||
masked(value: string): string;
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
|
||||
// basic usage
|
||||
$(document).ready(function () {
|
||||
$('.date').mask('00/00/0000');
|
||||
$('.time').mask('00:00:00');
|
||||
$('.date_time').mask('00/00/0000 00:00:00');
|
||||
$('.cep').mask('00000-000');
|
||||
$('.phone').mask('0000-0000');
|
||||
$('.phone_with_ddd').mask('(00) 0000-0000');
|
||||
$('.phone_us').mask('(000) 000-0000');
|
||||
$('.mixed').mask('AAA 000-S0S');
|
||||
$('.cpf').mask('000.000.000-00', { reverse: true });
|
||||
$('.cnpj').mask('00.000.000/0000-00', { reverse: true });
|
||||
$('.money').mask('000.000.000.000.000,00', { reverse: true });
|
||||
$('.money2').mask("#.##0,00", { reverse: true });
|
||||
$('.ip_address').mask('0ZZ.0ZZ.0ZZ.0ZZ', {
|
||||
translation: {
|
||||
'Z': {
|
||||
pattern: /[0-9]/, optional: true
|
||||
}
|
||||
}
|
||||
});
|
||||
$('.ip_address').mask('099.099.099.099');
|
||||
$('.percent').mask('##0,00%', { reverse: true });
|
||||
$('.clear-if-not-match').mask("00/00/0000", { clearIfNotMatch: true });
|
||||
$('.placeholder').mask("00/00/0000", {
|
||||
translation: {
|
||||
placeholder: "__/__/____"
|
||||
}
|
||||
});
|
||||
$('.fallback').mask("00r00r0000", {
|
||||
translation: {
|
||||
'r': {
|
||||
pattern: /[\/]/,
|
||||
fallback: '/'
|
||||
},
|
||||
placeholder: "__/__/____"
|
||||
}
|
||||
});
|
||||
$('.selectonfocus').mask("00/00/0000", { selectOnFocus: true });
|
||||
});
|
||||
|
||||
// callback examples
|
||||
let options: jQueryMask.Options = {
|
||||
onComplete: function (cep: Object) {
|
||||
alert('CEP Completed!:' + cep);
|
||||
},
|
||||
onKeyPress: function (cep: Object, event: Event, currentField: JQuery, options: jQueryMask.Options) {
|
||||
console.log('An key was pressed!:', cep, ' event: ', event,
|
||||
'currentField: ', currentField, ' options: ', options);
|
||||
},
|
||||
onChange: function (cep: Object) {
|
||||
console.log('cep changed! ', cep);
|
||||
},
|
||||
onInvalid: function (val: Object, e: Event, f: JQuery, invalid: jQueryMask.Invalid[], options: jQueryMask.Options) {
|
||||
var error = invalid[0];
|
||||
console.log("Digit: ", error.v, " is invalid for the position: ", error.p, ". We expect something like: ", error.e);
|
||||
}
|
||||
};
|
||||
|
||||
$('.cep_with_callback').mask('00000-000', options);
|
||||
|
||||
// on the fly mask changed
|
||||
options = {
|
||||
onKeyPress: function (cep, e, field, options) {
|
||||
let masks: string[] = ['00000-000', '0-00-00-00'];
|
||||
let mask: string = (cep.length > 7) ? masks[1] : masks[0];
|
||||
$('.crazy_cep').mask(mask, options);
|
||||
}
|
||||
};
|
||||
|
||||
$('.crazy_cep').mask('00000-000', options);
|
||||
|
||||
// mask as a function
|
||||
let maskBehavior: (value: string) => string = function (val: string) {
|
||||
return val.replace(/\D/g, '').length === 11 ? '(00) 00000-0000' : '(00) 0000-00009';
|
||||
};
|
||||
|
||||
options = {
|
||||
onKeyPress: function(val: string, e: Event, field: JQuery, options: jQueryMask.Options) {
|
||||
field.mask(maskBehavior.apply({}, arguments), options);
|
||||
}
|
||||
};
|
||||
|
||||
$('.sp_celphones').mask(maskBehavior, options);
|
||||
|
||||
// translation
|
||||
// now the digit 0 on your mask pattern will be interpreted
|
||||
// as valid characters like 0,1,2,3,4,5,6,7,8,9 and *
|
||||
$('.your-field').mask('00/00/0000', {'translation': {0: {pattern: /[0-9*]/}}});
|
||||
|
||||
// optional digits
|
||||
// way 1
|
||||
$('.ip_address').mask('099.099.099.099');
|
||||
// way 2
|
||||
$('.ip_address').mask('0ZZ.0ZZ.0ZZ.0ZZ', {translation: {'Z': {pattern: /[0-9]/, optional: true}}});
|
||||
|
||||
// recursive digits
|
||||
$('.money_example').mask('#.##0,00', {reverse: true});
|
||||
|
||||
// fallback digits
|
||||
$('.field_with_fallback').mask("00r00r0000", {
|
||||
translation: {
|
||||
'r': {
|
||||
pattern: /[\/]/,
|
||||
fallback: '/'
|
||||
},
|
||||
placeholder: "__/__/____"
|
||||
}
|
||||
});
|
||||
|
||||
// Removing the mask
|
||||
$('.date').unmask();
|
||||
|
||||
// Getting the unmasked typed value
|
||||
$('.date').cleanVal();
|
||||
|
||||
// Getting a masked value programmatically
|
||||
$('.date').masked('19840807');
|
||||
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"target": "es6",
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"jquery-mask-plugin-tests.ts"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"extends": "../tslint.json"
|
||||
}
|
||||
Reference in New Issue
Block a user