dateSelect.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <template>
  2. <view class="picker-select">
  3. <view class="dflex acenter" @click="show = true">
  4. <text
  5. class="tips-txt"
  6. :class="value ? 'value-txt' : 'place-txt'"
  7. >
  8. {{ showValue }}
  9. </text>
  10. <view
  11. style="margin-left: 3px"
  12. v-if="value || value === 0"
  13. @click.stop="clear"
  14. >
  15. <u-icon
  16. color="#c0c4cc"
  17. size="18"
  18. class="arrow-r"
  19. name="close-circle-fill"
  20. ></u-icon>
  21. </view>
  22. <u-icon
  23. v-else
  24. color="#c0c4cc"
  25. size="14"
  26. class="arrow-r"
  27. name="arrow-right"
  28. ></u-icon>
  29. </view>
  30. <u-datetime-picker
  31. closeOnClickOverlay
  32. :show="show"
  33. :value="value || +new Date()"
  34. @close="show = false"
  35. @cancel="show = false"
  36. @confirm="handleConfirm"
  37. :mode="mode"
  38. :formatter="formatter"
  39. ></u-datetime-picker>
  40. </view>
  41. </template>
  42. <script>
  43. export default {
  44. name: 'timeSelect',
  45. model: {
  46. event: 'change',
  47. prop: 'value',
  48. },
  49. props: {
  50. mode: String,
  51. placeholder: {
  52. default: '请选择',
  53. type: String,
  54. },
  55. value: {
  56. type: [Number, String, Date],
  57. },
  58. columns: {
  59. default: () => [],
  60. type: Array,
  61. },
  62. },
  63. data() {
  64. return {
  65. show: false,
  66. };
  67. },
  68. computed: {
  69. showValue() {
  70. const { value, placeholder } = this;
  71. return value || value === 0 ? value : placeholder;
  72. },
  73. },
  74. methods: {
  75. clear() {
  76. this.$emit('change');
  77. },
  78. formatter(type, value) {
  79. const allTypes = {
  80. year: '年',
  81. month: '月',
  82. day: '日',
  83. hour: '时',
  84. minute: '分',
  85. };
  86. return `${value}${allTypes[type]}`;
  87. },
  88. handleConfirm(e) {
  89. console.log(e);
  90. const value = e.value;
  91. if (this.mode === 'time') {
  92. this.$emit('change', value);
  93. this.show = false;
  94. return;
  95. }
  96. const date = uni.$u.timeFormat(value, 'yyyy-mm-dd');
  97. this.$emit('change', date);
  98. this.show = false;
  99. },
  100. },
  101. };
  102. </script>
  103. <style scoped lang="scss">
  104. .picker-select {
  105. .arrow-r {
  106. margin-top: 5rpx;
  107. }
  108. .place-txt {
  109. font-size: 14px;
  110. color: #c0c4cc;
  111. }
  112. .value-txt {
  113. font-size: 14px;
  114. color: #333;
  115. }
  116. }
  117. </style>