Skip to content

Commit

Permalink
Improve valuepicker interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
nickcoutsos committed Apr 29, 2022
1 parent c25dea8 commit 7f6b1db
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions app/src/ValuePicker/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fuzzysort from 'fuzzysort'
import PropTypes from 'prop-types'
import { useEffect, useMemo, useRef, useState } from 'react'
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'

import style from './style.module.css'

Expand All @@ -25,7 +25,6 @@ function ValuePicker (props) {
const { onCancel, onSelect } = props

const listRef = useRef(null)
const searchRef = useRef(null)

const [query, setQuery] = useState(null)
const [highlighted, setHighlighted] = useState(null)
Expand Down Expand Up @@ -109,7 +108,7 @@ function ValuePicker (props) {
ArrowDown: handleHighlightNext,
ArrowUp: handleHightightPrev,
Enter: handleSelectActive,
Esc: onCancel
Escape: onCancel
}

const action = mapping[event.key]
Expand All @@ -124,7 +123,13 @@ function ValuePicker (props) {
onCancel
])

useEffect(() => searchRef.current?.focus() , [searchRef])
const focusSearch = useCallback(node => {
if (node) {
node.focus()
node.select()
}
}, [])

useEffect(() => {
document.body.addEventListener('click', handleClickOutside)

Expand All @@ -138,7 +143,7 @@ function ValuePicker (props) {
<p>{prompt}</p>
{choices.length > searchThreshold && (
<input
ref={searchRef}
ref={focusSearch}
type="text"
value={query !== null ? query : value}
onChange={handleKeyPress}
Expand Down

0 comments on commit 7f6b1db

Please sign in to comment.