Skip to content

Repository files navigation

@rc-component/mentions

Ant Design Part of the Ant Design ecosystem.

💬 React mentions textarea with searchable suggestions and keyboard navigation.

NPM versionnpm downloadsbuild statusCodecovbundle sizedumi

English | 简体中文

Highlights

  • Textarea mentions with prefix based search and selection.
  • Keyboard navigation for suggestion menus.
  • Data-driven options API and Option children API.
  • Multiple prefixes, custom filtering, custom validation, and split token support.
  • Built on @rc-component/input, @rc-component/menu, and @rc-component/trigger.

Install

npm install @rc-component/mentions

The package is published as @rc-component/mentions. Legacy rc-mentions references should migrate to the scoped package name.

Usage

importMentionsfrom'@rc-component/mentions';constoptions=[{value: 'light',label: 'Light'},{value: 'bamboo',label: 'Bamboo'},{value: 'cat',label: 'Cat'},];exportdefault()=>(<Mentionsoptions={options}placeholder="Use @ to mention someone"/>);
importMentionsfrom'@rc-component/mentions';const{ Option }=Mentions;exportdefault()=>(<Mentionsprefix={['@','#']}><Optionvalue="design">Design</Option><Optionvalue="docs">Docs</Option></Mentions>);

Import the styles from assets/index.less or the compiled CSS generated by npm run compile.

Examples

Run the local dumi site:

npm install
npm start

Then open http://localhost:8000.

API

Mentions

PropertyTypeDefaultDescription
allowClearboolean | { clearIcon?: ReactNode }falseShow a clear button for the textarea value.
autoFocusbooleanfalseFocus the textarea when mounted.
autoSizeboolean | { minRows?: number; maxRows?: number }-Auto resize textarea height.
childrenReactNode-Mentions.Option children.
classNamestring-Class name for the root element.
classNamesMentionsProps['classNames']-Semantic class names for slots.
defaultValuestring-Initial value.
direction'ltr' | 'rtl'ltrPopup direction.
filterOptionfalse | (input: string, option: OptionProps) => boolean-Customize option filtering.
getPopupContainer() => HTMLElement-Container for the suggestion popup.
idstring-Textarea id.
notFoundContentReactNodeNot FoundContent shown when no option matches.
optionsDataDrivenOptionProps[]-Data-driven options.
placement'top' | 'bottom'bottomSuggestion popup placement.
popupClassNamestring-Class name for the popup.
prefixstring | string[]@Trigger prefix or prefixes.
prefixClsstringrc-mentionsClass name prefix.
rowsnumber1Textarea row count.
silentbooleanfalseIgnore Enter behavior during transition phases.
splitstring' 'String inserted after a selected mention.
styleReact.CSSProperties-Inline styles for the root element.
stylesMentionsProps['styles']-Semantic styles for slots.
transitionNamestring-Popup transition name.
validateSearch(text: string, split: MentionsProps['split']) => boolean-Customize trigger search logic.
valuestring-Controlled value.
onBlurReact.FocusEventHandler<HTMLTextAreaElement>-Triggered when textarea loses focus.
onChange(text: string) => void-Triggered when value changes.
onFocusReact.FocusEventHandler<HTMLTextAreaElement>-Triggered when textarea receives focus.
onKeyDownReact.KeyboardEventHandler<HTMLTextAreaElement>-Triggered on key down.
onKeyUpReact.KeyboardEventHandler<HTMLTextAreaElement>-Triggered on key up.
onPopupScroll(event: React.UIEvent<HTMLDivElement>) => void-Triggered when the popup scrolls.
onPressEnterReact.KeyboardEventHandler<HTMLTextAreaElement>-Triggered when Enter is pressed.
onResize(size: { width: number; height: number }) => void-Triggered when textarea size changes.
onSearch(text: string, prefix: string) => void-Triggered when a prefix starts a search.
onSelect(option: OptionProps, prefix: string) => void-Triggered when an option is selected.
popupRender(menu: React.ReactElement) => ReactNode-Customize the dropdown menu rendering

Option

PropertyTypeDefaultDescription
childrenReactNode-Option label.
classNamestring-Class name for the option.
disabledbooleanfalseDisable the option.
keystring-React key and option key.
styleReact.CSSProperties-Inline option style.
valuestring-Mention value inserted into the textarea.

Ref

importMentions,{typeMentionsRef}from'@rc-component/mentions';constref=React.useRef<MentionsRef>(null);ref.current?.focus();ref.current?.blur();
PropertyTypeDescription
focus() => voidFocus the textarea.
blur() => voidBlur the textarea.
textareaHTMLTextAreaElement | nullDeprecated textarea reference.
nativeElementHTMLElementRoot native element.

Development

npm install
npm start
npm test
npm run tsc
npm run compile
npm run build

The dumi site runs at http://localhost:8000 by default.

Release

npm run prepublishOnly

The release flow is handled by @rc-component/np through the rc-np command after the package build.

License

@rc-component/mentions is released under the MIT license.

About

💬 React mentions textarea with searchable suggestions and keyboard navigation.

Topics

Resources

Stars

112 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages