@taiga-ui/kit
Version:
Taiga UI Angular main components kit
294 lines (289 loc) • 12.5 kB
JavaScript
import { __extends, __decorate, __param } from 'tslib';
import { Optional, Self, Inject, ChangeDetectorRef, Input, ViewChild, HostListener, Component, ChangeDetectionStrategy, forwardRef, NgModule } from '@angular/core';
import { NgControl } from '@angular/forms';
import { tuiDefaultProp, TUI_FOCUSABLE_ITEM_ACCESSOR, AbstractTuiNullableControl, TuiMapperPipeModule } from '@taiga-ui/cdk';
import { tuiCreateNumberMask, tuiCreateAutoCorrectedNumberPipe, formatNumber, maskedMoneyValueIsEmpty, maskedNumberStringToNumber, TuiPrimitiveTextfieldComponent, TuiPrimitiveTextfieldModule, TuiTextfieldControllerModule } from '@taiga-ui/core';
import { TuiValueAccessorModule } from '@taiga-ui/kit/directives';
import { TextMaskModule } from 'angular2-text-mask';
var DEFAULT_MAX_LENGTH = 18;
// @dynamic
var TuiInputNumberComponent = /** @class */ (function (_super) {
__extends(TuiInputNumberComponent, _super);
function TuiInputNumberComponent(control, changeDetectorRef) {
var _this = _super.call(this, control, changeDetectorRef) || this;
_this.min = -Infinity;
_this.max = Infinity;
_this.decimal = "not-zero" /* NotZero */;
_this.precision = 2;
_this.postfix = '';
_this.mask = function (allowNegative, decimal, precision, nativeFocusableElement) { return ({
mask: tuiCreateNumberMask({
allowNegative: allowNegative,
allowDecimal: decimal !== 'never',
decimalLimit: precision,
requireDecimal: decimal === 'always',
}),
pipe: tuiCreateAutoCorrectedNumberPipe(decimal === 'always' ? precision : 0, ',', nativeFocusableElement || undefined),
guide: false,
}); };
return _this;
}
TuiInputNumberComponent_1 = TuiInputNumberComponent;
Object.defineProperty(TuiInputNumberComponent.prototype, "nativeFocusableElement", {
get: function () {
return !this.primitiveTextfield || this.computedDisabled
? null
: this.primitiveTextfield.nativeFocusableElement;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TuiInputNumberComponent.prototype, "focused", {
get: function () {
return !!this.primitiveTextfield && this.primitiveTextfield.focused;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TuiInputNumberComponent.prototype, "isNegativeAllowed", {
get: function () {
return this.min < 0;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TuiInputNumberComponent.prototype, "inputMode", {
get: function () {
return this.decimal === 'never' ? "numeric" /* Numeric */ : "decimal" /* Decimal */;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TuiInputNumberComponent.prototype, "calculatedMaxLength", {
get: function () {
return (DEFAULT_MAX_LENGTH +
(this.decimal !== "never" /* Never */ && this.nativeValue.includes(',')
? this.precision + 1
: 0));
},
enumerable: true,
configurable: true
});
Object.defineProperty(TuiInputNumberComponent.prototype, "formattedValue", {
get: function () {
var value = this.value || 0;
var absValue = Math.abs(value);
var hasFraction = absValue % 1 > 0;
var limit = this.decimal === 'always' || hasFraction ? this.precision : 0;
var fraction = hasFraction
? value.toString().split('.')[1].substr(0, this.precision)
: '';
if (this.focused && this.decimal !== 'always') {
limit = fraction.length;
}
return formatNumber(value, limit);
},
enumerable: true,
configurable: true
});
Object.defineProperty(TuiInputNumberComponent.prototype, "computedValue", {
get: function () {
if (this.focused || !this.isNativeValueInLimit) {
return this.nativeValue;
}
if (this.value === null) {
return maskedMoneyValueIsEmpty(this.nativeValue) ? this.nativeValue : '';
}
return this.formattedValue;
},
enumerable: true,
configurable: true
});
TuiInputNumberComponent.prototype.onValue = function (value) {
if (maskedMoneyValueIsEmpty(value)) {
this.updateValue(null);
return;
}
if (this.isNativeValueNotFinished) {
return;
}
var capped = this.absoluteCapInputValue(value);
if (capped === null || isNaN(capped)) {
return;
}
this.updateValue(capped);
if (capped !== maskedNumberStringToNumber(value)) {
this.nativeValue = this.formattedValue;
}
};
TuiInputNumberComponent.prototype.onKeyDown = function (event) {
if (event.key !== ',' && event.key !== '.') {
return;
}
if (this.decimal === 'never') {
event.preventDefault();
return;
}
if (this.nativeValue.includes(',')) {
event.preventDefault();
this.setCaretAfterComma();
}
};
TuiInputNumberComponent.prototype.onFocused = function (focused) {
this.updateFocused(focused);
if (focused) {
return;
}
var nativeNumberValue = maskedNumberStringToNumber(this.nativeValue);
if (isNaN(nativeNumberValue)) {
this.clear();
return;
}
var clamped = Math.min(this.max, Math.max(this.min, nativeNumberValue));
this.updateValue(clamped);
this.nativeValue = this.formattedValue;
};
TuiInputNumberComponent.prototype.onHovered = function (hovered) {
this.updateHovered(hovered);
};
TuiInputNumberComponent.prototype.onPressed = function (pressed) {
this.updatePressed(pressed);
};
TuiInputNumberComponent.prototype.onZero = function (event) {
var decimal = this.nativeValue.split(',')[1] || '';
var nativeFocusableElement = this.nativeFocusableElement;
if (decimal.length < this.precision ||
!nativeFocusableElement ||
!nativeFocusableElement.selectionStart ||
this.nativeValue[nativeFocusableElement.selectionStart] !== '0') {
return;
}
event.preventDefault();
nativeFocusableElement.selectionStart++;
};
Object.defineProperty(TuiInputNumberComponent.prototype, "isNativeValueInLimit", {
get: function () {
if (this.nativeValue === '') {
return true;
}
var nativeNumberValue = maskedNumberStringToNumber(this.nativeValue);
return nativeNumberValue >= this.min && nativeNumberValue <= this.max;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TuiInputNumberComponent.prototype, "isNativeValueNotFinished", {
get: function () {
var nativeNumberValue = maskedNumberStringToNumber(this.nativeValue);
return nativeNumberValue < 0
? nativeNumberValue > this.max
: nativeNumberValue < this.min;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TuiInputNumberComponent.prototype, "nativeValue", {
get: function () {
return this.nativeFocusableElement ? this.nativeFocusableElement.value : '';
},
set: function (value) {
if (!this.primitiveTextfield || !this.nativeFocusableElement) {
return;
}
this.primitiveTextfield.value = value;
this.nativeFocusableElement.value = value;
},
enumerable: true,
configurable: true
});
TuiInputNumberComponent.prototype.clear = function () {
this.nativeValue = '';
this.updateValue(null);
};
TuiInputNumberComponent.prototype.absoluteCapInputValue = function (inputValue) {
var value = maskedNumberStringToNumber(inputValue);
var capped = value < 0 ? Math.max(this.min, value) : Math.min(value, this.max);
var ineligibleValue = isNaN(capped) || capped < this.min || capped > this.max;
return ineligibleValue ? null : capped;
};
TuiInputNumberComponent.prototype.setCaretAfterComma = function () {
if (!this.nativeFocusableElement) {
return;
}
var afterCommaPosition = this.nativeValue.length - this.precision;
this.nativeFocusableElement.setSelectionRange(afterCommaPosition, afterCommaPosition);
};
var TuiInputNumberComponent_1;
TuiInputNumberComponent.ctorParameters = function () { return [
{ type: NgControl, decorators: [{ type: Optional }, { type: Self }, { type: Inject, args: [NgControl,] }] },
{ type: ChangeDetectorRef, decorators: [{ type: Inject, args: [ChangeDetectorRef,] }] }
]; };
__decorate([
Input(),
tuiDefaultProp()
], TuiInputNumberComponent.prototype, "min", void 0);
__decorate([
Input(),
tuiDefaultProp()
], TuiInputNumberComponent.prototype, "max", void 0);
__decorate([
Input(),
tuiDefaultProp()
], TuiInputNumberComponent.prototype, "decimal", void 0);
__decorate([
Input(),
tuiDefaultProp()
], TuiInputNumberComponent.prototype, "precision", void 0);
__decorate([
Input(),
tuiDefaultProp()
], TuiInputNumberComponent.prototype, "postfix", void 0);
__decorate([
ViewChild(TuiPrimitiveTextfieldComponent)
], TuiInputNumberComponent.prototype, "primitiveTextfield", void 0);
__decorate([
HostListener('keydown.0', ['$event'])
], TuiInputNumberComponent.prototype, "onZero", null);
TuiInputNumberComponent = TuiInputNumberComponent_1 = __decorate([
Component({
selector: 'tui-input-number',
template: "<tui-primitive-textfield\n class=\"textfield\"\n tuiValueAccessor\n tuiTextfieldInputMode=\"decimal\"\n [pseudoHovered]=\"pseudoHovered\"\n [pseudoFocused]=\"computedFocused\"\n [invalid]=\"computedInvalid\"\n [tuiTextfieldMaxLength]=\"calculatedMaxLength\"\n [readOnly]=\"readOnly\"\n [disabled]=\"computedDisabled\"\n [textMask]=\"isNegativeAllowed | tuiMapper: mask:decimal:precision:nativeFocusableElement\"\n [value]=\"computedValue\"\n [postfix]=\"postfix\"\n [focusable]=\"focusable\"\n (valueChange)=\"onValue($event)\"\n (hoveredChange)=\"onHovered($event)\"\n (focusedChange)=\"onFocused($event)\"\n (pressedChange)=\"onPressed($event)\"\n (keydown)=\"onKeyDown($event)\"\n>\n <ng-content></ng-content>\n</tui-primitive-textfield>\n",
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [
{
provide: TUI_FOCUSABLE_ITEM_ACCESSOR,
useExisting: forwardRef(function () { return TuiInputNumberComponent_1; }),
},
],
styles: [":host{display:block;border-radius:var(--tui-radius-m)}.textfield{border-radius:inherit}"]
}),
__param(0, Optional()),
__param(0, Self()),
__param(0, Inject(NgControl)),
__param(1, Inject(ChangeDetectorRef))
], TuiInputNumberComponent);
return TuiInputNumberComponent;
}(AbstractTuiNullableControl));
var TuiInputNumberModule = /** @class */ (function () {
function TuiInputNumberModule() {
}
TuiInputNumberModule = __decorate([
NgModule({
imports: [
TextMaskModule,
TuiMapperPipeModule,
TuiPrimitiveTextfieldModule,
TuiTextfieldControllerModule,
TuiValueAccessorModule,
],
declarations: [TuiInputNumberComponent],
exports: [TuiInputNumberComponent],
})
], TuiInputNumberModule);
return TuiInputNumberModule;
}());
/**
* Generated bundle index. Do not edit.
*/
export { TuiInputNumberComponent, TuiInputNumberModule };
//# sourceMappingURL=taiga-ui-kit-components-input-number.js.map