Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Add jump with keys into demos using onKeyDown event #599

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions site/comps/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import View, { ViewProps } from "comps/View"
import Text from "comps/Text"
import useClickOutside from "hooks/useClickOutside"
import useKey from "hooks/useKey"
import { findLastIndex } from "lib/array"

interface PromptProps {
name: string,
Expand Down Expand Up @@ -67,7 +68,6 @@ interface SelectProps {
}

// TODO: scroll to cur at expand
// TODO: jump with key
const Select: React.FC<SelectProps & ViewProps> = ({
options,
value,
Expand All @@ -78,6 +78,7 @@ const Select: React.FC<SelectProps & ViewProps> = ({

const dropdownRef = React.useRef<HTMLDivElement>(null)
const [ curItem, setCurItem ] = React.useState(value ?? options[0])
const [ curItemIndex, setCurItemIndex ] = React.useState<number | null>(0)
const [ expanded, setExpanded ] = React.useState(false)

useClickOutside(dropdownRef, () => setExpanded(false), [ setExpanded ])
Expand Down Expand Up @@ -138,13 +139,42 @@ const Select: React.FC<SelectProps & ViewProps> = ({
<Prompt name={curItem} options={options} expanded={expanded} />
<View height={2} stretchX bg={4} />
<View
// focusable
// focusable
ref={dropdownRef}
tabIndex={0}
stretchX
bg={2}
onKeyDown={(e) => {
// TODO
console.log(e.key)
const indexFound = options.findIndex((opt, i) => {
if (curItem[0] === e.key) {
if (!curItemIndex) return e.key === opt[0] && i > 0
if (curItemIndex === findLastIndex(options, curItem, 0)) {
setCurItemIndex(null)
return e.key === opt[0]
}
else return e.key === opt[0] && curItemIndex < i
} else if (curItem[0] !== e.key) {
return e.key === opt[0]
}
})

const newItem = options[indexFound]

if (e.key !== "Enter" && newItem && curItem !== newItem) {
setCurItem(newItem)
setCurItemIndex(indexFound)

const selected = dropdownRef.current!.children[indexFound] as HTMLDivElement

if (selected) {
dropdownRef.current!.scrollTop = selected.offsetTop
}
}

if (e.key === "Enter") {
onChange && onChange(curItem)
}

}}
css={{
overflowY: "auto",
Expand Down
6 changes: 6 additions & 0 deletions site/lib/array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const findLastIndex = (array: string[], searchValue: string, searchIndex: number) => {
const index = array.slice().reverse().findIndex((item: string) => item[searchIndex] === searchValue[searchIndex])
const count = array.length - 1
const lastIndex = index >= 0 ? count - index : index
return lastIndex
}