npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

expandable-textarea

v1.0.1

Published

ReactJS component provides expandable textarea

Downloads

18

Readme

expandable-textarea

ReactJS component provides expandable textarea

NPM JavaScript Style Guide

Textarea will expand or shrink against its content. Also configurable to work as an Input field, limiting its total line numbers, formating and more options.

Install

npm install --save expandable-textarea

How to use

Expand and shrink

Demo and Code

import ExpandableTextarea from 'expandable-textarea'
  <ExpandableTextarea
    initialValue={serverState}
    submitValue={handleSubmit}
    totalLines={5}
    name='expandShrink'
    minRows={1}
    maxRows={5}
  />

Fixed size like input field

Demo and Code

  <ExpandableTextarea
    className={'fixed-height'}
    rows={1}
    totalLines={1}
  />
.fixed-height > textarea {
  height: 2rem;
}

Bring to focus by clicking on icon

Demo and Code

import AddressIcon from '../address-icon/address-icon'
  <ExpandableTextarea
    beforeElement={<AddressIcon />}
    afterElement={<AddressIcon />}
  />

Credit card number formating

Demo and Code

import { maskFormating } from 'expandable-textarea'
 const creditCardFormat = maskFormating({
    maskString: '!!!!-!!!!-!!!!-!!!!',
    replaceChar: '!',
    validChar: /d/g,
    preVisibleMask: true,
    rightToLeft: false
  })
  <ExpandableTextarea
    formatFunction={creditCardFormat}
  />
  • maskString
    is string contains replaceChar and any other character excep validChar.
    (Default = '!!!!-!!!!-!!!!-!!!!')

  • replaceChar
    is single character that means user can type here
    (Default = '!')

  • validChar
    is regEx means which character allowed, must not contain any character of maskString or replaceChar
    (Default = /\d/g means 0 to 9)

  • preVisibleMask
    means always show the format even it is empty.
    (Default = true)

  • rightToLeft If true means masking starts from right.
    (Default = false)

Password format

Demo and Code

import { passwordFormating } from 'expandable-textarea'
const passwordFormat = passwordFormating(/[^-]/, '-')

First argument is allowd characters which here /[^-]/ means everything except - masking character.
Second argument is password masking character. (Default = '*')

  <ExpandableTextarea
    formatFunction={passwordFormat}
  />

Phone format

Demo and Code

import { maskFormating } from 'expandable-textarea'
  const phoneFormat = maskFormating({
    maskString: '(!!) !!!! !!!!',
    replaceChar: '!',
    validChar: /d/g,
    preVisibleMask: false,
    rightToLeft: false
  })
  <ExpandableTextarea
    formatFunction={phoneFormat}
  />

Custom format

Just to show how it works we will build a formating function for wraping typed numbers inside parentheses. Demo and Code

import { maskFormating } from 'expandable-textarea'
  const customFormat = (changeData) => {
    const { newValue, valid } = changeData
    if (!valid) return { ...changeData }
    const newUnformatedValue = (newValue.match(/d/g) || ['']).join('')
    const maskString = '(' + ''.padEnd(newUnformatedValue.length, '!') + ')'
    const newChangeData = maskFormating({
      maskString,
      validChar: /d/g
    })(changeData)
    return { ...newChangeData, unformatedValue: newUnformatedValue }
  }
      <ExpandableTextarea
        formatFunction={customFormat}
      />

changeData is an object prepared by ExpandableTextarea contains usefull information for applying formating logic.

    {
      iniValue,
      iniLineCount,
      iniSelectionStart,
      iniSelectionEnd,
      iniScrollTop,
      iniScrollLeft,
      pressedKey,
      newValue,
      newLineCount,
      excessIsShrinking,
      increasing,
      newSelectionStart,
      newSelectionEnd,
      newScrollTop,
      newScrollLeft,
      unformatedValue,
      valid
    }
  • valid===true means newValue is a valid change from ExpandableTextarea point of view.
  • unformatedValue must set to the unformated value after formating logic.
  • newValue must set to the formated value after formating logic

Props

beforeElement

React element like an Icon or label. Click on them brings textarea to focus.

afterElement

className

To style wrapper div around beforeElement, original textarea , afterElement

submitValue

Is a function. Called when onBlure happened and textarea value changed from initialValue
Passed parameter = {[name]: newValue, differFromInitial, name, unformatedValue, value}
newValue will be either formated or unformated value depends on if formating applied

name

Unique name will same key name in submitValue
Must be set for submiting new values.

initialValue = ''

totalLines

Unlimited lenght if not specified.

minRows

Sets minimum shrinking line count

maxRows

Sets maximum expanding line count

rows

Fixed line count if specified

formatFunction

Can be set to a built-in or custom formating function

resizeDebouncingDelay = 300

fitInField = false

Works with one line Like Input fields. It limits lenght to textarea view.

...rest

Additional standard textarea attributes like: disabled, wrap,...

License

MIT © makannew