Input 35

PreviousNext

Input field with real-time character counter, guiding users on remaining character allowance.

Docs
shadcn-studiocomponent

Preview

Loading preview…
registry/new-york/components/input/input-35.tsx
'use client'

import { useId, useState, type ChangeEvent } from 'react'

import { Input } from '@/registry/new-york/ui/input'
import { Label } from '@/registry/new-york/ui/label'

const maxLength = 12
const initialValue = ''

const InputCharacterLeftDemo = () => {
  const [value, setValue] = useState(initialValue)
  const [characterCount, setCharacterCount] = useState(initialValue.length)

  const id = useId()

  const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
    if (e.target.value.length <= maxLength) {
      setValue(e.target.value)
      setCharacterCount(e.target.value.length)
    }
  }

  return (
    <div className='w-full max-w-xs space-y-2'>
      <Label htmlFor={id}>Input with characters left</Label>
      <Input type='text' placeholder='Username' value={value} maxLength={maxLength} onChange={handleChange} />
      <p className='text-muted-foreground text-xs'>
        <span className='tabular-nums'>{maxLength - characterCount}</span> characters left
      </p>
    </div>
  )
}

export default InputCharacterLeftDemo

Installation

npx shadcn@latest add @shadcn-studio/input-35

Usage

import { Input35 } from "@/components/input-35"
<Input35 />