1.0.2 • Published 2 years ago

@jimsheen/react-dynamic-input-width v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

React Dynamic Input Width

Dynamically update the input width based on the input value length

npm package version number Actions Status License

Getting Started

npm install @jimsheen/react-dynamic-input-width

Examples

Basic example

import React from 'react';
import Input from '@jimsheen/react-dynamic-input-width';

export default function InputExample() {

  const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    console.log(e.target.value);
  }

  const handleKeyPress = (e: React.KeyboardEvent<HTMLInputElement>) => {
    console.log(e.key);
  }

  return (
    <Input
      initialValue="Hello there"
      padding={10}
      initialWidth={100}
      onChange={(e) => handleChange(e)}
      onKeyPress={(e) => handleKeyPress(e)}
    />
  )
}

Full example using refs:

import React, { useEffect } from "react";
import Input from "@jimsheen/react-dynamic-input-width";

export default function InputExample() {
  const [isEdit, setIsEdit] = React.useState(false);
  const [value, setValue] = React.useState("Hello there");

  // create a ref for the value span
  const valueRef = React.useRef<HTMLSpanElement>(null);
  
  // create a ref for the input
  const inputRef = React.useRef<HTMLInputElement>(null);

  // update value when input is changed
  const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    const { value } = e.target;
    setValue(value);
  };

  // example of handling "Enter" key press
  const handleKeyPress = (e: React.KeyboardEvent<HTMLInputElement>) => {
    if ((e && e.key === "Enter") || !e) {
      setIsEdit(!isEdit);
    }
  };

  // toggle the input visibility
  const handleClick = () => {
    setIsEdit(!isEdit);
  };

  // focus the input when isEdit is true
  useEffect(() => {
    if (isEdit) inputRef.current?.focus();
  }, [isEdit]);

  return (
    <>
      <button onClick={handleClick}>Toggle Edit</button>
      {!isEdit && <span ref={valueRef}>{value}</span>}
      {isEdit && (
        <Input
          initialValue={value}
          padding={0}
          // set initialWidth to the width of the value span element
          initialWidth={valueRef.current?.offsetWidth}
          onChange={(e) => handleChange(e)}
          onKeyPress={(e) => handleKeyPress(e)}
          ref={inputRef}
        />
      )}
    </>
  );
}

refs

Input accepts a ref by utilising the forwardRef HOC under the hood

const inputRef = React.useRef<HTMLInputElement>(null)

<Input
  ref={inputRef}
/>

other props

It's also possible to pass additional props to the underlying input component such as "placeholder" for example

<Input
  placeholder="Placeholder text"
/>

Props

PropTypeDefaultRequiredDescription
initialValuestringundefinedfalseThe input's initial value
initialWidthnumberundefinedfalseThe input's initial width value in px
paddingnumber1falseAdd's extra width to the input
fontSizestring16pxfalseThe font size of the input (used for measuring the offsetWidth)
classNamestringinput-dynamic-widthfalseDefault className

Types

initialValue?: string
initialWidth?: number
padding?: number
fontSize?: string
onChange?: (event: React.ChangeEvent<HTMLInputElement>) => void
onKeyPress?: (event: React.KeyboardEvent<HTMLInputElement>) => void
className?: string

Demo