TimePicker.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name2 in all)
  7. __defProp(target, name2, { get: all[name2], enumerable: true });
  8. };
  9. var __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. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var stdin_exports = {};
  19. __export(stdin_exports, {
  20. default: () => stdin_default,
  21. timePickerProps: () => timePickerProps
  22. });
  23. module.exports = __toCommonJS(stdin_exports);
  24. var import_vue = require("vue");
  25. var import_utils = require("../date-picker/utils");
  26. var import_utils2 = require("../utils");
  27. var import_use_expose = require("../composables/use-expose");
  28. var import_picker = require("../picker");
  29. const [name] = (0, import_utils2.createNamespace)("time-picker");
  30. const validateTime = (val) => /^([01]\d|2[0-3]):([0-5]\d):([0-5]\d)$/.test(val);
  31. const fullColumns = ["hour", "minute", "second"];
  32. const 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. });
  52. var 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. });