date-input-control
Version:
Capture dates using day, month and year components
133 lines (132 loc) • 4.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createDateInputControlPart = void 0;
const focus_1 = require("./focus");
const selection_1 = require("./selection");
const input_1 = require("./input");
const is_key_1 = require("./is-key");
exports.createDateInputControlPart = (elPrev, elCurr, elNext) => {
const onKeyDown = (e) => {
const { key } = e;
const currentTarget = e.currentTarget;
if (
// If backspace key was pressed
is_key_1.isKeyBackspace(key) &&
// at the beginning of the field
selection_1.isSelectionStart(currentTarget) &&
// and there is a field to the left
elPrev) {
// Move focus to end of the previous field
focus_1.focusEnd(elPrev);
return;
}
if (
// If delete key was pressed
is_key_1.isKeyDelete(key) &&
// at the end of the field
selection_1.isSelectionEnd(currentTarget) &&
// and there is a field to the right
elNext) {
// Move focus to start of the next field
focus_1.focusStart(elNext);
return;
}
if (
// If left arrow was pressed
is_key_1.isKeyArrowLeft(key) &&
// when the cursor is at the beginning of the field
selection_1.isSelectionStart(currentTarget) &&
// and there is a field to the left
elPrev) {
// Prevent cursor moving
e.preventDefault();
// Move focus to end of the previous field
focus_1.focusEnd(elPrev);
return;
}
if (
// If right arrow key was pressed
is_key_1.isKeyArrowRight(key) &&
// when the cursor is at the end of the field
selection_1.isSelectionEnd(currentTarget) &&
// and there is a field to the right
elNext) {
// Prevent cursor moving
e.preventDefault();
// Move focus to start of the next field
focus_1.focusStart(elNext);
return;
}
};
const onKeyPress = (e) => {
const { key } = e;
const currentTarget = e.currentTarget;
if (
// If punctuation key was pressed
is_key_1.isKeySeparator(key)) {
// Swallow key press
e.preventDefault();
if (
// If pressed on a non-empty field
!input_1.isInputEmpty(currentTarget) &&
// and no text was highlighted
selection_1.isSelectionEmpty(currentTarget) &&
// and there is a field to the right
elNext) {
// Move focus to the next field
focus_1.focus(elNext);
}
return;
}
if (
// If number key is pressed
is_key_1.isKeyNumeric(key)) {
if (
// when the cursor is at the end of the field
selection_1.isSelectionEnd(currentTarget) &&
// and the field is full
input_1.isInputFull(currentTarget) &&
// and the the next field is not full
elNext && !input_1.isInputFull(elNext)) {
// Move focus to start of the next field
focus_1.focusStart(elNext);
}
return;
}
// Ignore all other key presses
e.preventDefault();
};
const onKeyUp = (e) => {
const { key } = e;
const currentTarget = e.currentTarget;
if (
// If number key was pressed
is_key_1.isKeyNumeric(key) &&
// when the cursor was at the end of the field
selection_1.isSelectionEnd(currentTarget) &&
// and the field is full
input_1.isInputFull(currentTarget) &&
// and the the next field is not full
elNext && !input_1.isInputFull(elNext)) {
// Move focus to start of the next field
focus_1.focusStart(elNext);
return;
}
};
const onTextInput = (e) => {
const data = e.data;
if (data && data.search(/[^0-9]/) > -1) {
e.preventDefault();
}
};
elCurr.addEventListener('keydown', onKeyDown);
elCurr.addEventListener('keypress', onKeyPress);
elCurr.addEventListener('keyup', onKeyUp);
elCurr.addEventListener('textInput', onTextInput);
return () => {
elCurr.removeEventListener('keydown', onKeyDown);
elCurr.removeEventListener('keypress', onKeyPress);
elCurr.removeEventListener('keyup', onKeyUp);
elCurr.removeEventListener('textInput', onTextInput);
};
};