mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-25 22:27:32 +00:00
feat(tours): add non-motorized day tour planning
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import React, { useState, useRef, useEffect } from 'react'
|
||||
import React, { useState, useRef, useEffect, useId } from 'react'
|
||||
import { createPortal } from 'react-dom'
|
||||
import { ChevronDown, Check } from 'lucide-react'
|
||||
import { useAnchoredPosition, scrollAnchorIntoView } from '../../hooks/useAnchoredPosition'
|
||||
@@ -16,6 +16,7 @@ interface SelectOption {
|
||||
}
|
||||
|
||||
interface CustomSelectProps {
|
||||
ariaLabel?: string
|
||||
value: string | number
|
||||
onChange: (value: string | number) => void
|
||||
options?: SelectOption[]
|
||||
@@ -70,6 +71,7 @@ function menuRoom(anchor: HTMLElement | null, left: number): number {
|
||||
}
|
||||
|
||||
export default function CustomSelect({
|
||||
ariaLabel,
|
||||
value,
|
||||
onChange,
|
||||
options = [],
|
||||
@@ -84,8 +86,11 @@ export default function CustomSelect({
|
||||
const [open, setOpen] = useState(false)
|
||||
const [search, setSearch] = useState('')
|
||||
const ref = useRef<HTMLDivElement>(null)
|
||||
const triggerRef = useRef<HTMLButtonElement>(null)
|
||||
const dropRef = useRef<HTMLDivElement>(null)
|
||||
const searchRef = useRef<HTMLInputElement>(null)
|
||||
const keyboardDirection = useRef<'first' | 'last' | null>(null)
|
||||
const menuId = useId()
|
||||
|
||||
// Follows the trigger while the sheet scrolls and while the on-screen keyboard
|
||||
// resizes the viewport, instead of freezing at the rect measured on open (#1999).
|
||||
@@ -93,12 +98,20 @@ export default function CustomSelect({
|
||||
|
||||
useEffect(() => {
|
||||
if (!open || !searchable || !searchRef.current) return
|
||||
searchRef.current.focus()
|
||||
if (!keyboardDirection.current) searchRef.current.focus()
|
||||
// Focusing raises the keyboard on a phone; scroll the trigger up so the list
|
||||
// it just opened is not left underneath it (#2000).
|
||||
scrollAnchorIntoView(ref.current)
|
||||
}, [open, searchable])
|
||||
|
||||
useEffect(() => {
|
||||
if (!open || !keyboardDirection.current) return
|
||||
const buttons = dropRef.current?.querySelectorAll<HTMLButtonElement>('button')
|
||||
const direction = keyboardDirection.current
|
||||
keyboardDirection.current = null
|
||||
if (buttons?.length) buttons[direction === 'last' ? buttons.length - 1 : 0].focus()
|
||||
}, [open])
|
||||
|
||||
useEffect(() => {
|
||||
const handleClick = (e: MouseEvent) => {
|
||||
if (ref.current?.contains(e.target as Node)) return
|
||||
@@ -137,13 +150,46 @@ export default function CustomSelect({
|
||||
|
||||
const sm = size === 'sm'
|
||||
|
||||
const closeMenu = () => {
|
||||
setOpen(false)
|
||||
setSearch('')
|
||||
triggerRef.current?.focus()
|
||||
}
|
||||
|
||||
const handleKeyDown = (event: React.KeyboardEvent) => {
|
||||
if (disabled) return
|
||||
if (event.key === 'Escape' && open) {
|
||||
event.preventDefault()
|
||||
event.stopPropagation()
|
||||
closeMenu()
|
||||
} else if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
||||
event.preventDefault()
|
||||
const backwards = event.key === 'ArrowUp'
|
||||
if (!open) {
|
||||
keyboardDirection.current = backwards ? 'last' : 'first'
|
||||
setSearch('')
|
||||
setOpen(true)
|
||||
} else {
|
||||
const buttons = Array.from(dropRef.current?.querySelectorAll<HTMLButtonElement>('button') ?? [])
|
||||
const current = buttons.findIndex(button => button === document.activeElement)
|
||||
const next = current < 0 ? (backwards ? buttons.length - 1 : 0) : (current + (backwards ? -1 : 1) + buttons.length) % buttons.length
|
||||
buttons[next]?.focus()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div ref={ref} style={{ position: 'relative', ...style }}>
|
||||
{/* Trigger */}
|
||||
<button
|
||||
ref={triggerRef}
|
||||
id={id}
|
||||
type="button"
|
||||
aria-label={ariaLabel}
|
||||
aria-expanded={open}
|
||||
aria-controls={open ? menuId : undefined}
|
||||
disabled={disabled}
|
||||
onKeyDown={handleKeyDown}
|
||||
onClick={() => { if (!disabled) { setOpen(o => !o); setSearch('') } }}
|
||||
style={{
|
||||
width: '100%', display: 'flex', alignItems: 'center', gap: 8,
|
||||
@@ -174,7 +220,7 @@ export default function CustomSelect({
|
||||
|
||||
{/* Dropdown */}
|
||||
{open && createPortal(
|
||||
<div ref={dropRef} style={{
|
||||
<div ref={dropRef} id={menuId} role="group" aria-label={ariaLabel || selected?.label || placeholder || undefined} onKeyDown={handleKeyDown} style={{
|
||||
position: 'fixed',
|
||||
...(anchored
|
||||
? {
|
||||
@@ -255,7 +301,7 @@ export default function CustomSelect({
|
||||
<button
|
||||
key={option.value}
|
||||
type="button"
|
||||
onClick={() => { onChange(option.value); setOpen(false); setSearch('') }}
|
||||
onClick={() => { onChange(option.value); closeMenu() }}
|
||||
style={{
|
||||
width: '100%', display: 'flex', alignItems: 'center', gap: 8,
|
||||
padding: '7px 10px', borderRadius: 6,
|
||||
|
||||
Reference in New Issue
Block a user