igniteui-webcomponents
Version:
Ignite UI for Web Components is a complete library of UI components, giving you the ability to build modern web applications using encapsulation and the concept of reusable components in a dependency-free approach.
179 lines • 6.91 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { property } from 'lit/decorators.js';
import { addThemingController } from '../../theming/theming-controller.js';
import { addSlotController, setSlots } from '../common/controllers/slot.js';
import { registerComponent } from '../common/definitions/register.js';
import { createFormValueState } from '../common/mixins/forms/form-value.js';
import { MaskBehaviorMixin, } from '../common/mixins/mask-behavior.js';
import { renderMaskedNativeInput } from '../common/templates/masked-input.js';
import { IgcInputBaseComponent } from '../input/input-base.js';
import { styles } from '../input/themes/input.base.css.js';
import { styles as shared } from '../input/themes/shared/input.common.css.js';
import { all } from '../input/themes/themes.js';
import IgcValidationContainerComponent from '../validation-container/validation-container.js';
import { MaskParser } from './mask-parser.js';
import { maskValidators } from './validators.js';
const Slots = setSlots('prefix', 'suffix', 'helper-text', 'value-missing', 'bad-input', 'custom-error', 'invalid');
export default class IgcMaskInputComponent extends MaskBehaviorMixin(IgcInputBaseComponent) {
constructor() {
super(...arguments);
this._parser = new MaskParser();
this._themes = addThemingController(this, all);
this._slots = addSlotController(this, {
slots: Slots,
});
this._formValue = createFormValueState(this, {
initialValue: '',
transformers: {
setFormValue: (value) => this._isRawMode ? value || null : this._maskedValue || null,
},
});
this.valueMode = 'raw';
}
static { this.tagName = 'igc-mask-input'; }
static { this.styles = [styles, shared]; }
static register() {
registerComponent(IgcMaskInputComponent, IgcValidationContainerComponent);
}
get __validators() {
return maskValidators;
}
get _isRawMode() {
return this.valueMode === 'raw';
}
set value(string) {
const value = string ?? '';
this._maskedValue = this._parser.apply(value);
this._updateMaskedValue();
this._formValue.setValueAndFormState(value);
}
get value() {
const value = this._formValue.value;
if (this._isRawMode) {
return value;
}
return value ? this._maskedValue : value;
}
set mask(value) {
super.mask = value;
if (this.value) {
this._maskedValue = this._parser.apply(this._formValue.value);
}
}
get mask() {
return super.mask;
}
set prompt(value) {
super.prompt = value;
if (this.value) {
this._maskedValue = this._parser.apply(this._formValue.value);
}
}
get prompt() {
return super.prompt;
}
_handleDragEnter() {
if (!this._focused && !this._formValue.value) {
this._maskedValue = this._parser.emptyMask;
}
}
_handleDragLeave() {
if (!this._focused) {
this._updateMaskedValue();
}
}
async _handleFocus() {
this._focused = true;
if (this.readOnly) {
return;
}
if (!this._formValue.value) {
this._maskedValue = this._parser.emptyMask;
await this.updateComplete;
this.select();
}
}
_handleBlur() {
this._focused = false;
this._updateMaskedValue();
super._handleBlur();
}
_handleChange() {
this._setTouchedState();
this.emitEvent('igcChange', { detail: this.value });
}
_restoreDefaultValue() {
const value = this.defaultValue;
this._maskedValue = this._parser.apply(value);
this._updateMaskedValue();
this._formValue.setValueAndFormState(value);
}
async _updateInput(text, { start, end }) {
const result = this._parser.replace(this._maskedValue, text, start, end);
this._maskedValue = result.value;
this._formValue.setValueAndFormState(this._parser.parse(this._maskedValue));
this.requestUpdate();
if (start !== this.mask.length) {
this.emitEvent('igcInput', { detail: this.value });
}
await this.updateComplete;
this._input?.setSelectionRange(result.end, result.end);
}
_syncValueFromMask() {
this.value = this._parser.parse(this._maskedValue);
}
_updateMaskedValue() {
if (this._isEmptyMask) {
this._maskedValue = '';
}
}
isValidMaskPattern() {
return this._parser.isValidString(this._maskedValue);
}
_renderInput() {
const hasNegativeTabIndex = this.getAttribute('tabindex') === '-1';
const hasHelperText = this._slots.hasAssignedElements('helper-text');
return renderMaskedNativeInput({
id: this._inputId,
partNames: this._resolvePartNames('input'),
name: this.name,
value: this._maskedValue,
placeholder: this.placeholder ?? this._parser.escapedMask,
readOnly: this.readOnly,
disabled: this.disabled,
autofocus: this.autofocus,
inputMode: this.inputMode,
tabindex: hasNegativeTabIndex ? -1 : undefined,
ariaDescribedBy: hasHelperText ? 'helper-text' : undefined,
ariaLabelledByElements: this._resolvedLabelElements,
onInput: this._handleInput,
onFocus: this._handleFocus,
onBlur: this._handleBlur,
onClick: this._handleClick,
onSetMaskSelection: this._setMaskSelection,
onCompositionStart: this._handleCompositionStart,
onCompositionEnd: this._handleCompositionEnd,
onChange: this._handleChange,
onDragEnter: this._handleDragEnter,
onDragLeave: this._handleDragLeave,
});
}
}
__decorate([
property({ attribute: 'value-mode' })
], IgcMaskInputComponent.prototype, "valueMode", void 0);
__decorate([
property()
], IgcMaskInputComponent.prototype, "value", null);
__decorate([
property()
], IgcMaskInputComponent.prototype, "mask", null);
__decorate([
property()
], IgcMaskInputComponent.prototype, "prompt", null);
//# sourceMappingURL=mask-input.js.map