auto growing inputs

This commit is contained in:
Matthias Kretschmann 2024-04-01 01:49:17 +01:00
parent 4850c19417
commit 110744f4ce
Signed by: m
GPG Key ID: 606EEEF3C479A91F
3 changed files with 19 additions and 5 deletions

View File

@ -1,7 +1,6 @@
.input { .input {
all: unset; all: unset;
width: 60px; padding: 0 0.75rem;
padding: 0 0.2rem;
text-align: center; text-align: center;
border-right: 1px solid rgba(var(--foreground-rgb), 0.15); border-right: 1px solid rgba(var(--foreground-rgb), 0.15);
} }

View File

@ -1,4 +1,6 @@
import { Dispatch, SetStateAction } from 'react' 'use client'
import { Dispatch, SetStateAction, useRef, useState } from 'react'
import styles from './InputAmount.module.css' import styles from './InputAmount.module.css'
export function InputAmount({ export function InputAmount({
@ -8,6 +10,16 @@ export function InputAmount({
amount: number amount: number
setAmount: Dispatch<SetStateAction<number>> setAmount: Dispatch<SetStateAction<number>>
}) { }) {
function handleChange(e: React.ChangeEvent<HTMLInputElement>) {
const { value } = e.target
if (value === '') {
setAmount(0)
} else {
setAmount(Number(value))
}
}
return ( return (
<input <input
className={styles.input} className={styles.input}
@ -15,7 +27,10 @@ export function InputAmount({
inputMode="numeric" inputMode="numeric"
pattern="[0-9]*" pattern="[0-9]*"
value={amount} value={amount}
onChange={(e) => setAmount(Number(e.target.value))} onChange={handleChange}
style={{
width: Math.min(Math.max(amount.toString().length, 2), 50) + 'ch'
}}
/> />
) )
} }

View File

@ -1,7 +1,7 @@
.select { .select {
display: inline-block; display: inline-block;
all: unset; all: unset;
padding: 0 0.5rem; padding: 0 0.75rem;
} }
.select:hover:not(.select[disabled]) { .select:hover:not(.select[disabled]) {