UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

112 lines (111 loc) 4.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _helpers = require("../../../shared/helpers.js"); function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } class InputModeNumber { constructor() { _defineProperty(this, "onBlur", () => { this.hasFocus = false; }); _defineProperty(this, "onFocus", () => { if (this.hasFocus || !this.inputElement) { return; } this.hasFocus = true; this._type = this.inputElement.type; if (this._type === 'number') { return; } this._value = this.inputElement.value; this._width = this.inputElement.offsetWidth; this._cssText = this.inputElement.style.cssText; this._placeholder = this.inputElement.placeholder; this._selectionStart = this.inputElement.selectionStart; this._selectionEnd = this.inputElement.selectionEnd; this.inputElement.placeholder = this._value; this.inputElement.type = 'number'; this.inputElement.classList.add('dnb-input-masked--hide-controls'); this.inputElement.style.width = `${this._width}px`; clearTimeout(this.timeout); this.timeout = setTimeout(() => { this.reset(); }, 10); }); _defineProperty(this, "reset", () => { if (!this.inputElement) { return; } try { var _this$inputElement$ru, _this$inputElement; this.inputElement.type = this._type; this.inputElement.style.cssText = this._cssText; this.inputElement.classList.remove('dnb-input-masked--hide-controls'); if (this.inputElement.value === '' || this.inputElement.value == null) { this.inputElement.value = this._value; } this.inputElement.placeholder = this._placeholder; if (this._selectionStart > 0) { this.inputElement.selectionStart = this._selectionStart; this.inputElement.selectionEnd = this._selectionEnd; } (_this$inputElement$ru = (_this$inputElement = this.inputElement)['runCorrectCaretPosition']) === null || _this$inputElement$ru === void 0 || _this$inputElement$ru.call(_this$inputElement); } catch (error) { console.error(error); } }); } setElement(element) { if (!_helpers.IS_IOS) { return; } this.focusEventName = 'mouseenter'; this.blurEventName = 'blur'; if (!this.inputElement) { this.inputElement = element; this.add(); this.handleLabel(); } } handleLabel() { var _this$inputElement2; const id = (_this$inputElement2 = this.inputElement) === null || _this$inputElement2 === void 0 ? void 0 : _this$inputElement2.id; if (!id) { return; } this.labelElement = document.querySelector(`[for="${id}"]`); if (this.labelElement) { this.labelElement.addEventListener('mousedown', this.onFocus); } } add() { var _this$inputElement3; const fnId = '__getCorrectCaretPosition'; if (this.inputElement && !((_this$inputElement3 = this.inputElement) !== null && _this$inputElement3 !== void 0 && _this$inputElement3[fnId])) { this.inputElement[fnId] = true; this.inputElement.addEventListener(this.focusEventName, this.onFocus); this.inputElement.addEventListener(this.blurEventName, this.onBlur); } } removeEvent(element) { if (element) { element.removeEventListener(this.focusEventName, this.onFocus); element.removeEventListener(this.blurEventName, this.onBlur); element.removeEventListener('mousedown', this.onFocus); } } remove() { this.reset(); clearTimeout(this.timeout); this.removeEvent(this.inputElement); this.removeEvent(this.labelElement); delete this.inputElement; delete this.labelElement; } } exports.default = InputModeNumber; //# sourceMappingURL=InputModeNumber.js.map