Giter VIP home page Giter VIP logo

react-expanding-textarea's Introduction

react-expanding-textarea

All Contributors npm version npm downloads bundlephobia size

React textarea component to automatically expand and contract your textareas.

You can view the demo here.

Links

Installation

npm i react-expanding-textarea

Usage

Use this exactly like you would a normal <textarea>; the only difference is that it is doing some simple expanding work behind the scenes for you!

import React, { useCallback, useEffect, useRef } from 'react'
import Textarea from 'react-expanding-textarea'

const MyTextarea = () => {
  const textareaRef = useRef(null)

  const handleChange = useCallback(e => {
    console.log('Changed value to: ', e.target.value)
  }, [])

  useEffect(() => {
    textareaRef.current.focus()
  }, [])

  return (
    <>
      <label for="my-textarea">
        Please Enter Some Details:
      </label>
      <Textarea
        className="textarea"
        defaultValue="Lorem ipsum dolor sit amet, ..."
        id="my-textarea"
        maxLength="3000"
        name="pet[notes]"
        onChange={handleChange}
        placeholder="Enter additional notes..."
        ref={textareaRef}
      />
    </>
  )
}

Using The rows Prop

If you pass a rows prop, then this component will perform a calculation based on computed lineHeight, borderTopWidth, borderBottomWidth, paddingTop and paddingBottom to deduce what the minimum height-in-rows the component should be.

Manually Calling resize

If for some reason you need to manually resize a <textarea>, this package exports a resize function that has the following type:

interface Resize {
  (rows: number, el: HTMLTextAreaElement | null): void
}

And you can use it like this:

import { resize } from 'react-expanding-textarea'

// resize based on 3 minimum rows
// and using a React ref
resize(3, refTextarea.current)

// or

// resize based on no minimum rows
// and using a regular DOM Node
resize(0, document.querySelector('textarea'))

Contributors

Thanks goes to these wonderful people (emoji key):


Robert Pearce

๐Ÿ’ป ๐Ÿ“– ๐Ÿ’ก ๐Ÿค” โš ๏ธ

Anuj

๐Ÿ›

Lloyd Watkin

๐Ÿค”

Jordan Hornblow

๐Ÿ›

visgotti

๐Ÿค”

Thomas Sunde Nielsen

๐Ÿ› ๐Ÿค”

cibulka

๐Ÿ› ๐Ÿค”

Brett Smith

๐Ÿ›

Rauno Freiberg

๐Ÿ› ๐Ÿ’ป

Thomas Kristiansen

๐Ÿค”

Puspender

๐Ÿ›

Mark Thomas

๐Ÿ›

Artem

๐Ÿ›

Eva Raymond

๐Ÿ›

Chris Drackett

๐Ÿ›

Simon Smith

๐Ÿ› ๐Ÿค” ๐Ÿ‘€

jordie23

๐Ÿ› ๐Ÿค”

Mat Sz

๐Ÿ› ๐Ÿ’ป

crtl

๐Ÿ› ๐Ÿค”

Jonathan Wan

๐Ÿ› ๐Ÿ’ป

James Moss

๐Ÿ› ๐Ÿค”

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

react-expanding-textarea's People

Contributors

alex-lee-david avatar

Watchers

 avatar

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.