6.0.0 • Published 1 year ago

react-editext v6.0.0

Weekly downloads
2,486
License
MIT
Repository
github
Last release
1 year ago

react-editext

Editable Text Component for React Applications

NPM npm Github codecov Quality Gate Status Github All Contributors

This project is generated from react-typescript-library template.

Install

npm install --save react-editext

Or with yarn:

yarn add react-editext

Usage

EdiText is highly customizable. You can see more examples here. Here is a basic usage:

import React, { useState } from 'react';

import EdiText from 'react-editext';

function Example(props) {
  const [value, setValue] = useState('What is real? How do you define real?');

  const handleSave = (val) => {
    console.log('Edited Value -> ', val);
    setValue(val);
  };
  return (
    <div className="container">
      <EdiText type="text" value={value} onSave={handleSave} />
    </div>
  );
}

There is also a codesandbox template that you can fork and play with it:

Edit react-editext-template

You can customize almost everything based on your needs. Please navigate to Props section. I mean, just scroll down.

Props

PropTypeRequiredDefaultNote
valuestringYesValue of the content and input in edit mode
onSavefunctionYesFunction will be called when save button clicked. value and inputProps are passed to cb.
typestringNotextInput type. Possible options are: text, password, number, email, textarea, date, datetime-local, time, month, url, week, tel
hintnodeNo''A simple hint message appears at the bottom of input element. Any valid element is allowed.
inputPropsobjectNoProps to be passed to input element. Any kind of valid DOM attributes are welcome.
viewPropsobjectNoProps to be passed to div element that shows the text. You can specify your own styles or className
containerPropsobjectNoProps to be passed to div element that is container for all elements. You can use this if you want to style or select the whole container.
editButtonPropsobjectNoProps to be passed to edit button.
validationfunctionNoPass your own validation function. takes one param -> value. It must return true or false
validationMessagenodeNoInvalid ValueIf validation fails this message will appear
onValidationFailfunctionNoPass your own function to track when validation failed. See Examples page for the usage.
onCancelfunctionNoFunction will be called when editing is cancelled. value and inputProps are passed as params.
saveButtonContentnodeNo''Content for save button. Any valid element is allowed. Default is:
cancelButtonContentnodeNo''Content for cancel button. Any valid element is allowed. Default is:
editButtonContentnodeNo''Content for edit button. Any valid element is allowed. Default is:
saveButtonClassNamestringNoCustom class name for save button.
cancelButtonClassNamestringNoCustom class name for cancel button.
editButtonClassNamestringNoCustom class name for edit button.
viewContainerClassNamestringNoCustom class name for the container in view mode.See here
editContainerClassNamestringNoCustom class name for the container in edit mode. Will be set to viewContainerClassName if you set it and omit this. See here
mainContainerClassNamestringNodeprecated. Custom class name for the top-level main container. See here
hideIconsboolNofalseSet it to true if you don't want to see default icons on action buttons. See Examples page for more details.
buttonsAlignstringNoafterSet this to before if you want to locate action buttons before the input instead of after it. See here.
editOnViewClickboolNofalseSet it to true if you want clicking on the view to activate the editor.
editingboolNofalseSet it to true if you want the view state to be edit mode.
onEditingStartfunctionNoFunction that will be called when the editing mode is active. See here
showButtonsOnHoverboolNofalseSet it to true if you want to display action buttons only when the text hovered by the user. See here
submitOnEnterboolNofalseSet it to true if you want to submit the form when Enter is pressed. Be careful if you have multiple instances of <EdiText/> on the same page.
cancelOnEscapeboolNofalseSet it to true if you want to cancel the form when Escape is pressed. See here
cancelOnUnfocusboolNofalseSet it to true if you want to cancel the form when clicked outside of the input. See here
submitOnUnfocusboolNofalseSet it to true if you want to submit the form when clicked outside of the input. See here
startEditingOnFocusboolNofalseActivates the edit mode when the view container is in focus. See here
startEditingOnEnterboolNofalseActivates the edit mode when the Enter key is pressed. See here
tabIndexnumberNoAn helper shortcut in case you want to pass the same tabIndex to both viewProps and inputProps.
renderValuefunctionNoCustom render method for the content in the view mode.Use this prop to customize the displayed value in view mode. See here
canEditfunction or booleanNotrueA function or boolean prop that returns a boolean. If it returns true the input will be editable. If it returns false the input will be read-only.

Styling with styled-components

You can style your <EdiText/> components with styled-components or similar libraries. You can either target internal HTML elements by their type ( or order) , or you can select them by attribute values.

Each customizable HTML element has a editext=xxx attribute. Use below as a reference table:

Attr. ValueDescription
main-containerthe main container
view-containerthe container in view mode
edit-containerthe container in edit mode
button-containerthe container for the save and cancel buttons
validation-containerthe container for validation message area
edit-buttonuse this to style the edit button
save-buttonuse this to style the save button
cancel-buttonuse this to style the cancel button
inputThere is only one input. You can select it directly or just use this attr value.
hintTo style the hint container.

Usage:

button[editext='cancel-button'] {
  &:hover {
    background: crimson;
    color: #fff;
  }
}

div[editext='view-container'] {
  background: #6293c3;
  padding: 15px;
  border-radius: 5px;
  color: #fff;
}

div[editext='validation-container'] {
  color: #d3d3d3;
  text-decoration: underline;
}

input,
textarea {
  /** or input[editext="input"] {} */
  background: #1d2225;
  color: #f4c361;
  font-weight: bold;
  border-radius: 5px;
}

See the example code.

Browser Support

ChromeFirefoxSafariiOS SafariOperaEdge
:white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark::white_check_mark:

Development

With Nix

nix-shell --pure
develop

This will start a tmux session for both library and example app.

Without Nix

Install dependencies and start the rollup watch process.

yarn install
yarn start

Navigate to example/ folder and repeat the above steps.

cd example
yarn install
yarn start

Now open http://localhost:3000/ in your browser.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!

License

MIT © alioguzhan

6.0.0

1 year ago

5.1.0

2 years ago

5.0.2

2 years ago

5.0.1

2 years ago

5.0.0

2 years ago

4.2.1

3 years ago

4.2.0

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago

4.1.4

3 years ago

4.1.3

3 years ago

4.1.5

3 years ago

4.1.0

3 years ago

4.1.2

3 years ago

4.1.1

3 years ago

3.18.0

3 years ago

3.17.2

3 years ago

3.17.1

4 years ago

3.17.0

4 years ago

3.16.1

4 years ago

3.16.0

4 years ago

3.15.1

4 years ago

3.15.0

4 years ago

3.14.0

4 years ago

3.13.0

4 years ago

3.12.3

4 years ago

3.12.4

4 years ago

3.12.2

4 years ago

3.12.1

4 years ago

3.12.0

4 years ago

3.11.1

4 years ago

3.11.0

4 years ago

3.10.0

4 years ago

3.9.1

5 years ago

3.9.0

5 years ago

3.8.1

5 years ago

3.8.0

5 years ago

3.7.0

5 years ago

3.6.1

5 years ago

3.6.0

5 years ago

3.5.0

5 years ago

3.4.1

5 years ago

3.4.0

5 years ago

3.3.1

5 years ago

3.3.0

5 years ago

3.2.1

5 years ago

3.2.0

5 years ago

3.1.2

5 years ago

3.1.1

5 years ago

3.1.0

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.1.2

6 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago