VueMonthlyPicker.vue 11 KB

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