Giter Club home page Giter Club logo

react-autosize-textarea's Introduction

React Autosize Textarea

A light replacement for built-in <textarea /> component which automatically adjusts its height to match the content.

NB: It does not require any polyfill

import ReactDOM from 'react-dom';
import TextareaAutosize from 'react-autosize-textarea';

ReactDOM.renderComponent(
  <TextareaAutosize {...textareaProps} onResize={(e) => {}} />,
  document.body
);

Install

npm install --save react-autosize-textarea
yarn add react-autosize-textarea

Demo

Live Examples

Usage

TextareaAutosize is perfectly compatible with ReactJS default one, so to get started you can simply replace any <textarea></textarea> with <TextareaAutosize></TextareaAutosize>. It's that easy :)

Props

You can pass any prop you're allowed to use with the default React textarea (valueLink too).

In addition to them, TextareaAutosize comes with some optional custom props to facilitate its usage:

Name Type Default Description
onResize Function optional. Called whenever the textarea resizes
rows Number optional. Minimum number of visible rows
maxRows Number optional. Maximum number of visible rows
async Boolean false optional. Initialize autosize asynchronously. Enable it if you are using StyledComponents. This is forced to true when maxRows is set. Async initialization will make your page "jump" when the component appears, as it will first be rendered with the normal size, then resized to content.
ref Function optional. Ref to the DOM node

onResize

Sometimes you may need to react any time TextareaAutosize resizes itself. To do so, you should use the optional callback onResize which will be triggered at any resize with the autosize:resized event object:

function onResize(event) {
  console.log(event.type); // -> "autosize:resized"
}

<TextareaAutosize onResize={onResize} />

Set min/max height

You can set minHeight and maxHeight through CSS or inline-style as usual:

<TextareaAutosize style={{ minHeight: 20, maxHeight: 80 }} /> // min-height: 20px; max-height: 80px;

NB: you may need to take into account borders and/or padding.

In addition to minHeight, you can force TextareaAutosize to have a minimum number of rows by passing the prop rows:

<TextareaAutosize rows={3} /> // minimun height is three rows

In addition to maxHeight, you can force TextareaAutosize to have a maximum number of rows by passing the prop maxRows:

<TextareaAutosize maxRows={3} /> // maximum height is three rows

Refs to DOM nodes

In order to manually call textarea's DOM element functions like focus() or blur(), you need a ref to the DOM node.

You get one by using the prop ref as shown in the example below:

class Form extends React.Component {
  constructor() {
    this.textarea = React.createRef()
  }
  
  componentDidMount() {
    this.textarea.current.focus();
  }

  render() {
    return (
      <TextareaAutosize ref={this.textarea} />
    );
  }
}

Browser Compatibility

Chrome Firefox IE Safari Android
Yes Yes 9+ Yes 4+

Credits

This module is based on the very popular autosize script written by Jack Moore.

Check out his repo or website for more documentation.

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.