test-nut-ui
Version:
<p align="center"> <img alt="logo" src="https://img11.360buyimg.com/imagetools/jfs/t1/211965/25/7152/22022/61b16785E433119bb/aa41d7a9f7e823f3.png" width="150" style="margin-bottom: 10px;"> </p>
508 lines (507 loc) • 21.7 kB
JavaScript
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
var _excluded = ["startDate", "endDate", "type", "showChinese", "minuteStep", "visible", "title", "titles", "borderRadius", "cellHeight", "tabs", "tabsTitle", "isTabs", "defaultValue", "withSeparator", "tabsWithSeparator", "cancelText", "confirmText", "formatter", "onClose", "onConfirm", "filter", "onChange", "threeDimensional", "className", "style"];
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
enumerableOnly && (symbols = symbols.filter(function(sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
})), keys.push.apply(keys, symbols);
}
return keys;
}
function _objectSpread(target) {
for (var i = 1; i < arguments.length; i++) {
var source = null != arguments[i] ? arguments[i] : {};
i % 2 ? ownKeys(Object(source), true).forEach(function(key) {
_defineProperty(target, key, source[key]);
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function(key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
return target;
}
import React__default, { useState, useEffect } from "react";
import Picker from "./Picker.js";
import { useConfig } from "./ConfigProvider.js";
import { u as usePropsValue } from "./use-props-value.js";
import { C as ComponentDefaults } from "./typings.js";
var currentYear = /* @__PURE__ */ (/* @__PURE__ */ new Date()).getFullYear();
var defaultProps = _objectSpread(_objectSpread({}, ComponentDefaults), {}, {
visible: false,
title: "",
titles: [],
type: "date",
borderRadius: 8,
cellHeight: 50,
tabs: [],
tabsTitle: [],
isTabs: false,
showChinese: true,
threeDimensional: false,
minuteStep: 1,
tabsWithSeparator: [false, false],
startDate: new Date(currentYear - 10, 0, 1),
endDate: new Date(currentYear + 10, 11, 31)
});
var DatePicker = function DatePicker2(props) {
var _defaultProps$props = _objectSpread(_objectSpread({}, defaultProps), props), startDate = _defaultProps$props.startDate, endDate = _defaultProps$props.endDate, type = _defaultProps$props.type, showChinese = _defaultProps$props.showChinese, minuteStep = _defaultProps$props.minuteStep, visible = _defaultProps$props.visible, title = _defaultProps$props.title, titles = _defaultProps$props.titles, borderRadius = _defaultProps$props.borderRadius, cellHeight = _defaultProps$props.cellHeight, tabs = _defaultProps$props.tabs, tabsTitle = _defaultProps$props.tabsTitle, isTabs = _defaultProps$props.isTabs, defaultValue = _defaultProps$props.defaultValue, withSeparator = _defaultProps$props.withSeparator, tabsWithSeparator = _defaultProps$props.tabsWithSeparator, cancelText = _defaultProps$props.cancelText, confirmText = _defaultProps$props.confirmText, formatter = _defaultProps$props.formatter, onClose = _defaultProps$props.onClose, _onConfirm = _defaultProps$props.onConfirm, filter = _defaultProps$props.filter, onChange = _defaultProps$props.onChange, threeDimensional = _defaultProps$props.threeDimensional, className = _defaultProps$props.className, style = _defaultProps$props.style, rest = _objectWithoutProperties(_defaultProps$props, _excluded);
var _useConfig = useConfig(), locale = _useConfig.locale;
var datepickerLang = locale.datepicker;
var zhCNType = {
day: datepickerLang.day,
year: datepickerLang.year,
month: datepickerLang.month,
hour: datepickerLang.hour,
minute: datepickerLang.min,
seconds: datepickerLang.seconds
};
var _useState = useState([]), _useState2 = _slicedToArray(_useState, 2), defaultValueOfPicker = _useState2[0], setDefaultValueOfPicker = _useState2[1];
var _useState3 = useState([]), _useState4 = _slicedToArray(_useState3, 2), defaultValueOfPicker2 = _useState4[0], setDefaultValueOfPicker2 = _useState4[1];
var _useState5 = useState([]), _useState6 = _slicedToArray(_useState5, 2), options = _useState6[0], setOptions = _useState6[1];
var _useState7 = useState([]), _useState8 = _slicedToArray(_useState7, 2), options2 = _useState8[0], setOptions2 = _useState8[1];
var isDate = function isDate2(val) {
return Object.prototype.toString.call(val) === "[object Date]" && !Number.isNaN(val.getTime());
};
var formatValue = function formatValue2(value) {
var cvalue = value;
if (!cvalue || cvalue && !isDate(cvalue)) {
cvalue = startDate;
}
var timestmp = Math.max(cvalue.getTime(), startDate.getTime());
timestmp = Math.min(timestmp, endDate.getTime());
return new Date(timestmp);
};
var _useState9 = useState(null), _useState10 = _slicedToArray(_useState9, 2), currentDateC = _useState10[0], setcurrentDateC = _useState10[1];
var _usePropsValue = usePropsValue({
value: props.value && formatValue(props.value),
defaultValue: formatValue(props.defaultValue || null),
finalValue: null,
onChange: function onChange2(val) {
}
}), _usePropsValue2 = _slicedToArray(_usePropsValue, 2), currentDate = _usePropsValue2[0], setCurrentDate = _usePropsValue2[1];
function getMonthEndDay(year, month) {
return new Date(year, month, 0).getDate();
}
var getBoundary = function getBoundary2(type2, value) {
var _ref;
var boundary = type2 === "min" ? startDate : endDate;
var year = boundary.getFullYear();
var month = 1;
var date = 1;
var hour = 0;
var minute = 0;
if (type2 === "max") {
month = 12;
date = getMonthEndDay(value.getFullYear(), value.getMonth() + 1);
hour = 23;
minute = 59;
}
var seconds = minute;
if (value.getFullYear() === year) {
month = boundary.getMonth() + 1;
if (value.getMonth() + 1 === month) {
date = boundary.getDate();
if (value.getDate() === date) {
hour = boundary.getHours();
if (value.getHours() === hour) {
minute = boundary.getMinutes();
}
}
}
}
return _ref = {}, _defineProperty(_ref, "".concat(type2, "Year"), year), _defineProperty(_ref, "".concat(type2, "Month"), month), _defineProperty(_ref, "".concat(type2, "Date"), date), _defineProperty(_ref, "".concat(type2, "Hour"), hour), _defineProperty(_ref, "".concat(type2, "Minute"), minute), _defineProperty(_ref, "".concat(type2, "Seconds"), seconds), _ref;
};
var ranges = function ranges3() {
var curDate = new Date(currentDate);
if (!curDate)
return [];
var _getBoundary = getBoundary("max", curDate), maxYear = _getBoundary.maxYear, maxDate = _getBoundary.maxDate, maxMonth = _getBoundary.maxMonth, maxHour = _getBoundary.maxHour, maxMinute = _getBoundary.maxMinute, maxSeconds = _getBoundary.maxSeconds;
var _getBoundary2 = getBoundary("min", curDate), minYear = _getBoundary2.minYear, minDate = _getBoundary2.minDate, minMonth = _getBoundary2.minMonth, minHour = _getBoundary2.minHour, minMinute = _getBoundary2.minMinute, minSeconds = _getBoundary2.minSeconds;
var result = [{
type: "year",
range: [minYear, maxYear]
}, {
type: "month",
range: [minMonth, maxMonth]
}, {
type: "day",
range: [minDate, maxDate]
}, {
type: "hour",
range: [minHour, maxHour]
}, {
type: "minute",
range: [minMinute, maxMinute]
}, {
type: "seconds",
range: [minSeconds, maxSeconds]
}];
switch (type.toLocaleLowerCase()) {
case "date":
result = result.slice(0, 3);
break;
case "year":
result = result.slice(0, 1);
break;
case "month":
result = result.slice(1, 2);
break;
case "datetime":
result = result.slice(0, 5);
break;
case "datesecond":
result = result.slice(0, 6);
break;
case "time":
result = result.slice(3, 6);
break;
case "year-month":
result = result.slice(0, 2);
break;
case "hour-minutes":
result = result.slice(3, 5);
break;
case "month-day":
result = result.slice(1, 3);
break;
case "datehour":
result = result.slice(0, 4);
break;
case "month-day-year":
result = result.slice(1, 3).concat(result[0]);
break;
}
return result;
};
var ranges2 = function ranges22(typec, tabValue) {
var curDate = new Date(updateChooseValueCustmer2(typec, tabValue) || currentDate);
if (!curDate)
return [];
var _getBoundary3 = getBoundary("max", curDate), maxYear = _getBoundary3.maxYear, maxDate = _getBoundary3.maxDate, maxMonth = _getBoundary3.maxMonth, maxHour = _getBoundary3.maxHour, maxMinute = _getBoundary3.maxMinute, maxSeconds = _getBoundary3.maxSeconds;
var _getBoundary4 = getBoundary("min", curDate), minYear = _getBoundary4.minYear, minDate = _getBoundary4.minDate, minMonth = _getBoundary4.minMonth, minHour = _getBoundary4.minHour, minMinute = _getBoundary4.minMinute, minSeconds = _getBoundary4.minSeconds;
var result = [{
typec: "year",
range: [minYear, maxYear]
}, {
typec: "month",
range: [minMonth, maxMonth]
}, {
typec: "day",
range: [minDate, maxDate]
}, {
typec: "hour",
range: [minHour, maxHour]
}, {
typec: "minute",
range: [minMinute, maxMinute]
}, {
typec: "seconds",
range: [minSeconds, maxSeconds]
}];
switch (typec.toLocaleLowerCase()) {
case "date":
result = result.slice(0, 3);
break;
case "year":
result = result.slice(0, 1);
break;
case "month":
result = result.slice(1, 2);
break;
case "datetime":
result = result.slice(0, 5);
break;
case "datesecond":
result = result.slice(0, 6);
break;
case "time":
result = result.slice(3, 6);
break;
case "year-month":
result = result.slice(0, 2);
break;
case "hour-minutes":
result = result.slice(3, 5);
break;
case "month-day":
result = result.slice(1, 3);
break;
case "datehour":
result = result.slice(0, 4);
break;
case "month-day-year":
result = result.slice(1, 3).concat(result[0]);
break;
}
return result;
};
var _useState11 = useState([]), _useState12 = _slicedToArray(_useState11, 2), selectedValue = _useState12[0], setSelectedValue = _useState12[1];
var updateChooseValueCustmer = function updateChooseValueCustmer3(selectedOptions, selectedValue2, index, tabValue) {
var typec = isTabs ? tabs[tabValue] : JSON.parse(JSON.stringify(type));
var selectedValuecc = JSON.parse(JSON.stringify(selectedValue2));
isTabs && setSelectedValue(JSON.parse(JSON.stringify(selectedValue2)));
if (["date", "datetime", "datehour", "month-day", "year-month", "datesecond", "year", "month"].includes(typec.toLocaleLowerCase())) {
var _date;
var formatDate = [];
if (isTabs) {
if (selectedValuecc[tabValue].forEach) {
(selectedValuecc[tabValue] || []).forEach(function(item) {
formatDate.push(item);
});
}
} else {
selectedValuecc.forEach(function(item) {
formatDate.push(item);
});
}
if (typec.toLocaleLowerCase() === "month-day" && formatDate.length < 3) {
formatDate.unshift(new Date(defaultValue || startDate || endDate).getFullYear());
}
if (typec.toLocaleLowerCase() === "year-month" && formatDate.length < 3) {
formatDate.push(new Date(defaultValue || startDate || endDate).getDate());
}
if (typec.toLocaleLowerCase() === "year" && formatDate.length < 3) {
formatDate.push(new Date(defaultValue || startDate || endDate).getMonth() + 1);
formatDate.push(new Date(defaultValue || startDate || endDate).getDate());
}
if (typec.toLocaleLowerCase() === "month" && formatDate.length < 3) {
formatDate.unshift(new Date(defaultValue || startDate || endDate).getMonth() + 1);
formatDate.unshift(new Date(defaultValue || startDate || endDate).getFullYear());
}
var year = Number(formatDate[0]);
var month = Number(formatDate[1]) - 1;
var day = Math.min(Number(formatDate[2]), getMonthEndDay(Number(formatDate[0]), Number(formatDate[1])));
var date = null;
if (typec.toLocaleLowerCase() === "date" || typec.toLocaleLowerCase() === "month-day" || typec.toLocaleLowerCase() === "year-month" || typec.toLocaleLowerCase() === "month-day-year" || typec.toLocaleLowerCase() === "year" || typec.toLocaleLowerCase() === "month") {
date = new Date(year, month, day);
} else if (typec.toLocaleLowerCase() === "datetime") {
date = new Date(year, month, day, Number(formatDate[3]), Number(formatDate[4]));
} else if (typec.toLocaleLowerCase() === "datesecond") {
date = new Date(year, month, day, Number(formatDate[3]), Number(formatDate[4]), Number(formatDate[5]));
} else if (typec.toLocaleLowerCase() === "datehour") {
date = new Date(year, month, day, Number(formatDate[3]));
}
var isEqual = (currentDate === null || currentDate === void 0 ? void 0 : currentDate.getTime()) === ((_date = date) === null || _date === void 0 ? void 0 : _date.getTime());
date && isDate(date) && !isEqual && setCurrentDate(formatValue(date));
}
props.onChange && props.onChange(selectedOptions, selectedValue2, index);
};
var updateChooseValueCustmer2 = function updateChooseValueCustmer22(typec, tabValue) {
var formatDate = [];
var date = null;
if (selectedValue.length === 0) {
return date;
}
if (selectedValue[tabValue].forEach) {
selectedValue[tabValue].forEach(function(item) {
formatDate.push(item);
});
}
if (["date", "datetime", "datehour", "month-day", "year-month", "datesecond", "year", "month"].includes(typec.toLocaleLowerCase())) {
if (typec.toLocaleLowerCase() === "month-day" && formatDate.length < 3) {
formatDate.unshift(new Date(defaultValue || startDate || endDate).getFullYear());
}
if (typec.toLocaleLowerCase() === "year-month" && formatDate.length < 3) {
formatDate.push(new Date(defaultValue || startDate || endDate).getDate());
}
if (typec.toLocaleLowerCase() === "year" && formatDate.length < 3) {
formatDate.push(new Date(defaultValue || startDate || endDate).getMonth() + 1);
formatDate.push(new Date(defaultValue || startDate || endDate).getDate());
}
if (typec.toLocaleLowerCase() === "month" && formatDate.length < 3) {
formatDate.unshift(new Date(defaultValue || startDate || endDate).getMonth() + 1);
formatDate.unshift(new Date(defaultValue || startDate || endDate).getFullYear());
}
var year = Number(formatDate[0]);
var month = Number(formatDate[1]) - 1;
var day = Math.min(Number(formatDate[2]), getMonthEndDay(Number(formatDate[0]), Number(formatDate[1])));
if (typec.toLocaleLowerCase() === "date" || typec.toLocaleLowerCase() === "month-day" || typec.toLocaleLowerCase() === "year-month" || typec.toLocaleLowerCase() === "month-day-year" || typec.toLocaleLowerCase() === "year" || typec.toLocaleLowerCase() === "month") {
date = new Date(year, month, day);
} else if (typec.toLocaleLowerCase() === "datetime") {
date = new Date(year, month, day, Number(formatDate[3]), Number(formatDate[4]));
} else if (typec.toLocaleLowerCase() === "datesecond") {
date = new Date(year, month, day, Number(formatDate[3]), Number(formatDate[4]), Number(formatDate[5]));
} else if (typec.toLocaleLowerCase() === "datehour") {
date = new Date(year, month, day, Number(formatDate[3]));
}
return date;
} else {
if (typec.toLocaleLowerCase() === "hour-minutes") {
date = new Date(2023, 8, 31, Number(formatDate[0]), Number(formatDate[1]), 0);
} else {
date = new Date(2023, 8, 31, Number(formatDate[0]), Number(formatDate[1]), Number(formatDate[2]));
}
}
return date;
};
var padZero = function padZero2(num) {
var targetLength = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 2;
var str = "".concat(num);
while (str.length < targetLength) {
str = "0".concat(str);
}
return str;
};
var formatterOption = function formatterOption2(type2, value) {
var fOption = null;
if (formatter) {
fOption = formatter(type2, {
text: type2 === "month" || type2 === "day" ? value : padZero(value, 2),
value: type2 === "month" || type2 === "day" ? value : padZero(value, 2)
});
} else {
var padMin = type2 === "month" || type2 === "day" ? value : padZero(value, 2);
var fatter = showChinese ? zhCNType[type2] : "";
fOption = {
text: padMin + fatter,
value: padMin
};
}
return fOption;
};
var generateValue = function generateValue3(min, max, val, type2, columnIndex) {
var _arr$index;
var cmin = min;
var arr = [];
var index = 0;
while (cmin <= max) {
arr.push(formatterOption(type2, cmin));
if (type2 === "minute") {
cmin += minuteStep;
} else {
cmin++;
}
if (cmin <= val) {
index++;
}
}
defaultValueOfPicker[columnIndex] = (_arr$index = arr[index]) === null || _arr$index === void 0 ? void 0 : _arr$index.value;
setDefaultValueOfPicker(_toConsumableArray(defaultValueOfPicker));
if (props.filter && props.filter(type2, arr)) {
return props.filter(type2, arr);
}
return arr;
};
var generateValue2 = function generateValue22(min, max, val, type2, columnIndex, tabValue) {
var _arr$index2;
var cmin = min;
var arr = [];
var index = 0;
while (cmin <= max) {
arr.push(formatterOption(type2, cmin));
if (type2 === "minute") {
cmin += minuteStep;
} else {
cmin++;
}
if (cmin <= val) {
index++;
}
}
defaultValueOfPicker2[tabValue] || defaultValueOfPicker2.push([]);
defaultValueOfPicker2[tabValue][columnIndex] = (_arr$index2 = arr[index]) === null || _arr$index2 === void 0 ? void 0 : _arr$index2.value;
setDefaultValueOfPicker2(_toConsumableArray(defaultValueOfPicker2));
return arr;
};
var getDateIndex = function getDateIndex3(type2) {
if (!currentDate)
return 0;
var d = 0;
if (type2 === "year") {
d = currentDate.getFullYear();
} else if (type2 === "month") {
d = currentDate.getMonth() + 1;
} else if (type2 === "day") {
d = currentDate.getDate();
} else if (type2 === "hour") {
d = currentDate.getHours();
} else if (type2 === "minute") {
d = currentDate.getMinutes();
} else if (type2 === "seconds") {
d = currentDate.getSeconds();
}
return d;
};
var getDateIndex2 = function getDateIndex22(typec, tabValue) {
var curDate = new Date(updateChooseValueCustmer2(tabs[tabValue], tabValue) || currentDate);
if (!curDate)
return 0;
var d = 0;
if (typec === "year") {
d = curDate.getFullYear();
} else if (typec === "month") {
d = curDate.getMonth() + 1;
} else if (typec === "day") {
d = curDate.getDate();
} else if (typec === "hour") {
d = curDate.getHours();
} else if (typec === "minute") {
d = curDate.getMinutes();
} else if (typec === "seconds") {
d = curDate.getSeconds();
}
return d;
};
var columns = function columns3() {
var val = ranges().map(function(res, columnIndex) {
return generateValue(res.range[0], res.range[1], getDateIndex(res.type), res.type, columnIndex);
});
return val || [];
};
var columns2 = function columns22() {
var val = tabs.map(function(item, tabValue) {
return ranges2(item, tabValue).map(function(res, columnIndex) {
return generateValue2(res.range[0], res.range[1], getDateIndex2(res.typec, tabValue), res.typec, columnIndex, tabValue);
});
});
return val || [];
};
useEffect(function() {
if (currentDate) {
if (new Date(currentDate).getTime() == new Date(currentDateC).getTime())
return;
setcurrentDateC(new Date(currentDate));
if (isTabs) {
setOptions2(columns2());
}
setOptions(columns());
}
}, [currentDate]);
return React__default.createElement("div", _objectSpread({
className: "nut-datepicker ".concat(className),
style
}, rest), options.length > 0 && React__default.createElement(Picker, {
borderRadius,
title,
titles,
visible,
options,
options2,
tabsTitle,
cellHeight,
cancelText,
confirmText,
withSeparator,
tabsWithSeparator,
isTabs,
onClose,
defaultValue: defaultValueOfPicker,
defaultValue2: defaultValueOfPicker2,
onConfirm: function onConfirm(options3, value) {
_onConfirm && _onConfirm(options3, value);
},
onChange: function onChange2(options3, value, index, tabValue) {
return updateChooseValueCustmer(options3, value, index, tabValue);
},
threeDimensional
}));
};
DatePicker.defaultProps = defaultProps;
DatePicker.displayName = "NutDatePicker";
export {
DatePicker as default
};