UNPKG

@mantine/dates

Version:

Calendars, date and time pickers based on Mantine components

1 lines 7.05 kB
{"version":3,"file":"handle-control-key-down.cjs","names":[],"sources":["../../../src/utils/handle-control-key-down/handle-control-key-down.ts"],"sourcesContent":["import { RefObject } from 'react';\n\ntype ControlsRef = RefObject<HTMLButtonElement[][][]>;\ntype Direction = 'up' | 'down' | 'left' | 'right';\n\ntype NextIndexInput = Omit<ShiftFocusInput, 'controlsRef'>;\n\nfunction getNextIndex({ direction, levelIndex, rowIndex, cellIndex, size }: NextIndexInput) {\n switch (direction) {\n case 'up':\n if (levelIndex === 0 && rowIndex === 0) {\n return null;\n }\n if (rowIndex === 0) {\n return {\n levelIndex: levelIndex - 1,\n rowIndex:\n cellIndex <= size[levelIndex - 1][size[levelIndex - 1].length - 1] - 1\n ? size[levelIndex - 1].length - 1\n : size[levelIndex - 1].length - 2,\n cellIndex,\n };\n }\n return {\n levelIndex,\n rowIndex: rowIndex - 1,\n cellIndex,\n };\n\n case 'down':\n if (rowIndex === size[levelIndex].length - 1) {\n return {\n levelIndex: levelIndex + 1,\n rowIndex: 0,\n cellIndex,\n };\n }\n if (\n rowIndex === size[levelIndex].length - 2 &&\n cellIndex >= size[levelIndex][size[levelIndex].length - 1]\n ) {\n return {\n levelIndex: levelIndex + 1,\n rowIndex: 0,\n cellIndex,\n };\n }\n return {\n levelIndex,\n rowIndex: rowIndex + 1,\n cellIndex,\n };\n\n case 'left':\n if (levelIndex === 0 && rowIndex === 0 && cellIndex === 0) {\n return null;\n }\n if (rowIndex === 0 && cellIndex === 0) {\n return {\n levelIndex: levelIndex - 1,\n rowIndex: size[levelIndex - 1].length - 1,\n cellIndex: size[levelIndex - 1][size[levelIndex - 1].length - 1] - 1,\n };\n }\n if (cellIndex === 0) {\n return {\n levelIndex,\n rowIndex: rowIndex - 1,\n cellIndex: size[levelIndex][rowIndex - 1] - 1,\n };\n }\n return {\n levelIndex,\n rowIndex,\n cellIndex: cellIndex - 1,\n };\n\n case 'right':\n if (\n rowIndex === size[levelIndex].length - 1 &&\n cellIndex === size[levelIndex][rowIndex] - 1\n ) {\n return {\n levelIndex: levelIndex + 1,\n rowIndex: 0,\n cellIndex: 0,\n };\n }\n if (cellIndex === size[levelIndex][rowIndex] - 1) {\n return {\n levelIndex,\n rowIndex: rowIndex + 1,\n cellIndex: 0,\n };\n }\n return {\n levelIndex,\n rowIndex,\n cellIndex: cellIndex + 1,\n };\n\n default:\n return { levelIndex, rowIndex, cellIndex };\n }\n}\n\ninterface ShiftFocusInput {\n controlsRef: ControlsRef;\n direction: Direction;\n levelIndex: number;\n rowIndex: number;\n cellIndex: number;\n size: number[][];\n}\n\nfunction focusOnNextFocusableControl({\n controlsRef,\n direction,\n levelIndex,\n rowIndex,\n cellIndex,\n size,\n}: ShiftFocusInput) {\n const nextIndex = getNextIndex({ direction, size, rowIndex, cellIndex, levelIndex });\n\n if (!nextIndex) {\n return;\n }\n\n const controlToFocus =\n controlsRef.current?.[nextIndex.levelIndex]?.[nextIndex.rowIndex]?.[nextIndex.cellIndex];\n\n if (!controlToFocus) {\n return;\n }\n\n if (\n controlToFocus.disabled ||\n controlToFocus.getAttribute('data-hidden') ||\n controlToFocus.getAttribute('data-outside')\n ) {\n focusOnNextFocusableControl({\n controlsRef,\n direction,\n levelIndex: nextIndex.levelIndex,\n cellIndex: nextIndex.cellIndex,\n rowIndex: nextIndex.rowIndex,\n size,\n });\n } else {\n controlToFocus.focus();\n }\n}\n\nfunction getDirection(key: KeyboardEvent['key']): Direction {\n switch (key) {\n case 'ArrowDown':\n return 'down';\n case 'ArrowUp':\n return 'up';\n case 'ArrowRight':\n return 'right';\n case 'ArrowLeft':\n return 'left';\n default:\n return null!;\n }\n}\n\nfunction getControlsSize(controlsRef: ControlsRef) {\n return controlsRef.current?.map((column) => column.map((row) => row.length));\n}\n\ninterface HandleControlKeyDownInput {\n controlsRef: ControlsRef;\n levelIndex: number;\n rowIndex: number;\n cellIndex: number;\n event: React.KeyboardEvent<HTMLButtonElement>;\n}\n\nexport function handleControlKeyDown({\n controlsRef,\n levelIndex,\n rowIndex,\n cellIndex,\n event,\n}: HandleControlKeyDownInput) {\n const direction = getDirection(event.key);\n\n if (direction) {\n event.preventDefault();\n\n const size = getControlsSize(controlsRef)!;\n\n focusOnNextFocusableControl({\n controlsRef,\n direction,\n levelIndex,\n rowIndex,\n cellIndex,\n size,\n });\n }\n}\n"],"mappings":";;AAOA,SAAS,aAAa,EAAE,WAAW,YAAY,UAAU,WAAW,QAAwB;CAC1F,QAAQ,WAAR;EACE,KAAK;GACH,IAAI,eAAe,KAAK,aAAa,GACnC,OAAO;GAET,IAAI,aAAa,GACf,OAAO;IACL,YAAY,aAAa;IACzB,UACE,aAAa,KAAK,aAAa,EAAE,CAAC,KAAK,aAAa,EAAE,CAAC,SAAS,KAAK,IACjE,KAAK,aAAa,EAAE,CAAC,SAAS,IAC9B,KAAK,aAAa,EAAE,CAAC,SAAS;IACpC;GACF;GAEF,OAAO;IACL;IACA,UAAU,WAAW;IACrB;GACF;EAEF,KAAK;GACH,IAAI,aAAa,KAAK,WAAW,CAAC,SAAS,GACzC,OAAO;IACL,YAAY,aAAa;IACzB,UAAU;IACV;GACF;GAEF,IACE,aAAa,KAAK,WAAW,CAAC,SAAS,KACvC,aAAa,KAAK,WAAW,CAAC,KAAK,WAAW,CAAC,SAAS,IAExD,OAAO;IACL,YAAY,aAAa;IACzB,UAAU;IACV;GACF;GAEF,OAAO;IACL;IACA,UAAU,WAAW;IACrB;GACF;EAEF,KAAK;GACH,IAAI,eAAe,KAAK,aAAa,KAAK,cAAc,GACtD,OAAO;GAET,IAAI,aAAa,KAAK,cAAc,GAClC,OAAO;IACL,YAAY,aAAa;IACzB,UAAU,KAAK,aAAa,EAAE,CAAC,SAAS;IACxC,WAAW,KAAK,aAAa,EAAE,CAAC,KAAK,aAAa,EAAE,CAAC,SAAS,KAAK;GACrE;GAEF,IAAI,cAAc,GAChB,OAAO;IACL;IACA,UAAU,WAAW;IACrB,WAAW,KA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