Demo
Combobox input, keys and custom values
Documented in Combobox.
Combobox input, keys and custom values
A controlled input text with onInputChange, custom values validated by onCreateItem, the optional dropdown button, the editable-combobox keys and IME-safe filtering.
Value: · Input text:
Demo.tsx
'use client';
import { useState } from 'react';
import { Button } from '@salmonui/buttons/button';
import { Checkbox } from '@salmonui/inputs/checkbox';
import { Combobox, type ComboboxValue } from '@salmonui/dropdowns/combobox';
import { EventLog, useEventLog } from '../../components/demo-kit';
interface Code {
readonly value: string;
readonly label: string;
}
const AIRPORTS: Code[] = [
['AMS', 'Amsterdam'], ['CDG', 'Paris Charles de Gaulle'], ['FRA', 'Frankfurt'], ['HND', 'Tokyo Haneda'], ['ICN', 'Seoul Incheon'],
['JFK', 'New York JFK'], ['LHR', 'London Heathrow'], ['NRT', 'Tokyo Narita'], ['SIN', 'Singapore Changi'], ['SYD', 'Sydney'],
].map(([value, label]) => ({ value: value as string, label: `${value} · ${label}` }));
const CITIES = ['東京', '大阪', '京都', '札幌', '名古屋', '福岡', 'ソウル', '北京', '上海'];
// Created items must be three letters: anything else is rejected (onCreateItem returns null).
const createAirport = (text: string): Code | null => (/^[a-z]{3}$/i.test(text.trim()) ? { value: text.trim().toUpperCase(), label: `${text.trim().toUpperCase()} · custom code` } : null);
export default function ComboboxOptionsDemo() {
const [log, push] = useEventLog();
const [airport, setAirport] = useState<ComboboxValue | null>('LHR');
const [text, setText] = useState('LHR · London Heathrow');
const [showTrigger, setShowTrigger] = useState(true);
return (
<div style={{ display: 'grid', gap: 12, maxWidth: 480 }}>
<Checkbox checked={showTrigger} onCheckedChange={(checked) => setShowTrigger(checked)}>
Show the dropdown button
</Checkbox>
<div style={{ display: 'grid', gap: 4 }}>
<span id="cbx-options-airport">Airport (or any three-letter code)</span>
<Combobox<Code>
aria-labelledby="cbx-options-airport"
data={AIRPORTS}
searchOperator="contains"
creatable
onCreateItem={(created) => {
const item = createAirport(created);
push(item ? `onCreateItem "${created}" → ${item.value}` : `onCreateItem "${created}" → rejected`);
return item;
}}
showTrigger={showTrigger}
value={airport}
onValueChange={(next, event) => {
push(`onValueChange ${String(next)} (reason ${event.reason}, source ${event.source})`);
setAirport(next);
}}
inputValue={text}
onInputChange={(next, event) => {
push(`onInputChange "${next}" (reason ${event.reason})`);
setText(next);
}}
/>
</div>
<p>
Value: <output>{String(airport)}</output> · Input text: <output>{text}</output>
</p>
<Button variant="outline" style={{ justifySelf: 'start' }} onClick={() => setText('Tokyo')}>
Set the input text to “Tokyo”
</Button>
<div style={{ display: 'grid', gap: 4 }}>
<span id="cbx-options-ime">都市 (type with a Japanese or Chinese input method)</span>
<Combobox aria-labelledby="cbx-options-ime" lang="ja" data={CITIES} searchOperator="contains" onInputChange={(next, event) => push(`IME field onInputChange "${next}" (reason ${event.reason})`)} />
</div>
<EventLog entries={log} />
</div>
);
}
The airport field controls its text (inputValue / onInputChange) and
its value separately; the button sets the text from outside. Type a
three-letter code that is not listed, such as mad, and press Enter:
onCreateItem builds the item. Type Madrid and press Enter: it returns
null, so the text is rejected and reverts when focus leaves.
Keys: Arrow Down / Up open the list and move the active option, Alt+Down opens and Alt+Up closes it, Enter commits, Escape closes and a second Escape clears the text, Home / End move the caret, Tab commits and moves on. The dropdown button (uncheck "Show the dropdown button" to hide it) is not a tab stop.
In the city field, type with an IME (for example romaji toukyou in a
Japanese input method): nothing is filtered or committed while the
composition is open, and the search runs when it ends.