Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-input-autosize

JedWatson5.3mMIT3.0.0TypeScript support: definitely-typed

Auto-resizing Input Component for React

react, react-component, input, autosize

readme

React-Input-Autosize

A text input for React that resizes itself to the current content.

Demo & Examples

Live demo: jedwatson.github.io/react-input-autosize

To run the examples locally, run:

npm install
npm start

Then open localhost:8000 in a browser.

Installation

The easiest way to use React-Input-Autosize is to install it from NPM and include it in your own React build process (using Browserify, rollup, webpack, etc).

You can also use the umd build by including dist/AutosizeInput.js in your page. If you use this, make sure you have already included a umd React build.

npm install react-input-autosize --save

Usage

React-Input-Autosize generates an input field, wrapped in a <div> tag so it can detect the size of its value. Otherwise it behaves very similarly to a standard React input.

import AutosizeInput from 'react-input-autosize';

<AutosizeInput
    name="form-field-name"
    value={inputValue}
    onChange={function(event) {
        // event.target.value contains the new value
    }}
/>

Gotchas

Changing the styles at runtime

The styles applied to the input are only copied when the component mounts. Because of this, subsequent changes to the stylesheet may cause size to be detected incorrectly.

To work around this, either re-mount the input (e.g. by providing a different key prop) or call the copyInputStyles() method after the styles change.

CSP and the IE "clear" indicator

The input will automatically inject a stylesheet that hides IE/Edge's "clear" indicator, which otherwise breaks the UI. This has the downside of being incompatible with some CSP policies.

To work around this, you can pass the injectStyles={false} prop, but if you do this I strongly recommend targeting the input element in your own stylesheet with the following rule:

input::-ms-clear {display: none;}

Custom font sizes

If your input uses custom font sizes, you will need to provide the custom size to AutosizeInput.

<AutosizeInput
    name="form-field-name"
    value={inputValue}
    inputStyle={{ fontSize: 36 }}
    onChange={function(event) {
        // event.target.value contains the new value
    }}
/>

Uncontrolled input

AutosizeInput is a controlled input and depends on the value prop to work as intended. It does not support being used as an uncontrolled input.

License

Copyright (c) 2018 Jed Watson. MIT License.

changelog

react-input-autosize

v3.0.0 / 2020-12-11

This release adds support for React 17 and removes usage of UNSAFE React lifecycle methods, thanks to Nathan Bierema.

It should be a fairly painless major upgrade since the API hasn't been changed at all. It's being published as 3.0.0 because the peer dependency range has been updated and the internal implementation has changed.

v2.2.2 / 2019-01-10

  • added UNSAFE prefix to out of date react lifecycle hooks. thanks RanneyD

v2.2.1 / 2018-01-10

v2.2.0 / 2018-01-09

  • added; extraWidth prop allows you to customise how much width is added to the detected width
  • fixed; SSR support is more robust, thanks Ivo Bathke

v2.1.2 / 2017-11-27

  • fixed; window reference is now guarded for SSR support, thanks Wout Mertens

v2.1.1 / 2017-11-26

  • fixed; props.id was overriding the internal inputId on the input element, but not being used in the stylesheet
  • fixed; IE stylesheet is now only injected when IE is detected in navigator.userAgent

v2.1.0 / 2017-11-23

  • fixed; inputId wasn't being applied correctly, thanks Jacco Flenter
  • added; new injectStyles prop controls whether the IE stylesheet it injected
  • improved; allow override of boxSizing and width styles via inputStyles, thanks Mike Fanger
  • improved; propTypes are now stripped from the production build, thanks jochenberger

v2.0.1 / 2017-09-13

  • fixed; peer dependencies for prop-types

v2.0.0 / 2017-09-12

  • fixed; converted to es6 Class and removed create-react-class
  • changed; default export is now an es6 module

Note:

As of 2.0.0 this package exports an es6 module in the main entry (/lib/AutoSizeInput.js). If you are in an older environment, you'll need to refer to the .default export:

var AutoSizeInput = require('react-input-autosize').default;

Aside from this, the new version doesn't change any public API.

v1.2.0 / 2017-09-12

  • added; inputRef prop (function, passed the reference to the input node)
  • fixed; resize issues in IE11, thanks Constantine
  • fixed; copyInputStyles() never running, thanks Michael Elsdörfer