UNPKG

5.75 kBJavaScriptView Raw
1var __defProp = Object.defineProperty;
2var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3var __getOwnPropNames = Object.getOwnPropertyNames;
4var __hasOwnProp = Object.prototype.hasOwnProperty;
5var __export = (target, all) => {
6 for (var name2 in all)
7 __defProp(target, name2, { get: all[name2], enumerable: true });
8};
9var __copyProps = (to, from, except, desc) => {
10 if (from && typeof from === "object" || typeof from === "function") {
11 for (let key of __getOwnPropNames(from))
12 if (!__hasOwnProp.call(to, key) && key !== except)
13 __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14 }
15 return to;
16};
17var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18var stdin_exports = {};
19__export(stdin_exports, {
20 default: () => stdin_default,
21 timePickerProps: () => timePickerProps
22});
23module.exports = __toCommonJS(stdin_exports);
24var import_vue = require("vue");
25var import_utils = require("../date-picker/utils");
26var import_utils2 = require("../utils");
27var import_use_expose = require("../composables/use-expose");
28var import_picker = require("../picker");
29const [name] = (0, import_utils2.createNamespace)("time-picker");
30const validateTime = (val) => /^([01]\d|2[0-3]):([0-5]\d):([0-5]\d)$/.test(val);
31const fullColumns = ["hour", "minute", "second"];
32const timePickerProps = (0, import_utils2.extend)({}, import_utils.sharedProps, {
33 minHour: (0, import_utils2.makeNumericProp)(0),
34 maxHour: (0, import_utils2.makeNumericProp)(23),
35 minMinute: (0, import_utils2.makeNumericProp)(0),
36 maxMinute: (0, import_utils2.makeNumericProp)(59),
37 minSecond: (0, import_utils2.makeNumericProp)(0),
38 maxSecond: (0, import_utils2.makeNumericProp)(59),
39 minTime: {
40 type: String,
41 validator: validateTime
42 },
43 maxTime: {
44 type: String,
45 validator: validateTime
46 },
47 columnsType: {
48 type: Array,
49 default: () => ["hour", "minute"]
50 }
51});
52var stdin_default = (0, import_vue.defineComponent)({
53 name,
54 props: timePickerProps,
55 emits: ["confirm", "cancel", "change", "update:modelValue"],
56 setup(props, {
57 emit,
58 slots
59 }) {
60 const currentValues = (0, import_vue.ref)(props.modelValue);
61 const pickerRef = (0, import_vue.ref)();
62 const getValidTime = (time) => {
63 const timeLimitArr = time.split(":");
64 return fullColumns.map((col, i) => props.columnsType.includes(col) ? timeLimitArr[i] : "00");
65 };
66 const confirm = () => {
67 var _a;
68 return (_a = pickerRef.value) == null ? void 0 : _a.confirm();
69 };
70 const getSelectedTime = () => currentValues.value;
71 const columns = (0, import_vue.computed)(() => {
72 let {
73 minHour,
74 maxHour,
75 minMinute,
76 maxMinute,
77 minSecond,
78 maxSecond
79 } = props;
80 if (props.minTime || props.maxTime) {
81 const fullTime = {
82 hour: 0,
83 minute: 0,
84 second: 0
85 };
86 props.columnsType.forEach((col, i) => {
87 var _a;
88 fullTime[col] = (_a = currentValues.value[i]) != null ? _a : 0;
89 });
90 const {
91 hour,
92 minute
93 } = fullTime;
94 if (props.minTime) {
95 const [minH, minM, minS] = getValidTime(props.minTime);
96 minHour = minH;
97 minMinute = +hour <= +minHour ? minM : "00";
98 minSecond = +hour <= +minHour && +minute <= +minMinute ? minS : "00";
99 }
100 if (props.maxTime) {
101 const [maxH, maxM, maxS] = getValidTime(props.maxTime);
102 maxHour = maxH;
103 maxMinute = +hour >= +maxHour ? maxM : "59";
104 maxSecond = +hour >= +maxHour && +minute >= +maxMinute ? maxS : "59";
105 }
106 }
107 return props.columnsType.map((type) => {
108 const {
109 filter,
110 formatter
111 } = props;
112 switch (type) {
113 case "hour":
114 return (0, import_utils.genOptions)(+minHour, +maxHour, type, formatter, filter, currentValues.value);
115 case "minute":
116 return (0, import_utils.genOptions)(+minMinute, +maxMinute, type, formatter, filter, currentValues.value);
117 case "second":
118 return (0, import_utils.genOptions)(+minSecond, +maxSecond, type, formatter, filter, currentValues.value);
119 default:
120 if (process.env.NODE_ENV !== "production") {
121 throw new Error(`[Vant] DatePicker: unsupported columns type: ${type}`);
122 }
123 return [];
124 }
125 });
126 });
127 (0, import_vue.watch)(currentValues, (newValues) => {
128 if (!(0, import_utils2.isSameValue)(newValues, props.modelValue)) {
129 emit("update:modelValue", newValues);
130 }
131 });
132 (0, import_vue.watch)(() => props.modelValue, (newValues) => {
133 newValues = (0, import_utils.formatValueRange)(newValues, columns.value);
134 if (!(0, import_utils2.isSameValue)(newValues, currentValues.value)) {
135 currentValues.value = newValues;
136 }
137 }, {
138 immediate: true
139 });
140 const onChange = (...args) => emit("change", ...args);
141 const onCancel = (...args) => emit("cancel", ...args);
142 const onConfirm = (...args) => emit("confirm", ...args);
143 (0, import_use_expose.useExpose)({
144 confirm,
145 getSelectedTime
146 });
147 return () => (0, import_vue.createVNode)(import_picker.Picker, (0, import_vue.mergeProps)({
148 "ref": pickerRef,
149 "modelValue": currentValues.value,
150 "onUpdate:modelValue": ($event) => currentValues.value = $event,
151 "columns": columns.value,
152 "onChange": onChange,
153 "onCancel": onCancel,
154 "onConfirm": onConfirm
155 }, (0, import_utils2.pick)(props, import_utils.pickerInheritKeys)), slots);
156 }
157});