@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
154 lines • 6.13 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import adjustCaretPosition from './adjustCaretPosition';
import conformToMask from './conformToMask';
import { convertMaskToPlaceholder, isString, isNumber, processCaretTraps } from './utilities';
import { placeholderChar as defaultPlaceholderChar, strFunction } from './constants';
const emptyString = '';
const strObject = 'object';
const isAndroid = typeof navigator !== 'undefined' && /Android/i.test(navigator.userAgent);
const defer = typeof requestAnimationFrame !== 'undefined' ? requestAnimationFrame : setTimeout;
export default function createTextMaskInputElement(config) {
const state = {
previousConformedValue: undefined,
previousPlaceholder: undefined
};
return {
state,
update(rawValue) {
let {
inputElement,
mask: providedMask,
guide,
pipe,
placeholderChar = defaultPlaceholderChar,
keepCharPositions = false,
showMask = false
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : config;
if (typeof rawValue === 'undefined') {
rawValue = inputElement.value;
}
if (rawValue === state.previousConformedValue) {
return;
}
if (typeof providedMask === strObject && providedMask.pipe !== undefined && providedMask.mask !== undefined) {
pipe = providedMask.pipe;
providedMask = providedMask.mask;
}
let placeholder;
let mask;
if (providedMask instanceof Array) {
placeholder = convertMaskToPlaceholder(providedMask, placeholderChar);
}
if (providedMask === false) {
return;
}
const safeRawValue = getSafeRawValue(rawValue);
const currentCaretPosition = inputElement === null || inputElement === void 0 ? void 0 : inputElement.selectionEnd;
const {
previousConformedValue,
previousPlaceholder
} = state;
let caretTrapIndexes;
if (typeof providedMask === strFunction) {
mask = providedMask(safeRawValue, {
currentCaretPosition,
previousConformedValue,
placeholderChar
});
if (mask === false) {
return;
}
const {
maskWithoutCaretTraps,
indexes
} = processCaretTraps(mask);
mask = maskWithoutCaretTraps;
caretTrapIndexes = indexes;
placeholder = convertMaskToPlaceholder(mask, placeholderChar);
} else {
mask = providedMask;
}
const conformToMaskConfig = {
previousConformedValue,
guide,
placeholderChar,
pipe,
placeholder,
currentCaretPosition,
keepCharPositions
};
const {
conformedValue
} = conformToMask(safeRawValue, mask, conformToMaskConfig);
const piped = typeof pipe === strFunction;
let pipeResults = {};
if (piped) {
pipeResults = pipe(conformedValue, _objectSpread({
rawValue: safeRawValue
}, conformToMaskConfig));
if (pipeResults === false) {
pipeResults = {
value: previousConformedValue,
rejected: true
};
} else if (isString(pipeResults)) {
pipeResults = {
value: pipeResults
};
}
}
const finalConformedValue = piped ? pipeResults.value : conformedValue;
const adjustedCaretPosition = adjustCaretPosition({
previousConformedValue,
previousPlaceholder,
conformedValue: finalConformedValue,
placeholder,
rawValue: safeRawValue,
currentCaretPosition,
placeholderChar,
indexesOfPipedChars: pipeResults.indexesOfPipedChars,
caretTrapIndexes,
keepCharPositions
});
const inputValueShouldBeEmpty = finalConformedValue === placeholder && adjustedCaretPosition === 0;
const emptyValue = showMask ? placeholder : emptyString;
const inputElementValue = inputValueShouldBeEmpty ? emptyValue : finalConformedValue;
state.previousConformedValue = inputElementValue;
state.previousPlaceholder = placeholder;
if (inputElement.value === inputElementValue) {
return;
}
inputElement.value = inputElementValue;
safeSetSelection(inputElement, adjustedCaretPosition);
}
};
}
export function safeSetSelection(element, selectionPosition) {
var _element$setSelection;
if (document.activeElement === element || (element === null || element === void 0 ? void 0 : (_element$setSelection = element.setSelectionRange) === null || _element$setSelection === void 0 ? void 0 : _element$setSelection.name) === 'mockConstructor') {
const select = () => {
try {
element.setSelectionRange(selectionPosition, selectionPosition);
} catch (error) {}
};
if (isAndroid) {
defer(select, 0);
} else {
select();
}
}
}
function getSafeRawValue(inputValue) {
if (isString(inputValue)) {
return inputValue;
} else if (isNumber(inputValue)) {
return String(inputValue);
} else if (inputValue === undefined || inputValue === null) {
return emptyString;
} else {
throw new Error("The 'value' provided to Text Mask needs to be a string or a number. The value " + `received was:\n\n ${JSON.stringify(inputValue)}`);
}
}
//# sourceMappingURL=createTextMaskInputElement.js.map