1 | # [React Mentions](http://effektif.github.io/react-mentions)
|
2 |
|
3 | [![Build Status](https://travis-ci.org/effektif/react-mentions.svg?branch=master)](https://travis-ci.org/effektif/react-mentions)
|
4 | [![Dependency Status](https://david-dm.org/effektif/react-mentions.svg)](https://david-dm.org/effektif/react-mentions)
|
5 | [![npm version](https://badge.fury.io/js/react-mentions.svg)](http://badge.fury.io/js/react-mentions)
|
6 |
|
7 |
|
8 | A React component that let's you mention people in a textarea like you are used to on Facebook or Twitter.
|
9 |
|
10 | ##### Used in production at:
|
11 | - [Signavio](http://signavio.com)
|
12 | - [State](http://state.com)
|
13 | - [Swat.io](https://swat.io)
|
14 | - [GotDone](http://www.gotdone.me)
|
15 | - [Volinspire](https://volinspire.com)
|
16 |
|
17 | Please [let us know](mailto:wolf.pack@signavio.com?subject=we're%20using%20react-mentions%20at%20...) if you are using react-mentions, we'd love to add you to this list.
|
18 |
|
19 |
|
20 | ## Getting started
|
21 |
|
22 | Install the _react-mentions_ package via NPM:
|
23 |
|
24 | ```
|
25 | npm install react-mentions --save
|
26 | ```
|
27 |
|
28 |
|
29 | Require the _react-mentions_ package, which exports the two relevant React components for rendering the mentions textarea:
|
30 |
|
31 | ```javascript
|
32 | import { MentionsInput, Mention } from 'react-mentions'
|
33 | ```
|
34 |
|
35 | `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.
|
36 |
|
37 |
|
38 | Example:
|
39 |
|
40 | ```jsx
|
41 | <MentionsInput value={this.state.value} onChange={this.handleChange}>
|
42 | <Mention trigger="@"
|
43 | data={this.props.users}
|
44 | renderSuggestion={this.renderUserSuggestion} />
|
45 | <Mention trigger="#"
|
46 | data={this.requestTag}
|
47 | renderSuggestion={this.renderTagSuggestion} />
|
48 | </MentionsInput>
|
49 | ```
|
50 |
|
51 | You can find more examples here: [gh-pages/views/examples](https://github.com/effektif/react-mentions/tree/master/gh-pages/views/examples)
|
52 |
|
53 |
|
54 | ## Configuration
|
55 |
|
56 | The `MentionsInput` supports the following props for configuring the widget:
|
57 |
|
58 | | Prop name | Type | Default value | Description |
|
59 | |------------------|---------------------------------------------------------|----------------------------|------------------------------------------------------------------------------------------|
|
60 | | value | string | `''` | The value containing markup for mentions |
|
61 | | onChange | function (event, newValue, newPlainTextValue, mentions) | empty function | A callback that is invoked when the user changes the value in the mentions input |
|
62 | | markup | string | `'@[__display__](__id__)'` | A template string for the markup to use for mentions |
|
63 | | singleLine | boolean | `false` | Renders a single line text input instead of a textarea, if set to `true` |
|
64 | | displayTransform | function (id, display, type) | returns `display` | Accepts a function for customizing the string that is displayed for a mention |
|
65 | | onBlur | function (event, clickedSuggestion) | empty function | Passes `true` as second argument if the blur was caused by a mousedown on a suggestion |
|
66 |
|
67 |
|
68 | Each data source is configured using a `Mention` component, which has the following props:
|
69 |
|
70 | | Prop name | Type | Default value | Description |
|
71 | |------------------|------------------------|-----------------|-----------------------------------------------------------------------------|
|
72 | | trigger | regexp or string | `'@'` | Defines the char sequence upon which to trigger querying the data source |
|
73 | | type | string | `null` | Identifier for the data source, when using multiple data sources (optional) |
|
74 | | data | array or function (search, callback) | `null` | An array of the mentionable data entries (objects with `id` & `display` keys, or a filtering function that returns an array based on a query parameter |
|
75 | | renderSuggestion | function (entry, search, highlightedDisplay, index) | `null` | Allows customizing how mention suggestions are rendered (optional) |
|
76 | | onAdd | function (id, display) | empty function | Callback invoked when a suggestion has been added (optional) |
|
77 | | appendSpaceOnAdd | boolean | false | Append a space when a suggestion has been added (optional) |
|
78 | | allowSpaceInQuery | boolean | false | Keep suggestions open even if the user separates keywords with spaces. |
|
79 |
|
80 | 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.)
|
81 |
|
82 |
|
83 | ## Styling
|
84 |
|
85 | _react-mentions_ supports CSS 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 [gh-pages/views/examples/defaultStyle.js](https://github.com/effektif/react-mentions/tree/gh-pages/views/examples/defaultStyle.js). If you want to use CSS, simply assign a `className` prop to `MentionsInput`.
|
86 |
|
87 | You can also assign `className` and `style` props to the `Mention` components to define the mentions' highlighting.
|
88 |
|
89 |
|
90 | ## Contributing
|
91 |
|
92 | Spawn a development server with an example page and module hot loading all set up:
|
93 |
|
94 | ```
|
95 | npm start
|
96 | ```
|
97 |
|
98 | Update the examples page on Github Pages:
|
99 |
|
100 | ```
|
101 | npm run pages-publish
|
102 | ```
|