|
| 1 | +import React from 'react'; |
| 2 | +import { BaseA11yOption } from '../common.model'; |
| 3 | +import { getArrowDownIndex, getArrowUpIndex } from '../focus.common-helpers'; |
| 4 | +import { useOnKey } from '../on-key.hook'; |
| 5 | +import { onFocusOption, setInitialFocus } from './focus.helpers'; |
| 6 | +import { SetInitialFocusFn } from './list.model'; |
| 7 | +import { useOnTwoKeys } from '../on-two-Keys.hook'; |
| 8 | + |
| 9 | +export const useA11yList = <Option, A11yOption extends BaseA11yOption<Option>>( |
| 10 | + options: Option[], |
| 11 | + onSetInitialFocus: SetInitialFocusFn<Option, A11yOption> = setInitialFocus |
| 12 | +) => { |
| 13 | + const optionListRef = React.useRef<any>(null); |
| 14 | + const [internalOptions, setInternalOptions] = React.useState<A11yOption[]>( |
| 15 | + onSetInitialFocus(options) |
| 16 | + ); |
| 17 | + |
| 18 | + const handleFocus = (event: KeyboardEvent) => { |
| 19 | + const currentIndex = internalOptions.findIndex( |
| 20 | + option => option.tabIndex === 0 |
| 21 | + ); |
| 22 | + const nextIndex = |
| 23 | + event.key === 'ArrowUp' |
| 24 | + ? getArrowUpIndex(currentIndex) |
| 25 | + : getArrowDownIndex(currentIndex, internalOptions); |
| 26 | + |
| 27 | + if (currentIndex !== nextIndex) { |
| 28 | + setInternalOptions(prevOptions => |
| 29 | + prevOptions.map((option, index) => { |
| 30 | + switch (index) { |
| 31 | + case currentIndex: |
| 32 | + return { |
| 33 | + ...option, |
| 34 | + tabIndex: -1, |
| 35 | + }; |
| 36 | + case nextIndex: |
| 37 | + return { |
| 38 | + ...option, |
| 39 | + tabIndex: 0, |
| 40 | + }; |
| 41 | + default: |
| 42 | + return option; |
| 43 | + } |
| 44 | + }) |
| 45 | + ); |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + const handleFirstAndLast = (value: number) => { |
| 50 | + setInternalOptions(prevOptions => |
| 51 | + prevOptions.map((option, index) => { |
| 52 | + switch (index) { |
| 53 | + case value: |
| 54 | + return { |
| 55 | + ...option, |
| 56 | + tabIndex: 0, |
| 57 | + }; |
| 58 | + default: |
| 59 | + return { |
| 60 | + ...option, |
| 61 | + tabIndex: -1, |
| 62 | + }; |
| 63 | + } |
| 64 | + }) |
| 65 | + ); |
| 66 | + }; |
| 67 | + |
| 68 | + //Need this for Mac users |
| 69 | + useOnTwoKeys( |
| 70 | + optionListRef, |
| 71 | + ['ArrowUp', 'ArrowDown'], |
| 72 | + 'Meta', |
| 73 | + (event: KeyboardEvent) => |
| 74 | + event.key === 'ArrowUp' |
| 75 | + ? handleFirstAndLast(0) |
| 76 | + : handleFirstAndLast(internalOptions.length - 1) |
| 77 | + ); |
| 78 | + |
| 79 | + useOnKey(optionListRef, ['ArrowDown', 'ArrowUp'], (event: KeyboardEvent) => { |
| 80 | + handleFocus(event); |
| 81 | + }); |
| 82 | + |
| 83 | + useOnKey(optionListRef, ['Home', 'End'], (event: KeyboardEvent) => |
| 84 | + event.key === 'Home' |
| 85 | + ? handleFirstAndLast(0) |
| 86 | + : handleFirstAndLast(internalOptions.length - 1) |
| 87 | + ); |
| 88 | + |
| 89 | + return { |
| 90 | + optionListRef, |
| 91 | + options: internalOptions, |
| 92 | + setOptions: setInternalOptions, |
| 93 | + onFocusOption, |
| 94 | + }; |
| 95 | +}; |
0 commit comments