Documentation / Formulaires
MentionInput
Un champ de texte où taper @ ouvre une liste de suggestions : la personne choisie s’insère sous forme de Chip non éditable, que l’on retire par sa croix ou par Retour arrière. Flèches haut / bas pour parcourir la liste, Entrée ou Tab pour valider, Échap pour la fermer.
Le composant ne filtre pas les suggestions lui-même : il affiche celles qu’on lui passe et prévient de la saisie par onMentionSearch(query), à l’application de filtrer (localement ou par une requête). onChange rend un MentionInputValue : le texte brut, sauts de ligne compris, et la liste des mentions avec leur position. Le champ affiche la value qu’on lui passe : au montage, pour reprendre un message existant, et chaque fois que l’application la change ({ text: "", mentions: [] } le vide). Celle qu’il vient de rendre par onChange lui revient identique, et il n’y touche pas.
Composant client ("use client"), bâti sur un contentEditable exposé avec role="textbox" et nommé par son label.
Exemples
Utilisation de base
Tapez @ puis quelques lettres : onMentionSearch filtre la liste passée en suggestions.
import { useState } from "react";
import { MentionInput, type MentionSuggestion } from "@synertech/ui";
const team: MentionSuggestion[] = [
{ id: "u1", label: "claire.martin", data: { name: "Claire Martin", role: "Produit" } },
{ id: "u2", label: "hugo.bernard", data: { name: "Hugo Bernard", role: "Design" } },
{ id: "u3", label: "lea.dubois", data: { name: "Léa Dubois", role: "Développement" } },
{ id: "u4", label: "nora.petit", data: { name: "Nora Petit", role: "Support" } },
];
function filterTeam(query: string) {
const q = query.toLowerCase();
return team.filter((member) => member.label.includes(q));
}
export function Basic() {
const [suggestions, setSuggestions] = useState(team);
return (
<MentionInput
label="Message"
placeholder="Écrivez @ pour mentionner un collègue…"
suggestions={suggestions}
onMentionSearch={(query) => setSuggestions(filterTeam(query))}
/>
);
}Lire le texte et les mentions
onChange rend le texte brut et chaque mention avec sa position dans ce texte.
import { useState } from "react";
import { MentionInput, type MentionInputValue, type MentionSuggestion } from "@synertech/ui";
const team: MentionSuggestion[] = [
{ id: "u1", label: "claire.martin", data: { name: "Claire Martin", role: "Produit" } },
{ id: "u2", label: "hugo.bernard", data: { name: "Hugo Bernard", role: "Design" } },
{ id: "u3", label: "lea.dubois", data: { name: "Léa Dubois", role: "Développement" } },
{ id: "u4", label: "nora.petit", data: { name: "Nora Petit", role: "Support" } },
];
const emptyValue: MentionInputValue = { text: "", mentions: [] };
function filterTeam(query: string) {
const q = query.toLowerCase();
return team.filter((member) => member.label.includes(q));
}
export function ReadValue() {
const [suggestions, setSuggestions] = useState(team);
const [value, setValue] = useState<MentionInputValue>(emptyValue);
return (
<div className="flex flex-col gap-3">
<MentionInput
label="Note interne"
suggestions={suggestions}
onMentionSearch={(query) => setSuggestions(filterTeam(query))}
value={value}
onChange={setValue}
/>
<p className="text-sm text-gray-600 dark:text-gray-300">
Personnes mentionnées :{" "}
{value.mentions.map((m) => m.suggestion.label).join(", ") || "aucune"}
</p>
</div>
);
}Modifier un message existant
Le champ affiche la value qu’on lui passe, mentions et sauts de ligne compris : la reprendre telle que onChange l’a rendue suffit pour rouvrir un message.
import { useState } from "react";
import { MentionInput, type MentionInputValue, type MentionSuggestion } from "@synertech/ui";
const team: MentionSuggestion[] = [
{ id: "u1", label: "claire.martin", data: { name: "Claire Martin", role: "Produit" } },
{ id: "u2", label: "hugo.bernard", data: { name: "Hugo Bernard", role: "Design" } },
{ id: "u3", label: "lea.dubois", data: { name: "Léa Dubois", role: "Développement" } },
{ id: "u4", label: "nora.petit", data: { name: "Nora Petit", role: "Support" } },
];
function filterTeam(query: string) {
const q = query.toLowerCase();
return team.filter((member) => member.label.includes(q));
}
export function EditExisting() {
const [suggestions, setSuggestions] = useState(team);
const [value, setValue] = useState<MentionInputValue>({
text: "Merci @lea.dubois pour la correction.\nOn la livre demain ?",
mentions: [{ trigger: "@", suggestion: team[2], startIndex: 6, endIndex: 17 }],
});
return (
<MentionInput
label="Modifier le message"
suggestions={suggestions}
onMentionSearch={(query) => setSuggestions(filterTeam(query))}
value={value}
onChange={setValue}
/>
);
}Suggestions personnalisées
renderSuggestion dessine chaque ligne de la liste, à partir du champ libre data.
import { useState } from "react";
import { MentionInput, type MentionSuggestion } from "@synertech/ui";
const team: MentionSuggestion[] = [
{ id: "u1", label: "claire.martin", data: { name: "Claire Martin", role: "Produit" } },
{ id: "u2", label: "hugo.bernard", data: { name: "Hugo Bernard", role: "Design" } },
{ id: "u3", label: "lea.dubois", data: { name: "Léa Dubois", role: "Développement" } },
{ id: "u4", label: "nora.petit", data: { name: "Nora Petit", role: "Support" } },
];
function filterTeam(query: string) {
const q = query.toLowerCase();
return team.filter((member) => member.label.includes(q));
}
export function CustomSuggestion() {
const [suggestions, setSuggestions] = useState(team);
return (
<MentionInput
label="Assigner la tâche"
suggestions={suggestions}
onMentionSearch={(query) => setSuggestions(filterTeam(query))}
maxSuggestions={3}
chipColor="success"
renderSuggestion={(s) => (
<div className="flex flex-col px-3 py-2 hover:bg-gray-100 dark:hover:bg-gray-700">
<span className="text-sm font-medium text-gray-900 dark:text-white">
{String(s.data?.name)}
</span>
<span className="text-xs text-gray-500 dark:text-gray-400">{String(s.data?.role)}</span>
</div>
)}
/>
);
}Commentaire avec envoi
Un cas réel : les mentions sont notifiées à l’envoi, puis le champ est vidé en repassant une valeur vide.
import { useState } from "react";
import { Button, MentionInput, type MentionInputValue, type MentionSuggestion } from "@synertech/ui";
const team: MentionSuggestion[] = [
{ id: "u1", label: "claire.martin", data: { name: "Claire Martin", role: "Produit" } },
{ id: "u2", label: "hugo.bernard", data: { name: "Hugo Bernard", role: "Design" } },
{ id: "u3", label: "lea.dubois", data: { name: "Léa Dubois", role: "Développement" } },
{ id: "u4", label: "nora.petit", data: { name: "Nora Petit", role: "Support" } },
];
const emptyValue: MentionInputValue = { text: "", mentions: [] };
function filterTeam(query: string) {
const q = query.toLowerCase();
return team.filter((member) => member.label.includes(q));
}
export function CommentBox() {
const [suggestions, setSuggestions] = useState(team);
const [value, setValue] = useState<MentionInputValue>(emptyValue);
const send = () => {
const ids = value.mentions.map((m) => m.suggestion.id);
console.log("Commentaire envoyé :", value.text, "— notifiés :", ids);
setValue(emptyValue);
};
return (
<div className="flex flex-col gap-3">
<MentionInput
label="Ajouter un commentaire"
placeholder="Votre commentaire…"
suggestions={suggestions}
onMentionSearch={(query) => setSuggestions(filterTeam(query))}
onMentionSelect={(member) => console.log("Mentionné :", member.label)}
value={value}
onChange={setValue}
helperText="Les personnes mentionnées recevront une notification."
/>
<Button onClick={send} disabled={value.text.trim() === ""} className="self-end">
Publier
</Button>
</div>
);
}Référence d’API
Composants
import { MentionInput } from "@synertech/ui";Props — MentionInputProps
| Prop | Type | Défaut | Description |
|---|---|---|---|
borderColor |
string |
— | |
chipColor |
ChipColor |
"primary" |
|
chipSize |
ChipSize |
"sm" |
|
chipVariant |
ChipVariant |
"soft" |
|
disabled |
boolean |
false |
|
error |
boolean | string |
— |
|
helperText |
string |
— | Écrite sous le champ ; un message passé dans |
hideLabel |
boolean |
false |
Garde le libellé pour les lecteurs d’écran seulement : il reste le nom du champ. |
icon |
React.ReactNode |
— | |
iconPosition |
"left" | "right" |
"left" |
|
inline |
boolean |
false |
|
label |
string |
— | |
labelColor |
string |
— | |
loading |
boolean |
false |
|
loadingLabel |
string |
— | Affiché dans la liste de suggestions tant que |
maxSuggestions |
number |
10 |
|
mentionTrigger |
string |
"@" |
|
onChange |
(value: MentionInputValue) => void |
— | |
onMentionRemove |
(mention: MentionSuggestion) => void |
— | |
onMentionSearch |
(query: string) => void |
— | |
onMentionSelect |
(mention: MentionSuggestion) => void |
— | |
placeholder |
string |
— | |
readOnly |
boolean |
false |
|
renderSuggestion |
(suggestion: MentionSuggestion) => React.ReactNode |
— | |
size |
"sm" | "md" | "lg" |
"md" |
|
suggestions |
MentionSuggestion[] |
[] |
|
textColor |
string |
— | |
value |
MentionInputValue |
— |
+ 2 attributs natifs hérités de RefAttributes, Attributes (className, id, aria-*, gestionnaires d’événements…).
Types
MentionData
Interfaceinterface MentionData| Champ | Type | Défaut | Description |
|---|---|---|---|
endIndex• |
number |
— | |
startIndex• |
number |
— | |
suggestion• |
MentionSuggestion |
— | |
trigger• |
string |
— |
MentionInputValue
Interfaceinterface MentionInputValue| Champ | Type | Défaut | Description |
|---|---|---|---|
mentions• |
MentionData[] |
— | |
text• |
string |
— |
MentionSuggestion
Interfaceinterface MentionSuggestion| Champ | Type | Défaut | Description |
|---|---|---|---|
id• |
string |
— | |
label• |
string |
— | |
avatar |
string |
— | |
data |
Record<string, unknown> |
— |