Skip to content
EspañolEnglish
All components (283)

Mention field

Fields · MentionField

What it is and when to use it

A long text field where @ opens a list of people: comments, tasks, a chat. The list appears where the mention is, and the value stays plain text ("Hello @Marta Ruiz, look at this"), which is what gets stored and sent.

When to use it

  • Comments on an order or a task that notify someone.
  • options are the people and trigger the character that opens the list (@, by default; # for tags).
  • onSearch to search on the server: it is called with what is typed after the character, and loading says the options are on their way.
  • onMentionsChange hands over who is mentioned in the text, to notify them on saving.
  • In a chat, PromptInput does the same with its mentions prop. useMentions mounts it on any other textarea.

When not to

  • Choosing people without writing anything else: PeoplePicker.
  • Choosing from a closed list in a one-line field: Combobox.

Keyboard: @ opens the list and what you type filters it, ignoring case and accents. The arrows move, Enter or Tab choose and Escape closes the list without leaving the field. Focus always stays in the field. An email (ana@correo.es) opens nothing: the character only counts at the start or after a space.

Accessibility: the field points to the list with aria-controls and says which one is marked with aria-activedescendant. A spoken notice says how many suggestions there are. Like any field, it has a mandatory label, hint and error.

Fine-tuning: the field is a TextArea and accepts its --amset-field-* variables. The panel is adjusted with the amset-mention__panel class.

What it looks like

Type @ to notify someone
Nobody with that name

<MentionField
  label="Comment"
  hint="Type @ to notify someone"
  defaultValue="Reviewed with "
  options={personas}
/>

Props

Import it with import { MentionField } from '@amset/ui'. Required props are marked.

PropTypeWhat it does
labelrequiredstring—
optionsrequiredMentionOptionsWho can be mentioned. With `onSearch`, what the last search returned. With several triggers, a function that returns the list for each one: people for `@`, tags for `#`.
classNamestring—
countByCountStrategyHow characters are counted: `graphemes` (default, an emoji with its skin tone is one), `characters` (what `maxLength` measures) or a custom function (words, bytes).
counterboolean | "inside"Shows "123 / 500", with the limit of `maxCount` or `maxLength`. `inside` puts it inside the border, bottom right, instead of under the field.
defaultValuestring—
disabledboolean—
emptyMessagestringWhat is said when nobody matches.
errorstring—
filterOptionfalse | ((option: ListOption, query: string, trigger: string) => boolean)How an option matches what is typed after the character. By default, that it contains it, case- and accent-insensitive. `false` does not filter: the list is shown as is.
footerReactNodeA strip inside the border, under the text: formatting buttons, an attachment. The `inside` counter goes next to it.
hiddenLabelbooleanThe label for the screen reader only. For when the field sits inside something that already explains itself (a table cell, a group of filters), never to save space.
hintstring—
inputRefRef<HTMLTextAreaElement>The real `<textarea>`.
invalidboolean—
loadingbooleanThe options are arriving.
maxCountnumberA limit that WARNS without cutting: typing can go on, the count turns red, the field is marked invalid and how many are left over is announced. To cut off hard there is `maxLength`. With `maxCount` the counter shows even if not asked for.
maxRowsnumberHow many rows it grows to while typing; beyond that the scrollbar shows. By default, 20. With the same number as `rows` the field does not grow.
onChange(value: string) => void—
onChangeEvent(event: ChangeEvent<HTMLTextAreaElement>) => voidLike `onChange`, but with the native event of the `<textarea>`: `e.target.value`, `e.currentTarget`... It exists to migrate a front end that already writes its handlers with the event (AMS-735). If you only want the text, `onChange` already gives it to you.
onMention(option: ListOption, trigger: string) => voidCalled when someone is picked from the list, with the character that opened it.
onMentionsChange(mentioned: readonly ListOption[]) => voidWho is mentioned in the text, after each change. What you want to store with the text to notify them.
onSearch(query: string, trigger: string) => voidTo search elsewhere: called with the text typed after the character and with the character, and `options` are no longer filtered here.
placeholderstring—
readOnlyboolean—
requiredboolean—
resize"none" | "vertical"Whether it can be resized by hand: downwards (`vertical`, the default) or not at all (`none`). Not sideways, which breaks the grid.
rowsnumberRows shown at first (1 to 12). As more is typed, it grows by itself up to `maxRows`.
size"sm" | "md" | "lg"—
successstringThe confirmation that what was typed is valid. An error or a warning take precedence over it.
triggerstring | readonly string[]The character that opens the list, or several (`['@', '#']`). By default, `@`.
validateSearch(query: string, trigger: string) => booleanWhether what is typed after the character is a search. With `false` the list does not open and `onSearch` is not called.
validationBehavior"aria" | "native"`aria` (the default) announces the state without the browser interrupting submission with its bubble; `native` also adds its own validation.
valuestring—
variantFieldVariant—
warningstringA warning that lets you go on, with its icon and colour, where the error would go. The field is still valid.

It also accepts the attributes of the element it renders: className, style, data-*, aria-* and its events.

In the test bench

Every state and variant, to try one at a time, on its page at storybook.amsetui.com: