-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
VDatePickerHeader.ts
145 lines (130 loc) Β· 3.61 KB
/
VDatePickerHeader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import './VDatePickerHeader.sass'
// Components
import VBtn from '../VBtn'
import VIcon from '../VIcon'
// Mixins
import Colorable from '../../mixins/colorable'
import Localable from '../../mixins/localable'
import Themeable from '../../mixins/themeable'
// Utils
import { createNativeLocaleFormatter, monthChange } from './util'
import mixins from '../../util/mixins'
// Types
import { VNode, PropType } from 'vue'
import { DatePickerFormatter } from 'types'
export default mixins(
Colorable,
Localable,
Themeable
/* @vue/component */
).extend({
name: 'v-date-picker-header',
props: {
disabled: Boolean,
format: Function as PropType<DatePickerFormatter | undefined>,
min: String,
max: String,
nextIcon: {
type: String,
default: '$next',
},
prevIcon: {
type: String,
default: '$prev',
},
readonly: Boolean,
value: {
type: [Number, String],
required: true,
},
},
data () {
return {
isReversing: false,
}
},
computed: {
formatter (): DatePickerFormatter {
if (this.format) {
return this.format
} else if (String(this.value).split('-')[1]) {
return createNativeLocaleFormatter(this.currentLocale, { month: 'long', year: 'numeric', timeZone: 'UTC' }, { length: 7 })
} else {
return createNativeLocaleFormatter(this.currentLocale, { year: 'numeric', timeZone: 'UTC' }, { length: 4 })
}
},
},
watch: {
value (newVal, oldVal) {
this.isReversing = newVal < oldVal
},
},
methods: {
genBtn (change: number) {
const disabled = this.disabled ||
(change < 0 && this.min && this.calculateChange(change) < this.min) ||
(change > 0 && this.max && this.calculateChange(change) > this.max)
return this.$createElement(VBtn, {
props: {
dark: this.dark,
disabled,
icon: true,
light: this.light,
},
on: {
click: (e: Event) => {
e.stopPropagation()
this.$emit('input', this.calculateChange(change))
},
},
}, [
this.$createElement(VIcon, ((change < 0) === !this.$vuetify.rtl) ? this.prevIcon : this.nextIcon),
])
},
calculateChange (sign: number) {
const [year, month] = String(this.value).split('-').map(Number)
if (month == null) {
return `${year + sign}`
} else {
return monthChange(String(this.value), sign)
}
},
genHeader () {
const color = !this.disabled && (this.color || 'accent')
const header = this.$createElement('div', this.setTextColor(color, {
key: String(this.value),
}), [this.$createElement('button', {
attrs: {
type: 'button',
},
on: {
click: () => this.$emit('toggle'),
},
}, [this.$slots.default || this.formatter(String(this.value))])])
const transition = this.$createElement('transition', {
props: {
name: (this.isReversing === !this.$vuetify.rtl) ? 'tab-reverse-transition' : 'tab-transition',
},
}, [header])
return this.$createElement('div', {
staticClass: 'v-date-picker-header__value',
class: {
'v-date-picker-header__value--disabled': this.disabled,
},
}, [transition])
},
},
render (): VNode {
return this.$createElement('div', {
staticClass: 'v-date-picker-header',
class: {
'v-date-picker-header--disabled': this.disabled,
...this.themeClasses,
},
}, [
this.genBtn(-1),
this.genHeader(),
this.genBtn(+1),
])
},
})