primevue-mirror/components/lib/inputnumber/InputNumber.vue

992 lines
39 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2024-02-11 08:10:29 +00:00
<span :class="cx('root')" v-bind="ptmi('root')">
2022-09-14 11:26:01 +00:00
<INInputText
ref="input"
:id="inputId"
role="spinbutton"
2023-05-24 11:03:56 +00:00
:class="[cx('input'), inputClass]"
2022-09-14 11:26:01 +00:00
:style="inputStyle"
:value="formattedValue"
:aria-valuemin="min"
:aria-valuemax="max"
:aria-valuenow="modelValue"
:disabled="disabled"
:readonly="readonly"
:placeholder="placeholder"
:aria-labelledby="ariaLabelledby"
:aria-label="ariaLabel"
2024-03-18 10:02:11 +00:00
:invalid="invalid"
:variant="variant"
2022-09-14 11:26:01 +00:00
@input="onUserInput"
@keydown="onInputKeyDown"
@paste="onPaste"
@click="onInputClick"
@focus="onInputFocus"
@blur="onInputBlur"
v-bind="inputProps"
:pt="ptm('input')"
:unstyled="unstyled"
2022-09-14 11:26:01 +00:00
/>
2023-05-24 11:03:56 +00:00
<span v-if="showButtons && buttonLayout === 'stacked'" :class="cx('buttonGroup')" v-bind="ptm('buttonGroup')">
2024-03-18 10:02:11 +00:00
<slot name="incrementbutton" v-on="upButtonListeners">
2024-03-19 06:27:00 +00:00
<button :class="[cx('incrementButton'), incrementButtonClass]" v-on="upButtonListeners" :disabled="disabled" :tabindex="-1" aria-hidden="true" v-bind="{ ...incrementButtonProps, ...ptm('incrementButton') }">
<slot name="incrementbuttonicon">
2024-03-18 10:02:11 +00:00
<component :is="incrementButtonIcon ? 'span' : 'AngleUpIcon'" :class="incrementButtonIcon" v-bind="ptm('incrementButtonIcon')" data-pc-section="incrementbuttonicon" />
</slot>
2024-03-18 10:02:11 +00:00
</button>
</slot>
<slot name="decrementbutton" v-on="downButtonListeners">
2024-03-19 06:27:00 +00:00
<button :class="[cx('decrementButton'), decrementButtonClass]" v-on="downButtonListeners" :disabled="disabled" :tabindex="-1" aria-hidden="true" v-bind="{ ...decrementButtonProps, ...ptm('decrementButton') }">
<slot name="decrementbuttonicon">
2024-03-18 10:02:11 +00:00
<component :is="decrementButtonIcon ? 'span' : 'AngleDownIcon'" :class="decrementButtonIcon" v-bind="ptm('decrementButtonIcon')" data-pc-section="decrementbuttonicon" />
</slot>
2024-03-18 10:02:11 +00:00
</button>
</slot>
</span>
2024-03-18 10:02:11 +00:00
<slot name="incrementbutton" v-on="upButtonListeners">
<button
v-if="showButtons && buttonLayout !== 'stacked'"
:class="[cx('incrementButton'), incrementButtonClass]"
v-on="upButtonListeners"
:disabled="disabled"
:tabindex="-1"
aria-hidden="true"
2024-03-19 06:27:00 +00:00
v-bind="{ ...incrementButtonProps, ...ptm('incrementButton') }"
2024-03-18 10:02:11 +00:00
>
<slot name="incrementbuttonicon">
2024-03-18 10:02:11 +00:00
<component :is="incrementButtonIcon ? 'span' : 'AngleUpIcon'" :class="incrementButtonIcon" v-bind="ptm('incrementButtonIcon')" data-pc-section="incrementbuttonicon" />
</slot>
2024-03-18 10:02:11 +00:00
</button>
</slot>
<slot name="decrementbutton" v-on="downButtonListeners">
<button
v-if="showButtons && buttonLayout !== 'stacked'"
:class="[cx('decrementButton'), decrementButtonClass]"
v-on="downButtonListeners"
:disabled="disabled"
:tabindex="-1"
aria-hidden="true"
2024-03-19 06:27:00 +00:00
v-bind="{ ...decrementButtonProps, ...ptm('decrementButton') }"
2024-03-18 10:02:11 +00:00
>
<slot name="decrementbuttonicon">
2024-03-18 10:02:11 +00:00
<component :is="decrementButtonIcon ? 'span' : 'AngleDownIcon'" :class="decrementButtonIcon" v-bind="ptm('decrementButtonIcon')" data-pc-section="decrementbuttonicon" />
</slot>
2024-03-18 10:02:11 +00:00
</button>
</slot>
2022-09-06 12:03:37 +00:00
</span>
</template>
<script>
import AngleDownIcon from 'primevue/icons/angledown';
import AngleUpIcon from 'primevue/icons/angleup';
2022-12-08 11:04:25 +00:00
import InputText from 'primevue/inputtext';
import { DomHandler, ObjectUtils } from 'primevue/utils';
2023-05-24 11:03:56 +00:00
import BaseInputNumber from './BaseInputNumber.vue';
2022-09-06 12:03:37 +00:00
export default {
name: 'InputNumber',
2023-05-24 11:03:56 +00:00
extends: BaseInputNumber,
2024-02-11 08:10:29 +00:00
inheritAttrs: false,
2022-09-06 12:03:37 +00:00
emits: ['update:modelValue', 'input', 'focus', 'blur'],
numberFormat: null,
_numeral: null,
_decimal: null,
_group: null,
_minusSign: null,
_currency: null,
_suffix: null,
_prefix: null,
_index: null,
groupChar: '',
isSpecialChar: null,
prefixChar: null,
suffixChar: null,
timer: null,
data() {
return {
d_modelValue: this.modelValue,
focused: false
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
},
watch: {
modelValue(newValue) {
this.d_modelValue = newValue;
},
2022-09-14 11:26:01 +00:00
locale(newValue, oldValue) {
2022-09-06 12:03:37 +00:00
this.updateConstructParser(newValue, oldValue);
},
localeMatcher(newValue, oldValue) {
this.updateConstructParser(newValue, oldValue);
},
mode(newValue, oldValue) {
this.updateConstructParser(newValue, oldValue);
},
currency(newValue, oldValue) {
this.updateConstructParser(newValue, oldValue);
},
currencyDisplay(newValue, oldValue) {
this.updateConstructParser(newValue, oldValue);
},
useGrouping(newValue, oldValue) {
this.updateConstructParser(newValue, oldValue);
},
minFractionDigits(newValue, oldValue) {
this.updateConstructParser(newValue, oldValue);
},
maxFractionDigits(newValue, oldValue) {
this.updateConstructParser(newValue, oldValue);
},
suffix(newValue, oldValue) {
this.updateConstructParser(newValue, oldValue);
},
prefix(newValue, oldValue) {
this.updateConstructParser(newValue, oldValue);
}
},
created() {
this.constructParser();
},
methods: {
getOptions() {
return {
localeMatcher: this.localeMatcher,
style: this.mode,
currency: this.currency,
currencyDisplay: this.currencyDisplay,
useGrouping: this.useGrouping,
minimumFractionDigits: this.minFractionDigits,
maximumFractionDigits: this.maxFractionDigits,
2023-12-22 00:25:54 +00:00
roundingMode: this.roundingMode
2022-09-06 12:03:37 +00:00
};
},
constructParser() {
this.numberFormat = new Intl.NumberFormat(this.locale, this.getOptions());
2022-09-14 11:26:01 +00:00
const numerals = [...new Intl.NumberFormat(this.locale, { useGrouping: false }).format(9876543210)].reverse();
2022-09-06 12:03:37 +00:00
const index = new Map(numerals.map((d, i) => [d, i]));
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this._numeral = new RegExp(`[${numerals.join('')}]`, 'g');
this._group = this.getGroupingExpression();
this._minusSign = this.getMinusSignExpression();
this._currency = this.getCurrencyExpression();
this._decimal = this.getDecimalExpression();
this._suffix = this.getSuffixExpression();
this._prefix = this.getPrefixExpression();
2022-09-14 11:26:01 +00:00
this._index = (d) => index.get(d);
2022-09-06 12:03:37 +00:00
},
updateConstructParser(newValue, oldValue) {
if (newValue !== oldValue) {
this.constructParser();
}
},
escapeRegExp(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
},
getDecimalExpression() {
2022-09-14 11:26:01 +00:00
const formatter = new Intl.NumberFormat(this.locale, { ...this.getOptions(), useGrouping: false });
2022-09-06 12:03:37 +00:00
return new RegExp(`[${formatter.format(1.1).replace(this._currency, '').trim().replace(this._numeral, '')}]`, 'g');
},
getGroupingExpression() {
2022-09-14 11:26:01 +00:00
const formatter = new Intl.NumberFormat(this.locale, { useGrouping: true });
2022-09-06 12:03:37 +00:00
this.groupChar = formatter.format(1000000).trim().replace(this._numeral, '').charAt(0);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
return new RegExp(`[${this.groupChar}]`, 'g');
},
getMinusSignExpression() {
2022-09-14 11:26:01 +00:00
const formatter = new Intl.NumberFormat(this.locale, { useGrouping: false });
2022-09-06 12:03:37 +00:00
return new RegExp(`[${formatter.format(-1).trim().replace(this._numeral, '')}]`, 'g');
},
getCurrencyExpression() {
if (this.currency) {
const formatter = new Intl.NumberFormat(this.locale, { style: 'currency', currency: this.currency, currencyDisplay: this.currencyDisplay, minimumFractionDigits: 0, maximumFractionDigits: 0, roundingMode: this.roundingMode });
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
return new RegExp(`[${formatter.format(1).replace(/\s/g, '').replace(this._numeral, '').replace(this._group, '')}]`, 'g');
}
2022-09-14 11:26:01 +00:00
return new RegExp(`[]`, 'g');
2022-09-06 12:03:37 +00:00
},
getPrefixExpression() {
if (this.prefix) {
this.prefixChar = this.prefix;
2022-09-14 11:26:01 +00:00
} else {
const formatter = new Intl.NumberFormat(this.locale, { style: this.mode, currency: this.currency, currencyDisplay: this.currencyDisplay });
2022-09-06 12:03:37 +00:00
this.prefixChar = formatter.format(1).split('1')[0];
}
2022-09-14 11:26:01 +00:00
return new RegExp(`${this.escapeRegExp(this.prefixChar || '')}`, 'g');
2022-09-06 12:03:37 +00:00
},
getSuffixExpression() {
if (this.suffix) {
this.suffixChar = this.suffix;
2022-09-14 11:26:01 +00:00
} else {
const formatter = new Intl.NumberFormat(this.locale, { style: this.mode, currency: this.currency, currencyDisplay: this.currencyDisplay, minimumFractionDigits: 0, maximumFractionDigits: 0, roundingMode: this.roundingMode });
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this.suffixChar = formatter.format(1).split('1')[1];
}
2022-09-14 11:26:01 +00:00
return new RegExp(`${this.escapeRegExp(this.suffixChar || '')}`, 'g');
2022-09-06 12:03:37 +00:00
},
formatValue(value) {
if (value != null) {
2022-09-14 11:26:01 +00:00
if (value === '-') {
// Minus sign
2022-09-06 12:03:37 +00:00
return value;
}
if (this.format) {
let formatter = new Intl.NumberFormat(this.locale, this.getOptions());
let formattedValue = formatter.format(value);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (this.prefix) {
formattedValue = this.prefix + formattedValue;
}
if (this.suffix) {
formattedValue = formattedValue + this.suffix;
}
return formattedValue;
}
return value.toString();
}
return '';
},
parseValue(text) {
let filteredText = text
2022-09-14 11:26:01 +00:00
.replace(this._suffix, '')
.replace(this._prefix, '')
.trim()
.replace(/\s/g, '')
.replace(this._currency, '')
.replace(this._group, '')
.replace(this._minusSign, '-')
.replace(this._decimal, '.')
.replace(this._numeral, this._index);
2022-09-06 12:03:37 +00:00
if (filteredText) {
2022-09-14 11:26:01 +00:00
if (filteredText === '-')
// Minus sign
2022-09-06 12:03:37 +00:00
return filteredText;
let parsedValue = +filteredText;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
return isNaN(parsedValue) ? null : parsedValue;
}
return null;
},
repeat(event, interval, dir) {
if (this.readonly) {
return;
}
let i = interval || 500;
this.clearTimer();
this.timer = setTimeout(() => {
this.repeat(event, 40, dir);
}, i);
this.spin(event, dir);
},
spin(event, dir) {
if (this.$refs.input) {
let step = this.step * dir;
let currentValue = this.parseValue(this.$refs.input.$el.value) || 0;
let newValue = this.validateValue(currentValue + step);
this.updateInput(newValue, null, 'spin');
this.updateModel(event, newValue);
this.handleOnInput(event, currentValue, newValue);
}
},
onUpButtonMouseDown(event) {
if (!this.disabled) {
this.$refs.input.$el.focus();
this.repeat(event, null, 1);
event.preventDefault();
}
},
onUpButtonMouseUp() {
if (!this.disabled) {
this.clearTimer();
}
},
onUpButtonMouseLeave() {
if (!this.disabled) {
this.clearTimer();
}
},
onUpButtonKeyUp() {
if (!this.disabled) {
this.clearTimer();
}
},
onUpButtonKeyDown(event) {
if (event.code === 'Space' || event.code === 'Enter' || event.code === 'NumpadEnter') {
2022-09-06 12:03:37 +00:00
this.repeat(event, null, 1);
}
},
onDownButtonMouseDown(event) {
if (!this.disabled) {
this.$refs.input.$el.focus();
this.repeat(event, null, -1);
event.preventDefault();
}
},
onDownButtonMouseUp() {
if (!this.disabled) {
this.clearTimer();
}
},
onDownButtonMouseLeave() {
if (!this.disabled) {
this.clearTimer();
}
},
onDownButtonKeyUp() {
if (!this.disabled) {
this.clearTimer();
}
},
onDownButtonKeyDown(event) {
if (event.code === 'Space' || event.code === 'Enter' || event.code === 'NumpadEnter') {
2022-09-06 12:03:37 +00:00
this.repeat(event, null, -1);
}
},
onUserInput() {
if (this.isSpecialChar) {
this.$refs.input.$el.value = this.lastValue;
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this.isSpecialChar = false;
},
onInputKeyDown(event) {
if (this.readonly) {
return;
}
if (event.shiftKey || event.altKey) {
this.isSpecialChar = true;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
return;
}
2024-03-14 19:25:53 +00:00
this.lastValue = event.target.value;
2022-09-06 12:03:37 +00:00
let selectionStart = event.target.selectionStart;
let selectionEnd = event.target.selectionEnd;
let inputValue = event.target.value;
let newValueStr = null;
2022-12-08 11:04:25 +00:00
switch (event.code) {
case 'ArrowUp':
2022-09-06 12:03:37 +00:00
this.spin(event, 1);
event.preventDefault();
2022-09-14 11:26:01 +00:00
break;
2022-09-06 12:03:37 +00:00
2022-12-08 11:04:25 +00:00
case 'ArrowDown':
2022-09-06 12:03:37 +00:00
this.spin(event, -1);
event.preventDefault();
2022-09-14 11:26:01 +00:00
break;
2022-09-06 12:03:37 +00:00
2022-12-08 11:04:25 +00:00
case 'ArrowLeft':
2022-09-06 12:03:37 +00:00
if (!this.isNumeralChar(inputValue.charAt(selectionStart - 1))) {
event.preventDefault();
}
2022-09-14 11:26:01 +00:00
break;
2022-09-06 12:03:37 +00:00
2022-12-08 11:04:25 +00:00
case 'ArrowRight':
2022-09-06 12:03:37 +00:00
if (!this.isNumeralChar(inputValue.charAt(selectionStart))) {
event.preventDefault();
}
2022-09-14 11:26:01 +00:00
break;
2022-09-06 12:03:37 +00:00
2022-12-08 11:04:25 +00:00
case 'Tab':
case 'Enter':
case 'NumpadEnter':
2022-09-06 12:03:37 +00:00
newValueStr = this.validateValue(this.parseValue(inputValue));
this.$refs.input.$el.value = this.formatValue(newValueStr);
this.$refs.input.$el.setAttribute('aria-valuenow', newValueStr);
this.updateModel(event, newValueStr);
2022-09-14 11:26:01 +00:00
break;
2022-09-06 12:03:37 +00:00
2022-12-08 11:04:25 +00:00
case 'Backspace': {
2022-09-06 12:03:37 +00:00
event.preventDefault();
if (selectionStart === selectionEnd) {
const deleteChar = inputValue.charAt(selectionStart - 1);
const { decimalCharIndex, decimalCharIndexWithoutPrefix } = this.getDecimalCharIndexes(inputValue);
if (this.isNumeralChar(deleteChar)) {
const decimalLength = this.getDecimalLength(inputValue);
if (this._group.test(deleteChar)) {
this._group.lastIndex = 0;
newValueStr = inputValue.slice(0, selectionStart - 2) + inputValue.slice(selectionStart - 1);
2022-09-14 11:26:01 +00:00
} else if (this._decimal.test(deleteChar)) {
2022-09-06 12:03:37 +00:00
this._decimal.lastIndex = 0;
if (decimalLength) {
this.$refs.input.$el.setSelectionRange(selectionStart - 1, selectionStart - 1);
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
newValueStr = inputValue.slice(0, selectionStart - 1) + inputValue.slice(selectionStart);
}
2022-09-14 11:26:01 +00:00
} else if (decimalCharIndex > 0 && selectionStart > decimalCharIndex) {
2022-09-06 12:03:37 +00:00
const insertedText = this.isDecimalMode() && (this.minFractionDigits || 0) < decimalLength ? '' : '0';
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
newValueStr = inputValue.slice(0, selectionStart - 1) + insertedText + inputValue.slice(selectionStart);
2022-09-14 11:26:01 +00:00
} else if (decimalCharIndexWithoutPrefix === 1) {
2022-09-06 12:03:37 +00:00
newValueStr = inputValue.slice(0, selectionStart - 1) + '0' + inputValue.slice(selectionStart);
newValueStr = this.parseValue(newValueStr) > 0 ? newValueStr : '';
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
newValueStr = inputValue.slice(0, selectionStart - 1) + inputValue.slice(selectionStart);
}
}
this.updateValue(event, newValueStr, null, 'delete-single');
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
newValueStr = this.deleteRange(inputValue, selectionStart, selectionEnd);
this.updateValue(event, newValueStr, null, 'delete-range');
}
break;
}
2022-12-08 11:04:25 +00:00
case 'Delete':
2022-09-06 12:03:37 +00:00
event.preventDefault();
if (selectionStart === selectionEnd) {
const deleteChar = inputValue.charAt(selectionStart);
const { decimalCharIndex, decimalCharIndexWithoutPrefix } = this.getDecimalCharIndexes(inputValue);
if (this.isNumeralChar(deleteChar)) {
const decimalLength = this.getDecimalLength(inputValue);
if (this._group.test(deleteChar)) {
this._group.lastIndex = 0;
newValueStr = inputValue.slice(0, selectionStart) + inputValue.slice(selectionStart + 2);
2022-09-14 11:26:01 +00:00
} else if (this._decimal.test(deleteChar)) {
2022-09-06 12:03:37 +00:00
this._decimal.lastIndex = 0;
if (decimalLength) {
this.$refs.input.$el.setSelectionRange(selectionStart + 1, selectionStart + 1);
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
newValueStr = inputValue.slice(0, selectionStart) + inputValue.slice(selectionStart + 1);
}
2022-09-14 11:26:01 +00:00
} else if (decimalCharIndex > 0 && selectionStart > decimalCharIndex) {
2022-09-06 12:03:37 +00:00
const insertedText = this.isDecimalMode() && (this.minFractionDigits || 0) < decimalLength ? '' : '0';
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
newValueStr = inputValue.slice(0, selectionStart) + insertedText + inputValue.slice(selectionStart + 1);
2022-09-14 11:26:01 +00:00
} else if (decimalCharIndexWithoutPrefix === 1) {
2022-09-06 12:03:37 +00:00
newValueStr = inputValue.slice(0, selectionStart) + '0' + inputValue.slice(selectionStart + 1);
newValueStr = this.parseValue(newValueStr) > 0 ? newValueStr : '';
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
newValueStr = inputValue.slice(0, selectionStart) + inputValue.slice(selectionStart + 1);
}
}
this.updateValue(event, newValueStr, null, 'delete-back-single');
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
newValueStr = this.deleteRange(inputValue, selectionStart, selectionEnd);
this.updateValue(event, newValueStr, null, 'delete-range');
}
2022-09-14 11:26:01 +00:00
break;
2022-09-06 12:03:37 +00:00
2022-12-08 11:04:25 +00:00
case 'Home':
event.preventDefault();
if (!ObjectUtils.isEmpty(this.min)) {
2022-09-06 12:03:37 +00:00
this.updateModel(event, this.min);
}
2022-09-14 11:26:01 +00:00
break;
2022-09-06 12:03:37 +00:00
2022-12-08 11:04:25 +00:00
case 'End':
event.preventDefault();
if (!ObjectUtils.isEmpty(this.max)) {
2022-09-06 12:03:37 +00:00
this.updateModel(event, this.max);
}
2022-09-14 11:26:01 +00:00
break;
2022-09-06 12:03:37 +00:00
default:
if (this.readonly) {
return;
}
2022-09-06 12:03:37 +00:00
event.preventDefault();
let char = event.key;
const isDecimalSign = this.isDecimalSign(char);
const isMinusSign = this.isMinusSign(char);
2022-09-06 12:03:37 +00:00
if (((event.code.startsWith('Digit') || event.code.startsWith('Numpad')) && Number(char) >= 0 && Number(char) <= 9) || isMinusSign || isDecimalSign) {
this.insert(event, char, { isDecimalSign, isMinusSign });
}
break;
2022-09-06 12:03:37 +00:00
}
},
onPaste(event) {
event.preventDefault();
let data = (event.clipboardData || window['clipboardData']).getData('Text');
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (data) {
let filteredData = this.parseValue(data);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (filteredData != null) {
this.insert(event, filteredData.toString());
}
}
},
allowMinusSign() {
return this.min === null || this.min < 0;
},
isMinusSign(char) {
if (this._minusSign.test(char) || char === '-') {
this._minusSign.lastIndex = 0;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
return true;
}
return false;
},
isDecimalSign(char) {
if (this._decimal.test(char)) {
this._decimal.lastIndex = 0;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
return true;
}
return false;
},
isDecimalMode() {
return this.mode === 'decimal';
},
getDecimalCharIndexes(val) {
let decimalCharIndex = val.search(this._decimal);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this._decimal.lastIndex = 0;
const filteredVal = val.replace(this._prefix, '').trim().replace(/\s/g, '').replace(this._currency, '');
const decimalCharIndexWithoutPrefix = filteredVal.search(this._decimal);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this._decimal.lastIndex = 0;
return { decimalCharIndex, decimalCharIndexWithoutPrefix };
},
getCharIndexes(val) {
const decimalCharIndex = val.search(this._decimal);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this._decimal.lastIndex = 0;
const minusCharIndex = val.search(this._minusSign);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this._minusSign.lastIndex = 0;
const suffixCharIndex = val.search(this._suffix);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this._suffix.lastIndex = 0;
const currencyCharIndex = val.search(this._currency);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this._currency.lastIndex = 0;
return { decimalCharIndex, minusCharIndex, suffixCharIndex, currencyCharIndex };
},
insert(event, text, sign = { isDecimalSign: false, isMinusSign: false }) {
const minusCharIndexOnText = text.search(this._minusSign);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this._minusSign.lastIndex = 0;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (!this.allowMinusSign() && minusCharIndexOnText !== -1) {
return;
}
const selectionStart = this.$refs.input.$el.selectionStart;
const selectionEnd = this.$refs.input.$el.selectionEnd;
let inputValue = this.$refs.input.$el.value.trim();
const { decimalCharIndex, minusCharIndex, suffixCharIndex, currencyCharIndex } = this.getCharIndexes(inputValue);
let newValueStr;
if (sign.isMinusSign) {
if (selectionStart === 0) {
newValueStr = inputValue;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (minusCharIndex === -1 || selectionEnd !== 0) {
newValueStr = this.insertText(inputValue, text, 0, selectionEnd);
}
this.updateValue(event, newValueStr, text, 'insert');
}
2022-09-14 11:26:01 +00:00
} else if (sign.isDecimalSign) {
2022-09-06 12:03:37 +00:00
if (decimalCharIndex > 0 && selectionStart === decimalCharIndex) {
this.updateValue(event, inputValue, text, 'insert');
2022-09-14 11:26:01 +00:00
} else if (decimalCharIndex > selectionStart && decimalCharIndex < selectionEnd) {
2022-09-06 12:03:37 +00:00
newValueStr = this.insertText(inputValue, text, selectionStart, selectionEnd);
this.updateValue(event, newValueStr, text, 'insert');
2022-09-14 11:26:01 +00:00
} else if (decimalCharIndex === -1 && this.maxFractionDigits) {
2022-09-06 12:03:37 +00:00
newValueStr = this.insertText(inputValue, text, selectionStart, selectionEnd);
this.updateValue(event, newValueStr, text, 'insert');
}
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
const maxFractionDigits = this.numberFormat.resolvedOptions().maximumFractionDigits;
const operation = selectionStart !== selectionEnd ? 'range-insert' : 'insert';
if (decimalCharIndex > 0 && selectionStart > decimalCharIndex) {
2022-09-14 11:26:01 +00:00
if (selectionStart + text.length - (decimalCharIndex + 1) <= maxFractionDigits) {
const charIndex = currencyCharIndex >= selectionStart ? currencyCharIndex - 1 : suffixCharIndex >= selectionStart ? suffixCharIndex : inputValue.length;
2022-09-06 12:03:37 +00:00
newValueStr = inputValue.slice(0, selectionStart) + text + inputValue.slice(selectionStart + text.length, charIndex) + inputValue.slice(charIndex);
this.updateValue(event, newValueStr, text, operation);
}
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
newValueStr = this.insertText(inputValue, text, selectionStart, selectionEnd);
this.updateValue(event, newValueStr, text, operation);
}
}
},
insertText(value, text, start, end) {
let textSplit = text === '.' ? text : text.split('.');
if (textSplit.length === 2) {
const decimalCharIndex = value.slice(start, end).search(this._decimal);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this._decimal.lastIndex = 0;
2022-09-14 11:26:01 +00:00
return decimalCharIndex > 0 ? value.slice(0, start) + this.formatValue(text) + value.slice(end) : this.formatValue(text) || value;
2022-09-14 11:26:01 +00:00
} else if (end - start === value.length) {
2022-09-06 12:03:37 +00:00
return this.formatValue(text);
2022-09-14 11:26:01 +00:00
} else if (start === 0) {
2022-09-06 12:03:37 +00:00
return text + value.slice(end);
2022-09-14 11:26:01 +00:00
} else if (end === value.length) {
2022-09-06 12:03:37 +00:00
return value.slice(0, start) + text;
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
return value.slice(0, start) + text + value.slice(end);
}
},
deleteRange(value, start, end) {
let newValueStr;
2022-09-14 11:26:01 +00:00
if (end - start === value.length) newValueStr = '';
else if (start === 0) newValueStr = value.slice(end);
else if (end === value.length) newValueStr = value.slice(0, start);
else newValueStr = value.slice(0, start) + value.slice(end);
2022-09-06 12:03:37 +00:00
return newValueStr;
},
initCursor() {
let selectionStart = this.$refs.input.$el.selectionStart;
let inputValue = this.$refs.input.$el.value;
let valueLength = inputValue.length;
let index = null;
// remove prefix
let prefixLength = (this.prefixChar || '').length;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
inputValue = inputValue.replace(this._prefix, '');
selectionStart = selectionStart - prefixLength;
let char = inputValue.charAt(selectionStart);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (this.isNumeralChar(char)) {
return selectionStart + prefixLength;
}
//left
let i = selectionStart - 1;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
while (i >= 0) {
char = inputValue.charAt(i);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (this.isNumeralChar(char)) {
index = i + prefixLength;
break;
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
i--;
}
}
if (index !== null) {
this.$refs.input.$el.setSelectionRange(index + 1, index + 1);
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
i = selectionStart;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
while (i < valueLength) {
char = inputValue.charAt(i);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (this.isNumeralChar(char)) {
index = i + prefixLength;
break;
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
i++;
}
}
if (index !== null) {
this.$refs.input.$el.setSelectionRange(index, index);
}
}
return index || 0;
},
onInputClick() {
2022-12-08 11:04:25 +00:00
const currentValue = this.$refs.input.$el.value;
if (!this.readonly && currentValue !== DomHandler.getSelection()) {
2022-09-06 12:03:37 +00:00
this.initCursor();
}
},
isNumeralChar(char) {
if (char.length === 1 && (this._numeral.test(char) || this._decimal.test(char) || this._group.test(char) || this._minusSign.test(char))) {
this.resetRegex();
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
return true;
}
return false;
},
resetRegex() {
2022-09-14 11:26:01 +00:00
this._numeral.lastIndex = 0;
this._decimal.lastIndex = 0;
this._group.lastIndex = 0;
this._minusSign.lastIndex = 0;
2022-09-06 12:03:37 +00:00
},
updateValue(event, valueStr, insertedValueStr, operation) {
let currentValue = this.$refs.input.$el.value;
let newValue = null;
if (valueStr != null) {
newValue = this.parseValue(valueStr);
newValue = !newValue && !this.allowEmpty ? 0 : newValue;
this.updateInput(newValue, insertedValueStr, operation, valueStr);
this.handleOnInput(event, currentValue, newValue);
}
},
handleOnInput(event, currentValue, newValue) {
if (this.isValueChanged(currentValue, newValue)) {
2022-12-08 11:04:25 +00:00
this.$emit('input', { originalEvent: event, value: newValue, formattedValue: currentValue });
2022-09-06 12:03:37 +00:00
}
},
isValueChanged(currentValue, newValue) {
if (newValue === null && currentValue !== null) {
return true;
}
if (newValue != null) {
2022-09-14 11:26:01 +00:00
let parsedCurrentValue = typeof currentValue === 'string' ? this.parseValue(currentValue) : currentValue;
2022-09-06 12:03:37 +00:00
return newValue !== parsedCurrentValue;
}
return false;
},
validateValue(value) {
if (value === '-' || value == null) {
return null;
}
if (this.min != null && value < this.min) {
return this.min;
}
if (this.max != null && value > this.max) {
return this.max;
}
return value;
},
updateInput(value, insertedValueStr, operation, valueStr) {
insertedValueStr = insertedValueStr || '';
let inputValue = this.$refs.input.$el.value;
let newValue = this.formatValue(value);
let currentLength = inputValue.length;
if (newValue !== valueStr) {
newValue = this.concatValues(newValue, valueStr);
}
if (currentLength === 0) {
this.$refs.input.$el.value = newValue;
this.$refs.input.$el.setSelectionRange(0, 0);
const index = this.initCursor();
const selectionEnd = index + insertedValueStr.length;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this.$refs.input.$el.setSelectionRange(selectionEnd, selectionEnd);
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
let selectionStart = this.$refs.input.$el.selectionStart;
let selectionEnd = this.$refs.input.$el.selectionEnd;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this.$refs.input.$el.value = newValue;
let newLength = newValue.length;
if (operation === 'range-insert') {
const startValue = this.parseValue((inputValue || '').slice(0, selectionStart));
const startValueStr = startValue !== null ? startValue.toString() : '';
const startExpr = startValueStr.split('').join(`(${this.groupChar})?`);
const sRegex = new RegExp(startExpr, 'g');
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
sRegex.test(newValue);
const tExpr = insertedValueStr.split('').join(`(${this.groupChar})?`);
const tRegex = new RegExp(tExpr, 'g');
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
tRegex.test(newValue.slice(sRegex.lastIndex));
selectionEnd = sRegex.lastIndex + tRegex.lastIndex;
this.$refs.input.$el.setSelectionRange(selectionEnd, selectionEnd);
2022-09-14 11:26:01 +00:00
} else if (newLength === currentLength) {
if (operation === 'insert' || operation === 'delete-back-single') {
const re = /[.,]/g;
const newSelectionEnd = selectionEnd + Number(re.test(value) || re.test(insertedValueStr));
this.$refs.input.$el.setSelectionRange(newSelectionEnd, newSelectionEnd);
} else if (operation === 'delete-single') {
this.$refs.input.$el.setSelectionRange(selectionEnd - 1, selectionEnd - 1);
} else if (operation === 'delete-range' || operation === 'spin') {
this.$refs.input.$el.setSelectionRange(selectionEnd, selectionEnd);
}
2022-09-14 11:26:01 +00:00
} else if (operation === 'delete-back-single') {
2022-09-06 12:03:37 +00:00
let prevChar = inputValue.charAt(selectionEnd - 1);
let nextChar = inputValue.charAt(selectionEnd);
let diff = currentLength - newLength;
let isGroupChar = this._group.test(nextChar);
if (isGroupChar && diff === 1) {
selectionEnd += 1;
2022-09-14 11:26:01 +00:00
} else if (!isGroupChar && this.isNumeralChar(prevChar)) {
selectionEnd += -1 * diff + 1;
2022-09-06 12:03:37 +00:00
}
this._group.lastIndex = 0;
this.$refs.input.$el.setSelectionRange(selectionEnd, selectionEnd);
2022-09-14 11:26:01 +00:00
} else if (inputValue === '-' && operation === 'insert') {
2022-09-06 12:03:37 +00:00
this.$refs.input.$el.setSelectionRange(0, 0);
const index = this.initCursor();
const selectionEnd = index + insertedValueStr.length + 1;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this.$refs.input.$el.setSelectionRange(selectionEnd, selectionEnd);
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
selectionEnd = selectionEnd + (newLength - currentLength);
this.$refs.input.$el.setSelectionRange(selectionEnd, selectionEnd);
}
}
this.$refs.input.$el.setAttribute('aria-valuenow', value);
},
concatValues(val1, val2) {
if (val1 && val2) {
let decimalCharIndex = val2.search(this._decimal);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this._decimal.lastIndex = 0;
2022-12-08 11:04:25 +00:00
if (this.suffixChar) {
return decimalCharIndex !== -1 ? val1.replace(this.suffixChar, '').split(this._decimal)[0] + val2.replace(this.suffixChar, '').slice(decimalCharIndex) + this.suffixChar : val1;
2022-12-08 11:04:25 +00:00
} else {
return decimalCharIndex !== -1 ? val1.split(this._decimal)[0] + val2.slice(decimalCharIndex) : val1;
}
2022-09-06 12:03:37 +00:00
}
return val1;
},
getDecimalLength(value) {
if (value) {
const valueSplit = value.split(this._decimal);
if (valueSplit.length === 2) {
2022-09-14 11:26:01 +00:00
return valueSplit[1].replace(this._suffix, '').trim().replace(/\s/g, '').replace(this._currency, '').length;
2022-09-06 12:03:37 +00:00
}
}
return 0;
},
updateModel(event, value) {
this.d_modelValue = value;
this.$emit('update:modelValue', value);
},
onInputFocus(event) {
this.focused = true;
2022-12-08 11:04:25 +00:00
if (!this.disabled && !this.readonly && this.$refs.input.$el.value !== DomHandler.getSelection() && this.highlightOnFocus) {
event.target.select();
}
2022-09-06 12:03:37 +00:00
this.$emit('focus', event);
},
onInputBlur(event) {
this.focused = false;
let input = event.target;
let newValue = this.validateValue(this.parseValue(input.value));
2022-09-14 11:26:01 +00:00
this.$emit('blur', { originalEvent: event, value: input.value });
2022-09-06 12:03:37 +00:00
input.value = this.formatValue(newValue);
input.setAttribute('aria-valuenow', newValue);
this.updateModel(event, newValue);
if (!this.disabled && !this.readonly && this.highlightOnFocus) {
DomHandler.clearSelection();
}
2022-09-06 12:03:37 +00:00
},
clearTimer() {
if (this.timer) {
clearInterval(this.timer);
}
},
maxBoundry() {
return this.d_modelValue >= this.max;
},
minBoundry() {
return this.d_modelValue <= this.min;
2022-09-14 11:26:01 +00:00
}
2022-09-06 12:03:37 +00:00
},
computed: {
filled() {
2022-09-14 11:26:01 +00:00
return this.modelValue != null && this.modelValue.toString().length > 0;
2022-09-06 12:03:37 +00:00
},
upButtonListeners() {
return {
2022-09-14 11:26:01 +00:00
mousedown: (event) => this.onUpButtonMouseDown(event),
mouseup: (event) => this.onUpButtonMouseUp(event),
mouseleave: (event) => this.onUpButtonMouseLeave(event),
keydown: (event) => this.onUpButtonKeyDown(event),
keyup: (event) => this.onUpButtonKeyUp(event)
};
2022-09-06 12:03:37 +00:00
},
downButtonListeners() {
return {
2022-09-14 11:26:01 +00:00
mousedown: (event) => this.onDownButtonMouseDown(event),
mouseup: (event) => this.onDownButtonMouseUp(event),
mouseleave: (event) => this.onDownButtonMouseLeave(event),
keydown: (event) => this.onDownButtonKeyDown(event),
keyup: (event) => this.onDownButtonKeyUp(event)
};
2022-09-06 12:03:37 +00:00
},
formattedValue() {
const val = !this.modelValue && !this.allowEmpty ? 0 : this.modelValue;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
return this.formatValue(val);
},
getFormatter() {
return this.numberFormat;
}
},
components: {
2022-09-14 11:26:01 +00:00
INInputText: InputText,
AngleUpIcon: AngleUpIcon,
AngleDownIcon: AngleDownIcon
2022-09-06 12:03:37 +00:00
}
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
</script>