mirror of
https://github.com/hex248/sprint.git
synced 2026-02-07 18:23:03 +00:00
improved counter logic
This commit is contained in:
@@ -14,6 +14,7 @@ export function Field({
|
||||
tabIndex,
|
||||
spellcheck,
|
||||
maxLength,
|
||||
showCounter = true,
|
||||
}: {
|
||||
label: string;
|
||||
value?: string;
|
||||
@@ -26,6 +27,7 @@ export function Field({
|
||||
tabIndex?: number;
|
||||
spellcheck?: boolean;
|
||||
maxLength?: number;
|
||||
showCounter?: boolean;
|
||||
}) {
|
||||
const [internalTouched, setInternalTouched] = useState(false);
|
||||
const isTouched = submitAttempted || internalTouched;
|
||||
@@ -59,6 +61,7 @@ export function Field({
|
||||
tabIndex={tabIndex}
|
||||
spellCheck={spellcheck}
|
||||
maxLength={maxLength}
|
||||
showCounter={showCounter}
|
||||
/>
|
||||
<div className="flex items-end justify-end w-full text-xs mb-0 -mt-1">
|
||||
{error || invalidMessage !== "" ? (
|
||||
|
||||
@@ -2,30 +2,15 @@ import type * as React from "react";
|
||||
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
function Input({ className, type, ...props }: React.ComponentProps<"input">) {
|
||||
function Input({
|
||||
className,
|
||||
type,
|
||||
showCounter = true,
|
||||
...props
|
||||
}: React.ComponentProps<"input"> & { showCounter?: boolean }) {
|
||||
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 (
|
||||
<input
|
||||
type={type}
|
||||
data-slot="input"
|
||||
className={cn(
|
||||
"file:text-foreground placeholder:text-muted-foreground selection:bg-primary selection:text-primary-foreground dark:bg-input/30 border-input h-9 w-full min-w-0 border bg-transparent px-3 py-1 text-base shadow-xs transition-[color,box-shadow] 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",
|
||||
"focus-visible:border-ring",
|
||||
"aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive",
|
||||
className,
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
const counterPercent = currentLength / maxLength;
|
||||
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
@@ -48,18 +33,20 @@ function Input({ className, type, ...props }: React.ComponentProps<"input">) {
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
{showCounter && currentLength !== undefined && maxLength !== undefined && (
|
||||
<span
|
||||
className={cn(
|
||||
"px-2 text-[11px] tabular-nums",
|
||||
counterPercent >= 1
|
||||
"border-l px-2 h-full flex items-center justify-center text-[11px] tabular-nums",
|
||||
currentLength / maxLength >= 1
|
||||
? "text-destructive"
|
||||
: counterPercent >= 0.8
|
||||
: currentLength / maxLength >= 0.75
|
||||
? "text-amber-500"
|
||||
: "text-muted-foreground",
|
||||
)}
|
||||
>
|
||||
{currentLength}/{maxLength}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user