Add searchable sample picker

This commit is contained in:
Ninosaurier committed 2026-11-18 11:30:40 +01:00
1 parent 5425e1e3ff
commit fa42c6f8a0
2 files changed
+209 -91

No files matched your search

+123 -46
View File
@@ -1,5 +1,14 @@
import { useRef, useState } from 'react';
import { IonButton, IonIcon, IonPopover, IonSelect, IonSelectOption } from '@ionic/react';
import { useMemo, useRef, useState } from 'react';
import {
IonAccordion,
IonAccordionGroup,
IonButton,
IonIcon,
IonItem,
IonLabel,
IonPopover,
IonSearchbar,
} from '@ionic/react';
import { add } from 'ionicons/icons';
import {
INSTRUMENT_BY_ID,
@@ -14,17 +23,67 @@ interface InstrumentSelectorProps {
}
export function InstrumentSelector({ onSelectInstrument }: InstrumentSelectorProps) {
const [selectedCategoryId, setSelectedCategoryId] = useState('drums');
const [searchQuery, setSearchQuery] = useState('');
const [expandedCategoryIds, setExpandedCategoryIds] = useState<string[]>([]);
const popoverRef = useRef<HTMLIonPopoverElement>(null);
const popover = usePopoverDisclosure();
const selectedCategory = INSTRUMENT_CATEGORIES.find(
(category) => category.id === selectedCategoryId,
) ?? INSTRUMENT_CATEGORIES[0];
function handleCategoryChange(value: unknown): void {
if (typeof value !== 'string') return;
const category = INSTRUMENT_CATEGORIES.find((candidate) => candidate.id === value);
if (category) setSelectedCategoryId(category.id);
const filteredCategories = useMemo(() => {
const query = searchQuery.trim().toLowerCase();
return INSTRUMENT_CATEGORIES.flatMap((category) => {
const categoryMatches = category.label.toLowerCase().includes(query);
const instruments = category.instrumentIds
.map((instrumentId) => INSTRUMENT_BY_ID[instrumentId])
.filter((instrument) => (
!query
|| categoryMatches
|| `${instrument.label} ${instrument.description}`.toLowerCase().includes(query)
));
return instruments.length > 0 ? [{ category, instruments }] : [];
});
}, [searchQuery]);
function handleSearchInput(value: string | null | undefined): void {
const nextQuery = value ?? '';
const normalizedQuery = nextQuery.trim().toLowerCase();
setSearchQuery(nextQuery);
if (!normalizedQuery) {
setExpandedCategoryIds([]);
return;
}
setExpandedCategoryIds(
INSTRUMENT_CATEGORIES
.filter((category) => (
category.label.toLowerCase().includes(normalizedQuery)
|| category.instrumentIds.some((instrumentId) => {
const instrument = INSTRUMENT_BY_ID[instrumentId];
return `${instrument.label} ${instrument.description}`
.toLowerCase()
.includes(normalizedQuery);
})
))
.map((category) => category.id),
);
}
function handleAccordionChange(value: unknown): void {
if (Array.isArray(value)) {
setExpandedCategoryIds(value.filter((categoryId): categoryId is string => (
typeof categoryId === 'string'
)));
return;
}
setExpandedCategoryIds(typeof value === 'string' ? [value] : []);
}
function handlePopoverDidDismiss(): void {
popover.onDidDismiss();
setSearchQuery('');
setExpandedCategoryIds([]);
}
function addInstrument(value: unknown): void {
@@ -36,30 +95,16 @@ export function InstrumentSelector({ onSelectInstrument }: InstrumentSelectorPro
return (
<div className="instrument-selector-group">
<IonSelect
className="instrument-selector"
fill="solid"
interface="popover"
aria-label="Choose an instrument category"
value={selectedCategoryId}
onIonChange={(event) => handleCategoryChange(event.detail.value)}
>
{INSTRUMENT_CATEGORIES.map((category) => (
<IonSelectOption key={category.id} value={category.id}>
{category.label}
</IonSelectOption>
))}
</IonSelect>
<IonButton
id="instrument-add-trigger"
className="instrument-add-button"
fill="solid"
aria-label={`Show ${selectedCategory.label} instruments`}
aria-label="Open samples"
aria-haspopup="dialog"
aria-expanded={popover.isOpen}
>
<IonIcon icon={add} />
<IonIcon icon={add} slot="start" />
Samples
</IonButton>
<IonPopover
@@ -70,26 +115,58 @@ export function InstrumentSelector({ onSelectInstrument }: InstrumentSelectorPro
side="top"
alignment="start"
onDidPresent={popover.onDidPresent}
onDidDismiss={popover.onDidDismiss}
onDidDismiss={handlePopoverDidDismiss}
>
<div className="instrument-picker-menu" role="group" aria-label={`${selectedCategory.label} instruments`}>
{selectedCategory.instrumentIds.map((instrumentId) => {
const instrument = INSTRUMENT_BY_ID[instrumentId];
return (
<IonButton
className="instrument-picker-option"
fill="clear"
key={instrumentId}
aria-label={`Add ${instrument.label}`}
onClick={() => addInstrument(instrumentId)}
>
<span className="instrument-picker-option-copy">
<span>{instrument.label}</span>
<span>{instrument.description}</span>
</span>
</IonButton>
);
})}
<div className="instrument-picker-menu">
<IonSearchbar
className="instrument-picker-search"
aria-label="Search samples"
placeholder="Search samples"
value={searchQuery}
debounce={0}
showClearButton="always"
onIonInput={(event) => handleSearchInput(event.detail.value)}
onIonClear={() => handleSearchInput('')}
/>
{filteredCategories.length > 0 ? (
<IonAccordionGroup
className="instrument-picker-accordion-group"
multiple
value={expandedCategoryIds}
onIonChange={(event) => handleAccordionChange(event.detail.value)}
>
{filteredCategories.map(({ category, instruments }) => (
<IonAccordion
className="instrument-picker-category"
key={category.id}
value={category.id}
>
<IonItem className="instrument-category-header" slot="header" lines="none">
<IonLabel>{category.label}</IonLabel>
</IonItem>
<div className="instrument-category-content" slot="content">
{instruments.map((instrument) => (
<IonButton
className="instrument-picker-option"
fill="solid"
key={instrument.id}
aria-label={`Add ${instrument.label}`}
onClick={() => addInstrument(instrument.id)}
>
<span className="instrument-picker-option-copy">
<span>{instrument.label}</span>
<span>{instrument.description}</span>
</span>
</IonButton>
))}
</div>
</IonAccordion>
))}
</IonAccordionGroup>
) : (
<p className="instrument-picker-empty" role="status">No matching samples.</p>
)}
</div>
</IonPopover>
</div>