@@ -34,25 +34,31 @@ const isLeftMostColumn = (itemsPerRow: number, index: number) => (index % itemsP
3434const isRightMostColumn = ( grid : IGridItem [ ] , itemsPerRow : number , index : number ) => index % itemsPerRow === itemsPerRow - 1 || index === grid . length - 1 ;
3535const isTopRow = ( itemsPerRow : number , index : number ) => index <= itemsPerRow - 1 ;
3636
37+ const isEdgeColumn = ( grid : IGridItem [ ] , itemsPerRow : number , index : number , direction : Direction ) => {
38+ switch ( direction ) {
39+ case 'up' : return isTopRow ( itemsPerRow , index ) ;
40+ case 'down' : return isBottomRow ( grid , itemsPerRow , index ) ;
41+ case 'left' : return isLeftMostColumn ( itemsPerRow , index ) ;
42+ case 'right' : return isRightMostColumn ( grid , itemsPerRow , index ) ;
43+ default : return false ;
44+ }
45+ } ;
46+
3747/**
38- *
48+ * Given a direction (left or right), this function iterates through
49+ * a row until it finds a new grid item that is not disabled.
3950 */
4051const getAvailableAdjacentIndex = ( grid : IGridItem [ ] , itemsPerRow : number , startingIndex : number , direction : 'left' | 'right' ) => {
4152 let newIndex = startingIndex ;
4253 let countIndex = newIndex ;
4354 const offset = DIRECTIONAL_OFFSETS [ direction ] ;
4455
45- while ( ! ! grid [ countIndex ] ) {
56+ while ( ! ! grid [ countIndex ] && ! isEdgeColumn ( grid , itemsPerRow , countIndex , direction ) ) {
4657 const offsetIndex = countIndex + offset ;
4758
48- // Bail if it's an edge column
49- if ( direction === 'left' ? isLeftMostColumn ( itemsPerRow , countIndex ) : isRightMostColumn ( grid , itemsPerRow , countIndex ) ) {
59+ if ( ! ! grid [ offsetIndex ] && ! grid [ offsetIndex ] . isDisabled && grid [ offsetIndex ] . index !== grid [ startingIndex ] . index ) {
60+ newIndex = offsetIndex ;
5061 break ;
51- } else if ( ! ! grid [ offsetIndex ] ) {
52- if ( ! grid [ offsetIndex ] . isDisabled && grid [ offsetIndex ] . index !== grid [ startingIndex ] . index ) {
53- newIndex = offsetIndex ;
54- break ;
55- }
5662 }
5763
5864 countIndex += offset ;
@@ -61,14 +67,19 @@ const getAvailableAdjacentIndex = (grid: IGridItem[], itemsPerRow: number, start
6167 return newIndex ;
6268} ;
6369
70+ /**
71+ * Given a direction (up or down), this function finds an enabled item
72+ * on a row in the given direction.
73+ */
6474const getAvailableVerticalIndex = ( grid : IGridItem [ ] , itemsPerRow : number , startingIndex : number , direction : 'up' | 'down' ) => {
6575 let newIndex = startingIndex ;
6676 const offset = DIRECTIONAL_OFFSETS [ direction ] ;
6777
68- if ( direction === 'up' ? ! isTopRow ( itemsPerRow , startingIndex ) : ! isBottomRow ( grid , itemsPerRow , startingIndex ) ) {
78+ if ( ! isEdgeColumn ( grid , itemsPerRow , startingIndex , direction ) ) {
6979 /**
7080 * If destination grid is disabled, check if it's a left most column.
71- * If it is, select the tile adjacent right.
81+ * If it is, iterate right until we find an enabled grid item.
82+ * If it's not, iterate left until we find an enabled grid item.
7283 */
7384 do {
7485 newIndex += ( itemsPerRow * offset ) ;
0 commit comments