Mention component is used to tag objects in a text.
import { Mention } from 'primereact/mention';
Mention is used as a controlled component with value and onChange properties. In addition, suggestions and a onSearch are required to query the results.
<Mention value={value} onChange={(e) => setValue(e.target.value)} suggestions={suggestions} onSearch={onSearch} field="nickname"
placeholder="Enter @ to mention people" rows={5} cols={40} itemTemplate={itemTemplate} />
It is used to define the expected keyword/s in the input field to mention someone or something.
<Mention value={value} onChange={(e) => setValue(e.target.value)} trigger={['@', '#']} suggestions={multipleSuggestions} onSearch={onMultipleSearch}
field={['nickname']} placeholder="Enter @ to mention people, # to mention tag" itemTemplate={multipleItemTemplate} rows={5} cols={40} />
When autoResize is enabled, textarea grows instead of displaying a scrollbar.
<Mention value={value} onChange={(e) => setValue(e.target.value)} suggestions={suggestions} onSearch={onSearch} field="nickname"
placeholder="Enter @ to mention people" rows={5} cols={40} itemTemplate={itemTemplate} autoResize />
A floating label appears on top of the input field when focused.
<span className="p-float-label">
<Mention inputId="newpost" value={value} onChange={(e) => setValue(e.target.value)} suggestions={suggestions} onSearch={onSearch}
field="nickname" rows={5} cols={40} itemTemplate={itemTemplate} />
<label htmlFor="newpost">New Post</label>
</span>
Invalid state style is added using the p-invalid class to indicate a failed validation.
<Mention value={value} onChange={(e) => setValue(e.target.value)} suggestions={suggestions} onSearch={onSearch} field="nickname"
placeholder="Enter @ to mention people" rows={5} cols={40} itemTemplate={itemTemplate} inputClassName="p-invalid" />
When disabled is present, the element cannot be edited and focused.
<Mention disabled />
Compatibility with popular React form libraries.
<Toast ref={toast} />
<Mention
id="item"
name="item"
field="nickname"
onSearch={onSearch}
placeholder="Please enter @ to mention people"
rows={5}
cols={40}
suggestions={suggestions}
itemTemplate={itemTemplate}
className={classNames({ 'p-invalid': isFormFieldInvalid('item') })}
value={formik.values.item}
onChange={(e) => {
formik.setFieldValue('item', e.target.value);
}}
/>
{getFormErrorMessage('item')}
<Button label="Submit" type="submit" icon="pi pi-check mt-2" />
React Hook Form is another popular React library to handle forms.
<Controller name="value" control={form.control}
rules={{ required: 'Value is required.' }} render={({ field, fieldState }) => (
<Mention
id={field.name}
field="nickname"
{...field}
rows={5}
cols={40}
className={classNames({ 'p-invalid': fieldState.error })}
suggestions={suggestions}
onSearch={onSearch}
placeholder="Please enter @ to mention people"
itemTemplate={itemTemplate}
/>)}
/>
Following is the list of structural style classes
Name | Element |
---|---|
p-mention | Container element |
p-mention-panel | Overlay panel of suggestions. |
p-mention-items | List container of suggestions. |
p-mention-item | List item of a suggestion. |
Value to describe the component can either be provided via label tag combined with inputId prop or using aria-labelledby, aria-label props. The input element has combobox role in addition to aria-autocomplete, aria-haspopup and aria-expanded attributes. The relation between the input and the popup is created with aria-controls and aria-activedescendant attribute is used to instruct screen reader which option to read during keyboard navigation within the popup list.
The popup list has an id that refers to the aria-controls attribute of the input element and uses listbox as the role. Each list item has option role and an id to match the aria-activedescendant of the input element.
<label htmlFor="men1">Username</label>
<Mention inputId="men1" />
<span id="men2">Email</span>
<Mention aria-labelledby="men2" />
<Mention aria-label="City" />
Key | Function |
---|---|
tab | Moves focus to the input element when popup is not visible. If the popup is open and an item is highlighted then popup gets closed, item gets selected and focus moves to the next focusable element. |
up arrow | Highlights the previous item if popup is visible. |
down arrow | Highlights the next item if popup is visible. |
enter | Selects the highlighted item and closes the popup if popup is visible. |
home | Highlights the first item if popup is visible. |
end | Highlights the last item if popup is visible. |
escape | Hides the popup. |