v14 type definition (tests still needed for new options)

This commit is contained in:
Márton Molnár
2018-10-31 16:52:53 +01:00
parent d67d2fbad2
commit 244f5c736b
3 changed files with 308 additions and 356 deletions
+276 -246
View File
@@ -1,260 +1,290 @@
// Type definitions for intl-tel-input
// Type definitions for intl-tel-input 14.0
// Project: https://github.com/jackocnr/intl-tel-input
// Definitions by: Fidan Hakaj <https://github.com/fdnhkj>, Leonard Thieu <https://github.com/leonard-thieu>
// Definitions by: Fidan Hakaj <https://github.com/fdnhkj>, Leonard Thieu <https://github.com/leonard-thieu>, Márton Molnár <https://github.com/molnarm>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
/// <reference types="jquery" />
export interface IntlTelInputStatic {
/**
* Get all of the plugin's country data - either to re-use elsewhere
* e.g. to populate a country dropdown.
*/
getCountryData(): CountryData[];
declare namespace IntlTelInput {
/**
* Load the utils.js script (included in the lib directory) to enable
* formatting/validation etc.
*/
loadUtils(path: string, utilsScriptDeferred?: boolean): void;
interface Plugin {
/**
* Get all of the plugin's country data - either to re-use elsewhere
* e.g. to populate a country dropdown.
*/
getCountryData(): CountryData[];
/**
* Load the utils.js script (included in the lib directory) to enable
* formatting/validation etc.
*/
loadUtils(path: string, utilsScriptDeferred?: boolean): void;
/**
* Remove the plugin from the input, and unbind any event listeners.
*/
(method: 'destroy'): void;
/**
* Get the extension from the current number.
* Requires the utilsScript option.
* e.g. if the input value was "(702) 555-5555 ext. 1234", this would
* return "1234".
*/
(method: 'getExtension'): string;
/**
* Get the current number in the given format (defaults to E.164 standard).
* The different formats are available in the enum
* intlTelInputUtils.numberFormat - taken from here.
* Requires the utilsScript option.
* Note that even if nationalMode is enabled, this can still return a full
* international number.
*/
(method: 'getNumber'): string;
/**
* Get the type (fixed-line/mobile/toll-free etc) of the current number.
* Requires the utilsScript option.
* Returns an integer, which you can match against the various options in the
* global enum intlTelInputUtils.numberType.
* Note that in the US there's no way to differentiate between fixed-line and
* mobile numbers, so instead it will return FIXED_LINE_OR_MOBILE.
*/
(method: 'getNumberType'): intlTelInputUtils.numberType;
/**
* Get the country data for the currently selected flag.
*/
(method: 'getSelectedCountryData'): IntlTelInput.CountryData;
/**
* Get more information about a validation error.
* Requires the utilsScript option.
* Returns an integer, which you can match against the various options in the
* global enum intlTelInputUtils.validationError
*/
(method: 'getValidationError'): intlTelInputUtils.validationError;
/**
* Validate the current number. Expects an internationally formatted number
* (unless nationalMode is enabled). If validation fails, you can use
* getValidationError to get more information.
* Requires the utilsScript option.
* Also see getNumberType if you want to make sure the user enters a certain
* type of number e.g. a mobile number.
*/
(method: 'isValidNumber'): boolean;
(method: string): void;
/**
* Change the country selection (e.g. when the user is entering their address).
* @param countryCode country code of the country to be set.
*/
(method: 'setCountry', countryCode: string): void;
/**
* Insert a number, and update the selected flag accordingly.
* Note that by default, if nationalMode is enabled it will try to use
* national formatting.
* @param aNumber number to be set.
*/
(method: 'setNumber', aNumber: string): void;
(method: string, value: string): void;
/**
* Get the current number in the given format (defaults to E.164 standard).
* The different formats are available in the enum
* intlTelInputUtils.numberFormat - taken from here.
* Requires the utilsScript option.
* Note that even if nationalMode is enabled, this can still return a full
* international number.
* @param numberFormat the format in which the number will be returned.
*/
(method: 'getNumber', numberFormat: intlTelInputUtils.numberFormat): string;
(method: string, numberFormat: intlTelInputUtils.numberFormat): string;
/**
* initialise the plugin with optional options.
* @param options options that can be provided during initialization.
*/
(options?: IntlTelInput.Options): JQueryDeferred<any>;
}
interface Options {
/**
* Whether or not to allow the dropdown. If disabled, there is no dropdown
* arrow, and the selected flag is not clickable. Also we display the
* selected flag on the right instead because it is just a marker of state.
*/
allowDropdown?: boolean;
/**
* If there is just a dial code in the input: remove it on blur or submit,
* and re-add it on focus. This is to prevent just a dial code getting
* submitted with the form. Requires nationalMode to be set to false.
*/
autoHideDialCode?: boolean;
/**
* Set the input's placeholder to an example number for the selected country.
* You can specify the number type using the numberType option.
* If there is already a placeholder attribute set on the input then that
* will take precedence. Requires the utilsScript option.
*/
autoPlaceholder?: boolean;
/**
* Change the placeholder generated by autoPlaceholder. Must return a string.
*/
customPlaceholder?: (selectedCountryPlaceholder: string, selectedCountryData: CountryData) => string;
/**
* Specify the container for the country dropdown (use a jQuery selector
* e.g. "body"). This is useful when the input is within a scrolling element,
* or an element with overflow: hidden. Wherever you put the dropdown it
* will automatically close on the window scroll event to prevent positioning
* issues. If you want it to close when a different element is scrolled
* (such as the input's parent), simply listen for the that scroll event,
* and trigger $(window).scroll() e.g.
*/
dropdownContainer?: string;
/**
* Don't display the countries you specify.
*/
excludeCountries?: Array<string>;
/**
* Format the input value during initialisation.
*/
formatOnInit?: boolean;
/**
* When setting initialCountry to "auto", you must use this option to
* specify a custom function that looks up the user's location. Also note
* that when instantiating the plugin, we now return a deferred object, so
* you can use .done(callback) to know when initialisation requests like
* this have completed.
* Note that the callback must still be called in the event of an error.
*/
geoIpLookup?: (callback: (countryCode: string) => void) => void;
/**
* Set the initial country selection by specifying it's country code.
* You can also set it to "auto", which will lookup the user's country based
* on their IP address (requires the geoIpLookup option).
* Note that the "auto" option will not update the country selection if the
* input already contains a number. If you leave initialCountry blank,
* it will default to the first country in the list.
*/
initialCountry?: string;
/**
* Allow users to enter national numbers (and not have to think about
* international dial codes). Formatting, validation and placeholders still
* work. Then you can use getNumber to extract a full international number.
* This option now defaults to true, and it is recommended that you leave it
* that way as it provides a better experience for the user.
*/
nationalMode?: boolean;
/**
* Display only the countries you specify.
*/
onlyCountries?: Array<string>;
/**
* Specify one of the keys from the global enum intlTelInputUtils.numberType
* e.g. "FIXED_LINE" to set the number type to use for the placeholder.
*/
placeholderNumberType?:
| "FIXED_LINE_OR_MOBILE"
| "FIXED_LINE"
| "MOBILE"
| "PAGER"
| "PERSONAL_NUMBER"
| "PREMIUM_RATE"
| "SHARED_COST"
| "TOLL_FREE"
| "UAN"
| "UNKNOWN"
| "VOICEMAIL"
| "VOIP";
/**
* Specify the countries to appear at the top of the list.
*/
preferredCountries?: Array<string>;
/**
* Display the country dial code next to the selected flag so it's not part
* of the typed number. Note that this will disable nationalMode because
* technically we are dealing with international numbers, but with the
* dial code separated.
*/
separateDialCode?: boolean;
/**
* Enable formatting/validation etc. by specifying the path to the included
* utils.js script, which is fetched only when the page has finished loading
* (on window.load) to prevent blocking. When instantiating the plugin,
* we return a deferred object, so you can use .done(callback) to know when
* initialisation requests like this have finished. Note that if you're
* lazy loading the plugin script itself (intlTelInput.js) this will not
* work and you will need to use the loadUtils method instead.
*/
utilsScript?: string;
}
interface CountryData {
name: string;
iso2: string;
dialCode: string;
}
/**
* initialise the plugin with optional options.
* @param options options that can be provided during initialization.
*/
(node: Element, options?: IntlTelInputOptions): IntlTelInput;
}
declare namespace intlTelInputUtils {
export interface IntlTelInput {
/**
* Remove the plugin from the input, and unbind any event listeners.
*/
destroy(): void;
const enum numberFormat {
E164 = 0,
INTERNATIONAL = 1,
NATIONAL = 2,
RFC3966 = 3
}
/**
* Get the extension from the current number.
* Requires the utilsScript option.
* e.g. if the input value was "(702) 555-5555 ext. 1234", this would
* return "1234".
*/
getExtension(): string;
const enum numberType {
FIXED_LINE = 0,
MOBILE = 1,
FIXED_LINE_OR_MOBILE = 2,
TOLL_FREE = 3,
PREMIUM_RATE = 4,
SHARED_COST = 5,
VOIP = 6,
PERSONAL_NUMBER = 7,
PAGER = 8,
UAN = 9,
VOICEMAIL = 10,
UNKNOWN = -1
}
/**
* Get the current number in the given format (defaults to E.164 standard).
* The different formats are available in the enum
* intlTelInputUtils.numberFormat - taken from here.
* Requires the utilsScript option.
* Note that even if nationalMode is enabled, this can still return a full
* international number.
* @param numberFormat the format in which the number will be returned.
*/
getNumber(numberFormat?: NumberFormat): string;
const enum validationError {
IS_POSSIBLE = 0,
INVALID_COUNTRY_CODE = 1,
TOO_SHORT = 2,
TOO_LONG = 3,
NOT_A_NUMBER = 4
}
/**
* Get the type (fixed-line/mobile/toll-free etc) of the current number.
* Requires the utilsScript option.
* Returns an integer, which you can match against the various options in the
* global enum intlTelInputUtils.numberType.
* Note that in the US there's no way to differentiate between fixed-line and
* mobile numbers, so instead it will return FIXED_LINE_OR_MOBILE.
*/
getNumberType(): NumberType;
/**
* Get the country data for the currently selected flag.
*/
getSelectedCountryData(): CountryData;
/**
* Get more information about a validation error.
* Requires the utilsScript option.
* Returns an integer, which you can match against the various options in the
* global enum intlTelInputUtils.validationError
*/
getValidationError(): ValidationError;
/**
* Validate the current number. Expects an internationally formatted number
* (unless nationalMode is enabled). If validation fails, you can use
* getValidationError to get more information.
* Requires the utilsScript option.
* Also see getNumberType if you want to make sure the user enters a certain
* type of number e.g. a mobile number.
*/
isValidNumber(): boolean;
/**
* Change the country selection (e.g. when the user is entering their address).
* @param countryCode country code of the country to be set.
*/
setCountry(countryCode: string): void;
/**
* Insert a number, and update the selected flag accordingly.
* Note that by default, if nationalMode is enabled it will try to use
* national formatting.
* @param aNumber number to be set.
*/
setNumber(aNumber: string): void;
/**
* Set the type of the placeholder number
* @param type Placeholder number type to be set
*/
setPlaceholderNumberType(type: NumberType): void;
}
interface JQuery {
intlTelInput: IntlTelInput.Plugin;
export interface IntlTelInputOptions {
/**
* Whether or not to allow the dropdown. If disabled, there is no dropdown
* arrow, and the selected flag is not clickable. Also we display the
* selected flag on the right instead because it is just a marker of state.
* Default = true
*/
allowDropdown?: boolean;
/**
* If there is just a dial code in the input: remove it on blur or submit,
* and re-add it on focus. This is to prevent just a dial code getting
* submitted with the form. Requires nationalMode to be set to false.
* Default = true
*/
autoHideDialCode?: boolean;
/**
* Set the input's placeholder to an example number for the selected country, and update it if the country changes.
* You can specify the number type using the placeholderNumberType option.
* By default it is set to "polite", which means it will only set the placeholder if the input doesn't already have one.
* You can also set it to "aggressive", which will replace any existing placeholder, or "off".
* Requires the utilsScript option.
* Default = "polite"
*/
autoPlaceholder?: "off" | "polite" | "aggressive";
/**
* Change the placeholder generated by autoPlaceholder. Must return a string.
* Default = null
*/
customPlaceholder?: (selectedCountryPlaceholder: string, selectedCountryData: CountryData) => string;
/**
* Expects a node e.g. document.body. Instead of putting the country dropdown next to the input,
* append it to the specified node, and it will then be positioned absolutely next to the input using JavaScript.
* This is useful when the input is inside a container with overflow: hidden.
* Note that the absolute positioning can be broken by scrolling, so it will automatically close on the window scroll event.
* Default = null
*/
dropdownContainer?: Node;
/**
* In the dropdown, display all countries except the ones you specify here.
* Default = null
*/
excludeCountries?: string[];
/**
* Format the input value (according to the nationalMode option) during initialisation, and on setNumber.
* Requires the utilsScript option.
* Default = true
*/
formatOnDisplay?: boolean;
/**
* When setting initialCountry to "auto", you must use this option to
* specify a custom function that looks up the user's location,
* and then calls the success callback with the relevant country code.
* Also note that when instantiating the plugin, if the Promise object is defined,
* one of those is returned under the promise instance property, so you can
* do something like iti.promise.then(callback) to know when initialisation requests like this have completed.
* Default = null
*/
geoIpLookup?: (callback: (countryCode: string) => void) => void;
/**
* Add a hidden input with the given name (or if your input name contains square brackets then it will give the hidden input the same name,
* replacing the contents of the brackets with the given name). On submit, populate it with the full international number (using getNumber).
* This is a quick way for people using non-ajax forms to get the full international number, even when nationalMode is enabled.
* Note: requires the input to be inside a form element, as this feature works by listening for the submit event on the closest form element.
* Also note that since this uses getNumber internally, it expects a valid number, and so should only be used after validation.
* Default = ""
*/
hiddenInput?: string;
/**
* Set the initial country selection by specifying it's country code.
* You can also set it to "auto", which will lookup the user's country based
* on their IP address (requires the geoIpLookup option).
* Note that the "auto" option will not update the country selection if the
* input already contains a number. If you leave initialCountry blank,
* it will default to the first country in the list.
*/
initialCountry?: string;
/**
* Allows to translate the countries by its given iso code e.g.: { 'de': 'Deutschland' }
* Default = {}
*/
localizedCountries?: object;
/**
* Allow users to enter national numbers (and not have to think about
* international dial codes). Formatting, validation and placeholders still
* work. Then you can use getNumber to extract a full international number.
* This option now defaults to true, and it is recommended that you leave it
* that way as it provides a better experience for the user.
* Default = true
*/
nationalMode?: boolean;
/**
* In the dropdown, display only the countries you specify.
* Default = undefined
*/
onlyCountries?: string[];
/**
* Specify one of the keys from the global enum intlTelInputUtils.numberType
* e.g. "FIXED_LINE" to set the number type to use for the placeholder.
* Default = MOBILE
*/
placeholderNumberType?: NumberType;
/**
* Specify the countries to appear at the top of the list.
* Default = ["us", "gb"]
*/
preferredCountries?: string[];
/**
* Display the country dial code next to the selected flag so it's not part
* of the typed number. Note that this will disable nationalMode because
* technically we are dealing with international numbers, but with the
* dial code separated.
* Default = false
*/
separateDialCode?: boolean;
/**
* Enable formatting/validation etc. by specifying the URL of the included utils.js script
* (or alternatively just point it to the file on cdnjs.com). The script is fetched when the page has finished loading (on the window load event)
* to prevent blocking (the script is ~215KB). When instantiating the plugin, if the Promise object is defined,
* one of those is returned under the promise instance property, so you can do something like
* iti.promise.then(callback) to know when initialisation requests like this have finished.
* Note that if you're lazy loading the plugin script itself (intlTelInput.js)
* this will not work and you will need to use the loadUtils method instead.
* Example: "build/js/utils.js"
* Default = ""
*/
utilsScript?: string;
}
export interface CountryData {
name: string;
iso2: string;
dialCode: string;
}
export enum NumberFormat {
E164 = 0,
INTERNATIONAL = 1,
NATIONAL = 2,
RFC3966 = 3
}
export enum NumberType {
FIXED_LINE = 0,
MOBILE = 1,
FIXED_LINE_OR_MOBILE = 2,
TOLL_FREE = 3,
PREMIUM_RATE = 4,
SHARED_COST = 5,
VOIP = 6,
PERSONAL_NUMBER = 7,
PAGER = 8,
UAN = 9,
VOICEMAIL = 10,
UNKNOWN = -1
}
export enum ValidationError {
IS_POSSIBLE = 0,
INVALID_COUNTRY_CODE = 1,
TOO_SHORT = 2,
TOO_LONG = 3,
NOT_A_NUMBER = 4
}
declare global {
interface Window {
intlTelInput: IntlTelInputStatic;
}
}
+31 -32
View File
@@ -1,70 +1,69 @@
$('#phone').intlTelInput();
import IntlTelInput = require("intl-tel-input");
$('#phone').intlTelInput({
customPlaceholder: function(selectedCountryPlaceholder, selectedCountryData) {
const input = document.querySelector("#phone");
window.intlTelInput(input);
window.intlTelInput(input, {
customPlaceholder(selectedCountryPlaceholder, selectedCountryData) {
return 'e.g. ' + selectedCountryPlaceholder;
}
});
$('#phone').intlTelInput({
placeholderNumberType: "MOBILE",
window.intlTelInput(input, {
placeholderNumberType: IntlTelInput.NumberType.MOBILE,
});
$('#phone').intlTelInput({
geoIpLookup: function(callback) {
$.get('http://ipinfo.io', function() {}, 'jsonp').always(function(resp) {
let countryCode = (resp && resp.country) ? <string> resp.country : '';
window.intlTelInput(input, {
geoIpLookup(callback) {
const countryCode = 'XY';
callback(countryCode);
});
}
});
$('#phone').intlTelInput('destroy');
window.intlTelInput(input).destroy();
let extension = $('#phone').intlTelInput('getExtension');
const extension = window.intlTelInput(input).getExtension();
let intlNumber = $('#phone').intlTelInput('getNumber');
let ntlNumber = $('#phone').intlTelInput('getNumber', intlTelInputUtils.numberFormat.NATIONAL);
const intlNumber = window.intlTelInput(input).getNumber();
const ntlNumber = window.intlTelInput(input).getNumber(IntlTelInput.NumberFormat.NATIONAL);
let numberType = $('#phone').intlTelInput('getNumberType');
if (numberType === intlTelInputUtils.numberType.MOBILE) {}
const numberType = window.intlTelInput(input).getNumberType();
if (numberType === IntlTelInput.NumberType.MOBILE) {}
let selectedCountryData = $('#phone').intlTelInput('getSelectedCountryData');
const selectedCountryData = window.intlTelInput(input).getSelectedCountryData();
let error = $('#phone').intlTelInput('getValidationError');
if (error === intlTelInputUtils.validationError.TOO_SHORT) {}
const error = window.intlTelInput(input).getValidationError();
if (error === IntlTelInput.ValidationError.TOO_SHORT) {}
let isValid = $('#phone').intlTelInput('isValidNumber');
const isValid = window.intlTelInput(input).isValidNumber();
$('#phone').intlTelInput('setCountry', 'gb');
window.intlTelInput(input).setCountry('gb');
$('#phone').intlTelInput('setNumber', '+447733123456');
window.intlTelInput(input).setNumber('+447733123456');
let countryData = $.fn.intlTelInput.getCountryData();
const countryData = window.intlTelInput.getCountryData();
$.fn.intlTelInput.loadUtils('build/js/utils.js');
window.intlTelInput.loadUtils('build/js/utils.js');
$('#phone').intlTelInput({
window.intlTelInput(input, {
utilsScript: '../../build/js/utils.js'
});
$('#phone').intlTelInput({
window.intlTelInput(input, {
initialCountry: 'auto',
geoIpLookup: function(callback) {
$.get('http://ipinfo.io', function() {}, 'jsonp').always(function(resp) {
let countryCode = (resp && resp.country) ? resp.country : '';
geoIpLookup: (callback) => {
const countryCode = 'XY';
callback(countryCode);
});
},
utilsScript: '../../build/js/utils.js'
});
$('#phone').intlTelInput({
window.intlTelInput(input, {
nationalMode: true,
utilsScript: '../../build/js/utils.js'
});
$('#phone').intlTelInput({
window.intlTelInput(input, {
onlyCountries: ['al'],
utilsScript: '../../build/js/utils.js'
});
+1 -78
View File
@@ -1,80 +1,3 @@
{
"extends": "dtslint/dt.json",
"rules": {
"adjacent-overload-signatures": false,
"array-type": false,
"arrow-return-shorthand": false,
"ban-types": false,
"callable-types": false,
"comment-format": false,
"dt-header": false,
"eofline": false,
"export-just-namespace": false,
"import-spacing": false,
"interface-name": false,
"interface-over-type-literal": false,
"jsdoc-format": false,
"max-line-length": false,
"member-access": false,
"new-parens": false,
"no-any-union": false,
"no-boolean-literal-compare": false,
"no-conditional-assignment": false,
"no-consecutive-blank-lines": false,
"no-const-enum": false,
"no-construct": false,
"no-declare-current-package": false,
"no-duplicate-imports": false,
"no-duplicate-variable": false,
"no-empty-interface": false,
"no-for-in-array": false,
"no-inferrable-types": false,
"no-internal-module": false,
"no-irregular-whitespace": false,
"no-mergeable-namespace": false,
"no-misused-new": false,
"no-namespace": false,
"no-object-literal-type-assertion": false,
"no-padding": false,
"no-redundant-jsdoc": false,
"no-redundant-jsdoc-2": false,
"no-redundant-undefined": false,
"no-reference-import": false,
"no-relative-import-in-test": false,
"no-self-import": false,
"no-single-declare-module": false,
"no-string-throw": false,
"no-unnecessary-callback-wrapper": false,
"no-unnecessary-class": false,
"no-unnecessary-generics": false,
"no-unnecessary-qualifier": false,
"no-unnecessary-type-assertion": false,
"no-useless-files": false,
"no-var-keyword": false,
"no-var-requires": false,
"no-void-expression": false,
"no-trailing-whitespace": false,
"object-literal-key-quotes": false,
"object-literal-shorthand": false,
"one-line": false,
"one-variable-per-declaration": false,
"only-arrow-functions": false,
"prefer-conditional-expression": false,
"prefer-const": false,
"prefer-declare-function": false,
"prefer-for-of": false,
"prefer-method-signature": false,
"prefer-template": false,
"radix": false,
"semicolon": false,
"space-before-function-paren": false,
"space-within-parens": false,
"strict-export-declare-modifiers": false,
"trim-file": false,
"triple-equals": false,
"typedef-whitespace": false,
"unified-signatures": false,
"void-return": false,
"whitespace": false
}
"extends": "dtslint/dt.json"
}