|
| 1 | +// colViews screen size reference |
| 2 | +/* |
| 3 | + na: Not visible at any screen width |
| 4 | + xs: width < 585, |
| 5 | + s: width > 585 && width < 690, |
| 6 | + m: width > 690 && width < 775, |
| 7 | + l: width > 775 && width < 915, |
| 8 | + xl: width > 915 && width < 1140 |
| 9 | + All columns except "na" are visible for width > 1140 |
| 10 | +*/ |
| 11 | + |
| 12 | +export interface ColView { |
| 13 | + 0: string; // column name |
| 14 | + 1: 'na' | 'xs' | 's' | 'm' | 'l' | 'xl'; // screen size |
| 15 | +} |
| 16 | + |
| 17 | +export const updateVisibleColumns = ( |
| 18 | + colViews: ColView[], |
| 19 | + width: number |
| 20 | +): Record<string, boolean> => { |
| 21 | + // showCols object contains key value pairs that represent whether a column is visible or hidden. |
| 22 | + // i.e, Here, key = column name, and value = true/false where true means visible and false means hidden |
| 23 | + const showCols: Record<string, boolean> = {}; |
| 24 | + |
| 25 | + // colViews is a 2D array where each element is an array of 2 elements namely, |
| 26 | + // column name and the screen size till which they are visible |
| 27 | + colViews.forEach((col) => { |
| 28 | + // Hide the columns for any screen size |
| 29 | + if (col[1] === 'na') { |
| 30 | + showCols[col[0]] = false; |
| 31 | + } else if (width > 1140) { |
| 32 | + // Display all columns above width 1140 |
| 33 | + showCols[col[0]] = true; |
| 34 | + } else if (width >= 915 && width < 1140) { |
| 35 | + if (['xs', 's', 'm', 'l', 'xl'].includes(col[1])) { |
| 36 | + showCols[col[0]] = true; |
| 37 | + } else { |
| 38 | + showCols[col[0]] = false; |
| 39 | + } |
| 40 | + } else if (width >= 775 && width < 915) { |
| 41 | + if (['xs', 's', 'm', 'l'].includes(col[1])) { |
| 42 | + showCols[col[0]] = true; |
| 43 | + } else { |
| 44 | + showCols[col[0]] = false; |
| 45 | + } |
| 46 | + } else if (width >= 690 && width < 775) { |
| 47 | + if (['xs', 's', 'm'].includes(col[1])) { |
| 48 | + showCols[col[0]] = true; |
| 49 | + } else { |
| 50 | + showCols[col[0]] = false; |
| 51 | + } |
| 52 | + } else if (width >= 585 && width < 690) { |
| 53 | + if (['xs', 's'].includes(col[1])) { |
| 54 | + showCols[col[0]] = true; |
| 55 | + } else { |
| 56 | + showCols[col[0]] = false; |
| 57 | + } |
| 58 | + } else if (width < 585) { |
| 59 | + if (col[1] === 'xs') { |
| 60 | + showCols[col[0]] = true; |
| 61 | + } else { |
| 62 | + showCols[col[0]] = false; |
| 63 | + } |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + return showCols; |
| 68 | +}; |
0 commit comments