UNPKG

2.49 kBPlain TextView Raw
1<template>
2 <div class="vdatetime-month-picker">
3 <div class="vdatetime-month-picker__list vdatetime-month-picker__list" ref="monthList">
4 <div class="vdatetime-month-picker__item" v-for="month in months" @click="select(month)" :class="{'vdatetime-month-picker__item--selected': month.selected, 'vdatetime-month-picker__item--disabled': month.disabled}">{{ month.label }}
5 </div>
6 </div>
7 </div>
8</template>
9
10<script>
11import { DateTime } from 'luxon'
12import { monthIsDisabled, months } from './util'
13
14export default {
15 props: {
16 year: {
17 type: Number,
18 required: true
19 },
20 month: {
21 type: Number,
22 required: true
23 },
24 minDate: {
25 type: DateTime,
26 default: null
27 },
28 maxDate: {
29 type: DateTime,
30 default: null
31 }
32 },
33
34 computed: {
35 months () {
36 return months(this.month).map((month, index) => ({
37 number: ++index,
38 label: month,
39 selected: index === this.month,
40 disabled: !index || monthIsDisabled(this.minDate, this.maxDate, this.year, index)
41 }))
42 }
43 },
44
45 methods: {
46 select (month) {
47 if (month.disabled) {
48 return
49 }
50
51 this.$emit('change', parseInt(month.number))
52 },
53
54 scrollToCurrent () {
55 const selectedMonth = this.$refs.monthList.querySelector('.vdatetime-month-picker__item--selected')
56 this.$refs.monthList.scrollTop = selectedMonth ? selectedMonth.offsetTop - 250 : 0
57 }
58 },
59
60 mounted () {
61 this.scrollToCurrent()
62 },
63
64 updated () {
65 this.scrollToCurrent()
66 }
67}
68</script>
69
70<style>
71.vdatetime-month-picker {
72 box-sizing: border-box;
73
74 &::after {
75 content: '';
76 display: table;
77 clear: both;
78 }
79
80 & * {
81 box-sizing: border-box;
82 }
83}
84
85.vdatetime-month-picker__list {
86 float: left;
87 width: 100%;
88 height: 305px;
89 overflow-y: scroll;
90 -webkit-overflow-scrolling: touch;
91
92 &::-webkit-scrollbar {
93 width: 3px;
94 }
95
96 &::-webkit-scrollbar-track {
97 background: #efefef;
98 }
99
100 &::-webkit-scrollbar-thumb {
101 background: #ccc;
102 }
103}
104
105.vdatetime-month-picker__item {
106 padding: 10px 0;
107 font-size: 20px;
108 text-align: center;
109 cursor: pointer;
110 transition: font-size .3s;
111}
112
113.vdatetime-month-picker__item:hover {
114 font-size: 32px;
115}
116
117.vdatetime-month-picker__item--selected {
118 color: #3f51b5;
119 font-size: 32px;
120}
121
122.vdatetime-month-picker__item--disabled {
123 opacity: 0.4;
124 cursor: default;
125
126 &:hover {
127 color: inherit;
128 background: transparent;
129 }
130}
131</style>