forked from hotwired/stimulus
/
tabs_controller.js
44 lines (37 loc) · 974 Bytes
/
tabs_controller.js
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
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = [ "tab", "tabpanel" ]
static classes = [ "current" ]
static values = { index: { default: 0, type: Number } }
next() {
if (this.indexValue < this.lastIndex) {
this.indexValue++
return
}
this.indexValue = 0
}
previous() {
if (this.indexValue > 0) {
this.indexValue--
return
}
this.indexValue = this.lastIndex
}
open(evt) {
this.indexValue = this.tabTargets.indexOf(evt.currentTarget)
}
get lastIndex() {
return this.tabTargets.length - 1
}
indexValueChanged(current, old) {
let panels = this.tabpanelTargets
let tabs = this.tabTargets
if (old != null) {
panels[old].classList.remove(...this.currentClasses)
tabs[old].tabIndex = -1
}
panels[current].classList.add(...this.currentClasses)
tabs[current].tabIndex = 0
tabs[current].focus()
}
}