Implement keybindings for combobox

pull/2773/head
Uku Taht 4 years ago
parent 2e0852cd03
commit 4ed62cb0e3

@ -3,8 +3,9 @@ import { Transition } from '@headlessui/react'
import { ChevronDownIcon } from '@heroicons/react/20/solid'
import debounce from 'debounce-promise'
import classNames from 'classnames'
import { useKeybinds } from '../keybinding'
function Option({isHighlighted, onClick, onMouseEnter, text}) {
function Option({isHighlighted, onClick, onMouseEnter, text, id}) {
const className = classNames('relative cursor-pointer select-none py-2 px-3', {
'text-gray-900 dark:text-gray-300': !isHighlighted,
'bg-indigo-600 text-white': isHighlighted,
@ -13,6 +14,7 @@ function Option({isHighlighted, onClick, onMouseEnter, text}) {
return (
<li
className={className}
id={id}
onClick={onClick}
onMouseEnter={onMouseEnter}
>
@ -20,6 +22,20 @@ function Option({isHighlighted, onClick, onMouseEnter, text}) {
</li>
)
}
function scrollTo(wrapper, id) {
if (wrapper) {
const el = wrapper.querySelector('#' + id);
console.log(el)
if (el) {
el.scrollIntoView({block: 'center'});
}
}
}
function optionId(index) {
return `plausible-combobox-option-${index}`
}
export default function PlausibleCombobox(props) {
const [options, setOptions] = useState([])
@ -29,14 +45,61 @@ export default function PlausibleCombobox(props) {
const [highlightedIndex, setHighlightedIndex] = useState(0);
const searchRef = useRef(null);
const containerRef = useRef(null);
const listRef = useRef(null);
function highLight(index) {
let newIndex = index
if (index < 0) {
newIndex = options.length - 1
} else if (index >= options.length) {
newIndex = 0
}
setHighlightedIndex(newIndex)
scrollTo(listRef.current, optionId(newIndex))
}
useKeybinds(containerRef.current, (e) => {
if (e.key === 'enter') {
if (!isOpen || loading) return null
selectOption(options[highlightedIndex])
return false
}
if (e.key === 'escape') {
if (!isOpen || loading) return null
setOpen(false)
searchRef.current?.focus()
return false
}
if (e.key === 'arrowdown') {
if (isOpen) {
highLight(highlightedIndex + 1)
} else {
fetchOptions(input)
}
}
if (e.key === 'arrowup') {
if (isOpen) {
highLight(highlightedIndex - 1)
} else {
fetchOptions(input, 'bottom')
}
}
}, [options, isOpen, loading, highlightedIndex, input])
function fetchOptions(query) {
function fetchOptions(query, highlight = 'top') {
setLoading(true)
setOpen(true)
return props.fetchOptions(query).then((loadedOptions) => {
setLoading(false)
setHighlightedIndex(0)
if (highlight === 'top') {
setHighlightedIndex(0)
} else if (highlight === 'bottom') {
setHighlightedIndex(loadedOptions.length - 1)
scrollTo(listRef.current, optionId(loadedOptions.length - 1))
}
setOptions(loadedOptions)
})
}
@ -90,7 +153,7 @@ export default function PlausibleCombobox(props) {
return () => { document.removeEventListener("mousedown", handleClick, false); }
}, [])
const isWildCard = !loading && input.includes('*')
const isWildCard = props.allowWildcard && !loading && input.includes('*')
const matchesFound = !loading && !isWildCard && options.length > 0
const noMatchesFound = !loading && !isWildCard && options.length === 0
@ -99,7 +162,7 @@ export default function PlausibleCombobox(props) {
<div onClick={toggleOpen} className={classNames('pl-2 pr-8 py-1 w-full dark:bg-gray-900 dark:text-gray-300 rounded-md shadow-sm border border-gray-300 dark:border-gray-700', {'border-indigo-500 ring-1 ring-indigo-500': isOpen, '': !isOpen})}>
{ props.values.map((value) => {
return (
<div key={value.value} className="bg-indigo-100 flex justify-between w-full rounded-sm px-2 py-0.5 m-0.5 text-sm">{value.label} <button onClick={(e) => removeOption(value, e)} className="font-bold ml-1">&times;</button></div>
<div key={value.value} className="bg-indigo-100 flex justify-between w-full rounded-sm px-2 py-0.5 m-0.5 text-sm">{value.label} <span onClick={(e) => removeOption(value, e)} className="cursor-pointer font-bold ml-1">&times;</span></div>
)
})
}
@ -116,7 +179,7 @@ export default function PlausibleCombobox(props) {
leaveTo="opacity-0"
show={isOpen}
>
<ul className="z-50 absolute mt-1 max-h-60 w-full overflow-auto rounded-md bg-white py-1 text-base shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none sm:text-sm dark:bg-gray-900">
<ul ref={listRef} className="z-50 absolute mt-1 max-h-60 w-full overflow-auto rounded-md bg-white py-1 text-base shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none sm:text-sm dark:bg-gray-900">
{ loading && (
<div className="relative cursor-default select-none py-2 px-4 text-gray-700 dark:text-gray-300">
Loading options...
@ -132,6 +195,7 @@ export default function PlausibleCombobox(props) {
return (
<Option
key={option.value}
id={optionId(i)}
isHighlighted={highlightedIndex === i}
onClick={() => selectOption(option)}
onMouseEnter={() => setHighlightedIndex(i)}

@ -1,3 +1,5 @@
import { useEffect, useCallback } from "react";
/**
* Returns whether a keydown or keyup event should be ignored or not.
*
@ -20,6 +22,28 @@ export function shouldIgnoreKeypress(event) {
return modifierPressed || isTyping
}
export function useKeybinds(el, callback, deps) {
const handleKeydown = useCallback((e) => {
const modifierPressed = event.ctrlKey || event.metaKey || event.altKey || event.keyCode == 229
if (modifierPressed) return true
e = {...e, key: e.key.toLowerCase()}
const retValue = callback(e)
if (retValue === false) {
e.preventDefault()
e.stopPropagation()
}
}, deps)
useEffect(() => {
if (el) {
el.addEventListener("keydown", handleKeydown);
return () => { el.removeEventListener("keydown", handleKeydown); }
}
})
}
/**
* Returns whether the given keybinding has been pressed and should be
@ -30,6 +54,6 @@ export function shouldIgnoreKeypress(event) {
*
*/
export function isKeyPressed(event, keybinding) {
const keyPressed = event.key.toLowerCase() == keybinding.toLowerCase()
const keyPressed = event.key.toLowerCase() == keybinding.toLowerCase()
return keyPressed && !shouldIgnoreKeypress(event)
}

@ -10,6 +10,7 @@ import { FILTER_GROUPS, parseQueryFilter, formatFilterGroup, formattedFilters, F
import { parseQuery } from '../../query'
import * as api from '../../api'
import { apiPath, siteBasePath } from '../../util/url'
import { shouldIgnoreKeypress } from '../../keybinding'
function getFormState(filterGroup, query) {
if (filterGroup === 'props') {
@ -77,9 +78,7 @@ class FilterModal extends React.Component {
}
handleKeydown(e) {
if (e.ctrlKey || e.metaKey || e.shiftKey || e.altKey || e.isComposing || e.keyCode === 229) {
return
}
if (shouldIgnoreKeypress(e)) return
if (e.target.tagName === 'BODY' && e.key === 'Enter') {
this.handleSubmit()
@ -185,8 +184,8 @@ class FilterModal extends React.Component {
}
renderSearchBox(filter) {
const isStrict = this.state.selectedFilterGroup === 'location'
return <Combobox fetchOptions={this.fetchOptions(filter)} strict={isStrict} values={this.state.formState[filter].clauses} onChange={this.onChange(filter)} placeholder={`Select ${withIndefiniteArticle(formattedFilters[filter])}`} />
const allowWildcard = this.state.selectedFilterGroup === 'page'
return <Combobox fetchOptions={this.fetchOptions(filter)} allowWildcard={allowWildcard} values={this.state.formState[filter].clauses} onChange={this.onChange(filter)} placeholder={`Select ${withIndefiniteArticle(formattedFilters[filter])}`} />
}
renderFilterInputs() {
@ -274,7 +273,7 @@ class FilterModal extends React.Component {
<div className="mt-4 border-b border-gray-300"></div>
<main className="modal__content">
<form className="flex flex-col" id="filter-form" onSubmit={this.handleSubmit.bind(this)}>
<form className="flex flex-col" onSubmit={this.handleSubmit.bind(this)}>
{this.renderFilterInputs()}
<div className="mt-6 flex items-center justify-start">

@ -1,6 +1,7 @@
import React from "react";
import { createPortal } from "react-dom";
import { withRouter } from 'react-router-dom';
import { shouldIgnoreKeypress } from '../../keybinding'
// This corresponds to the 'md' breakpoint on TailwindCSS.
const MD_WIDTH = 768;
@ -46,7 +47,7 @@ class Modal extends React.Component {
}
handleKeyup(e) {
if (e.code === 'Escape') {
if (!shouldIgnoreKeypress(e) && e.code === 'Escape') {
this.close()
}
}

Loading…
Cancel
Save