Multi-select example

Required choices and field errors.

Keep validation explicit and preserve selected values while the field recovers.

Complete React client module · MIT · Next.js and Vite

Interactive example

Required choices and field errors

Keep validation explicit and preserve selected values while the field recovers.

Keyboard

  • Submit to show the field error and focus the input.
  • Choose an option with ArrowDown and Enter.
  • Remove a choice with its labelled chip action; disabled managed choices stay unchanged.

States covered

  • required
  • invalid
  • recovery
  • disabled field
  • compact density

Install this complete example and its component dependencies.

Terminal
npx shadcn@latest add https://ui.bld8.dev/r/example-multi-select-validation.json
Complete source
React / TypeScript
/*!
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 { useRef, useState } from 'react';
import { Button, MultiSelect, type Option } from '@bld8/ui';
const reviewers: Option[] = [{ value: 'erin', label: 'Erin Vale' }, { value: 'tomas', label: 'Tomas Reed' }];
export default function ValidatedMultiSelectExample() {
  const [value, setValue] = useState<string[]>([]);
  const [attempted, setAttempted] = useState(false);
  const [submitted, setSubmitted] = useState('Not submitted');
  const input = useRef<HTMLInputElement>(null);
  return <form aria-label="Required reviewers example" noValidate onSubmit={event => {
    event.preventDefault(); setAttempted(true);
    if (!value.length) { setSubmitted('Choose a reviewer first.'); input.current?.focus(); }
    else setSubmitted(new FormData(event.currentTarget).getAll('requiredReviewers').map(String).join(', '));
  }} style={{ display: 'grid', gap: 16, maxWidth: 440 }}>
    <MultiSelect label="Required reviewers" name="requiredReviewers" options={reviewers} value={value} onValueChange={setValue} inputRef={input}
      required density="compact" error={attempted && !value.length ? 'Choose at least one reviewer.' : undefined} hint="One or more reviewers are required." />
    <MultiSelect label="Managed review panel" options={reviewers} defaultValue={['tomas']} disabled />
    <Button type="submit">Check reviewers</Button><output aria-label="Required reviewer form values">{submitted}</output>
  </form>;
}