UNPKG

@kelvininc/ui-components

Version:
27 lines (26 loc) 1.34 kB
import { getUTF8StringLength } from "../../utils/string.helper"; import { COMMON_INPUT_MASK_CONFIG, DATE_TIME_INPUT_MASK_CONFIG } from "./text-field.config"; import { EInputFieldType } from "./text-field.types"; export function getInputMaskConfig(type) { if (type === EInputFieldType.Text) { return COMMON_INPUT_MASK_CONFIG; } if (type === EInputFieldType.DateTime) { return DATE_TIME_INPUT_MASK_CONFIG; } return Object.assign({ alias: 'numeric' }, COMMON_INPUT_MASK_CONFIG); } export function isInputMaskCompatibleType(type) { return [EInputFieldType.Number, EInputFieldType.Text, EInputFieldType.DateTime].includes(type); } export function buildInputMask(input, inputType, options, maxLength) { // @ts-ignore the types library for Inputmask has the callback type definition for onBeforePaste incorrect, it should return string | false return Inputmask(Object.assign(Object.assign(Object.assign({}, getInputMaskConfig(inputType)), options), { onBeforePaste: (fieldValue) => { if (!maxLength) return fieldValue; return getUTF8StringLength(fieldValue) <= maxLength ? fieldValue : false; } })).mask(input); } export function getValueAsString(newValue) { return typeof newValue === 'number' ? newValue.toString() : (newValue || '').toString(); }