@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
54 lines (53 loc) • 2.06 kB
JavaScript
export var backspaceHandler = function (initialValue, format, selectionStart, selectionEnd) {
var multiCharDelete = selectionStart !== selectionEnd;
if (multiCharDelete) {
var isCursorAtEnd = selectionEnd === initialValue.length;
if (!isCursorAtEnd) {
return format.replaceDigitsWithZeroes(initialValue, selectionStart, selectionEnd);
}
return {
value: initialValue.slice(0, selectionStart),
position: selectionStart
};
}
var isSeparator = format.isSegmentStart(selectionStart);
var atEnd = selectionStart === initialValue.length;
if (!atEnd) {
if (isSeparator) {
return format.deleteSeparator(initialValue, selectionStart);
}
else {
return format.deleteDigit(initialValue, selectionStart);
}
}
if (isSeparator) {
return {
value: initialValue.slice(0, selectionStart - 2),
position: selectionStart - 2
};
}
return {
value: initialValue.slice(0, selectionStart - 1),
position: selectionStart - 1
};
};
export var keyHandler = function (initialValue, key, format, selectionStart, selectionEnd) {
var value = initialValue;
var position = selectionStart;
if (selectionStart === value.length && value.length === format.getMaxLength()) {
return { value: value, position: position };
}
if (selectionStart !== value.length && selectionEnd === value.length) {
var sliceEnd = format.isCursorAtSeparator(selectionStart) ? selectionStart + 1 : selectionStart;
value = initialValue.slice(0, sliceEnd);
}
if (format.isCursorAtSeparator(position)) {
return { value: value, position: position + 1 };
}
return format.processKey(value, key, position);
};
export var enterHandler = function (value, format) {
var autoCompletedValue = format.autoComplete(value);
var position = autoCompletedValue.length;
return { value: autoCompletedValue, position: position };
};