Sha256: 5ecfd20d41c8872d2cbaa21497b05c27bbbb059a39c4b49abfbc5f78a71c970b

Contents?: true

Size: 1.41 KB

Versions: 566

Compression:

Stored size: 1.41 KB

Contents

import React, { useContext } from "react";
import DropdownContext from "../context";


export const useHandleOnKeyDown = () => {

const {
  autocomplete,
  filteredOptions,
  focusedOptionIndex,
  handleBackspace,
  handleOptionClick,
  selected,
  setFocusedOptionIndex,
  setIsDropDownClosed,
}= useContext(DropdownContext)

  return (e: React.KeyboardEvent) => {

    if (e.key !== "Tab" && autocomplete && selected && selected.label) {
      handleBackspace();
    }

    switch (e.key) {
    case "Backspace":
    case "Delete":
      if (autocomplete) {
      handleBackspace();
      }
      break;
    case "ArrowDown": {
      e.preventDefault();
      setIsDropDownClosed(false);
      const nextIndex = (focusedOptionIndex + 1) % filteredOptions.length;
      setFocusedOptionIndex(nextIndex);
      break;
    }
    case "ArrowUp": {
      e.preventDefault();
      const nextIndexUp =
        (focusedOptionIndex - 1 + filteredOptions.length) %
        filteredOptions.length;
      setFocusedOptionIndex(nextIndexUp);
      break;
    }
    case "Enter":
      if (focusedOptionIndex !== -1) {
        e.preventDefault();
        handleOptionClick(filteredOptions[focusedOptionIndex]);
        setFocusedOptionIndex(-1)
      } else if (focusedOptionIndex === -1) {
        setIsDropDownClosed(false)
      }
      break;
      case "Tab":
        setIsDropDownClosed(true);
        setFocusedOptionIndex(-1)
        break;
  }
}
};

Version data entries

566 entries across 566 versions & 1 rubygems

Version Path
playbook_ui-14.12.0.pre.alpha.play1790darkaudittable5802 app/pb_kits/playbook/pb_dropdown/hooks/useHandleOnKeydown.tsx
playbook_ui-14.12.0.pre.alpha.play1752updatecontenttag5801 app/pb_kits/playbook/pb_dropdown/hooks/useHandleOnKeydown.tsx
playbook_ui-14.13.0.pre.rc.6 app/pb_kits/playbook/pb_dropdown/hooks/useHandleOnKeydown.tsx
playbook_ui-14.13.0.pre.rc.5 app/pb_kits/playbook/pb_dropdown/hooks/useHandleOnKeydown.tsx
playbook_ui-14.12.0.pre.alpha.PLAY1602lightboxoverlapnitrobug5781 app/pb_kits/playbook/pb_dropdown/hooks/useHandleOnKeydown.tsx
playbook_ui-14.13.0.pre.rc.4 app/pb_kits/playbook/pb_dropdown/hooks/useHandleOnKeydown.tsx
playbook_ui-14.12.0.pre.alpha.playrailsinputmaskissue5775 app/pb_kits/playbook/pb_dropdown/hooks/useHandleOnKeydown.tsx
playbook_ui-14.12.0.pre.alpha.PBNTR456fixedconftoastrailsautoclose5757 app/pb_kits/playbook/pb_dropdown/hooks/useHandleOnKeydown.tsx
playbook_ui-14.12.0.pre.alpha.PBNTR456fixedconftoastrailsautoclose5754 app/pb_kits/playbook/pb_dropdown/hooks/useHandleOnKeydown.tsx
playbook_ui-14.12.0.pre.alpha.PLAY1602lightboxoverlapnitrobugzindextoken5751 app/pb_kits/playbook/pb_dropdown/hooks/useHandleOnKeydown.tsx
playbook_ui-14.12.0.pre.alpha.PBNTR456fixedconftoastrailsautoclose5738 app/pb_kits/playbook/pb_dropdown/hooks/useHandleOnKeydown.tsx
playbook_ui-14.13.0.pre.rc.3 app/pb_kits/playbook/pb_dropdown/hooks/useHandleOnKeydown.tsx
playbook_ui-14.12.0.pre.alpha.PLAY1865reactdatepickerreinitializingbug5732 app/pb_kits/playbook/pb_dropdown/hooks/useHandleOnKeydown.tsx
playbook_ui-14.12.0.pre.alpha.PBNTR456fixedconftoastrailsautoclose5728 app/pb_kits/playbook/pb_dropdown/hooks/useHandleOnKeydown.tsx
playbook_ui-14.12.0.pre.alpha.play1862buttondisabledlinkbug5716 app/pb_kits/playbook/pb_dropdown/hooks/useHandleOnKeydown.tsx
playbook_ui-14.12.0.pre.alpha.play1862buttondisabledlinkbug5714 app/pb_kits/playbook/pb_dropdown/hooks/useHandleOnKeydown.tsx
playbook_ui-14.13.0.pre.rc.2 app/pb_kits/playbook/pb_dropdown/hooks/useHandleOnKeydown.tsx
playbook_ui-14.13.0.pre.rc.1 app/pb_kits/playbook/pb_dropdown/hooks/useHandleOnKeydown.tsx
playbook_ui-14.13.0.pre.rc.0 app/pb_kits/playbook/pb_dropdown/hooks/useHandleOnKeydown.tsx
playbook_ui-14.12.0.pre.alpha.advancedtablealignmentfixes5693 app/pb_kits/playbook/pb_dropdown/hooks/useHandleOnKeydown.tsx