VueMonthlyPickerExample.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <template>
  2. <div>
  3. <h1>月份选择器</h1>
  4. <div class="demo-component">
  5. <vue-monthly-picker
  6. v-model="selectedMonth"
  7. :input-class="{'input': isDisplayInput}"
  8. :disabled="isDisable"
  9. :month-labels="locale"
  10. :clear-option="clearOption"
  11. :min="min"
  12. :max="max"
  13. :alignment="alignment"
  14. :selected-background-color="selectedBackgroundColor"
  15. @selected="handleSelect"
  16. />
  17. </div>
  18. <div>选择的月份: {{ selectedMonth }}</div>
  19. <div>
  20. <div class="form-group">
  21. <label>只读</label>
  22. <div class="input-group">
  23. <Switches v-model="isDisable" />
  24. {{ isDisable ? 'Disabled': 'Enable' }}
  25. </div>
  26. </div>
  27. <div class="form-group">
  28. <label>限制范围</label>
  29. <div class="input-group">
  30. <Switches v-model="isLimitRange" />
  31. {{ rangeDisplay }}
  32. </div>
  33. </div>
  34. <div class="form-group">
  35. <label>显示</label>
  36. <div class="input-group">
  37. <Switches
  38. v-model="isDisplayInput"
  39. true-value="Input"
  40. false-value="Label"
  41. />
  42. {{ isDisplayInput? 'Input': 'Label' }}
  43. </div>
  44. </div>
  45. <div class="form-group">
  46. <label>清空</label>
  47. <div class="input-group">
  48. <Switches v-model="clearOption" />
  49. {{ clearOption ? 'Enable': 'Disabled' }}
  50. </div>
  51. </div>
  52. <div class="form-group">
  53. <label>国际化</label>
  54. <select
  55. v-model="locale"
  56. placeholder="Select a language"
  57. class="form-control"
  58. >
  59. <option
  60. v-for="option in options"
  61. :key="option.id"
  62. :value="option.monthLabels"
  63. >
  64. {{ option.title }}
  65. </option>
  66. </select>
  67. </div>
  68. <div class="form-group">
  69. <label>对齐</label>
  70. <select
  71. v-model="alignment"
  72. placeholder="Select an alignment"
  73. class="form-control"
  74. >
  75. <option
  76. v-for="alignment1 in alignments"
  77. :key="alignment1"
  78. :value="alignment1"
  79. >
  80. {{ alignment1 }}
  81. </option>
  82. </select>
  83. </div>
  84. <div class="form-group">
  85. <label>背景色</label>
  86. <select
  87. v-model="selectedBackgroundColor"
  88. placeholder="Select an color"
  89. class="form-control"
  90. >
  91. <option
  92. v-for="color in colorExamples"
  93. :key="color"
  94. :value="color"
  95. >
  96. {{ color }}
  97. </option>
  98. </select>
  99. </div>
  100. </div>
  101. </div>
  102. </template>
  103. <script>
  104. import moment from 'moment';
  105. import VueMonthlyPicker from '@/vue-monthly-picker/index.js';
  106. import Switches from '@/switches/index.js';
  107. export default {
  108. name: 'App',
  109. components: {
  110. VueMonthlyPicker, Switches,
  111. },
  112. data() {
  113. return {
  114. selectedMonth: moment(),
  115. isDisable: false,
  116. isDisplayInput: true,
  117. locale: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  118. alignment: 'left',
  119. selectedBackgroundColor: 'blue',
  120. options: [
  121. {
  122. id: 1,
  123. title: 'English',
  124. monthLabels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  125. },
  126. {
  127. id: 2,
  128. title: '中文',
  129. monthLabels: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
  130. },
  131. {
  132. id: 3,
  133. title: 'Korean',
  134. monthLabels: ['1월', '2월', '3월', '4월', '5월', '6월', '7월', '8월', '9월', '10월', '11월', '12월'],
  135. },
  136. ],
  137. alignments: ['left', 'center', 'right'],
  138. colorExamples: ['blue', 'red', 'black'],
  139. min: null,
  140. max: null,
  141. isLimitRange: false,
  142. clearOption: true,
  143. };
  144. },
  145. computed: {
  146. rangeDisplay() {
  147. if (!this.min || !this.max) {
  148. return 'Disabled';
  149. }
  150. return this.min.format('YYYY/MM') + '-' + this.max.format('YYYY/MM');
  151. },
  152. },
  153. watch: {
  154. isLimitRange(newValue) {
  155. if (newValue) {
  156. this.setSelectRange(moment().subtract(6, 'months'), moment().add(6, 'months'));
  157. } else {
  158. this.setSelectRange(null, null);
  159. }
  160. },
  161. },
  162. methods: {
  163. handleSelect(value) {
  164. console.log('Select', value);
  165. },
  166. setSelectRange(min, max) {
  167. this.min = min;
  168. this.max = max;
  169. },
  170. },
  171. };
  172. </script>
  173. <style scoped>
  174. #app {
  175. font-family: "Avenir", Helvetica, Arial, sans-serif;
  176. -webkit-font-smoothing: antialiased;
  177. -moz-osx-font-smoothing: grayscale;
  178. text-align: center;
  179. color: #2c3e50;
  180. margin-top: 60px;
  181. }
  182. .demo-component {
  183. width: 250px;
  184. margin: auto;
  185. }
  186. .control-group {
  187. margin-top: 100px;
  188. }
  189. .control {
  190. text-align: center;
  191. }
  192. .option-list {
  193. margin-top: 20px;
  194. }
  195. </style>