VueMonthlyPicker.vue 10 KB

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