# slate-autocomplete

> A Slate plugin to display autocomplete suggestions.

Latest version **0.0.9** (published 2019-07-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install slate-autocomplete
pnpm add slate-autocomplete
yarn add slate-autocomplete
bun add slate-autocomplete
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.9 |
| Published | 2019-07-08 |
| First published | 2019-06-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 12 |
| Unpacked size | 829.8 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 11 |
| Maintainers | fedelochbaum |
| Keywords | slate, slate-editor, autocomplete, autosuggest, suggestions, custom, plugin |

## Links

- npm: https://www.npmjs.com/package/slate-autocomplete
- Repository: https://github.com/FedeLochbaum/slate-autocomplete
- Homepage: https://github.com/FedeLochbaum/slate-autocomplete/#readme
- Issues: https://github.com/FedeLochbaum/slate-autocomplete/issues
- npm.io page: https://npm.io/package/slate-autocomplete

## Dependencies (12)

- [ramda](https://npm.io/package/ramda.md) ^0.26.1
- [react](https://npm.io/package/react.md) ^16.8.6
- [slate](https://npm.io/package/slate.md) ^0.47.4
- [immutable](https://npm.io/package/immutable.md) ^4.0.0-rc.12
- [node-sass](https://npm.io/package/node-sass.md) ^4.12.0
- [react-dom](https://npm.io/package/react-dom.md) ^16.8.6
- [recompose](https://npm.io/package/recompose.md) ^0.30.0
- [classnames](https://npm.io/package/classnames.md) ^2.2.6
- [slate-react](https://npm.io/package/slate-react.md) ^0.22.4
- [babel-loader](https://npm.io/package/babel-loader.md) ^8.0.6
- [react-portal](https://npm.io/package/react-portal.md) ^4.2.0
- [escape-string-regexp](https://npm.io/package/escape-string-regexp.md) ^2.0.0

## Alternatives

- [ext-list](https://npm.io/package/ext-list.md) — 6.3M weekly downloads
- [@lexical/selection](https://npm.io/package/@lexical/selection.md) — 3.8M weekly downloads
- [@lexical/text](https://npm.io/package/@lexical/text.md) — 3.6M weekly downloads
- [@lexical/clipboard](https://npm.io/package/@lexical/clipboard.md) — 3.0M weekly downloads
- [@tiptap/extension-mention](https://npm.io/package/@tiptap/extension-mention.md) — 3.0M weekly downloads

## Recent versions

- 0.0.9 (latest) — 2019-07-08
- 0.0.8 — 2019-06-27
- 0.0.7 — 2019-06-27
- 0.0.6 — 2019-06-19
- 0.0.5 — 2019-06-19
- 0.0.4 — 2019-06-18
- 0.0.3 — 2019-06-18
- 0.0.2 — 2019-06-16
- 0.0.1 — 2019-06-16

## README

<h3 align="center"><code>slate-autocomplete</code></h3>

A [**Slate**](https://github.com/ianstormtaylor/slate) plugin to suggestion replacements or actions based on input. Useful for implementing autocomplete suggestions by node type.

<p align="center"><img src="./demo.gif"></p>

---

### Install

With npm:

```
npm install --save slate-autocomplete
```

With yarn:

```
yarn add slate-autocomplete
```

_You will need to have installed `slate` as a dependency already._

---

### Usage

### Define your custom plugin:

#### With static suggestions:
```js
import autoCompletePlugin from 'slate-autocomplete'

const suggestions = [
  "Afganistán",
  "Albania",
  "Argentina",
  ...
]

export default autoCompletePlugin({
  suggestions,
  resultSize: 5,
  totalText: true,
  shouldHandleNode: (editor, currentNode) => true,
  onEnter: (suggestion, editor) => {
    replaceCurrentText(editor, suggestion)
  }
})
```

#### With dynamic suggestions:
```js
import autoCompletePlugin from 'slate-autocomplete'

export default autoCompletePlugin({
  renderPortal: (Portal, props) => (
    <YourDynamicComp>
      {names => (<Portal {...props} suggestions={names} />)}
    </YourDynamicComp>
  ),
  shouldHandleNode: (editor, currentNode) => true,
  totalText: false,
  onEnter: (suggestion, editor) => {
    replaceCurrentText(editor, suggestion)
  }
})
```
#### In this case, you need define `renderPortal` instead of `suggestions`.

```js
import customPlugin from 'your_plugin_path'
import { Editor } from 'slate-react'

const Example = ({ value, onChange, renderNode }) => (
  <React.Fragment>
    <Editor
      value={value}
      plugins={[customPlugin]}
      onChange={onChange}
      renderNode={renderNode}
    />
    {plugins.filter(({ component }) => !!component).map(({ component: Comp }, index) => <Comp key={index} />)}
  </React.Fragment>
)
```

Option | Type | Optional | Description
--- | --- | --- | ---
**`suggestions`** | `Array` | Yes | An array of suggestions.
**`totalText`** | `Boolean` | Yes | A boolean used to kwow if the suggestions should be applied to complete node text or only to the current word.
**`resultSize`** | `Number` | Yes | An optional number use to set size of suggestion result.
**`renderPortal`** | `Function` | Yes | A function use to wrap the portal component with dynamics suggestions. 
**`onEnter`** | `Function` | No | A function use to handle return/enter keypress to append suggestion into editor.
**`shouldHandleNode`** | `Function` | No | A function use to know if the current slate node should be handled.

---

### Development

Clone the repository and then run:

```
yarn
yarn storybook
```

You should see something like:

<p align="center"><img src="./storybook.gif"></p>



### Collaboration

If you have some idea/suggestion or you found an error, please, let me know sending me an email: federico.lochbaum@gmail.com or creating a new issue.

---
_Source: https://npm.io/package/slate-autocomplete · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
