Empty choices and validation
Show the unavailable state, load choices explicitly and retain useful field feedback.
Keyboard
- The empty list explains why no choice can be made.
- After loading choices, use normal select keyboard navigation.
- The managed field remains disabled and cannot change.
States covered
- empty options
- required
- invalid
- disabled field
- recovery
Install this complete example and its component dependencies.
npx shadcn@latest add https://ui.bld8.dev/r/example-select-empty-validation.jsonComplete source
/*!
MIT License
Copyright (c) 2026 Jaison
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
'use client';
import '@bld8/ui/styles.css';
import { useState } from 'react';
import { Button, Select, type Option } from '@bld8/ui';
const destinations: Option[] = [{ value: 'edge', label: 'Edge destination' }, { value: 'batch', label: 'Batch destination' }];
export default function EmptySelectExample() {
const [available, setAvailable] = useState(false);
const [value, setValue] = useState<string | null>(null);
const [attempted, setAttempted] = useState(false);
const [submitted, setSubmitted] = useState('Not submitted');
return <form aria-label="Destination validation example" noValidate onSubmit={event => {
event.preventDefault(); setAttempted(true); setSubmitted(value ?? 'Choose a destination first.');
}} style={{ display: 'grid', gap: 16, maxWidth: 440 }}>
<Select label="Release destination" name="destination" options={available ? destinations : []} value={value} onValueChange={setValue}
required emptyMessage="No destinations are available." placeholder="Choose a destination"
error={attempted && !value ? 'Choose a release destination.' : undefined}
hint={available ? 'Choices are ready.' : 'Load the example destinations to continue.'} />
<Select label="Managed region" options={[{ value: 'central', label: 'Central region' }]} defaultValue="central" disabled hint="Managed by the application." />
<div style={{ display: 'flex', gap: 8, flexWrap: 'wrap' }}><Button variant="secondary" onClick={() => setAvailable(true)} disabled={available}>Load example destinations</Button><Button type="submit">Check destination</Button></div>
<output aria-label="Destination validation result">{submitted}</output>
</form>;
}