shared.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import { times } from './utils';
  3. import { padZero } from '../utils/format/string';
  4. import { pickerProps } from '../picker/shared';
  5. import Picker from '../picker';
  6. export var sharedProps = _extends({}, pickerProps, {
  7. value: null,
  8. filter: Function,
  9. columnsOrder: Array,
  10. showToolbar: {
  11. type: Boolean,
  12. default: true
  13. },
  14. formatter: {
  15. type: Function,
  16. default: function _default(type, value) {
  17. return value;
  18. }
  19. }
  20. });
  21. export var TimePickerMixin = {
  22. data: function data() {
  23. return {
  24. innerValue: this.formatValue(this.value)
  25. };
  26. },
  27. computed: {
  28. originColumns: function originColumns() {
  29. var _this = this;
  30. return this.ranges.map(function (_ref) {
  31. var type = _ref.type,
  32. rangeArr = _ref.range;
  33. var values = times(rangeArr[1] - rangeArr[0] + 1, function (index) {
  34. var value = padZero(rangeArr[0] + index);
  35. return value;
  36. });
  37. if (_this.filter) {
  38. values = _this.filter(type, values);
  39. }
  40. return {
  41. type: type,
  42. values: values
  43. };
  44. });
  45. },
  46. columns: function columns() {
  47. var _this2 = this;
  48. return this.originColumns.map(function (column) {
  49. return {
  50. values: column.values.map(function (value) {
  51. return _this2.formatter(column.type, value);
  52. })
  53. };
  54. });
  55. }
  56. },
  57. watch: {
  58. columns: 'updateColumnValue',
  59. innerValue: function innerValue(val, oldVal) {
  60. if (!oldVal) {
  61. this.$emit('input', null);
  62. } else {
  63. this.$emit('input', val);
  64. }
  65. }
  66. },
  67. mounted: function mounted() {
  68. var _this3 = this;
  69. this.updateColumnValue();
  70. this.$nextTick(function () {
  71. _this3.updateInnerValue();
  72. });
  73. },
  74. methods: {
  75. getPicker: function getPicker() {
  76. return this.$refs.picker;
  77. },
  78. // https://github.com/vant-ui/vant/issues/10013
  79. getProxiedPicker: function getProxiedPicker() {
  80. var _this4 = this;
  81. var picker = this.$refs.picker;
  82. if (picker) {
  83. var proxy = function proxy(fn) {
  84. return function () {
  85. picker[fn].apply(picker, arguments);
  86. _this4.updateInnerValue();
  87. };
  88. };
  89. return _extends({}, picker, {
  90. setValues: proxy('setValues'),
  91. setIndexes: proxy('setIndexes'),
  92. setColumnIndex: proxy('setColumnIndex'),
  93. setColumnValue: proxy('setColumnValue')
  94. });
  95. }
  96. },
  97. onConfirm: function onConfirm() {
  98. this.$emit('input', this.innerValue);
  99. this.$emit('confirm', this.innerValue);
  100. },
  101. onCancel: function onCancel() {
  102. this.$emit('cancel');
  103. }
  104. },
  105. render: function render() {
  106. var _this5 = this;
  107. var h = arguments[0];
  108. var props = {};
  109. Object.keys(pickerProps).forEach(function (key) {
  110. props[key] = _this5[key];
  111. });
  112. return h(Picker, {
  113. "ref": "picker",
  114. "attrs": {
  115. "columns": this.columns,
  116. "readonly": this.readonly
  117. },
  118. "scopedSlots": this.$scopedSlots,
  119. "on": {
  120. "change": this.onChange,
  121. "confirm": this.onConfirm,
  122. "cancel": this.onCancel
  123. },
  124. "props": _extends({}, props)
  125. });
  126. }
  127. };