ukelli-ui
Version:
Base on React's UI lib. Make frontend's dev simpler and faster.
298 lines (297 loc) • 13.6 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
/* eslint-disable @typescript-eslint/camelcase */
import React from 'react';
import { Call, DateFormat, UUID } from 'basic-helper';
import { LoadScript } from '../utils';
import { DateBasic } from '../date-basic';
import { Icon } from '../icon';
import { PopoverEntity } from '../popover/popover-entity';
import Mandarin from './zh';
var flatpickrCDNUrl = 'https://cdn.jsdelivr.net/npm/flatpickr@4.6.3/dist/flatpickr.min.js';
var isLoadingScript = false;
var loadJSFormCDN = function () {
return new Promise(function (resolve) {
if (window.flatpickr) {
// 如果已经加载成功,直接 resolve
return resolve();
}
if (!isLoadingScript) {
// 如果没有加载,则加载
isLoadingScript = true;
LoadScript({
src: flatpickrCDNUrl
})
.then(function (res) {
resolve();
});
}
else {
// 如果有在加载中,则等待加载完成在 resolve
var checkIsLoad_1 = function () {
if (!window.flatpickr) {
setTimeout(function () {
checkIsLoad_1();
}, 50);
}
else {
resolve();
}
};
checkIsLoad_1();
}
});
};
/**
* 基于 Flatpickr 的时间控件,样式可以根据喜好选择
*
* @export
* @class DatetimePicker
* @extends {DateBasic}
*/
var DatetimePicker = /** @class */ (function (_super) {
__extends(DatetimePicker, _super);
function DatetimePicker(props) {
var _this = _super.call(this, props) || this;
_this._refs = {};
_this.isControl = false;
_this._id = UUID();
// componentDidUpdate(prevProps) {
// if (JSON.stringify(this.props.value) !== JSON.stringify(prevProps.value)) {
// this.datepicker.setDate(this.props.value, false);
// }
// }
_this.setDate = function (nextValue, tiggerChange) {
if (tiggerChange === void 0) { tiggerChange = true; }
_this.datepicker.setDate(nextValue);
if (tiggerChange) {
_this.changeDate(nextValue);
}
};
_this.handleInputError = function (inputElem, isError) {
inputElem.classList.toggle('error', isError);
if (!_this.popTipEntity) {
_this.popTipEntity = new PopoverEntity({
id: _this._id,
});
}
if (isError) {
_this.popTipEntity.show({
elem: inputElem,
props: {
type: 'red',
position: 'bottom',
},
children: (React.createElement("div", { className: "p10" }, "\u8F93\u5165\u65F6\u95F4\u6709\u8BEF\uFF0C\u8BF7\u68C0\u67E5"))
});
}
else {
_this.popTipEntity.close();
}
};
_this.getInputValAsync = function () { return new Promise(function (resolve) {
setTimeout(function () {
resolve(_this.getDateRangeFromInput());
}, 50);
}); };
_this.getDateRangeFromInput = function () {
var _a = _this.props, defaultTimes = _a.defaultTimes, needTime = _a.needTime, mode = _a.mode;
var isRange = mode === 'range';
// const expectLen = isRange ? 2 : 1;
var rangeSeparator = _this.datepicker.l10n.rangeSeparator;
var inputElem = _this._refs[_this._id];
var inputVal = inputElem.value;
/** 如果没有任何值,则没有下一步 */
if (!inputVal)
return null;
var valueRange = inputVal.split(rangeSeparator).filter(function (i) { return !!i; });
var valueRangeLen = valueRange.length;
if (isRange && needTime && valueRangeLen === 1) {
/**
* 只选了一天的特殊处理, 给需要时间的添加后缀
* 把后面的日期加上,
* 并且改变输入框显示的值 */
valueRange[1] = DateFormat(valueRange[0], 'YYYY-MM-DD') + (needTime ? " " + defaultTimes[1] : '');
_this.datepicker.setDate(valueRange, false);
valueRangeLen = valueRange.length;
}
// if(valueRange !== expectLen) return null;
var dateRange = (function () {
var resDataRange = [];
var isValid = true;
for (var i = 0; i < valueRangeLen; i++) {
var targetDate = new Date(valueRange[i]);
var isValidDate = !isNaN(targetDate.getTime());
if (isValidDate) {
resDataRange.push(targetDate);
}
else {
isValid = false;
}
}
_this.handleInputError(inputElem, !isValid);
return resDataRange;
})();
return dateRange;
};
_this.handleChange = function (rangeValues, dateStr, instance) { return __awaiter(_this, void 0, void 0, function () {
var mode, dateRange;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
mode = this.props.mode;
return [4 /*yield*/, this.getInputValAsync()];
case 1:
dateRange = _a.sent();
if (!dateRange)
return [2 /*return*/];
if (mode === 'single' && Array.isArray(dateRange)) {
this.changeDate(dateRange[0]);
}
else if (mode === 'range') {
this.changeDate(dateRange);
}
return [2 /*return*/];
}
});
}); };
_this.initPicker = function () {
if (!window.flatpickr)
return console.error("\u52A0\u8F7D flatpickr \u5931\u8D25");
window.flatpickr.l10ns.zh = Mandarin;
var _a = _this.props, mode = _a.mode, needTime = _a.needTime, lang = _a.lang, allowInput = _a.allowInput, value = _a.value, defaultValue = _a.defaultValue, onChange = _a.onChange,
// enableTime,
defaultTimes = _a.defaultTimes, others = __rest(_a, ["mode", "needTime", "lang", "allowInput", "value", "defaultValue", "onChange", "defaultTimes"]);
var flatpickrOptions = __assign(__assign({}, others), {
/** 自带的时分秒选择不符合实际要求 */
enableTime: false, time_24hr: true, dateFormat: "Y-m-d" + (needTime ? ' H:i:S' : ''), disableMobile: true, defaultDate: _this.value, defaultHour: 0, enableSeconds: false, locale: lang, mode: mode,
// wrap: true,
allowInput: allowInput, onClose: _this.handleChange });
_this.datepicker = window.flatpickr(_this._refs[_this._id], flatpickrOptions);
};
_this.changeDate = function (val) {
var id = _this._id;
/** 继承 DateBasic 获取的 emitChangeValue 统一处理过滤并广播的 value 接口 */
var emitVal = _this.emitChangeValue(val);
_this.value = emitVal;
_this._refs[id].blur && _this._refs[id].blur();
};
var _a = _this.props, value = _a.value, defaultValue = _a.defaultValue;
_this.isControl = props.hasOwnProperty('value');
var defaultVal = value || defaultValue;
_this.value = defaultVal;
return _this;
}
DatetimePicker.prototype.componentDidMount = function () {
var _this = this;
loadJSFormCDN()
.then(function () {
_this.initPicker();
});
Call(this.props.didMount, this.value);
};
DatetimePicker.prototype.componentWillUnmount = function () {
if (this.datepicker)
this.datepicker.destroy();
if (this.popTipEntity)
this.popTipEntity.destroy();
this._id = '';
};
DatetimePicker.prototype.render = function () {
var _this = this;
var _a = this.props, needTime = _a.needTime, mode = _a.mode;
return (React.createElement("div", { className: "__flatpickr input-control " + (needTime ? 'long' : '') + " " + mode },
React.createElement("div", { className: "input-group" },
React.createElement("span", { className: "input-addon", onClick: function (e) {
if (_this.datepicker)
_this.datepicker.toggle();
} },
React.createElement(Icon, { n: "date" })),
React.createElement("input", { type: "text", className: "form-control input-sm", id: this._id, ref: function (e) { _this._refs[_this._id] = e; } }))));
};
DatetimePicker.setCDNUrl = function (url) {
flatpickrCDNUrl = url;
};
DatetimePicker.defaultProps = {
needTime: true,
toUTC: true,
allowInput: true,
// clickToClose: true,
// enableTime: false,
outputAsString: false,
mode: 'single',
lang: 'zh',
defaultTimes: ['00:00:00', '23:59:59'],
};
return DatetimePicker;
}(DateBasic));
export default DatetimePicker;