Coder Social home page Coder Social logo

clonardo / slate-autocomplete Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fedelochbaum/slate-autocomplete

0.0 1.0 0.0 1.09 MB

Slate autocomplete custom plugin

Home Page: https://www.npmjs.com/package/slate-autocomplete

CSS 3.99% HTML 4.65% JavaScript 91.36%

slate-autocomplete's Introduction

slate-autocomplete

A Slate plugin to suggestion replacements or actions based on input. Useful for implementing autocomplete suggestions by node type.


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:

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:

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.

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:

Collaboration

If you have some idea/suggestion or you found an error, please, let me know sending me an email: [email protected] or creating a new issue.

slate-autocomplete's People

Contributors

aunswjx avatar fedelochbaum avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.