1.0.0 • Published 5 years ago

simple-rich-text-editor-in-react v1.0.0

Weekly downloads
5
License
Apache-2.0
Repository
github
Last release
5 years ago

react-contenteditable

React component for a div with editable contents

Build Status download count bundle size license

Install

npm install react-contenteditable

Usage

import React from 'react'
import ContentEditable from 'react-contenteditable'

class MyComponent extends React.Component {
  constructor() {
    super()
    this.contentEditable = React.createRef();
    this.state = {html: "<b>Hello <i>World</i></b>"};
  };

  handleChange = evt => {
    this.setState({html: evt.target.value});
  };

  render = () => {
    return <ContentEditable
              innerRef={this.contentEditable}
              html={this.state.html} // innerHTML of the editable div
              disabled={false}       // use true to disable editing
              onChange={this.handleChange} // handle innerHTML change
              tagName='article' // Use a custom HTML tag (uses a div by default)
            />
  };
};

Available props

propdescriptiontype
innerRefelement's ref attributeObject | Function
htmlrequired: innerHTML of the editable elementString
disableduse true to disable editingBoolean
onChangecalled whenever innerHTML changesFunction
onBlurcalled whenever the html element is blurredFunction
onKeyUpcalled whenever a key is releasedFunction
onKeyDowncalled whenever a key is pressedFunction
classNamethe element's CSS classString
stylea collection of CSS properties to apply to the elementObject

Examples

You can try react-contenteditable right from your browser to see if it fits your project's needs: