@@ -41,13 +41,16 @@ export default {
4141 },
4242
4343 onKeyNav (dir ) {
44- // somewhat hacky key navigation, but it works!
45- const currentEl = this .$el .querySelector (' .instance.selected' )
46- let current = currentEl && currentEl .__vue__
44+ const all = getAllInstances (this .$refs .instances )
45+ if (! all .length ) {
46+ return
47+ }
48+
49+ const { current , currentIndex } = findCurrent (all, i => i .selected )
4750 if (! current) {
48- current = this .$children [0 ]
49- current .select ()
51+ return
5052 }
53+
5154 if (dir === ' left' ) {
5255 if (current .expanded ) {
5356 current .collapse ()
@@ -56,29 +59,14 @@ export default {
5659 }
5760 } else if (dir === ' right' ) {
5861 if (current .expanded && current .$children .length ) {
59- current = this .findByOffset (current, 1 )
60- current .select ()
62+ findByIndex (all, currentIndex + 1 ).select ()
6163 } else {
6264 current .expand ()
6365 }
6466 } else if (dir === ' up' ) {
65- current = this .findByOffset (current, - 1 )
66- current .select ()
67- } else {
68- current = this .findByOffset (current, 1 )
69- current .select ()
70- }
71- },
72-
73- findByOffset (current , offset ) {
74- const all = getAllInstances (this .$refs .instances )
75- const index = all .indexOf (current) + offset
76- if (index < 0 ) {
77- return all[0 ]
78- } else if (index >= all .length ) {
79- return all[all .length - 1 ]
67+ findByIndex (all, currentIndex - 1 ).select ()
8068 } else {
81- return all[index]
69+ findByIndex ( all, currentIndex + 1 ). select ()
8270 }
8371 }
8472 }
@@ -89,6 +77,31 @@ function getAllInstances (list) {
8977 return [instance, ... getAllInstances (instance .$children )]
9078 }))
9179}
80+
81+ function findCurrent (all , check ) {
82+ for (let i = 0 ; i < all .length ; i++ ) {
83+ if (check (all[i])) {
84+ return {
85+ current: all[i],
86+ currentIndex: i
87+ }
88+ }
89+ }
90+ return {
91+ current: null ,
92+ currentIndex: - 1
93+ }
94+ }
95+
96+ function findByIndex (all , index ) {
97+ if (index < 0 ) {
98+ return all[0 ]
99+ } else if (index >= all .length ) {
100+ return all[all .length - 1 ]
101+ } else {
102+ return all[index]
103+ }
104+ }
92105 </script >
93106
94107<style lang="stylus">
0 commit comments