@alifd/next
Version:
A configurable component library for web built on React.
64 lines (63 loc) • 2.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.onTimeKeydown = exports.checkDateValue = exports.checkDayjsObj = void 0;
var util_1 = require("../../util");
// 检查传入值是否为 dayjs 对象
function checkDayjsObj(props, propName, componentName) {
if (props[propName] && !util_1.datejs.isSelf(props[propName])) {
return new Error("Invalid prop ".concat(propName, " supplied to ").concat(componentName, ". Required a dayjs object."));
}
}
exports.checkDayjsObj = checkDayjsObj;
// 检查传入值是否为 dayjs 对象
function checkDateValue(props, propName, componentName) {
if (props[propName] && !util_1.datejs.isSelf(props[propName]) && typeof props[propName] !== 'string') {
return new Error("Invalid prop ".concat(propName, " supplied to ").concat(componentName, ". Required a dayjs object or format date string."));
}
return null;
}
exports.checkDateValue = checkDateValue;
/**
* 监听键盘事件,操作时间
* @param e - 键盘事件
* @param param1 - Object
* @param type - second hour minute
*/
function onTimeKeydown(e, _a, type) {
var format = _a.format, timeInputStr = _a.timeInputStr, steps = _a.steps, value = _a.value;
if ([util_1.KEYCODE.UP, util_1.KEYCODE.DOWN, util_1.KEYCODE.PAGE_UP, util_1.KEYCODE.PAGE_DOWN].indexOf(e.keyCode) === -1)
return;
if ((e.altKey && [util_1.KEYCODE.PAGE_UP, util_1.KEYCODE.PAGE_DOWN].indexOf(e.keyCode) === -1) ||
// @ts-expect-error e.controlKey 是旧标准的用法,新标准使用 e.ctrlKey 来代表 Control 键是否被按下
e.controlKey ||
e.shiftKey)
return;
var time = (0, util_1.datejs)(timeInputStr, format, true);
if (time.isValid()) {
var stepUnit = e.altKey ? 'hour' : 'minute';
switch (e.keyCode) {
case util_1.KEYCODE.UP:
time = time.subtract(steps[type], type);
break;
case util_1.KEYCODE.DOWN:
time = time.add(steps[type], type);
break;
case util_1.KEYCODE.PAGE_UP:
time = time.subtract(steps[stepUnit], stepUnit);
break;
case util_1.KEYCODE.PAGE_DOWN:
time = time.add(steps[stepUnit], stepUnit);
break;
}
}
else if (value) {
time = value.clone();
}
else {
time = (0, util_1.datejs)();
time = time.hour(0).minute(0).second(0);
}
e.preventDefault();
return time.format(format);
}
exports.onTimeKeydown = onTimeKeydown;