This repository has been archived by the owner on Jan 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 574
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6e1f6c3
commit 3d76b59
Showing
21 changed files
with
707 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
client/src/CommandBar/steps/Documentation/ActionsDropdown.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { | ||
memo, | ||
useCallback, | ||
useContext, | ||
useEffect, | ||
useMemo, | ||
useState, | ||
} from 'react'; | ||
import SectionItem from '../../../components/Dropdown/Section/SectionItem'; | ||
import DropdownSection from '../../../components/Dropdown/Section'; | ||
import { CommandBarContext } from '../../../context/commandBarContext'; | ||
import useKeyboardNavigation from '../../../hooks/useKeyboardNavigation'; | ||
|
||
type Props = { | ||
handleClose: () => void; | ||
}; | ||
|
||
const ActionsDropDown = ({ handleClose }: Props) => { | ||
const { focusedItem } = useContext(CommandBarContext.FooterValues); | ||
const [focusedIndex, setFocusedIndex] = useState(0); | ||
|
||
const focusedDropdownItems = useMemo(() => { | ||
return ( | ||
(focusedItem && | ||
'focusedItemProps' in focusedItem && | ||
focusedItem.focusedItemProps?.dropdownItems) || | ||
[] | ||
); | ||
}, [focusedItem]); | ||
|
||
const focusedDropdownItemsLength = useMemo(() => { | ||
return focusedDropdownItems.reduce( | ||
(prev: number, curr: { items: Record<string, any>[]; key: string }) => | ||
prev + curr.items.length, | ||
0, | ||
); | ||
}, [focusedDropdownItems]); | ||
|
||
useEffect(() => { | ||
if (!focusedDropdownItemsLength) { | ||
handleClose(); | ||
} | ||
}, [focusedDropdownItemsLength]); | ||
|
||
const handleKeyEvent = useCallback( | ||
(e: KeyboardEvent) => { | ||
if (e.key === 'ArrowDown') { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
setFocusedIndex((prev) => | ||
prev < focusedDropdownItemsLength - 1 ? prev + 1 : 0, | ||
); | ||
} else if (e.key === 'ArrowUp') { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
setFocusedIndex((prev) => | ||
prev > 0 ? prev - 1 : focusedDropdownItemsLength - 1, | ||
); | ||
} else if (e.key === 'Enter') { | ||
let currentIndex = 0; | ||
|
||
for (let i = 0; i < focusedDropdownItems.length; i++) { | ||
for (let j = 0; j < focusedDropdownItems[i].items.length; j++) { | ||
if (currentIndex === focusedIndex) { | ||
return focusedDropdownItems[i].items[j].onClick(); | ||
} | ||
currentIndex++; | ||
} | ||
} | ||
} | ||
}, | ||
[focusedIndex, focusedDropdownItems, focusedDropdownItemsLength], | ||
); | ||
useKeyboardNavigation(handleKeyEvent); | ||
|
||
return ( | ||
<div> | ||
{!!focusedDropdownItems.length && | ||
focusedDropdownItems.map( | ||
(section: { | ||
items: Record<string, any>[]; | ||
key: string; | ||
itemsOffset: number; | ||
}) => ( | ||
<DropdownSection key={section.key}> | ||
{section.items.map((item: Record<string, any>, i: number) => ( | ||
<SectionItem | ||
color="base" | ||
shortcut={item.shortcut} | ||
key={item.key} | ||
isFocused={focusedIndex === i + section.itemsOffset} | ||
onClick={item.onClick} | ||
label={item.label} | ||
icon={item.icon} | ||
/> | ||
))} | ||
</DropdownSection> | ||
), | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default memo(ActionsDropDown); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.