mirror of
https://github.com/hex248/sprint.git
synced 2026-02-08 18:33:01 +00:00
length indicator on maxLength fields
This commit is contained in:
@@ -13,6 +13,7 @@ export function Field({
|
|||||||
error,
|
error,
|
||||||
tabIndex,
|
tabIndex,
|
||||||
spellcheck,
|
spellcheck,
|
||||||
|
maxLength,
|
||||||
}: {
|
}: {
|
||||||
label: string;
|
label: string;
|
||||||
value?: string;
|
value?: string;
|
||||||
@@ -24,6 +25,7 @@ export function Field({
|
|||||||
error?: string;
|
error?: string;
|
||||||
tabIndex?: number;
|
tabIndex?: number;
|
||||||
spellcheck?: boolean;
|
spellcheck?: boolean;
|
||||||
|
maxLength?: number;
|
||||||
}) {
|
}) {
|
||||||
const [internalTouched, setInternalTouched] = useState(false);
|
const [internalTouched, setInternalTouched] = useState(false);
|
||||||
const isTouched = submitAttempted || internalTouched;
|
const isTouched = submitAttempted || internalTouched;
|
||||||
@@ -56,6 +58,7 @@ export function Field({
|
|||||||
type={hidden ? "password" : "text"}
|
type={hidden ? "password" : "text"}
|
||||||
tabIndex={tabIndex}
|
tabIndex={tabIndex}
|
||||||
spellCheck={spellcheck}
|
spellCheck={spellcheck}
|
||||||
|
maxLength={maxLength}
|
||||||
/>
|
/>
|
||||||
<div className="flex items-end justify-end w-full text-xs mb-0 -mt-1">
|
<div className="flex items-end justify-end w-full text-xs mb-0 -mt-1">
|
||||||
{error || invalidMessage !== "" ? (
|
{error || invalidMessage !== "" ? (
|
||||||
|
|||||||
@@ -3,6 +3,12 @@ import type * as React from "react";
|
|||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
|
|
||||||
function Input({ className, type, ...props }: React.ComponentProps<"input">) {
|
function Input({ className, type, ...props }: React.ComponentProps<"input">) {
|
||||||
|
const maxLength = typeof props.maxLength === "number" ? props.maxLength : undefined;
|
||||||
|
const currentLength = typeof props.value === "string" ? props.value.length : undefined;
|
||||||
|
|
||||||
|
const showCounter = typeof maxLength === "number" && maxLength > 0 && typeof currentLength === "number";
|
||||||
|
|
||||||
|
if (!showCounter) {
|
||||||
return (
|
return (
|
||||||
<input
|
<input
|
||||||
type={type}
|
type={type}
|
||||||
@@ -18,4 +24,44 @@ function Input({ className, type, ...props }: React.ComponentProps<"input">) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const counterPercent = currentLength / maxLength;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className={cn(
|
||||||
|
"border-input dark:bg-input/30 flex h-9 w-full min-w-0 items-center border bg-transparent shadow-xs",
|
||||||
|
"transition-[color,box-shadow]",
|
||||||
|
"has-[:focus-visible]:border-ring",
|
||||||
|
"aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive",
|
||||||
|
"aria-invalid:border-destructive",
|
||||||
|
className,
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<input
|
||||||
|
type={type}
|
||||||
|
data-slot="input"
|
||||||
|
className={cn(
|
||||||
|
"file:text-foreground placeholder:text-muted-foreground selection:bg-primary selection:text-primary-foreground",
|
||||||
|
"h-full w-full min-w-0 bg-transparent px-3 py-1 pr-1 text-base outline-none",
|
||||||
|
"file:inline-flex file:h-7 file:border-0 file:bg-transparent file:text-sm file:font-medium",
|
||||||
|
"disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 md:text-sm",
|
||||||
|
)}
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
|
<span
|
||||||
|
className={cn(
|
||||||
|
"px-2 text-[11px] tabular-nums",
|
||||||
|
counterPercent >= 1
|
||||||
|
? "text-destructive"
|
||||||
|
: counterPercent >= 0.8
|
||||||
|
? "text-amber-500"
|
||||||
|
: "text-muted-foreground",
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{currentLength}/{maxLength}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export { Input };
|
export { Input };
|
||||||
|
|||||||
Reference in New Issue
Block a user