datePicker.vue 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <!--
  2. * @Description: 日期选择器
  3. * @Author: kcz
  4. * @Date: 2020-01-11 15:38:28
  5. * @LastEditors: kcz
  6. * @LastEditTime: 2020-03-28 17:37:49
  7. -->
  8. <template>
  9. <!-- 月份选择 -->
  10. <a-month-picker
  11. :style="`width:${record.options.width}`"
  12. v-if="
  13. record.type === 'date' &&
  14. record.options.format === 'YYYY-MM' &&
  15. record.options.range === false
  16. "
  17. :disabled="record.options.disabled || parentDisabled"
  18. :allowClear="record.options.clearable"
  19. :placeholder="record.options.placeholder"
  20. :format="record.options.format"
  21. @change="handleSelectChange"
  22. :value="date"
  23. />
  24. <!-- 日期选择 -->
  25. <a-date-picker
  26. :style="`width:${record.options.width}`"
  27. v-else-if="record.type === 'date' && record.options.range === false"
  28. :disabled="record.options.disabled || parentDisabled"
  29. :show-time="record.options.showTime"
  30. :allowClear="record.options.clearable"
  31. :placeholder="record.options.placeholder"
  32. :format="record.options.format"
  33. @change="handleSelectChange"
  34. :value="date"
  35. />
  36. <!-- 范围日期选择 -->
  37. <a-range-picker
  38. :style="`width:${record.options.width}`"
  39. v-else-if="record.type === 'date' && record.options.range === true"
  40. :show-time="record.options.showTime"
  41. :disabled="record.options.disabled || parentDisabled"
  42. :allowClear="record.options.clearable"
  43. :placeholder="record.options.rangePlaceholder"
  44. :format="record.options.format"
  45. @change="handleSelectChange"
  46. :value="date"
  47. />
  48. </template>
  49. <script>
  50. import moment from "moment";
  51. export default {
  52. // eslint-disable-next-line vue/require-prop-types
  53. props: ["record", "value", "parentDisabled"],
  54. data() {
  55. return {
  56. // date: undefined
  57. };
  58. },
  59. computed: {
  60. date() {
  61. if (
  62. !this.value ||
  63. (this.record.options.range && this.value.length === 0)
  64. ) {
  65. return undefined;
  66. } else if (this.record.options.range) {
  67. return this.value.map(item => moment(item, this.record.options.format));
  68. } else {
  69. return moment(this.value, this.record.options.format);
  70. }
  71. }
  72. },
  73. methods: {
  74. handleSelectChange(val) {
  75. let date;
  76. if (!val || (this.record.options.range && val.length === 0)) {
  77. date = "";
  78. } else if (this.record.options.range) {
  79. date = val.map(item => item.format(this.record.options.format));
  80. } else {
  81. date = val.format(this.record.options.format);
  82. }
  83. this.$emit("change", date);
  84. this.$emit("input", date);
  85. }
  86. }
  87. };
  88. </script>