react-rich-mentions

React library to handle @mentions, #channels, :smileys: and whatever with styles.
Getting started
Install the react-rich-mentions package via npm:
npm install react-rich-mentions --save
Or yarn:
yarn add react-rich-mentions
The package exports React components for rendering the mentions autocomplete and contenteditable :
import {
RichMentionsInput,
RichMentionsAutocomplete,
RichMentionsContext,
RichMentionsProvider,
} from 'react-rich-mentions';
RichMentionsProvider
- Feed it with your components and the mention configs
RichMentionsInput
- The div[contenteditable] used as TextField
RichMentionsAutocomplete
- The default Autocomplete component given with the library (can be overwritten)
RichMentionsContext
- Use it to create your own Autocomplete or custom controller.
Example:
const configs = [
{
match: /<(@\w+)\|([^>]+)>/g,
matchDisplay: '$1',
query: /@([a-zA-Z0-9_-]+)?/,
async onMention(text) {
const query = text.substr(1);
const results = await callYourAPI(query);
return results.map(user => ({
ref: `<@${user.nickname}|${user.id}>`,
name: user.nickname,
}));
},
customizeFragment(span: HTMLSpanElement, final: boolean) {
span.className = final ? 'final' : 'pending';
},
},
];
const MyComponent = () => {
const ref = useRef();
const onClear = () => ref.current.setValue('');
const onSubmit = () => {
console.log(ref.current.getTransformedValue());
};
return (
<RichMentionsProvider configs={configs} getContext={ref}>
<RichMentionsInput defaultValue="The default Text" />
<RichMentionsAutocomplete fixed={false} />
<button type="button" onClick={onSubmit}>
Send
</button>
<button type="reset" onClick={onClear}>
Clear
</button>
</RichMentionsProvider>
);
};
RichMentionsInput props
defaultValue | string | '' | The default value of the input (cannot be updated) |
RichMentionsAutocomplete props
fixed | boolean (optional) | false | Is the autocomplete on a fixed position element ? |
RichMentionsProvider props
configs | TMentionConfig[] | undefined | List of configs to fetch mentions |
getContext | function (optional) | undefined | Get rich mention context (can be used with a useRef) |
getInitialHTML | function (optional) | undefined | Can be used to overwrite the function used to preprocess value data |
RichMentionsPublicContext props
The context returned by getContext
props.
getTransformedValue | function | const text = ctx.getTransformedValue() | Get the input value with fragment transformed to valid code |
setValue | function | ctx.setValue('Hello <@world|U15151>') | Change the input value, will transform the code with valid fragment |
insertFragment | function | ctx.insertFragment('<@world|U45454>') | Add a fragment at the current cursor position |
Building Locally
After cloning the repository, install all dependencies :
yarn
or
npm install
Testing
To test this project, we use cypress : https://docs.cypress.io/guides/overview/why-cypress.html
cd ./examples
yarn (OR npm install)
cd ..
yarn cypress:headless
If you develop a new feature, be sure to add tests in the cypress
folder, following documentation from the above website.