UNPKG

@awsui/components-react

Version:

AWS UI is a collection of [React](https://reactjs.org/) components that help create intuitive, responsive, and accessible user experiences for web applications. It is developed by Amazon Web Services (AWS). This work is available under the terms of the [A

261 lines (260 loc) • 11.9 kB
import { __assign, __spreadArrays } from "tslib"; import { padLeftZeros, insertAt } from './strings'; var MaskFormat = (function () { function MaskFormat(_a) { var separator = _a.separator, _b = _a.inputSeparators, inputSeparators = _b === void 0 ? [] : _b, segments = _a.segments; this.positionFormats = {}; this.segments = []; this.separator = separator; this.inputSeparators = __spreadArrays(inputSeparators, [separator]); this.enrichSegmentDefinitions(segments); } MaskFormat.prototype.tryAppendSeparator = function (value) { var withSeparator = "" + value + this.separator; return this.isValid(withSeparator) ? withSeparator : value; }; MaskFormat.prototype.isSeparator = function (key) { return this.inputSeparators.indexOf(key) !== -1; }; MaskFormat.prototype.isValid = function (value) { var _this = this; var inputSegments = value.split(this.separator); if (inputSegments.length > this.segments.length) { return false; } return inputSegments.every(function (segmentValue, i) { var segment = _this.segments[i]; if (segmentValue === '') { if (i === inputSegments.length - 1) { return true; } else { return false; } } if (!segmentValue.match(/^\d+$/)) { return false; } if (segmentValue.length < segment.length && i !== inputSegments.length - 1) { return false; } var intValue = parseInt(segmentValue, 10); if (segmentValue.length > segment.length) { return false; } if (intValue < segment.min || intValue > segment.max(value)) { if (i === inputSegments.length - 1 && segmentValue.length < segment.length) { return true; } return false; } return true; }); }; MaskFormat.prototype.getValidValue = function (value) { var validValue = value; do { if (this.isValid(validValue)) { return this.tryAppendSeparator(validValue); } validValue = validValue.substring(0, validValue.length - 1); } while (validValue.length > 0); return ''; }; MaskFormat.prototype.autoComplete = function (value) { var _a = value.split(this.separator).reverse(), lastSegmentValue = _a[0], completeSegmentValues = _a.slice(1); var lastSegment = this.segments[completeSegmentValues.length]; var paddedLastSegmentValue = this.padWithDefaultValue(lastSegmentValue, lastSegment); var partial = __spreadArrays(completeSegmentValues.reverse(), [paddedLastSegmentValue]); while (partial.length < this.segments.length) { var nextSegment = this.segments[partial.length]; var segmentValue = this.padWithDefaultValue('', nextSegment); partial.push(segmentValue); } value = partial.join(this.separator); value = this.correctMinMaxValues(value); return value; }; MaskFormat.prototype.getSegmentValueWithAddition = function (position, value, enteredDigit) { var segment = this.positionFormats[position]; var segmentValue = value.substr(segment.start, segment.length); var segmentPosition = position - segment.start; var newValue = insertAt(segmentValue, enteredDigit, segmentPosition, segmentPosition + 1); return parseInt(newValue, 10); }; MaskFormat.prototype.replaceDigitsWithZeroes = function (value, cursorStart, cursorEnd) { var position = this.isCursorAtSeparator(cursorStart) ? cursorStart + 1 : cursorStart; if (this.isCursorAtSeparator(cursorStart)) { cursorStart++; } if (!this.isSegmentStart(cursorStart)) { var segment = this.positionFormats[cursorStart]; value = insertAt(value, padLeftZeros('', segment.end - cursorStart), cursorStart, segment.end); cursorStart = segment.end + 1; } var currentSegment; while (cursorStart < cursorEnd && (currentSegment = this.positionFormats[cursorStart + 1])) { var insertionEnd = Math.min(cursorEnd, currentSegment.end); value = insertAt(value, padLeftZeros('', insertionEnd - currentSegment.start), currentSegment.start, insertionEnd); cursorStart = insertionEnd + 1; } value = this.correctMinMaxValues(value); return { value: value, position: position }; }; MaskFormat.prototype.handleSeparatorInput = function (value, position) { if (position === value.length && !this.isSegmentStart(position)) { var segment = this.positionFormats[position]; var segmentValue = value.substr(segment.start, segment.length); segmentValue = this.padWithDefaultValue(segmentValue, segment); value = insertAt(value, segmentValue, segment.start, segment.end); value = this.correctMinMaxValues(value); return { value: value, position: value.length }; } }; MaskFormat.prototype.isCursorAtSeparator = function (position) { return 0 < position && position < this.getMaxLength() && this.positionFormats[position] === undefined; }; MaskFormat.prototype.isSegmentStart = function (position) { return position === 0 || this.isCursorAtSeparator(position - 1); }; MaskFormat.prototype.getSegmentMaxValue = function (value, position) { return this.positionFormats[position].max(value); }; MaskFormat.prototype.getSegmentMinValue = function (value, position) { return this.positionFormats[position].min; }; MaskFormat.prototype.getMaxLength = function () { var last = this.segments[this.segments.length - 1]; return last.start + last.length; }; MaskFormat.prototype.deleteSeparator = function (value, position) { value = insertAt(value, '0', position - 2, position - 1); return { value: this.correctMinMaxValues(value), position: position - 2 }; }; MaskFormat.prototype.deleteDigit = function (value, position) { value = insertAt(value, '0', position - 1, position); var length = value.length; if (value.slice(length - 2) === '0:') { value = value.slice(0, length - 2); } return { value: this.correctMinMaxValues(value), position: position - 1 }; }; MaskFormat.prototype.correctMinMaxValues = function (value) { var segment = this.positionFormats[0]; while (segment && value.length >= segment.end) { var segmentValue = parseInt(value.substr(segment.start, segment.length), 10); var segmentMax = segment.max(value); if (segmentValue < segment.min) { var toInsert = segment.min.toFixed(); toInsert = padLeftZeros(toInsert, segment.length); value = insertAt(value, toInsert, segment.start, segment.end); } if (segmentValue > segmentMax) { value = insertAt(value, segmentMax.toFixed(), segment.start, segment.end); } segment = this.positionFormats[segment.end + 1]; } return value.substr(0, this.segments[this.segments.length - 1].end); }; MaskFormat.prototype.formatPastedText = function (text, value, cursorStart, cursorEnd) { var keyArr = text.trim().split(''); var position = cursorStart; var formattedValue = value; if (cursorEnd > cursorStart && cursorEnd === value.length) { formattedValue = value.slice(0, cursorStart); } for (var _i = 0, keyArr_1 = keyArr; _i < keyArr_1.length; _i++) { var key = keyArr_1[_i]; if (position >= this.getMaxLength()) { break; } var result = this.processKey(formattedValue, key, position); formattedValue = result.value; position = result.position; } return this.tryAppendSeparator(formattedValue); }; MaskFormat.prototype.processKey = function (initialValue, key, initialPosition) { var value = initialValue; var position = initialPosition; if (this.isSeparator(key)) { var result = this.handleSeparatorInput(value, position); if (result) { value = result.value; position = result.position; } } else { var isCursorAtEnd = position === value.length; var segmentValue = this.getSegmentValueWithAddition(position, value, key); var segmentMaxValue = this.getSegmentMaxValue(value, position); var segmentMinValue = this.getSegmentMinValue(value, position); var firstDigitGreater = parseInt(key, 10) > parseInt(segmentMaxValue.toFixed()[0], 10); var isValidPosition = isCursorAtEnd || segmentValue.toFixed().length === 1; var exceedsMaxAtSegmentStart = this.isSegmentStart(position) && isValidPosition && firstDigitGreater; if (exceedsMaxAtSegmentStart) { value = insertAt(value, "0" + key, position, position + 2); position += 2; } else if (segmentValue > segmentMaxValue && this.isSegmentStart(position)) { value = insertAt(value, segmentMaxValue.toFixed(), position, position + segmentMaxValue.toFixed().length); position += segmentMaxValue.toFixed().length; } else if (segmentValue > segmentMaxValue) { value = insertAt(value, segmentMaxValue.toFixed(), position - 1, position + 1); position += 1; } else if (segmentValue < segmentMinValue && !this.isSegmentStart(position)) { value = insertAt(value, segmentMinValue.toFixed(), position, position + 1); position += 1; } else { value = insertAt(value, key, position, position + 1); position += 1; } } value = this.tryAppendSeparator(value); if (this.isCursorAtSeparator(position)) { position++; } return { value: value, position: position }; }; MaskFormat.prototype.padWithDefaultValue = function (segmentValue, segment) { var defaultValue = (segment["default"] || segment.min).toFixed(); defaultValue = padLeftZeros(defaultValue, segment.length); return insertAt(defaultValue, segmentValue, segment.length - segmentValue.length, segment.length); }; MaskFormat.prototype.enrichSegmentDefinitions = function (segments) { this.positionFormats = {}; this.segments = []; var position = 0; var _loop_1 = function (segment) { var max = segment.max; var fullSegment = __assign(__assign({}, segment), { max: typeof max === 'number' ? function () { return max; } : max, start: position, end: position + segment.length }); this_1.segments.push(fullSegment); for (var j = 0; j < fullSegment.length; j++) { this_1.positionFormats[position++] = fullSegment; } position++; }; var this_1 = this; for (var _i = 0, segments_1 = segments; _i < segments_1.length; _i++) { var segment = segments_1[_i]; _loop_1(segment); } }; return MaskFormat; }()); export default MaskFormat;