Merge pull request #40 from ragusa-it/feat/ui-input-char-counter-5721943779298145011

🎨 Palette: Add character counter to inputs
This commit was merged in pull request #40.
This commit is contained in:
Melvin Ragusa
2026-01-30 06:00:30 +01:00
committed by GitHub
3 changed files with 21 additions and 0 deletions

View File

@@ -5,3 +5,7 @@
## 2024-05-24 - Accessible Loading Buttons
**Learning:** Replacing button text with a spinner destroys the accessible name.
**Action:** Use `aria-busy="true"`, keep children in DOM (visually hidden via opacity/class), and overlay spinner absolutely. Ensure wrapper element replicates flex layout (gap/alignment) to prevent layout shifts.
## 2024-05-24 - Enhancing Inputs Safely
**Learning:** Adding features like character counters to generic input components must handle both controlled and uncontrolled states. Assuming a component is controlled (using `props.value`) can break uncontrolled usage by showing stale data (e.g., sticking at 0/100).
**Action:** When enhancing generic components, detect uncontrolled state (e.g., `props.value === undefined`) and either implement internal state tracking or gracefully degrade (hide the feature) to avoid misleading UX.

View File

@@ -61,3 +61,10 @@
font-size: 0.8125rem;
color: var(--md-sys-color-error);
}
.charCount {
font-size: 0.75rem;
color: var(--md-sys-color-outline);
text-align: right;
font-variant-numeric: tabular-nums;
}

View File

@@ -30,6 +30,11 @@ export const Input = forwardRef<HTMLInputElement, InputProps>(
{...props}
/>
{error && <span id={errorId} className={styles.error}>{error}</span>}
{props.maxLength !== undefined && props.value !== undefined && (
<div className={styles.charCount} aria-hidden="true">
{String(props.value).length} / {props.maxLength}
</div>
)}
</div>
);
}
@@ -66,6 +71,11 @@ export const Textarea = forwardRef<HTMLTextAreaElement, TextareaProps>(
{...props}
/>
{error && <span id={errorId} className={styles.error}>{error}</span>}
{props.maxLength !== undefined && props.value !== undefined && (
<div className={styles.charCount} aria-hidden="true">
{String(props.value).length} / {props.maxLength}
</div>
)}
</div>
);
}