GithubHelp home page GithubHelp logo

insin / react-plain-editable Goto Github PK

View Code? Open in Web Editor NEW
21.0 3.0 2.0 251 KB

[DEPRECATED] React component for editing plain text via contentEditable

Home Page: http://insin.github.io/react-plain-editable/

License: Other

JavaScript 50.10% HTML 49.90%

react-plain-editable's Introduction

[DEPRECATED] This component is unnecessary, use an <input type="text"/> or something like react-textarea-autosize with rows="1" instead

react-plain-editable

A PlainEditable React component which uses contentEditable to edit plain text.

Note: contentEditable seems like an inconsistent mess across browsers and this has only been tested in the latest stable Firefox (35), Chrome (40) and Internet Explorer (11) with a <div> container with an unaltered CSS display style.

Pull requests and compatilbility issue reports to help improve this component are welcome!

You can also see PlainEditable in action in ideas-md, a float-to-the-top ideas log app.

Install

npm

PlainEditable can be used on the server, or bundled for the client using an npm-compatible packaging system such as Browserify or webpack.

npm install react-plain-editable --save

Browser bundle

The browser bundle exposes a global PlainEditable variable and expects to find a global React variable to work with.

You can find it in the /dist directory.

Usage

Provide PlainEditable with at least an onBlur or an onChange callback function to get input data back at the desired time, and provide any initial value as a value prop.

var Editor = React.createClass({
  _onBlur(e, value) {
    this.props.onChange(value)
  },

  render() {
    <PlainEditable onBlur={this._onBlur} value={this.props.value}/>
  }
})

For Internet Explorer (and any other browser which generates <p> elements in a contentEditable), you must set up a CSS rule to make <p> elements have the same visual effect as a <br>:

.PlainEditable p {
  margin: 0;
}

API

PlainEditable component

PlainEditable is implemented as an "uncontrolled" component which uses contentEditable to edit a given value - i.e. changes to the initial value prop passed to it will not be reflected.

It expects to be given a plain text value and will provide edited input back as plain text via its onBlur and onChange callbacks.

Leading & trailing whitespance is trimmed in the returned text by default. This can be disabled by using the noTrim prop.

Multi-line editing is enabled by default. You can restrict editing to a single line by using the singleLine prop.

The component attempts to work around the most obvious contentEditable quirks, but bugs are likely due to the nature of how contentEditable has been implemented across various browsers.

Props

Note: any props passed in addition to those documented below will be passed to the component created in PlainEditable's render() method - if you need to give your contentEditable an id, data-, or any other additional props, just pass them as you normally would.

value: String

Initial value to be displayed in the contentEditable.

PlainEditable is currently implemented as an "uncontrolled" component - i.e. changes to the initial value prop given to it will not be reflected in the contentEditable.

onBlur: Function(event: SyntheticEvent, value: String)
onChange: Function(event: SyntheticEvent, value: String)

These callback props are used to receive edited values from the contentEditable via the value argument when the appropriate event fires.

If onChange is given, the input event is used to trigger the callback on every change.

Since Internet Explorer doesn't currently support input on contentEditables, the keydown and keyup events are used to trigger the onChange callback for it instead.

autoFocus: Boolean

If true when the component mounts, the contentEditable will be given focus.

className: String

An additional CSS class to append to the default PlainEditable CSS class.

component: String|ReactCompositeComponent (default: 'div')

The HTML tag name or React component to be created for use as a contentEditable in PlainEditable's render() method.

noTrim: Boolean

Pass this prop to disable trimming of leading and trailing whitespace in text passed to the onBlur and onChange callbacks.

<PlainEditable onBlur={this._onBlur} noTrim/>
onFocus: Function(event: SyntheticEvent, selecting: Boolean)

This callback prop is accepted because this event is already used with the contentEditable to implement selection of placeholder content.

The selecting argument will be true if the contentEditable's contents will be selected after giving the browser a chance to complete other operations.

onKeyDown: Function(event: SyntheticEvent)
onKeyUp: Function(event: SyntheticEvent)

These callback props are accepted because these events are already used with the contentEditable to make onChange work in IE.

If you're using IE and you prevent the evant's default action using event.preventDefault(), onChange will not be triggered.

placeholder: String

If provided, the contents of the contentEditable will be selected if they match this prop when it gains focus.

This can be used to make it more convenient for users to edit an initial value you provide as a placeholder.

singleLine: Boolean

Pass this prop to disable entry of linebreaks into the contentEditable by pressing the Enter key, which will force a blur().

Linebreaks can still be pasted in, but will be replaced with spaces in text passed to the onBlur and onChange callbacks.

<PlainEditable onBlur={this._onBlur} singleLine/>
spellcheck: String (default: 'false')

Coinfig for the contentEditable's spellcheck prop, which is disabled by default.

Methods

focus()

Gives focus to the contentEditable DOM node.

MIT Licensed

react-plain-editable's People

Contributors

insin avatar winkler1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

winkler1 openube

react-plain-editable's Issues

Single-line editing prop

Add a prop to force a blur() when event.key == 'Enter'

This should also either attempt to detect multi-line pastes (which I assume is fraught with cross-browser peril because contentEditable) and designDecision(chop everything but the first line | join the lines), or check for linebreaks in _onInput() and update innerHTML to remove them.

Make better use of the placeholder prop

Automatically use placeholder as innerHTML when html is empty or equal to DEFAULT_CONTENTEDITABLE_HTML.

Set innerHTML to DEFAULT_CONTENTEDITABLE_HTML on focus if it's currently equal to placeholder.

Return a blank value if innerHTML is equal to placeholder onChange or onBlur.

Give the container an extra modifier in className if it's currently displaying the placeholder value: PlainEditable--placeholder.

Support actual plain text editing

Currently, incoming and outgoing html is expected to have <br> and ampersand-encoded entities present.

This is just because I didn't check how contentEditable handles encoding of special characters other than the obvious < > &, which could use the usual kind of HTML escape on the way in/unescape on the way out.

Go the full hog and expect/provide plain text with regular linebreaks for input/output

Convert pasted rich text to plain text

Current flat-out stripping of tags loses linebreaks and leads to other weird spacing issues when rich text is pasted.

Searching around suggests implementing this is fraught with pain and terror.

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.