-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
VListItem.ts
187 lines (161 loc) Β· 4.29 KB
/
VListItem.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Styles
import './VListItem.sass'
// Mixins
import Colorable from '../../mixins/colorable'
import Routable from '../../mixins/routable'
import { factory as GroupableFactory } from '../../mixins/groupable'
import Themeable from '../../mixins/themeable'
import { factory as ToggleableFactory } from '../../mixins/toggleable'
// Directives
import Ripple from '../../directives/ripple'
// Utilities
import { keyCodes } from './../../util/helpers'
import { ExtractVue } from './../../util/mixins'
import { removed } from '../../util/console'
// Types
import mixins from '../../util/mixins'
import { VNode } from 'vue'
import { PropType, PropValidator } from 'vue/types/options'
const baseMixins = mixins(
Colorable,
Routable,
Themeable,
GroupableFactory('listItemGroup'),
ToggleableFactory('inputValue')
)
interface options extends ExtractVue<typeof baseMixins> {
$el: HTMLElement
isInGroup: boolean
isInList: boolean
isInMenu: boolean
isInNav: boolean
}
/* @vue/component */
export default baseMixins.extend<options>().extend({
name: 'v-list-item',
directives: {
Ripple,
},
inheritAttrs: false,
inject: {
isInGroup: {
default: false,
},
isInList: {
default: false,
},
isInMenu: {
default: false,
},
isInNav: {
default: false,
},
},
props: {
activeClass: {
type: String,
default (): string | undefined {
if (!this.listItemGroup) return ''
return this.listItemGroup.activeClass
},
} as any as PropValidator<string>,
dense: Boolean,
inactive: Boolean,
link: Boolean,
selectable: {
type: Boolean,
},
tag: {
type: String,
default: 'div',
},
threeLine: Boolean,
twoLine: Boolean,
value: null as any as PropType<any>,
},
data: () => ({
proxyClass: 'v-list-item--active',
}),
computed: {
classes (): object {
return {
'v-list-item': true,
...Routable.options.computed.classes.call(this),
'v-list-item--dense': this.dense,
'v-list-item--disabled': this.disabled,
'v-list-item--link': this.isClickable && !this.inactive,
'v-list-item--selectable': this.selectable,
'v-list-item--three-line': this.threeLine,
'v-list-item--two-line': this.twoLine,
...this.themeClasses,
}
},
isClickable (): boolean {
return Boolean(
Routable.options.computed.isClickable.call(this) ||
this.listItemGroup
)
},
},
created () {
/* istanbul ignore next */
if (this.$attrs.hasOwnProperty('avatar')) {
removed('avatar', this)
}
},
methods: {
click (e: MouseEvent | KeyboardEvent) {
if (e.detail) this.$el.blur()
this.$emit('click', e)
this.to || this.toggle()
},
genAttrs () {
const attrs: Record<string, any> = {
'aria-disabled': this.disabled ? true : undefined,
tabindex: this.isClickable && !this.disabled ? 0 : -1,
...this.$attrs,
}
if (this.$attrs.hasOwnProperty('role')) {
// do nothing, role already provided
} else if (this.isInNav) {
// do nothing, role is inherit
} else if (this.isInGroup) {
attrs.role = 'listitem'
attrs['aria-selected'] = String(this.isActive)
} else if (this.isInMenu) {
attrs.role = this.isClickable ? 'menuitem' : undefined
attrs.id = attrs.id || `list-item-${this._uid}`
} else if (this.isInList) {
attrs.role = 'listitem'
}
return attrs
},
},
render (h): VNode {
let { tag, data } = this.generateRouteLink()
data.attrs = {
...data.attrs,
...this.genAttrs(),
}
data[this.to ? 'nativeOn' : 'on'] = {
...data[this.to ? 'nativeOn' : 'on'],
keydown: (e: KeyboardEvent) => {
/* istanbul ignore else */
if (e.keyCode === keyCodes.enter) this.click(e)
this.$emit('keydown', e)
},
}
if (this.inactive) tag = 'div'
if (this.inactive && this.to) {
data.on = data.nativeOn
delete data.nativeOn
}
const children = this.$scopedSlots.default
? this.$scopedSlots.default({
active: this.isActive,
toggle: this.toggle,
})
: this.$slots.default
return h(tag, this.setTextColor(this.color, data), children)
},
})