28
28
<tr v-for =" (row, i) in years" :key =" i" >
29
29
<td
30
30
v-for =" (cell, j) in row"
31
- :ref =" handleRef (cell)"
31
+ :ref =" handleRefName (cell, i, j )"
32
32
:key =" j"
33
33
aria-hidden =" false"
34
34
class =" cell"
35
35
role =" button"
36
36
:tabindex =" handleTabIndex(cell)"
37
37
:data-year =" cell"
38
38
:class =" getCellClasses(cell)"
39
- @blur.prevent =" onBlur(i, j)"
40
39
@keydown.tab.prevent.stop
41
- @keydown.up.prevent =" handleArrowUp(i, j)"
42
- @keydown.down.prevent =" handleArrowDown(i, j)"
43
- @keydown.left.prevent =" handleArrowLeft(i, j)"
44
- @keydown.right.prevent =" handleArrowRight(i, j)"
40
+ @keydown.up.prevent =" handleArrowUp(cell, i, j)"
41
+ @keydown.down.prevent =" handleArrowDown(cell, i, j)"
42
+ @keydown.left.prevent =" handleArrowLeft(cell, i, j)"
43
+ @keydown.right.prevent =" handleArrowRight(cell, i, j)"
45
44
>
46
45
<div >{{ cell }}</div >
47
46
</td >
54
53
<script >
55
54
import IconButton from ' ./icon-button' ;
56
55
import { chunk } from ' ../util/base' ;
57
- import { setYear } from ' ../util/date' ;
56
+ import { createDate , setYear } from ' ../util/date' ;
58
57
import { getLocale } from ' ../locale' ;
59
58
60
59
export default {
@@ -84,6 +83,10 @@ export default {
84
83
getYearPanel: {
85
84
type: Function ,
86
85
},
86
+ isDisabled: {
87
+ type: Function ,
88
+ default : () => false ,
89
+ },
87
90
},
88
91
computed: {
89
92
years () {
@@ -103,6 +106,12 @@ export default {
103
106
locale () {
104
107
return this .getLocale ();
105
108
},
109
+ refsArray () {
110
+ if (this .$refs ) {
111
+ return Object .entries (this .$refs );
112
+ }
113
+ return [];
114
+ },
106
115
},
107
116
methods: {
108
117
isDisabledArrows (type ) {
@@ -128,66 +137,64 @@ export default {
128
137
}
129
138
return chunk (years, 2 );
130
139
},
131
- handleArrowUp (row , column ) {
140
+ handleArrowUp (cell , row , column ) {
132
141
if (row === 0 ) {
133
142
return ;
134
143
}
135
- const year = this .years [ row - 1 ][ column] ;
136
- const ref = this .$refs [` year-cell- ${ year } ` ]? .[0 ];
144
+ const refName = this .handleRefName (cell, row - 1 , column) ;
145
+ const ref = this .$refs [refName ]? .[0 ];
137
146
if (ref) {
138
147
ref .focus ();
139
- ref .classList .add (' focus' );
140
148
}
141
149
},
142
- handleArrowDown (row , column ) {
150
+ handleArrowDown (cell , row , column ) {
143
151
if (row === this .years .length - 1 ) {
144
152
return ;
145
153
}
146
- const year = this .years [ row + 1 ][ column] ;
147
- const ref = this .$refs [` year-cell- ${ year } ` ]? .[0 ];
154
+ const refName = this .handleRefName (cell, row + 1 , column) ;
155
+ const ref = this .$refs [refName ]? .[0 ];
148
156
if (ref) {
149
157
ref .focus ();
150
- ref .classList .add (' focus' );
151
158
}
152
159
},
153
- handleArrowLeft (row , column ) {
154
- if (column % 2 === 0 ) {
155
- if (row === 0 && column === 0 ) {
156
- this .handleIconDoubleLeftClick ();
157
- const ref = this .$refs [` year-cell-${ this .lastYear } ` ]? .[0 ];
158
- if (ref) {
159
- ref .focus ();
160
+ handleArrowLeft (cell , row , column ) {
161
+ const currentRefName = this .handleRefName (cell, row, column);
162
+ const firstRef = this .refsArray [0 ];
163
+ if (currentRefName !== firstRef[0 ]) {
164
+ const refName = this .handleRefName (cell, row, column - 1 );
165
+ const ref = this .$refs [refName]? .[0 ];
166
+ if (ref) {
167
+ ref .focus ();
168
+ }
169
+ } else {
170
+ this .handleIconDoubleLeftClick ();
171
+ const lastRef = this .refsArray [this .refsArray .length - 1 ];
172
+ if (lastRef .length ) {
173
+ const element = lastRef[1 ];
174
+ if (element .length ) {
175
+ element[0 ].focus ();
160
176
}
161
177
}
162
- return ;
163
- }
164
- const year = this .years [row][column - 1 ];
165
- const ref = this .$refs [` year-cell-${ year} ` ]? .[0 ];
166
- if (ref) {
167
- ref .focus ();
168
- ref .classList .add (' focus' );
169
178
}
170
179
},
171
- handleArrowRight (row , column ) {
172
- if (column % 2 === 1 ) {
173
- if (row === this .years .length - 1 ) {
174
- const lastRow = this .years [row];
175
- if (column === lastRow .length - 1 ) {
176
- this .handleIconDoubleRightClick ();
177
- const year = this .years [0 ][0 ];
178
- const ref = this .$refs [` year-cell-${ year} ` ]? .[0 ];
179
- if (ref) {
180
- ref .focus ();
181
- }
180
+ handleArrowRight (cell , row , column ) {
181
+ const currentRefName = this .handleRefName (cell, row, column);
182
+ const lastRef = this .refsArray [this .refsArray .length - 1 ];
183
+ if (currentRefName !== lastRef[0 ]) {
184
+ const refName = this .handleRefName (cell, row, column + 1 );
185
+ const ref = this .$refs [refName]? .[0 ];
186
+ if (ref) {
187
+ ref .focus ();
188
+ }
189
+ } else {
190
+ this .handleIconDoubleRightClick ();
191
+ const firstRef = this .refsArray [0 ];
192
+ if (firstRef .length ) {
193
+ const element = firstRef[1 ];
194
+ if (element .length ) {
195
+ element[0 ].focus ();
182
196
}
183
197
}
184
- return ;
185
- }
186
- const year = this .years [row][column + 1 ];
187
- const ref = this .$refs [` year-cell-${ year} ` ]? .[0 ];
188
- if (ref) {
189
- ref .focus ();
190
- ref .classList .add (' focus' );
191
198
}
192
199
},
193
200
handleIconDoubleLeftClick () {
@@ -215,28 +222,16 @@ export default {
215
222
this .selectedYear = parseInt (year, 10 );
216
223
}
217
224
},
218
- handleRef (cellDate ) {
219
- return this .disabledCalendarChanger (cellDate, ' year' ) ? undefined : ` year-cell-${ cellDate} ` ;
220
- },
221
- handleTabIndex (cellDate ) {
222
- return this .disabledCalendarChanger (cellDate, ' year' ) ? - 1 : 0 ;
223
- },
224
- moveToFirstCell () {
225
- const year = this .years [0 ][0 ];
226
- const ref = this .$refs [` year-cell-${ year} ` ]? .[0 ];
227
- if (ref) {
228
- setTimeout (() => {
229
- ref .focus ();
230
- ref .classList .add (' focus' );
231
- }, 1 );
225
+ handleRefName (cellDate , row , col ) {
226
+ const date = createDate (cellDate, 0 );
227
+ if (! this .isDisabled (date)) {
228
+ return ` year-cell-${ row} -${ col} ` ;
232
229
}
230
+ return undefined ;
233
231
},
234
- onBlur (i , j ) {
235
- const year = this .years [i][j];
236
- const ref = this .$refs [` year-cell-${ year} ` ]? .[0 ];
237
- if (ref) {
238
- ref .classList .remove (' focus' );
239
- }
232
+ handleTabIndex (cellDate ) {
233
+ const date = createDate (cellDate, 0 );
234
+ return this .isDisabled (date) ? - 1 : 0 ;
240
235
},
241
236
},
242
237
};
0 commit comments