0bit-mentions

中文文档
0.0.1发布于 2022/01/29MITCommonJS零依赖

Mentions for 0bit

原始使用文档
文档目录7 节
  1. Installation
  2. Demo
  3. Usage
  4. How it works
  5. Props {property : type}
  6. Example
  7. License

react-native-mentions-editor npm version

Mentions TextInput for React Native. Tested on iOS and should work on Android as well. Because it's a plain Javascript base solution with some react-native TextInput support.

Installation

yarn add react-native-mentions-editor or npm install --save react-native-mentions-editor

If you love this component, give a star, you will be a ray of sunshine :)

Demo

alt text alt text alt text alt text alt text alt text

Usage

import Editor, { displayTextWithMentions} from 'react-native-mentions-editor';
const users = [ 
    { "id": 1, "name": "Raza Dar", "username": "mrazadar", "gender": "male"},
    { "id": 3, "name": "Atif Rashid", "username": "atif.rashid", "gender": "male"},
    { "id": 4, "name": "Peter Pan", "username": "peter.pan", "gender": "male"},
    { "id": 5, "name": "John Doe", "username": "john.doe", "gender": "male"}, 
    { "id": 6, "name": "Meesha Shafi", "username": "meesha.shafi", "gender": "female"}
];
<Editor 
    list={users} 
    initialValue={this.state.initialValue}
    clearInput={this.state.clearInput}
    onChange={this.onChangeHandler}
    showEditor={this.state.showEditor}
    toggleEditor={this.toggleEditor}
    showMentions={this.state.showMentions}
    onHideMentions={this.onHideMentions}
    ....
/>

const formatMentionNode = (txt, key)=> (
  <Text key={key} style={styles.mention}>
      {txt}
  </Text>
)

<Text style={styles.messageText}>
    {displayTextWithMentions(message.text, formatMentionNode)}
</Text>

How it works

This component allows you to @mention anywhere in the input value. (Not possible using react-native-mentions). Work nicely with selection and highlight of text. This component used special mark-up @[username](id:1) to differentiate mentions in the input value. Whenever input value change the onChange callback will be called, with an object containing two properties.

this.props.onChange({
    displayText: text,// displayText: "Hey @mrazadar this is good work"
    text: this.formatTextWithMentions(text) //text: "Hey @[mrazadar](id:1) this is good work" 
});

displayText Will have raw text user will see on the screen. You can see that in the comment. text Will have formatted text with some markup to parse mentions on the server and other clients. There is a function called displayTextWithMentions you can use this function to parse this mark-up with the parser function (Which format the mention node according to formatter function. Check the example app).

If you want to only parse mentions in the string but don't want to format them you can use this EditorUtils.findMentions function to actually parse the mentions in the string. This will parse special mark-up @[username](id:1) and gives you the exact positions and username and id for that mention. Which you can use for tagging / emailing purposes on the server etc. You can use this function as:

import { EU as EditorUtils } from 'react-native-mentions-editor';
EditorUtils.findMentions("Hey @[mrazadar](id:1) this is good work" );

//Check the definition of this function
findMentions: (val) => {
    /**
     * Both Mentions and Selections are 0-th index based in the strings
     * meaning their indexes in the string start from 0
     * findMentions finds starting and ending positions of mentions in the given text
     * @param val string to parse to find mentions
     * @returns list of found mentions 
     */
    let reg = /@\[([^\]]+?)\]\(id:([^\]]+?)\)/igm;
    let indexes = [];
    while (match = reg.exec(val)) {
        indexes.push({
            start: match.index, 
            end: (reg.lastIndex-1),
            username: match[1],
            userId: match[2],
            type: EU.specialTagsEnum.mention
        });
    }
    return indexes;
},

Props {property : type}

list: array This should be the list of objects to be used as options for the mentions list. Note This must contain id and username properties to uniqely identify object in the list.

initialValue: string Use this to initialize TextInput with the initial value. Usage. initalValue: "Hey @[mrazadar](id:1) this is good work"

clearInput: bool When true input will be clear automatically.

onChange: function This function will be called on input change event.

showEditor: bool Programmatically show/hide editor by using this property.

toggleEditor: function Use this to handle blur event on input.

showMentions: bool Use this property to programmatically trigger the mentionsList this will add @ character in the value.

onHideMentions: function This callback will be called when user stop tracking of mention.

placeholder: string placeholder for empty input.

renderMentionList: function If you want to render totally different list. You can use this property to provide alternative mention list renderer. It will be called with certain properties to controll the functionality of list.

renderMentionList Props: object

 mentionListProps= {
    list: props.list, //the default list you passed to this component
    keyword: state.keyword, //keyword to filter the list. e.g. `@m`
    isTrackingStarted: state.isTrackingStarted, // will be true if user started typing `@` 
    onSuggestionTap: this.onSuggestionTap.bind(this), //this function should be called once user press on the list item
    editorStyles: props.editorStyles, // these will be the props passed to the Editor component. 
};

editorStyles: object This object will contain the overriding styles for different nodes. Check the below object to see how you can override styles.

editorStyles: {
    mainContainer: {}, 
    editorContainer: {...}, 
    inputMaskTextWrapper: {},
    inputMaskText: {},
    input: {},
    mentionsListWrapper:{},
    mentionListItemWrapper: {} 
    mentionListItemTextWrapper: {},
    mentionListItemTitle: {}
    mentionListItemUsername: {}
}

Example

Check out the full example in example folder

License

MIT License. © Muhammad Raza Dar

版本与发布记录

0bit-mentions 共发布 1 个版本,首次发布于 2022/01/29;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.0.1latest2022/01/294 年前

关于 0bit-mentions 的常见问题

0bit-mentions 是什么?

提到0位

如何安装 0bit-mentions?

在项目目录运行 npm install 0bit-mentions,或使用 pnpm add / yarn add / bun add。

0bit-mentions 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/0bit-mentions 是否存在。

0bit-mentions 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

0bit-mentions 可以商用吗?

0bit-mentions 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

0bit-mentions 还在维护吗?

最新版本 0.0.1 发布于 2022/01/29(4 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/01/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。