All files / src/components/DateField/hooks useFocusManager.ts

79.36% Statements 50/63
59.61% Branches 31/52
75% Functions 9/12
78.33% Lines 47/60

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 16124x                                                             413x   413x   413x       248x 95x     153x 3x           150x 150x           413x   413x                     62x 62x     62x     45x 18x   27x       37x 37x 37x 37x       2x 2x 2x 2x                                                 173x 173x 173x 1315x 1315x       1315x         1315x   1315x 744x 1886x 1886x 1886x 1886x 173x       744x       173x         440x 440x 440x 4x   436x    
import React from 'react';
 
import type {DateFieldSection, DateFieldSectionType} from '../types';
import {isEditableSectionType} from '../utils';
 
interface FocusManagerOptions {
    sections: DateFieldSection[];
}
 
interface FocusManager {
    /** Active section index */
    activeSectionIndex: number;
    /** Selected sections */
    selectedSectionIndexes: {startIndex: number; endIndex: number} | null;
    /** Return true if it is the first section */
    isFistSection: (position: number) => boolean;
    /** Return true if it is the last section */
    isLastSection: (position: number) => boolean;
    /** Focus section in position or all sections. */
    focusSection: (position: number | 'all') => void;
    /** Focuses the next section if present */
    focusNextSection: () => void;
    /** Focuses the previous section if present */
    focusPreviousSection: () => void;
    /** Focuses the first section */
    focusFirstSection: () => void;
    /** Focuses the last section */
    focusLastSection: () => void;
}
 
export function useFocusManager({sections}: FocusManagerOptions): FocusManager {
    const [selectedSections, setSelectedSections] = React.useState<number | 'all'>(-1);
 
    const connections = React.useMemo(() => connectEditableSections(sections), [sections]);
 
    const selectedSectionIndexes = React.useMemo<{
        startIndex: number;
        endIndex: number;
    } | null>(() => {
        if (selectedSections === -1) {
            return null;
        }
 
        if (selectedSections === 'all') {
            return {
                startIndex: 0,
                endIndex: connections.length - 1,
            };
        }
 
        Eif (typeof selectedSections === 'number') {
            return {startIndex: selectedSections, endIndex: selectedSections};
        }
 
        return selectedSections;
    }, [selectedSections, connections]);
 
    const activeSectionIndex = getCurrentEditableSectionIndex(connections, selectedSections);
 
    return {
        activeSectionIndex,
        selectedSectionIndexes,
        isFistSection(position) {
            const p = connections[position];
            if (!p) {
                return false;
            }
            return position === p.previousEditableSection;
        },
        isLastSection(position) {
            const p = connections[position];
            Iif (!p) {
                return false;
            }
            return position === p.nextEditableSection;
        },
        focusSection(index: number | 'all') {
            if (index === 'all' || index === -1) {
                setSelectedSections(index);
            } else {
                setSelectedSections(getCurrentEditableSectionIndex(connections, index));
            }
        },
        focusNextSection() {
            const currentIndex = selectedSections === 'all' ? 0 : selectedSections;
            const newIndex = connections[currentIndex]?.nextEditableSection ?? -1;
            Eif (newIndex !== -1) {
                setSelectedSections(newIndex);
            }
        },
        focusPreviousSection() {
            const currentIndex = selectedSections === 'all' ? 0 : selectedSections;
            const newIndex = connections[currentIndex]?.previousEditableSection ?? -1;
            Eif (newIndex !== -1) {
                setSelectedSections(newIndex);
            }
        },
        focusFirstSection() {
            const newIndex = connections[0]?.previousEditableSection ?? -1;
            if (newIndex !== -1) {
                setSelectedSections(newIndex);
            }
        },
        focusLastSection() {
            const newIndex = connections[sections.length - 1]?.nextEditableSection ?? -1;
            if (newIndex !== -1) {
                setSelectedSections(newIndex);
            }
        },
    };
}
 
interface Connection {
    type: DateFieldSectionType;
    previousEditableSection: number;
    nextEditableSection: number;
}
 
function connectEditableSections(sections: DateFieldSection[]) {
    let previousEditableSection = -1;
    const connections: Connection[] = new Array(sections.length);
    for (let i = 0; i < sections.length; i++) {
        const section = sections[i];
        Iif (!section) {
            throw new Error('section must be defined');
        }
 
        const connection = {
            type: section.type,
            previousEditableSection,
            nextEditableSection: previousEditableSection,
        };
        connections[i] = connection;
 
        if (isEditableSectionType(connection.type)) {
            for (let j = Math.max(0, previousEditableSection); j <= i; j++) {
                const prevSection = connections[j];
                Eif (prevSection) {
                    prevSection.nextEditableSection = i;
                    if (prevSection.previousEditableSection === -1) {
                        prevSection.previousEditableSection = i;
                    }
                }
            }
            previousEditableSection = i;
        }
    }
 
    return connections;
}
 
function getCurrentEditableSectionIndex(sections: Connection[], selectedSections: 'all' | number) {
    const currentIndex =
        selectedSections === 'all' || selectedSections === -1 ? 0 : selectedSections;
    const section = sections[currentIndex];
    if (section && !isEditableSectionType(section.type)) {
        return section.nextEditableSection;
    }
    return isEditableSectionType(section?.type) ? currentIndex : -1;
}