TimePicker.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. exports.__esModule = true;
  4. exports.default = void 0;
  5. var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  6. var _utils = require("../utils");
  7. var _string = require("../utils/format/string");
  8. var _number = require("../utils/format/number");
  9. var _shared = require("./shared");
  10. var _createNamespace = (0, _utils.createNamespace)('time-picker'),
  11. createComponent = _createNamespace[0];
  12. var _default = createComponent({
  13. mixins: [_shared.TimePickerMixin],
  14. props: (0, _extends2.default)({}, _shared.sharedProps, {
  15. minHour: {
  16. type: [Number, String],
  17. default: 0
  18. },
  19. maxHour: {
  20. type: [Number, String],
  21. default: 23
  22. },
  23. minMinute: {
  24. type: [Number, String],
  25. default: 0
  26. },
  27. maxMinute: {
  28. type: [Number, String],
  29. default: 59
  30. }
  31. }),
  32. computed: {
  33. ranges: function ranges() {
  34. return [{
  35. type: 'hour',
  36. range: [+this.minHour, +this.maxHour]
  37. }, {
  38. type: 'minute',
  39. range: [+this.minMinute, +this.maxMinute]
  40. }];
  41. }
  42. },
  43. watch: {
  44. filter: 'updateInnerValue',
  45. minHour: function minHour() {
  46. var _this = this;
  47. this.$nextTick(function () {
  48. _this.updateInnerValue();
  49. });
  50. },
  51. maxHour: function maxHour(value) {
  52. var _this$innerValue$spli = this.innerValue.split(':'),
  53. hour = _this$innerValue$spli[0],
  54. minute = _this$innerValue$spli[1];
  55. if (hour >= value) {
  56. this.innerValue = this.formatValue(value + ":" + minute);
  57. this.updateColumnValue();
  58. } else {
  59. this.updateInnerValue();
  60. }
  61. },
  62. minMinute: 'updateInnerValue',
  63. maxMinute: function maxMinute(value) {
  64. var _this$innerValue$spli2 = this.innerValue.split(':'),
  65. hour = _this$innerValue$spli2[0],
  66. minute = _this$innerValue$spli2[1];
  67. if (minute >= value) {
  68. this.innerValue = this.formatValue(hour + ":" + value);
  69. this.updateColumnValue();
  70. } else {
  71. this.updateInnerValue();
  72. }
  73. },
  74. value: function value(val) {
  75. val = this.formatValue(val);
  76. if (val !== this.innerValue) {
  77. this.innerValue = val;
  78. this.updateColumnValue();
  79. }
  80. }
  81. },
  82. methods: {
  83. formatValue: function formatValue(value) {
  84. if (!value) {
  85. value = (0, _string.padZero)(this.minHour) + ":" + (0, _string.padZero)(this.minMinute);
  86. }
  87. var _value$split = value.split(':'),
  88. hour = _value$split[0],
  89. minute = _value$split[1];
  90. hour = (0, _string.padZero)((0, _number.range)(hour, this.minHour, this.maxHour));
  91. minute = (0, _string.padZero)((0, _number.range)(minute, this.minMinute, this.maxMinute));
  92. return hour + ":" + minute;
  93. },
  94. updateInnerValue: function updateInnerValue() {
  95. var _this$getPicker$getIn = this.getPicker().getIndexes(),
  96. hourIndex = _this$getPicker$getIn[0],
  97. minuteIndex = _this$getPicker$getIn[1];
  98. var _this$originColumns = this.originColumns,
  99. hourColumn = _this$originColumns[0],
  100. minuteColumn = _this$originColumns[1];
  101. var hour = hourColumn.values[hourIndex] || hourColumn.values[0];
  102. var minute = minuteColumn.values[minuteIndex] || minuteColumn.values[0];
  103. this.innerValue = this.formatValue(hour + ":" + minute);
  104. this.updateColumnValue();
  105. },
  106. onChange: function onChange(picker) {
  107. var _this2 = this;
  108. this.updateInnerValue();
  109. this.$nextTick(function () {
  110. _this2.$nextTick(function () {
  111. // https://github.com/vant-ui/vant/issues/9775
  112. _this2.updateInnerValue();
  113. _this2.$emit('change', picker);
  114. });
  115. });
  116. },
  117. updateColumnValue: function updateColumnValue() {
  118. var _this3 = this;
  119. var formatter = this.formatter;
  120. var pair = this.innerValue.split(':');
  121. var values = [formatter('hour', pair[0]), formatter('minute', pair[1])];
  122. this.$nextTick(function () {
  123. _this3.getPicker().setValues(values);
  124. });
  125. }
  126. }
  127. });
  128. exports.default = _default;