VueMonthlyPicker.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <template>
  2. <div class="vue-monthly-picker">
  3. <div
  4. class="month-picker-wrapper"
  5. :class="{ 'active visible':showMenu }"
  6. >
  7. <div class="month-year-label picker" type="text" tabindex="0" @click="openMenu">
  8. <div
  9. class="month-year-display"
  10. :disabled="disabled"
  11. :class="[inputClass, {'placeholder': !modelValue}]"
  12. @click="openMenu"
  13. >
  14. <div class="display-text" :class="'display-text-'+alignment" :style="[{'text-align': alignment}]">{{ displayText }}</div>
  15. <span v-if="clearOption && modelValue" class="vmp-input-append" @click.stop.prevent="clearSelect">
  16. <i class="vmp-clear-icon" />
  17. </span>
  18. </div>
  19. </div>
  20. <div class="text" />
  21. <div class="date-popover" :class="menuClass" :style="menuStyle" tabindex="-1">
  22. <div class="picker" style="text-align: center">
  23. <div class="flexbox">
  24. <span class="prev" :class="{deactive: !canBack}" @click="prevYear" />
  25. <div>{{ year }}</div>
  26. <span class="next" :class="{deactive: !canNext}" @click="nextYear" />
  27. </div>
  28. <div class="flexbox monthItem">
  29. <template v-for="(monthLabel, idx) in monthLabels">
  30. <div
  31. v-if="isActive(idx)"
  32. :key="'active-' + idx"
  33. class="item active"
  34. :class="{'selected': isCurrentSelected(year, idx)}"
  35. :style="[{'background-color': getBackgroundColor(year, idx)}]"
  36. @click="selectMonth(idx)"
  37. >
  38. {{ monthLabel }}
  39. </div>
  40. <div
  41. v-else
  42. :key="'deactive-' + idx"
  43. :class="{'selected': isCurrentSelected(year, idx)}"
  44. class="item deactive"
  45. >
  46. {{ monthLabel }}
  47. </div>
  48. </template>
  49. </div>
  50. </div>
  51. </div>
  52. </div>
  53. </div>
  54. </template>
  55. <script>
  56. import dayjs from 'dayjs';
  57. export default {
  58. name: 'VueMonthlyPicker',
  59. props: {
  60. 'modelValue': {
  61. type: [String, Object] ,
  62. default: () => {
  63. return null;
  64. },
  65. },
  66. 'disabled': {
  67. type: Boolean,
  68. default: false,
  69. },
  70. 'inputClass': {
  71. type: [String, Object],
  72. default: 'input',
  73. },
  74. 'placeHolder': {
  75. type: String,
  76. default: '',
  77. },
  78. 'alignment': {
  79. type: String,
  80. default: 'left',
  81. validator: function (value) {
  82. // The value must match one of these strings
  83. return ['left', 'right', 'center'].indexOf(value) !== -1;
  84. },
  85. },
  86. 'selectedBackgroundColor': {
  87. type: String,
  88. default: '#007bff',
  89. },
  90. monthLabels: {
  91. type: Array,
  92. default: function () {
  93. return ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'];
  94. },
  95. },
  96. min: {
  97. type: Date,
  98. default: null,
  99. },
  100. max: {
  101. type: Date,
  102. default: null,
  103. },
  104. dateFormat: {
  105. type: String,
  106. default: 'YYYY/MM',
  107. },
  108. clearOption: {
  109. type: Boolean,
  110. default: true,
  111. },
  112. },
  113. emits: ['update:modelValue', 'selected'],
  114. data () {
  115. return {
  116. showMenu: false,
  117. year: dayjs().format('YYYY'),
  118. month: dayjs().format('MM'),
  119. };
  120. },
  121. computed: {
  122. menuClass () {
  123. return {
  124. visible: this.showMenu,
  125. hidden: !this.showMenu,
  126. };
  127. },
  128. menuStyle () {
  129. return {
  130. display: this.showMenu ? 'block' : 'none',
  131. 'left': this.alignment === 'right' ? '100%' : this.alignment === 'center' ? '50%' : '',
  132. 'transform': this.alignment === 'right' ? 'translate(-100%,0)' : this.alignment === 'center' ? 'translate(-50%,0)' : '',
  133. };
  134. },
  135. displayText () {
  136. if (this.modelValue) {
  137. let valueMoment = null;
  138. if (typeof this.modelValue === 'string') {
  139. valueMoment = dayjs(this.modelValue);
  140. } else {
  141. valueMoment = this.modelValue;
  142. }
  143. if (valueMoment && valueMoment.isValid()) {
  144. return valueMoment.format(this.dateFormat);
  145. }
  146. return null;
  147. } else {
  148. return this.placeHolder;
  149. }
  150. },
  151. canBack () {
  152. if (!this.min) return true;
  153. const currentVal = this.internalMomentValue.clone().startOf('year');
  154. return this.min.isBefore(currentVal);
  155. },
  156. canNext () {
  157. if (!this.max) return true;
  158. const currentVal = this.internalMomentValue.clone().endOf('year');
  159. return currentVal.isBefore(this.max);
  160. },
  161. internalMomentValue () {
  162. const yrMonth = this.year + '-' + this.month + '-01';
  163. return dayjs(yrMonth);
  164. },
  165. },
  166. watch: {
  167. modelValue: function (value) {
  168. this.setValue(value);
  169. },
  170. },
  171. mounted () {
  172. this.init();
  173. },
  174. methods: {
  175. init () {
  176. document.addEventListener('click', e => {
  177. if (this.$el && !this.$el.contains(e.target)) {
  178. this.closeMenu();
  179. }
  180. }, false);
  181. this.setValue(this.modelValue);
  182. },
  183. openMenu () {
  184. if (!this.disabled) {
  185. this.showMenu = true;
  186. }
  187. },
  188. closeMenu () {
  189. this.showMenu = false;
  190. },
  191. prevYear () {
  192. if (!this.canBack) return;
  193. let newYear = parseInt(this.year) - 1;
  194. this.year = newYear.toString();
  195. },
  196. nextYear () {
  197. if (!this.canNext) return;
  198. let newYear = parseInt(this.year) + 1;
  199. this.year = newYear.toString();
  200. },
  201. selectMonth (idx) {
  202. this.month = (parseInt(idx) + 1).toString();
  203. this.selectPicker();
  204. this.closeMenu();
  205. },
  206. selectPicker () {
  207. const currentValue = this.internalMomentValue.clone();
  208. if (typeof this.modelValue === 'string') {
  209. this.$emit('update:modelValue', currentValue.format('YYYY-MM'));
  210. this.$emit('selected', currentValue.format('YYYY-MM'));
  211. }else{
  212. this.$emit('update:modelValue', currentValue);
  213. this.$emit('selected', currentValue);
  214. }
  215. },
  216. setValue (value) {
  217. if (typeof value === 'string') {
  218. value = dayjs(value);
  219. }
  220. if (value && value.isValid()) {
  221. this.month = value.format('MM');
  222. this.year = value.format('YYYY');
  223. }
  224. },
  225. isActive (idx) {
  226. let realMonth = idx + 1;
  227. const yrMonth = this.year + '/' + (realMonth < 10 ? '0' + realMonth : realMonth);
  228. if (this.min && dayjs(yrMonth, 'YYYY/MM').isBefore(this.min)) {
  229. return false;
  230. }
  231. if (this.max && dayjs(yrMonth, 'YYYY/MM').isAfter(this.max)) {
  232. return false;
  233. }
  234. return true;
  235. },
  236. isCurrentSelected (year, monthIdx) {
  237. if (!this.modelValue) {
  238. return false;
  239. }
  240. let checkValue = this.modelValue;
  241. if (typeof this.modelValue === 'string') {
  242. checkValue = dayjs(this.modelValue);
  243. }
  244. if (checkValue && checkValue.isValid()) {
  245. const currentMonth = checkValue.format('MM');
  246. const currentYear = checkValue.format('YYYY');
  247. return Number(currentMonth) === Number(monthIdx + 1) && Number(currentYear) === Number(year);
  248. }
  249. return false;
  250. },
  251. getBackgroundColor (year, monthIdx) {
  252. if (this.isCurrentSelected(year, monthIdx)) {
  253. return this.selectedBackgroundColor;
  254. }
  255. },
  256. clearSelect () {
  257. this.$emit('update:modelValue', null);
  258. this.$emit('selected', null);
  259. },
  260. },
  261. };
  262. </script>
  263. <style>
  264. .vue-monthly-picker .picker .next:hover,
  265. .vue-monthly-picker .picker .prev:hover {
  266. cursor: pointer;
  267. }
  268. .vue-monthly-picker .picker .monthItem .item {
  269. border-top: 1px solid #d4d4d4;
  270. }
  271. .vue-monthly-picker .picker .monthItem .item.active:hover {
  272. cursor: pointer;
  273. background-color: #d4d4d4;
  274. }
  275. .vue-monthly-picker .picker .flexbox {
  276. padding: 0px;
  277. display: flex;
  278. flex-wrap: wrap;
  279. }
  280. .vue-monthly-picker .picker .flexbox div {
  281. flex-grow: 1;
  282. padding: 15px 0;
  283. }
  284. .vue-monthly-picker .picker .flexbox .item {
  285. flex: 1;
  286. flex-basis: 25%;
  287. }
  288. .vue-monthly-picker .placeholder {
  289. color: #8b8b8b;
  290. }
  291. .vue-monthly-picker .date-popover {
  292. overflow-x: hidden;
  293. overflow-y: hidden;
  294. outline: none;
  295. max-width: 350px;
  296. width: 100%;
  297. border-radius: 0 0 .28571429rem .28571429rem;
  298. box-shadow: 0 2px 3px 0 rgba(34, 36, 38, 0.15);
  299. background: #fff;
  300. transition: opacity .1s ease;
  301. position: absolute;
  302. margin: auto;
  303. z-index: 10;
  304. border: 1px solid #d4d4d4;
  305. font-size: 1rem;
  306. font-weight: 200;
  307. }
  308. .vue-monthly-picker .month-picker-wrapper {
  309. position: relative;
  310. display: block;
  311. min-width: 200px;
  312. }
  313. .vue-monthly-picker .month-year-label {
  314. outline: none;
  315. }
  316. .vue-monthly-picker .month-year-label .vmp-input-append {
  317. display: none;
  318. }
  319. .vue-monthly-picker .month-year-label:hover .vmp-input-append {
  320. display: block;
  321. }
  322. .vue-monthly-picker .text {
  323. position: relative;
  324. z-index: 2;
  325. }
  326. .vue-monthly-picker .month-year-display:hover {
  327. cursor: pointer;
  328. }
  329. .vue-monthly-picker .next,
  330. .vue-monthly-picker .prev {
  331. width: 16%;
  332. float: left;
  333. text-indent: -10000px;
  334. position: relative;
  335. }
  336. .vue-monthly-picker .next:after,
  337. .vue-monthly-picker .prev:after {
  338. content: "";
  339. position: absolute;
  340. left: 50%;
  341. top: 50%;
  342. -webkit-transform: translateX(-50%) translateY(-50%);
  343. transform: translateX(-50%) translateY(-50%);
  344. border: 6px solid transparent;
  345. }
  346. .vue-monthly-picker .next:after {
  347. border-left: 10px solid #000;
  348. margin-left: 5px;
  349. }
  350. .vue-monthly-picker .next.deactive:hover {
  351. cursor: default;
  352. }
  353. .vue-monthly-picker .next.deactive:after {
  354. border-left: 10px solid #999999;
  355. }
  356. .vue-monthly-picker .prev:after {
  357. border-right: 10px solid #000;
  358. margin-left: -5px;
  359. }
  360. .vue-monthly-picker .prev.deactive:hover {
  361. cursor: default;
  362. }
  363. .vue-monthly-picker .prev.deactive:after {
  364. border-right: 10px solid #999999;
  365. }
  366. .vue-monthly-picker .input {
  367. -moz-appearance: none;
  368. -webkit-appearance: none;
  369. align-items: center;
  370. border: 1px solid transparent;
  371. border-radius: 3px;
  372. box-shadow: none;
  373. display: inline-flex;
  374. font-size: 1rem;
  375. height: 2.25em;
  376. justify-content: flex-start;
  377. line-height: 1.5;
  378. padding: 2px calc(.625em - 1px);
  379. position: relative;
  380. vertical-align: top;
  381. background-color: #fff;
  382. border-color: #dbdbdb;
  383. color: #363636;
  384. box-shadow: inset 0 1px 2px rgba(10, 10, 10, 0.1);
  385. max-width: 100%;
  386. width: 100%;
  387. }
  388. .vue-monthly-picker .deactive {
  389. color: #999999;
  390. }
  391. .vue-monthly-picker .selected {
  392. color: #fff;
  393. text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25);
  394. font-weight: bold;
  395. }
  396. .vue-monthly-picker .display-text {
  397. width: 100%;
  398. }
  399. .vue-monthly-picker .display-text-right {
  400. margin-right: 20px;
  401. }
  402. .vue-monthly-picker .vmp-input-append {
  403. position: absolute;
  404. top: 0;
  405. right: 0;
  406. width: 30px;
  407. height: 100%;
  408. padding: 6px;
  409. }
  410. .vue-monthly-picker .vmp-clear-icon {
  411. display: inline-block;
  412. width: 100%;
  413. height: 100%;
  414. font-style: normal;
  415. color: #555;
  416. text-align: center;
  417. cursor: pointer;
  418. }
  419. .vue-monthly-picker .vmp-clear-icon:before {
  420. display: inline-block;
  421. content: '\2716';
  422. vertical-align: middle;
  423. }
  424. </style>