0.9.10 • Published 10 months ago

react-easy-editables v0.9.10

Weekly downloads
48
License
MIT
Repository
-
Last release
10 months ago

React Easy Editables

Editable fields for inline content editing

Demo video of editable fields

Currently the package includes the following types of editable fields:

Content typeEditor type
Plain textText input
ParagraphRich text editor
NumberNumber input
ImageImage uploader with preview
Background ImageImage uploader with preview
FileFile uploader
LinkText inputs for URL and anchor

Check out the demo page to see the different types of editable fields in action.

You can also create custom editable areas by using the generic Editable component, as demonstrated further down.

Usage

The EditablesContext uses React's context API to provide the properties showEditingControls and theme to the editable components lower in the tree.

Each of the editable fields receives a content object and an onSave function that defines the content to be displayed/edited and the callback that receives the updated content.

Example

import React from 'react';
import { EditablesContext, theme } from 'react-easy-editables';

import PlainText from 'react-easy-editables';

class App extends React.Component {
  state = {
    showEditingControls: true,
    theme: theme,
    title: { text: "Editable title" },
  }

  handleSave = text => {
    this.setState({ title: { text } })
  }

  render() {
    const { title, ...rest } = this.state;

    return(
      <EditablesContext.Provider value={ {...rest} }>
        <h1>
          <PlainText content={title} onSave={this.handleSave} />
        </h1>
      </EditablesContext.Provider>
    );
  }
}

Theme

You can update the appearance of the editable fields by updating the theme object. These are the defaults:

export const theme = {
  primaryColor: "#FF6C45",
  fontFamily: "sans-serif",
  fontSize: 14,
  editContainer: {
    backgroundColor: "rgba(255,255,255,0.3)",
    border: "1px solid black",
    position: "relative",
    padding: "8px",
  },
  editContainerHighlight: {
    backgroundColor: "rgba(255,255,255,0.9)",
    border: "1px solid #FF6C45",
    zIndex: "2500",
  },
  actions: {
    position: "absolute",
    left: "4px",
    top: "-10px",
    display: "flex",
    alignItems: "center",
    zIndex: "99",
  },
  button: {
    border: "1px solid #000",
    color: "black",
    backgroundColor: "#fff",
    height: "18px",
    width: "18px",
    borderRadius: "30px",
    cursor: "pointer",
    display: "flex",
    justifyContent: "center",
    alignItems: "center",
    marginRight: "4px",
    "&:hover": {
      backgroundColor: "grey"
    }
  },
  saveButton: {
    backgroundColor: "#FF6C45",
  },
  cancelButton: {
    backgroundColor: "#FF6C45",
  },
  icon: {
    fontSize: "14px"
  }
};

Custom editable fields

You can create custom editable fields by using the generic Editable component. The Editable component does the following:

  • reads from context whether or not to show the editing controls
  • renders the content wrapped in the editing controls or just the content according to the showEditingControls property on the context
  • toggles the isEditing state which then renders the editor component
  • passes a content object to the editor component
  • passes the updated content object from the editor component back to the parent component when the save button is clicked

Example

import React from "react";
import Editable from "./Editable";
import PlainTextEditor from "react-easy-editables";

const EditableFeatureCard = props => {
  const handleSave = field => newContent => {
    props.updateContent(field, newContent);
  };

  const { content } = props;

  return (
    <div className="col-md-3 col-sm-12 service-sub text-center">
      <i className="icon-tools  icon-extra-large fast-yellow-text margin-seven no-margin-lr no-margin-top"></i>
      <span className="text-medium font-weight-600 letter-spacing-2 text-uppercase black-text margin-one no-margin-lr no-margin-top display-block alt-font">
        <Editable
          editor={PlainTextEditor}
          content={{ text: content.header }}
          handleSave={ handleSave("header") }
        >
          {content.header}
        </Editable>
      </span>

      <Editable
        editor={PlainTextEditor}
        content={{ text: content.description }}
        handleSave={ handleSave("description") }
      >
        <p className="text-medium width-80 center-col">{content.description}</p>
      </Editable>
    </div>
  );
};

export default EditableFeatureCard;

Component API

EditableText

import { EditableText } from "react-easy-editables"

Props

NameTypeDescription
contentobject (required)The content to be displayed or edited, in the shape { text: string }
onSavefunc (required)Callback function when editor is saved, receives the content object as the only argument
onDeletefuncCallback function when editable field is deleted. The delete button only renders if this prop is present.
classesstringString to be applied as the className property on the text and input.
EditorPropsobjectProps to be passed to the PlainTextEditor component that is rendered in editing mode.

EditableTextArea

import { EditableTextArea } from "react-easy-editables"

Props

NameTypeDescription
contentobject (required)The content to be displayed or edited, in the shape { text: string }. The string can include newline /n characters.
onSavefunc (required)Callback function when editor is saved, receives the content object as the only argument
onDeletefuncCallback function when editable field is deleted. The delete button only renders if this prop is present.
classesstringString to be applied as the className property on the text and input.
EditorPropsobjectProps to be passed to the TextAreaEditor component that is rendered in editing mode, ex. rows, cols, etc.

EditableParagraph

import { EditableParagraph } from "react-easy-editables"

Props

NameTypeDescription
contentobject (required)The content to be displayed or edited, in the shape { text: string }. The string can be HTML.
onSavefunc (required)Callback function when editor is saved, receives the content object as the only argument
onDeletefuncCallback function when editable field is deleted. The delete button only renders if this prop is present.
classesstringString to be applied as the className property on the text and input.
EditorPropsobjectProps to be passed to the RichTextEditor component that is rendered in editing mode. Under the hood it's the RichTextEditor from react-rte so check the documentation to see which additional props you can use.

EditableNumber

import { EditableNumber } from "react-easy-editables"

Props

NameTypeDescription
contentobject (required)The content to be displayed or edited, in the shape { text: number }
onSavefunc (required)Callback function when editor is saved, receives the content object as the only argument
onDeletefuncCallback function when editable field is deleted. The delete button only renders if this prop is present.
classesstringString to be applied as the className property on the text and input.
EditorPropsobjectProps to be passed to the NumberEditor component that is rendered in editing mode.

EditableLink

import { EditableLink } from "react-easy-editables"

Props

NameTypeDescription
contentobject (required)The content to be displayed or edited, in the shape { anchor: string, link: string }
onSavefunc (required)Callback function when editor is saved, receives the content object as the only argument
onDeletefuncCallback function when editable field is deleted. The delete button only renders if this prop is present.
classesstringString to be applied as the className property on the link and editor component.
EditorPropsobjectProps to be passed to the LinkEditor component that is rendered in editing mode. Since the editor has two inputs for the anchor text and the url, you can define props for each input via the EditorProps object. i.e. { anchor: object, link: object }

EditableImageUpload

import { EditableImageUpload } from "react-easy-editables"

Props

NameTypeDescription
contentobject (required)The content to be displayed or edited, in the shape { anchor: string, link: string }
onSavefunc (required)Callback function when editor is saved, receives the content object as the only argument
onDeletefuncCallback function when editable field is deleted. The delete button only renders if this prop is present.
showCaptionboolDisplays caption below image and enables caption editor if true
maxSizenumberMaximum size allowed for the uploaded image, in bytes. The default is 2097152, or 2MB.
stylesobjectStyles to be applied to the image container and the image itself, i.e { container: {}, image: {}
classesstringString to be applied as the className property on the image and editor components.
EditorPropsobjectProps to be passed to the ImageUploadEditor component that is rendered in editing mode. Since the editor has two inputs for the image upload and the caption, you can define props for each input via the EditorProps object. i.e. { image: object, caption: object }

EditableFileUpload

import { EditableFileUpload } from "react-easy-editables"

Props

NameTypeDescription
contentobject (required)The content to be displayed or edited, in the shape { anchor: string, link: string }
onSavefunc (required)Callback function when editor is saved, receives the content object as the only argument
onDeletefuncCallback function when editable field is deleted. The delete button only renders if this prop is present.
maxSizenumberMaximum size allowed for the uploaded file, in bytes. The default is 2097152, or 2MB.
classesstringString to be applied as the className property on the file and editor components.
EditorPropsobjectProps to be passed to the FileUploadEditor component that is rendered in editing mode.

EditableBackgroundImage

import { EditableBackgroundImage } from "react-easy-editables"

Props

NameTypeDescription
contentobject (required)The content to be displayed or edited, in the shape { anchor: string, link: string }
onSavefunc (required)Callback function when editor is saved, receives the content object as the only argument
onDeletefuncCallback function when editable field is deleted. The delete button only renders if this prop is present.
maxSizenumberMaximum size allowed for the uploaded file, in bytes. The default is 2097152, or 2MB.
classesstringString to be applied as the className property on the image and editor components.
stylesobjectStyles to be applied to the image
EditorPropsobjectProps to be passed to the ImageUploadEditor component that is rendered in editing mode, i.e. { image: object }

Editable

The generic Editable component allows you to create custom editable fields. It renders the EditorWrapper when the showEditingControls is true, and its children otherwise. The EditorWrapper shows the editor controls, and when the "edit" button is clicked it renders the editor component passed in through the Editor prop.

import { Editable } from "react-easy-editables"

Props

NameTypeDescription
Editorcomponent (required)The editor component to be rendered when editing mode is toggled
contentobject (required)The content to be displayed or edited
handleSavefunc (required)Callback function when editor is saved, receives the content object as the only argument
childrennode (required)Children to be rendered within the component
EditorPropsnumberProps to be passed to the editor component
onDeletefuncCallback function when editable field is deleted. The delete button only renders if this prop is present.
classesstringString to be applied as the className property on the editor components.
fullWidthboolWhen true, the editor wrapper takes up the full width of its container and editing controls are moved within the wrapper.
disableDeleteboolWhen true, the delete button is removed, even if the onDelete function is present
isContentClickTargetboolShould the element itself be a click target to start editing - defaults to true. When false, the edit button must be clicked to start editing.
0.9.10

10 months ago

0.9.9

2 years ago

0.9.8

3 years ago

0.9.7

3 years ago

0.9.6

3 years ago

0.9.5

3 years ago

0.9.4

3 years ago

0.9.3

3 years ago

0.9.2

3 years ago

0.9.1

3 years ago

0.9.0

3 years ago

0.8.15

3 years ago

0.8.14

4 years ago

0.8.13

4 years ago

0.8.12

4 years ago

0.8.11

4 years ago

0.8.10

4 years ago

0.8.9

4 years ago

0.8.8

4 years ago

0.8.7

4 years ago

0.8.6

4 years ago

0.8.5

4 years ago

0.8.4

4 years ago

0.8.3

4 years ago

0.8.1

4 years ago

0.8.2

4 years ago

0.8.0

4 years ago

0.7.11

4 years ago

0.7.10

4 years ago

0.7.9

4 years ago

0.7.8

4 years ago

0.7.7

4 years ago

0.7.6

4 years ago

0.7.5

4 years ago

0.7.2

4 years ago

0.7.4

4 years ago

0.7.3

4 years ago

0.7.1

4 years ago

0.7.0

4 years ago

0.6.2

5 years ago

0.6.1

5 years ago

0.6.0

5 years ago

0.5.6

5 years ago

0.5.5

5 years ago

0.5.4

5 years ago

0.5.3

5 years ago

0.5.2

5 years ago

0.5.1

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago