vue-currency-input
Advanced tools
Comparing version 1.20.3 to 1.21.0
/** | ||
* Vue Currency Input 1.20.3 | ||
* Vue Currency Input 1.21.0 | ||
* (c) 2018-2020 Matthias Stiller | ||
@@ -16,2 +16,21 @@ * @license MIT | ||
var DEFAULT_OPTIONS = { | ||
locale: undefined, | ||
currency: 'EUR', | ||
valueAsInteger: false, | ||
distractionFree: true, | ||
precision: undefined, | ||
autoDecimalMode: false, | ||
valueRange: undefined, | ||
allowNegative: true | ||
}; | ||
var getValue = function (el) { | ||
var ref = el.$ci; | ||
var numberValue = ref.numberValue; | ||
var currencyFormat = ref.currencyFormat; | ||
var options = ref.options; | ||
return toExternalNumberModel(numberValue, options.valueAsInteger, currencyFormat.maximumFractionDigits) | ||
}; | ||
var setValue = function (el, value) { return dispatchEvent(el, 'format', { value: value }); }; | ||
var escapeRegExp = function (str) { return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); }; | ||
@@ -23,2 +42,54 @@ var removeLeadingZeros = function (str) { return str.replace(/^0+(0$|[^0])/, '$1'); }; | ||
var setCaretPosition = function (el, position) { return el.setSelectionRange(position, position); }; | ||
var getCaretPositionAfterFormat = function (newValue, inputtedValue, caretPosition, numberFormat, options) { | ||
var prefix = numberFormat.prefix; | ||
var suffix = numberFormat.suffix; | ||
var decimalSymbol = numberFormat.decimalSymbol; | ||
var maximumFractionDigits = numberFormat.maximumFractionDigits; | ||
var groupingSymbol = numberFormat.groupingSymbol; | ||
var decimalSymbolPosition = inputtedValue.indexOf(decimalSymbol) + 1; | ||
var caretPositionFromLeft = inputtedValue.length - caretPosition; | ||
if (Math.abs(newValue.length - inputtedValue.length) > 1 && caretPosition <= decimalSymbolPosition) { | ||
return newValue.indexOf(decimalSymbol) + 1 | ||
} else if (newValue.substr(caretPosition, 1) === groupingSymbol && count(newValue, groupingSymbol) === count(inputtedValue, groupingSymbol) + 1) { | ||
return newValue.length - caretPositionFromLeft - 1 | ||
} else { | ||
if (!options.autoDecimalMode && decimalSymbolPosition !== 0 && caretPosition > decimalSymbolPosition) { | ||
if (numberFormat.onlyDigits(inputtedValue.substr(decimalSymbolPosition)).length - 1 === maximumFractionDigits) { | ||
caretPositionFromLeft -= 1; | ||
} | ||
} | ||
return options.distractionFree.hideCurrencySymbol | ||
? newValue.length - caretPositionFromLeft | ||
: Math.max(newValue.length - Math.max(caretPositionFromLeft, suffix.length), prefix.length === 0 ? 0 : prefix.length + 1) | ||
} | ||
}; | ||
var getDistractionFreeCaretPosition = function (numberFormat, options, value, caretPosition) { | ||
var result = caretPosition; | ||
if (options.distractionFree.hideCurrencySymbol) { | ||
result -= numberFormat.prefix.length; | ||
} | ||
if (options.distractionFree.hideGroupingSymbol) { | ||
result -= count(value.substring(0, caretPosition), numberFormat.groupingSymbol); | ||
} | ||
return Math.max(0, result) | ||
}; | ||
var equal = function (a, b) { | ||
if (a === b) { | ||
return true | ||
} | ||
if (!a || !b || typeof a !== 'object' || typeof b !== 'object') { | ||
return false | ||
} | ||
var keys = Object.keys(a); | ||
if (keys.length !== Object.keys(b).length) { | ||
return false | ||
} | ||
if (!keys.every(Object.prototype.hasOwnProperty.bind(b))) { | ||
return false | ||
} | ||
return keys.every(function (key) { return equal(a[key], b[key]); }) | ||
}; | ||
var DECIMAL_SYMBOLS = [',', '.', '٫']; | ||
@@ -130,71 +201,2 @@ var NumberFormat = function NumberFormat (options) { | ||
var DEFAULT_OPTIONS = { | ||
locale: undefined, | ||
currency: 'EUR', | ||
valueAsInteger: false, | ||
distractionFree: true, | ||
precision: undefined, | ||
autoDecimalMode: false, | ||
valueRange: undefined, | ||
allowNegative: true | ||
}; | ||
var parseCurrency = function (formattedValue, options) { | ||
var mergedOptions = Object.assign({}, DEFAULT_OPTIONS, options); | ||
var numberFormat = new NumberFormat(mergedOptions); | ||
return toExternalNumberModel(numberFormat.parse(formattedValue), mergedOptions.valueAsInteger, numberFormat.maximumFractionDigits) | ||
}; | ||
var setValue = function (el, value) { return dispatchEvent(el, 'format', { value: value }); }; | ||
var setCaretPosition = function (el, position) { return el.setSelectionRange(position, position); }; | ||
var getCaretPositionAfterFormat = function (newValue, inputtedValue, caretPosition, numberFormat, options) { | ||
var prefix = numberFormat.prefix; | ||
var suffix = numberFormat.suffix; | ||
var decimalSymbol = numberFormat.decimalSymbol; | ||
var maximumFractionDigits = numberFormat.maximumFractionDigits; | ||
var groupingSymbol = numberFormat.groupingSymbol; | ||
var decimalSymbolPosition = inputtedValue.indexOf(decimalSymbol) + 1; | ||
var caretPositionFromLeft = inputtedValue.length - caretPosition; | ||
if (Math.abs(newValue.length - inputtedValue.length) > 1 && caretPosition <= decimalSymbolPosition) { | ||
return newValue.indexOf(decimalSymbol) + 1 | ||
} else if (newValue.substr(caretPosition, 1) === groupingSymbol && count(newValue, groupingSymbol) === count(inputtedValue, groupingSymbol) + 1) { | ||
return newValue.length - caretPositionFromLeft - 1 | ||
} else { | ||
if (!options.autoDecimalMode && decimalSymbolPosition !== 0 && caretPosition > decimalSymbolPosition) { | ||
if (numberFormat.onlyDigits(inputtedValue.substr(decimalSymbolPosition)).length - 1 === maximumFractionDigits) { | ||
caretPositionFromLeft -= 1; | ||
} | ||
} | ||
return options.distractionFree.hideCurrencySymbol | ||
? newValue.length - caretPositionFromLeft | ||
: Math.max(newValue.length - Math.max(caretPositionFromLeft, suffix.length), prefix.length === 0 ? 0 : prefix.length + 1) | ||
} | ||
}; | ||
var getDistractionFreeCaretPosition = function (numberFormat, options, value, caretPosition) { | ||
var result = caretPosition; | ||
if (options.distractionFree.hideCurrencySymbol) { | ||
result -= numberFormat.prefix.length; | ||
} | ||
if (options.distractionFree.hideGroupingSymbol) { | ||
result -= count(value.substring(0, caretPosition), numberFormat.groupingSymbol); | ||
} | ||
return Math.max(0, result) | ||
}; | ||
var equal = function (a, b) { | ||
if (a === b) { | ||
return true | ||
} | ||
if (!a || !b || typeof a !== 'object' || typeof b !== 'object') { | ||
return false | ||
} | ||
var keys = Object.keys(a); | ||
if (keys.length !== Object.keys(b).length) { | ||
return false | ||
} | ||
if (!keys.every(Object.prototype.hasOwnProperty.bind(b))) { | ||
return false | ||
} | ||
return keys.every(function (key) { return equal(a[key], b[key]); }) | ||
}; | ||
var DefaultNumberMask = function DefaultNumberMask (numberFormat) { | ||
@@ -263,3 +265,3 @@ this.numberFormat = numberFormat; | ||
var init = function (el, optionsFromBinding, ref) { | ||
var $CI_DEFAULT_OPTIONS = ref.$CI_DEFAULT_OPTIONS; | ||
var $ci = ref.$ci; | ||
var inputElement = el.tagName.toLowerCase() === 'input' ? el : el.querySelector('input'); | ||
@@ -269,3 +271,3 @@ if (!inputElement) { | ||
} | ||
var options = Object.assign({}, ($CI_DEFAULT_OPTIONS || DEFAULT_OPTIONS), optionsFromBinding); | ||
var options = Object.assign({}, ($ci ? $ci.GLOBAL_OPTIONS : DEFAULT_OPTIONS), optionsFromBinding); | ||
var distractionFree = options.distractionFree; | ||
@@ -305,5 +307,2 @@ var autoDecimalMode = options.autoDecimalMode; | ||
}; | ||
var validateValueRange = function (value, valueRange) { | ||
return Math.min(Math.max(value, valueRange.min), valueRange.max) | ||
}; | ||
var triggerEvent = function (el, eventName) { | ||
@@ -322,3 +321,7 @@ var ref = el.$ci; | ||
var options = ref.options; | ||
format(el, value != null ? currencyFormat.format(validateValueRange(value, options.valueRange)) : null); | ||
var ref$1 = options.valueRange; | ||
var min = ref$1.min; | ||
var max = ref$1.max; | ||
var validateValueRange = function () { return Math.min(Math.max(value, min), max); }; | ||
format(el, value != null ? currencyFormat.format(validateValueRange()) : null); | ||
if (value !== el.$ci.numberValue || forcedChange) { | ||
@@ -539,3 +542,3 @@ triggerEvent(el, 'change'); | ||
var this$1 = this; | ||
var options = Object.assign({}, this.$CI_DEFAULT_OPTIONS || DEFAULT_OPTIONS); | ||
var options = Object.assign({}, this.$ci ? this.$ci.GLOBAL_OPTIONS : DEFAULT_OPTIONS); | ||
Object.keys(DEFAULT_OPTIONS).forEach(function (key) { | ||
@@ -565,8 +568,8 @@ if (this$1[key] !== undefined) { | ||
var globalOptions = ref.globalOptions; if ( globalOptions === void 0 ) globalOptions = {}; | ||
Vue.prototype.$CI_DEFAULT_OPTIONS = Object.assign({}, DEFAULT_OPTIONS, globalOptions); | ||
Vue.component(componentName, component); | ||
Vue.directive(directiveName, directive); | ||
Vue.prototype.$parseCurrency = function (str, options) { | ||
if ( options === void 0 ) options = {}; | ||
return parseCurrency(str, Object.assign({}, globalOptions, options)); | ||
Vue.prototype.$ci = { | ||
getValue: getValue, | ||
setValue: setValue, | ||
GLOBAL_OPTIONS: Object.assign({}, DEFAULT_OPTIONS, globalOptions) | ||
}; | ||
@@ -581,2 +584,2 @@ } | ||
export default plugin; | ||
export { directive as CurrencyDirective, component as CurrencyInput, parseCurrency, setValue }; | ||
export { directive as CurrencyDirective, component as CurrencyInput, getValue, setValue }; |
/** | ||
* Vue Currency Input 1.20.3 | ||
* Vue Currency Input 1.21.0 | ||
* (c) 2018-2020 Matthias Stiller | ||
@@ -22,2 +22,21 @@ * @license MIT | ||
var DEFAULT_OPTIONS = { | ||
locale: undefined, | ||
currency: 'EUR', | ||
valueAsInteger: false, | ||
distractionFree: true, | ||
precision: undefined, | ||
autoDecimalMode: false, | ||
valueRange: undefined, | ||
allowNegative: true | ||
}; | ||
var getValue = function (el) { | ||
var ref = el.$ci; | ||
var numberValue = ref.numberValue; | ||
var currencyFormat = ref.currencyFormat; | ||
var options = ref.options; | ||
return toExternalNumberModel(numberValue, options.valueAsInteger, currencyFormat.maximumFractionDigits) | ||
}; | ||
var setValue = function (el, value) { return dispatchEvent(el, 'format', { value: value }); }; | ||
var escapeRegExp = function (str) { return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); }; | ||
@@ -29,2 +48,54 @@ var removeLeadingZeros = function (str) { return str.replace(/^0+(0$|[^0])/, '$1'); }; | ||
var setCaretPosition = function (el, position) { return el.setSelectionRange(position, position); }; | ||
var getCaretPositionAfterFormat = function (newValue, inputtedValue, caretPosition, numberFormat, options) { | ||
var prefix = numberFormat.prefix; | ||
var suffix = numberFormat.suffix; | ||
var decimalSymbol = numberFormat.decimalSymbol; | ||
var maximumFractionDigits = numberFormat.maximumFractionDigits; | ||
var groupingSymbol = numberFormat.groupingSymbol; | ||
var decimalSymbolPosition = inputtedValue.indexOf(decimalSymbol) + 1; | ||
var caretPositionFromLeft = inputtedValue.length - caretPosition; | ||
if (Math.abs(newValue.length - inputtedValue.length) > 1 && caretPosition <= decimalSymbolPosition) { | ||
return newValue.indexOf(decimalSymbol) + 1 | ||
} else if (newValue.substr(caretPosition, 1) === groupingSymbol && count(newValue, groupingSymbol) === count(inputtedValue, groupingSymbol) + 1) { | ||
return newValue.length - caretPositionFromLeft - 1 | ||
} else { | ||
if (!options.autoDecimalMode && decimalSymbolPosition !== 0 && caretPosition > decimalSymbolPosition) { | ||
if (numberFormat.onlyDigits(inputtedValue.substr(decimalSymbolPosition)).length - 1 === maximumFractionDigits) { | ||
caretPositionFromLeft -= 1; | ||
} | ||
} | ||
return options.distractionFree.hideCurrencySymbol | ||
? newValue.length - caretPositionFromLeft | ||
: Math.max(newValue.length - Math.max(caretPositionFromLeft, suffix.length), prefix.length === 0 ? 0 : prefix.length + 1) | ||
} | ||
}; | ||
var getDistractionFreeCaretPosition = function (numberFormat, options, value, caretPosition) { | ||
var result = caretPosition; | ||
if (options.distractionFree.hideCurrencySymbol) { | ||
result -= numberFormat.prefix.length; | ||
} | ||
if (options.distractionFree.hideGroupingSymbol) { | ||
result -= count(value.substring(0, caretPosition), numberFormat.groupingSymbol); | ||
} | ||
return Math.max(0, result) | ||
}; | ||
var equal = function (a, b) { | ||
if (a === b) { | ||
return true | ||
} | ||
if (!a || !b || typeof a !== 'object' || typeof b !== 'object') { | ||
return false | ||
} | ||
var keys = Object.keys(a); | ||
if (keys.length !== Object.keys(b).length) { | ||
return false | ||
} | ||
if (!keys.every(Object.prototype.hasOwnProperty.bind(b))) { | ||
return false | ||
} | ||
return keys.every(function (key) { return equal(a[key], b[key]); }) | ||
}; | ||
var DECIMAL_SYMBOLS = [',', '.', '٫']; | ||
@@ -136,71 +207,2 @@ var NumberFormat = function NumberFormat (options) { | ||
var DEFAULT_OPTIONS = { | ||
locale: undefined, | ||
currency: 'EUR', | ||
valueAsInteger: false, | ||
distractionFree: true, | ||
precision: undefined, | ||
autoDecimalMode: false, | ||
valueRange: undefined, | ||
allowNegative: true | ||
}; | ||
var parseCurrency = function (formattedValue, options) { | ||
var mergedOptions = Object.assign({}, DEFAULT_OPTIONS, options); | ||
var numberFormat = new NumberFormat(mergedOptions); | ||
return toExternalNumberModel(numberFormat.parse(formattedValue), mergedOptions.valueAsInteger, numberFormat.maximumFractionDigits) | ||
}; | ||
var setValue = function (el, value) { return dispatchEvent(el, 'format', { value: value }); }; | ||
var setCaretPosition = function (el, position) { return el.setSelectionRange(position, position); }; | ||
var getCaretPositionAfterFormat = function (newValue, inputtedValue, caretPosition, numberFormat, options) { | ||
var prefix = numberFormat.prefix; | ||
var suffix = numberFormat.suffix; | ||
var decimalSymbol = numberFormat.decimalSymbol; | ||
var maximumFractionDigits = numberFormat.maximumFractionDigits; | ||
var groupingSymbol = numberFormat.groupingSymbol; | ||
var decimalSymbolPosition = inputtedValue.indexOf(decimalSymbol) + 1; | ||
var caretPositionFromLeft = inputtedValue.length - caretPosition; | ||
if (Math.abs(newValue.length - inputtedValue.length) > 1 && caretPosition <= decimalSymbolPosition) { | ||
return newValue.indexOf(decimalSymbol) + 1 | ||
} else if (newValue.substr(caretPosition, 1) === groupingSymbol && count(newValue, groupingSymbol) === count(inputtedValue, groupingSymbol) + 1) { | ||
return newValue.length - caretPositionFromLeft - 1 | ||
} else { | ||
if (!options.autoDecimalMode && decimalSymbolPosition !== 0 && caretPosition > decimalSymbolPosition) { | ||
if (numberFormat.onlyDigits(inputtedValue.substr(decimalSymbolPosition)).length - 1 === maximumFractionDigits) { | ||
caretPositionFromLeft -= 1; | ||
} | ||
} | ||
return options.distractionFree.hideCurrencySymbol | ||
? newValue.length - caretPositionFromLeft | ||
: Math.max(newValue.length - Math.max(caretPositionFromLeft, suffix.length), prefix.length === 0 ? 0 : prefix.length + 1) | ||
} | ||
}; | ||
var getDistractionFreeCaretPosition = function (numberFormat, options, value, caretPosition) { | ||
var result = caretPosition; | ||
if (options.distractionFree.hideCurrencySymbol) { | ||
result -= numberFormat.prefix.length; | ||
} | ||
if (options.distractionFree.hideGroupingSymbol) { | ||
result -= count(value.substring(0, caretPosition), numberFormat.groupingSymbol); | ||
} | ||
return Math.max(0, result) | ||
}; | ||
var equal = function (a, b) { | ||
if (a === b) { | ||
return true | ||
} | ||
if (!a || !b || typeof a !== 'object' || typeof b !== 'object') { | ||
return false | ||
} | ||
var keys = Object.keys(a); | ||
if (keys.length !== Object.keys(b).length) { | ||
return false | ||
} | ||
if (!keys.every(Object.prototype.hasOwnProperty.bind(b))) { | ||
return false | ||
} | ||
return keys.every(function (key) { return equal(a[key], b[key]); }) | ||
}; | ||
var DefaultNumberMask = function DefaultNumberMask (numberFormat) { | ||
@@ -269,3 +271,3 @@ this.numberFormat = numberFormat; | ||
var init = function (el, optionsFromBinding, ref) { | ||
var $CI_DEFAULT_OPTIONS = ref.$CI_DEFAULT_OPTIONS; | ||
var $ci = ref.$ci; | ||
var inputElement = el.tagName.toLowerCase() === 'input' ? el : el.querySelector('input'); | ||
@@ -275,3 +277,3 @@ if (!inputElement) { | ||
} | ||
var options = Object.assign({}, ($CI_DEFAULT_OPTIONS || DEFAULT_OPTIONS), optionsFromBinding); | ||
var options = Object.assign({}, ($ci ? $ci.GLOBAL_OPTIONS : DEFAULT_OPTIONS), optionsFromBinding); | ||
var distractionFree = options.distractionFree; | ||
@@ -311,5 +313,2 @@ var autoDecimalMode = options.autoDecimalMode; | ||
}; | ||
var validateValueRange = function (value, valueRange) { | ||
return Math.min(Math.max(value, valueRange.min), valueRange.max) | ||
}; | ||
var triggerEvent = function (el, eventName) { | ||
@@ -328,3 +327,7 @@ var ref = el.$ci; | ||
var options = ref.options; | ||
format(el, value != null ? currencyFormat.format(validateValueRange(value, options.valueRange)) : null); | ||
var ref$1 = options.valueRange; | ||
var min = ref$1.min; | ||
var max = ref$1.max; | ||
var validateValueRange = function () { return Math.min(Math.max(value, min), max); }; | ||
format(el, value != null ? currencyFormat.format(validateValueRange()) : null); | ||
if (value !== el.$ci.numberValue || forcedChange) { | ||
@@ -545,3 +548,3 @@ triggerEvent(el, 'change'); | ||
var this$1 = this; | ||
var options = Object.assign({}, this.$CI_DEFAULT_OPTIONS || DEFAULT_OPTIONS); | ||
var options = Object.assign({}, this.$ci ? this.$ci.GLOBAL_OPTIONS : DEFAULT_OPTIONS); | ||
Object.keys(DEFAULT_OPTIONS).forEach(function (key) { | ||
@@ -571,8 +574,8 @@ if (this$1[key] !== undefined) { | ||
var globalOptions = ref.globalOptions; if ( globalOptions === void 0 ) globalOptions = {}; | ||
Vue.prototype.$CI_DEFAULT_OPTIONS = Object.assign({}, DEFAULT_OPTIONS, globalOptions); | ||
Vue.component(componentName, component); | ||
Vue.directive(directiveName, directive); | ||
Vue.prototype.$parseCurrency = function (str, options) { | ||
if ( options === void 0 ) options = {}; | ||
return parseCurrency(str, Object.assign({}, globalOptions, options)); | ||
Vue.prototype.$ci = { | ||
getValue: getValue, | ||
setValue: setValue, | ||
GLOBAL_OPTIONS: Object.assign({}, DEFAULT_OPTIONS, globalOptions) | ||
}; | ||
@@ -589,3 +592,3 @@ } | ||
exports.default = plugin; | ||
exports.parseCurrency = parseCurrency; | ||
exports.getValue = getValue; | ||
exports.setValue = setValue; | ||
@@ -592,0 +595,0 @@ |
{ | ||
"name": "vue-currency-input", | ||
"description": "Easy input of currency formatted numbers for Vue.js.", | ||
"version": "1.20.3", | ||
"version": "1.21.0", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "unpkg": "dist/vue-currency-input.umd.js", |
import dispatchEvent from './utils/dispatchEvent' | ||
import { toExternalNumberModel } from './utils/numberUtils' | ||
import NumberFormat from './numberFormat' | ||
@@ -17,20 +16,18 @@ export const DEFAULT_OPTIONS = { | ||
/** | ||
* Parses a number from a currency formatted string. | ||
* Returns the current number value of an input. | ||
* | ||
* @param {String} formattedValue The currency formatted string to be parsed, for example `$1,234.50`. | ||
* @param {Object} options The configured options of the respective `v-currency` directive. | ||
* @returns {Number | null} The parsed number or `null` if the formatted string does not match. | ||
* @param {HTMLInputElement} el The input element the `v-currency` directive is bound to. | ||
* @returns {Number | null} The current number value or `null` if empty. | ||
*/ | ||
export const parseCurrency = (formattedValue, options) => { | ||
const mergedOptions = { ...DEFAULT_OPTIONS, ...options } | ||
const numberFormat = new NumberFormat(mergedOptions) | ||
return toExternalNumberModel(numberFormat.parse(formattedValue), mergedOptions.valueAsInteger, numberFormat.maximumFractionDigits) | ||
export const getValue = el => { | ||
const { numberValue, currencyFormat, options } = el.$ci | ||
return toExternalNumberModel(numberValue, options.valueAsInteger, currencyFormat.maximumFractionDigits) | ||
} | ||
/** | ||
* Sets a value of a input programmatically. | ||
* Sets the value of an input programmatically. | ||
* | ||
* @param {HTMLInputElement} el An input element using on the `v-currency` directive. | ||
* @param {HTMLInputElement} el The input element the `v-currency` directive is bound to. | ||
* @param {Number} value The number to be set. | ||
*/ | ||
export const setValue = (el, value) => dispatchEvent(el, 'format', { value }) |
@@ -73,3 +73,3 @@ import { DEFAULT_OPTIONS, setValue } from './api' | ||
options () { | ||
const options = { ...this.$CI_DEFAULT_OPTIONS || DEFAULT_OPTIONS } | ||
const options = { ...this.$ci ? this.$ci.GLOBAL_OPTIONS : DEFAULT_OPTIONS } | ||
Object.keys(DEFAULT_OPTIONS).forEach(key => { | ||
@@ -76,0 +76,0 @@ if (this[key] !== undefined) { |
@@ -11,3 +11,3 @@ import { DEFAULT_OPTIONS, setValue } from './api' | ||
const init = (el, optionsFromBinding, { $CI_DEFAULT_OPTIONS }) => { | ||
const init = (el, optionsFromBinding, { $ci }) => { | ||
const inputElement = el.tagName.toLowerCase() === 'input' ? el : el.querySelector('input') | ||
@@ -17,3 +17,3 @@ if (!inputElement) { | ||
} | ||
const options = { ...($CI_DEFAULT_OPTIONS || DEFAULT_OPTIONS), ...optionsFromBinding } | ||
const options = { ...($ci ? $ci.GLOBAL_OPTIONS : DEFAULT_OPTIONS), ...optionsFromBinding } | ||
const { distractionFree, autoDecimalMode, valueRange } = options | ||
@@ -55,6 +55,2 @@ if (typeof distractionFree === 'boolean') { | ||
const validateValueRange = (value, valueRange) => { | ||
return Math.min(Math.max(value, valueRange.min), valueRange.max) | ||
} | ||
const triggerEvent = (el, eventName) => { | ||
@@ -68,3 +64,5 @@ let { numberValue, currencyFormat, options } = el.$ci | ||
const { currencyFormat, options } = el.$ci | ||
format(el, value != null ? currencyFormat.format(validateValueRange(value, options.valueRange)) : null) | ||
const { min, max } = options.valueRange | ||
const validateValueRange = () => Math.min(Math.max(value, min), max) | ||
format(el, value != null ? currencyFormat.format(validateValueRange()) : null) | ||
if (value !== el.$ci.numberValue || forcedChange) { | ||
@@ -71,0 +69,0 @@ triggerEvent(el, 'change') |
@@ -48,3 +48,3 @@ import { Component, DirectiveOptions, PluginFunction } from 'vue' | ||
export function parseCurrency (formattedValue: string, options: CurrencyInputOptions): number | ||
export function getValue (el: HTMLInputElement): number | null | ||
@@ -55,4 +55,8 @@ export function setValue (el: HTMLInputElement, value: Number): void | ||
interface Vue { | ||
$parseCurrency (formattedValue: string, options?: CurrencyInputOptions): number | ||
GLOBAL_OPTIONS: CurrencyInputOptions | ||
$getValue (el: HTMLInputElement): number | null | ||
$setValue (el: HTMLInputElement, value: Number): void | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
import { parseCurrency, setValue } from './api' | ||
import { getValue, setValue } from './api' | ||
import component from './component' | ||
@@ -8,3 +8,3 @@ import directive from './directive' | ||
export { | ||
parseCurrency, | ||
getValue, | ||
setValue, | ||
@@ -11,0 +11,0 @@ component as CurrencyInput, |
@@ -1,2 +0,2 @@ | ||
import { DEFAULT_OPTIONS, parseCurrency } from './api' | ||
import { DEFAULT_OPTIONS, getValue, setValue } from './api' | ||
import component from './component' | ||
@@ -11,7 +11,10 @@ import directive from './directive' | ||
} = {}) { | ||
Vue.prototype.$CI_DEFAULT_OPTIONS = { ...DEFAULT_OPTIONS, ...globalOptions } | ||
Vue.component(componentName, component) | ||
Vue.directive(directiveName, directive) | ||
Vue.prototype.$parseCurrency = (str, options = {}) => parseCurrency(str, { ...globalOptions, ...options }) | ||
Vue.prototype.$ci = { | ||
getValue, | ||
setValue, | ||
GLOBAL_OPTIONS: { ...DEFAULT_OPTIONS, ...globalOptions } | ||
} | ||
} | ||
} |
1746
75582