fishies-ui/src/select-state.ts
2026-10-07 19:32:19 +00:00

59 lines
3.3 KiB
TypeScript

export type SelectOptionState = { value: string; label: string; disabled?: boolean }
export type SelectState = { open: boolean; active: number }
export type SelectKeyResult = SelectState & { choose?: string; close?: 'focus' | 'blur'; handled: boolean }
const enabled = (options: readonly SelectOptionState[]) => options.map((option, index) => option.disabled ? -1 : index).filter(index => index >= 0)
export function selectedIndex(options: readonly SelectOptionState[], value: string) {
const index = options.findIndex(option => option.value === value)
return index >= 0 && !options[index]!.disabled ? index : enabled(options)[0] ?? -1
}
export function moveActive(options: readonly SelectOptionState[], from: number, step: 1 | -1) {
for (let index = from + step; index >= 0 && index < options.length; index += step) if (!options[index]!.disabled) return index
return from
}
export function typeahead(options: readonly SelectOptionState[], from: number, key: string) {
const needle = key.toLocaleLowerCase()
for (let offset = 1; offset <= options.length; offset++) {
const index = (from + offset + options.length) % options.length
const option = options[index]!
if (!option.disabled && option.label.trim().toLocaleLowerCase().startsWith(needle)) return index
}
return -1
}
export function selectKey(state: SelectState, key: string, options: readonly SelectOptionState[], value: string): SelectKeyResult {
const list = enabled(options)
if (!state.open) {
if (key === 'Enter' || key === ' ' || key === 'ArrowDown' || key === 'ArrowUp') return { open: true, active: selectedIndex(options, value), handled: true }
return { ...state, handled: false }
}
switch (key) {
case 'ArrowDown': return { open: true, active: moveActive(options, state.active, 1), handled: true }
case 'ArrowUp': return { open: true, active: moveActive(options, state.active, -1), handled: true }
case 'Home': return { open: true, active: list[0] ?? -1, handled: true }
case 'End': return { open: true, active: list[list.length - 1] ?? -1, handled: true }
case 'Enter': case ' ': {
const option = options[state.active]
return { open: false, active: state.active, choose: option && !option.disabled ? option.value : undefined, close: 'focus', handled: true }
}
case 'Escape': return { open: false, active: state.active, close: 'focus', handled: true }
case 'Tab': return { open: false, active: state.active, close: 'blur', handled: false }
}
if (key.length === 1) {
const found = typeahead(options, state.active, key)
return { open: true, active: found >= 0 ? found : state.active, handled: true }
}
return { ...state, handled: false }
}
export function placePopup(rect: { top: number; bottom: number; left: number; width: number }, viewport: { width: number; height: number }, wanted: number, gap = 6, margin = 8) {
const below = viewport.height - rect.bottom - gap - margin, above = rect.top - gap - margin
const up = below < Math.min(wanted, 180) && above > below
const maxHeight = Math.max(0, Math.min(wanted, up ? above : below))
const width = Math.min(Math.max(rect.width, 160), viewport.width - margin * 2)
const left = Math.min(Math.max(margin, rect.left), viewport.width - margin - width)
return { up, maxHeight, width, left, top: up ? undefined : rect.bottom + gap, bottom: up ? viewport.height - rect.top + gap : undefined }
}