Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/abinavseelan/react-input-trigger
React component for handling character triggers inside textareas and input fields. 🐼
https://github.com/abinavseelan/react-input-trigger
javascript react react-component
Last synced: 18 days ago
JSON representation
React component for handling character triggers inside textareas and input fields. 🐼
- Host: GitHub
- URL: https://github.com/abinavseelan/react-input-trigger
- Owner: abinavseelan
- License: mit
- Created: 2018-02-27T01:23:40.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-03-01T20:23:28.000Z (over 1 year ago)
- Last Synced: 2024-05-16T18:05:32.148Z (6 months ago)
- Topics: javascript, react, react-component
- Language: JavaScript
- Homepage:
- Size: 1.52 MB
- Stars: 88
- Watchers: 6
- Forks: 20
- Open Issues: 18
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# React Input Trigger
[![npm][npm-badge]][npm-url]
[![license][license-badge]][license-url]
[![downloads][downloads-badge]][downloads-url]
[![size][size-badge]][size-url][![deps][deps-badge]][deps-url]
[![peer-deps][peer-deps-badge]][peer-deps-url]> React component for handling character triggers inside textareas and input fields. 🐼
## Description
Useful for building applications that need Slack-like emoji suggestions (triggered by typing `:`) or Github-like user mentions (triggered by typing `@`).
The component provides the following hooks:
* `onStart`: whenever the trigger is first activated (eg. when `@` is first typed).
* `onType`: when something is being typed after it's been triggered.
* `onCancel`: when the trigger is canceled.The hooks pass some meta-data such as the cursor position and/or the text that has been typed since the trigger has been activated.
![reactinputtrigger](https://user-images.githubusercontent.com/6417910/36937827-0e615e4e-1f3f-11e8-9623-c4141bda3d2e.gif)
## Demo
A live demo of this component can be found [here](https://abinavseelan.com/react-input-trigger).
A detailed guide on using this component to build a Github-style user mentions component [can be found on CampVanilla](https://blog.campvanilla.com/reactjs-input-trigger-github-twitter-mentions-8ad1d878110d).
## Usage
### Getting Started
* Install the component
```bash
$ npm install react-input-trigger
```* Import the component from the package.
```js
import InputTrigger from 'react-input-trigger';
```* Wrap your existing `` or `` element with ``
```jsx
```
---
Or get it in the browser directly via [unpkg](https://unpkg.com/react-input-trigger@latest/build/lib/react-input-trigger.js):
```html
```
## Component Props
`` can take in the following props:
### trigger
This prop takes an object that defines the trigger. The object can have the following properties
* `keyCode`: This is the character code that will fire the trigger.
* `shiftKey`: (Optional) Set this to `true` if you need the shift key to be pressed along with the `keyCode` to start the trigger. Ignore this property if it's not required.
* `ctrlKey`: (Optional) Set this to `true` if you need the ctrl key to be pressed along with the `keyCode` to start the trigger. Ignore this property if it's not required.
* `metaKey`: (Optional) Set this to `true` if you need the cmd key to be pressed along with the `keyCode` to start the trigger. Ignore this property if it's not required.```jsx
```
### onStart
This prop takes a function that will fire whenever trigger is activated. The function is passed some meta information about the cursor's position that you can use.
```jsx
{ console.log(obj); }}
>
```The parameter `obj` contains the following meta information
```js
{
"hookType": "start",
"cursor": {
"selectionStart",
"selectionEnd",
"top",
"left",
"height"
}
}
```### onCancel
This prop takes a function that will fire everytime the user presses backspace and removes the trigger from the input section. The function is passed some meta information about the cursor's position that you can use.
```jsx
{ console.log(obj); }}
>
```The parameter `obj` contains the following meta information
```js
{
"hookType": "cancel",
"cursor": {
"selectionStart",
"selectionEnd",
"top",
"left",
"height"
}
}
```### onType
This prop takes a function that will trigger everytime the user continues typing after starting the trigger. The function is passed some meta information about the cursor's position, as well as the text that the user has typed after triggering that you can use.
```jsx
{ console.log(obj); }}
>
```The parameter `obj` contains the following meta information
```js
{
"hookType": "typing",
"cursor": {
"selectionStart",
"selectionEnd",
"top",
"left",
"height"
},
"text"
}
```### endTrigger
This prop takes a function that returns a function that you need to keep in your parent component. This returned method needs to be called manually by the parent component whenever you are done using the trigger and want to end the trigger.
```jsx
{
this.endTriggerHandler = endTriggerHandler;/*
Now you can call `this.endTriggerHandler`
anywhere inside the parent component
whenever you want to stop the trigger!
*/
}
}
>
```## Contributing
Want to fix something, add a new feature or raise an issue? Please read the [contributing guide](https://github.com/abinavseelan/react-input-trigger/blob/master/CONTRIBUTING.md) to get started. :smile:
## Contributors
Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):
| [
Abinav Seelan](https://abinavseelan.com)
[💻](https://github.com/abinavseelan/react-input-trigger/commits?author=abinavseelan "Code") [📖](https://github.com/abinavseelan/react-input-trigger/commits?author=abinavseelan "Documentation") | [
Aditi Mohanty](https://github.com/rheaditi)
[💻](https://github.com/abinavseelan/react-input-trigger/commits?author=rheaditi "Code") [📖](https://github.com/abinavseelan/react-input-trigger/commits?author=rheaditi "Documentation") | [
Adam Goldman](https://github.com/goldylucks)
[💻](https://github.com/abinavseelan/react-input-trigger/commits?author=goldylucks "Code") |
| :---: | :---: | :---: |This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!
[npm-badge]: https://img.shields.io/npm/v/react-input-trigger.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/react-input-trigger
[license-badge]: https://img.shields.io/npm/l/react-input-trigger.svg?style=flat-square&color=blue
[license-url]: https://github.com/abinavseelan/react-input-trigger/blob/master/LICENSE
[downloads-badge]: https://img.shields.io/npm/dt/react-input-trigger.svg?style=flat-square&color=blue
[downloads-url]: https://www.npmjs.com/package/react-input-trigger
[deps-badge]: https://img.shields.io/david/abinavseelan/react-input-trigger.svg?style=flat-square
[deps-url]: https://david-dm.org/abinavseelan/react-input-trigger
[peer-deps-badge]: https://img.shields.io/david/peer/abinavseelan/react-input-trigger.svg?style=flat-square
[peer-deps-url]: https://david-dm.org/abinavseelan/react-input-trigger/peer-status
[size-badge]: https://img.shields.io/bundlephobia/minzip/react-input-trigger.svg?style=flat-square&label=gzipped
[size-url]: https://bundlephobia.com/result?p=react-input-trigger