React-mentions 库介绍

React-mentions是一个React库,用于在textarea中实现类似微博评论的@用户功能。它包括MentionsInput和Mention两个组件,支持自定义触发器、数据源和渲染建议。配置包括输入框和提及组件的属性,如触发器、数据、渲染方法等。通过npm安装后,可以方便地集成到React应用中,提高开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

React-mentions 库介绍

最近需要做一个类似微博评论功能@用户的功能,手写JS的效率很低,所以使用 React-mentions 库实现这个功能。看了一下网上没中文介绍,所以自己看完英文介绍简单总结一个中文介绍。

使用

1、安装

npm 安装

npm install react-mentions --save

2、导入

The package exports two React components for rendering the mentions textarea:

可以使用两个React组件来渲染 textarea

import { MentionsInput, Mention } from 'react-mentions';

MensionsInput 父组件是主要的渲染输入框(textarea),内部有一个或者多个 Mension 子组件,每一个子组件代表特定的提及的数据对象(用户、问题、模板变量等,使用不同的提示符号)。例如:

<MentionsInput value={this.state.value} onChange={this.handleChange}>
  <Mention
    trigger="@"
    data={this.props.users}
    renderSuggestion={this.renderUserSuggestion}
  />
  <Mention
    trigger="#"
    data={this.requestTag}
    renderSuggestion={this.renderTagSuggestion}
  />
</MentionsInput>

更多链接请点击:https://github.com/signavio/react-mentions/tree/master/demo/src/examples

配置

1、父组件

MentionsInput 支持下面的 props:

Props 名称类型Default valueDescription
valuestring''输入框的内容
onChangefunction (event, newValue, newPlainTextValue, mentions)empty function输入框变化的回调函数
singleLinebooleanfalse渲染一个单下划线的输入框还是一个 textarea。
onBlurfunction (event, clickedSuggestion)empty function输入框失去焦点回调函数
allowSpaceInQuerybooleanfalse查询关键词中间是否支持空格
suggestionsPortalHostDOM ElementundefinedRender suggestions into the DOM in the supplied host element.
inputRefReact refundefinedref 可以获取DOM元素

2、子组件

每一个数据源都使用 Mention 子组件,具有下面的 props 配置

Prop nameTypeDefault valueDescription
triggerregexp or string'@'Defines the char sequence upon which to trigger querying the data source 触发器,很重要
dataarray or function (search, callback)nullAn array of the mentionable data entries (objects with id & display keys, or a filtering function that returns an array based on a query parameter 一个数组或者一个过滤函数,可以根据一个查询参数返回一个数组的函数,很重要
renderSuggestionfunction (entry, search, highlightedDisplay, index, focused)nullAllows customizing how mention suggestions are rendered (optional) 可选参数,允许自定义如何渲染文本建议
markupstring'@[__display__](__id__)'A template string for the markup to use for mentions 用于标记的模板字符串(用于@用户)
displayTransformfunction (id, display)returns displayAccepts a function for customizing the string that is displayed for a mention 自定义字符串来设置提醒(例如使用 foo 代替 @)
regexRegExpautomatically derived from markup patternAllows providing a custom regular expression for parsing your markup and extracting the placeholder interpolations (optional)
onAddfunction (id, display)empty functionCallback invoked when a suggestion has been added (optional) 当建议选项被添加后的回调函数
appendSpaceOnAddbooleanfalseAppend a space when a suggestion has been added (optional) 当候选人被添加后,加入一个空格(默认不加入)

如果data参数中传入一个函数,这个函数的第一个参数是当前的搜索函数,第二个参数是回调函数。回调函数可以异步提供结果(如果用户的输入发生变化,回调函数可以多次调用,更新候选人菜单)。

样式

组件支持行内样式,外联样式等。如果你想使用CSS,只需要在父组件上设置一个 className ,所有的DOM组件都会加上这个前缀。

源码

下面是源码链接

github 链接:https://github.com/signavio/react-mentions

npm 链接:https://www.npmjs.com/package/react-mentions

思路

刚开始自己手动写这个功能的思路(基于React框架):

1、捕获textarea区域的输入事件,当 event.target.keyCode 是对应的键(例如@时),打开一个下拉菜单。

2、继续捕获用户输入,将当前@后面输入的字符获取到,调用后端API搜索符合条件的用户,将获取的结果数组展示到下拉菜单中。如果没有结果下拉菜单中显示”没有找到这个用户“。

3、如果用户继续输入,重复步骤二;如果用户点击菜单中某个选项,调用后端API将这个用户的信息发送即可,关闭下拉菜单。

难点:如果@多人,使用字符串切割关键词可能出现问题;如果用户频繁输入删除,需要大量后端请求(现在没有设置缓存)性能不佳。

英文文档

A React component that let's you mention people in a textarea like you are used to on Facebook or Twitter.

Install the react-mentions package via npm:

npm install react-mentions --save

The package exports two React components for rendering the mentions textarea:

import { MentionsInput, Mention } from 'react-mentions'

MentionsInput is the main component rendering the textarea control. It takes one or multiple Mention components as its children. Each Mention component represents a data source for a specific class of mentionable objects, such as users, template variables, issues, etc.

Example:

<MentionsInput value={this.state.value} onChange={this.handleChange}>
  <Mention
    trigger="@"
    data={this.props.users}
    renderSuggestion={this.renderUserSuggestion}
  />
  <Mention
    trigger="#"
    data={this.requestTag}
    renderSuggestion={this.renderTagSuggestion}
  />
</MentionsInput>

You can find more examples here: demo/src/examples

Configuration

The MentionsInput supports the following props for configuring the widget:

Prop nameTypeDefault valueDescription
valuestring''The value containing markup for mentions
onChangefunction (event, newValue, newPlainTextValue, mentions)empty functionA callback that is invoked when the user changes the value in the mentions input
singleLinebooleanfalseRenders a single line text input instead of a textarea, if set to true
onBlurfunction (event, clickedSuggestion)empty functionPasses true as second argument if the blur was caused by a mousedown on a suggestion
allowSpaceInQuerybooleanfalseKeep suggestions open even if the user separates keywords with spaces.
suggestionsPortalHostDOM ElementundefinedRender suggestions into the DOM in the supplied host element.
inputRefReact refundefinedAccepts a React ref to forward to the underlying input element

Each data source is configured using a Mention component, which has the following props:

Prop nameTypeDefault valueDescription
triggerregexp or string'@'Defines the char sequence upon which to trigger querying the data source
dataarray or function (search, callback)nullAn array of the mentionable data entries (objects with id & display keys, or a filtering function that returns an array based on a query parameter
renderSuggestionfunction (entry, search, highlightedDisplay, index, focused)nullAllows customizing how mention suggestions are rendered (optional)
markupstring'@[__display__](__id__)'A template string for the markup to use for mentions
displayTransformfunction (id, display)returns displayAccepts a function for customizing the string that is displayed for a mention
regexRegExpautomatically derived from markup patternAllows providing a custom regular expression for parsing your markup and extracting the placeholder interpolations (optional)
onAddfunction (id, display)empty functionCallback invoked when a suggestion has been added (optional)
appendSpaceOnAddbooleanfalseAppend a space when a suggestion has been added (optional)

If a function is passed as the data prop, that function will be called with the current search query as first, and a callback function as second argument. The callback can be used to provide results asynchronously, e.g., after fetch requests. (It can even be called multiple times to update the list of suggestions.)

Styling

react-mentions supports css, css modules, and inline styles. It is shipped with only some essential inline style definitions and without any css. Some example inline styles demonstrating how to customize the appearance of the MentionsInput can be found at demo/src/examples/defaultStyle.js.

If you want to use css, simply assign a className prop to MentionsInput. All DOM nodes rendered by the component will then receive class name attributes that are derived from the base class name you provided.

If you want to avoid global class names and use css modules instead, you can provide the automatically generated class names as classNames to the MentionsInput. See demo/src/examples/CssModules.js for an example of using react-mentions with css modules.

You can also assign className and style props to the Mention elements to define how to highlight the mentioned words.

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值