Features
Unlike traditional input-based multi-selects, this component is built on top of Plate editor, providing:
- Full history support (undo/redo)
- Native cursor navigation between and within tags
- Select one to many tags
- Copy/paste tags
- Drag and drop to reorder tags
- Read-only mode
- Duplicate tags prevention
- Create new tags with case insensitive matching
- Search text cleanup and whitespace trimming
- Fuzzy search powered by cmdk
Manual Usage
Installation
pnpm add @platejs/tag
Add Plugins
import { MultiSelectPlugin } from '@platejs/tag/react';
import { createPlateEditor } from 'platejs/react';
const editor = createPlateEditor({
plugins: [
// ...otherPlugins,
MultiSelectPlugin, // Multi-select editor with tag functionality
],
});
Configure Plugins
import { MultiSelectPlugin } from '@platejs/tag/react';
import { createPlateEditor } from 'platejs/react';
import { TagElement } from '@/components/ui/tag-node';
const editor = createPlateEditor({
plugins: [
// ...otherPlugins,
MultiSelectPlugin.withComponent(TagElement),
],
});
MultiSelectPlugin
: Extends TagPlugin and constrains the editor to only contain tag elementswithComponent
: AssignsTagElement
to render tag components
Add SelectEditor
npx shadcn@latest add https://platejs.org/r/select-editor
Basic Example
import { MultiSelectPlugin } from '@platejs/tag/react';
import { TagElement } from '@/components/ui/tag-node';
import {
SelectEditor,
SelectEditorContent,
SelectEditorInput,
SelectEditorCombobox,
type SelectItem,
} from '@/components/ui/select-editor';
// Define your items
const ITEMS: SelectItem[] = [
{ value: 'React' },
{ value: 'TypeScript' },
{ value: 'JavaScript' },
];
export default function MySelectEditor() {
const [value, setValue] = React.useState<SelectItem[]>([ITEMS[0]]);
return (
<SelectEditor
value={value}
onValueChange={setValue}
items={ITEMS}
>
<SelectEditorContent>
<SelectEditorInput placeholder="Select items..." />
<SelectEditorCombobox />
</SelectEditorContent>
</SelectEditor>
);
}
Form Example
Plugins
TagPlugin
Inline void element plugin for individual tag functionality.
MultiSelectPlugin
Extension of TagPlugin
that constrains the editor to only contain tag elements, enabling multi-select behavior with automatic text cleanup and duplicate prevention.
API
tf.insert.tag
Inserts new multi-select element at current selection.
getSelectedItems
Gets all tag items in the editor.
isEqualTags
Utility function to compare two sets of tags for equality, ignoring order.
Hooks
useSelectedItems
Hook to get the currently selected tag items in the editor.
useSelectableItems
Hook to get the available items that can be selected, filtered by search and excluding already selected items.
allowNew optional boolean
Whether to allow creating new items.
- Default:
true
- Default:
filter optional (value: string, search: string) => boolean
Custom filter function for items.
items optional T[]
Array of available items.
newItemFilter optional (search: string) => boolean
Filter function for new items.
newItemPosition optional 'end' | 'start'
Position of new items in list.
- Default:
'end'
- Default:
useSelectEditorCombobox
Hook to handle combobox behavior in the editor, including text cleanup and item selection.
Types
TTagElement
type TTagElement = TElement & {
value: string;
[key: string]: unknown;
};
TTagProps
type TTagProps = {
value: string;
[key: string]: unknown;
};