File size: 4,380 Bytes
9b906ea | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | import { forwardRef } from "react";
import { cn } from "#/utils/utils";
import { formControlSettingsFieldClassName } from "#/utils/form-control-classes";
import { OptionalTag } from "./optional-tag";
interface SettingsInputProps {
testId?: string;
name?: string;
label: string;
type: React.HTMLInputTypeAttribute;
defaultValue?: string;
value?: string;
placeholder?: string;
showOptionalTag?: boolean;
isDisabled?: boolean;
startContent?: React.ReactNode;
className?: string;
onChange?: (value: string) => void;
onKeyDown?: (e: React.KeyboardEvent<HTMLInputElement>) => void;
required?: boolean;
min?: number;
max?: number;
step?: number;
pattern?: string;
/** Validation message shown when pattern doesn't match */
title?: string;
labelClassName?: string;
/**
* The input's accessible name, for the caller that renders the visible label
* itself. Only for those: a field labelled by this component reads its label,
* and two names would disagree.
*/
ariaLabel?: string;
/** ARIA describedby attribute for accessibility */
ariaDescribedBy?: string;
/** ARIA invalid attribute for accessibility */
ariaInvalid?: boolean;
/**
* Validation error message. When set, the input gets a red border and
* the message is rendered below it. Also sets aria-invalid automatically.
*/
error?: string;
/** Renders a red asterisk next to the label to mark the field as required. */
showRequiredTag?: boolean;
/**
* Short guidance rendered next to the label, above the input. It sits inside
* the `<label>`, so it is announced as part of the field's accessible name —
* keep it to a phrase that reads well after the label text.
*/
hint?: string;
onBlur?: () => void;
/** Extra classes merged onto the `<input>` element. */
inputClassName?: string;
}
export const SettingsInput = forwardRef<HTMLInputElement, SettingsInputProps>(
function SettingsInput(
{
testId,
name,
label,
type,
defaultValue,
value,
placeholder,
showOptionalTag,
isDisabled,
startContent,
className,
onChange,
onKeyDown,
required,
min,
max,
step,
pattern,
title,
labelClassName,
ariaLabel,
ariaDescribedBy,
ariaInvalid,
error,
showRequiredTag,
hint,
onBlur,
inputClassName,
},
ref,
) {
const errorId = error && testId ? `${testId}-error` : undefined;
return (
<label className={cn("flex flex-col gap-2.5 w-full min-w-0", className)}>
<div className="flex items-center gap-2">
{startContent}
<span className={cn("text-sm", labelClassName)}>{label}</span>
{showRequiredTag && (
<span className="text-red-400 text-sm leading-none" aria-hidden>
*
</span>
)}
{showOptionalTag && <OptionalTag />}
{hint && (
<span
data-testid={testId ? `${testId}-hint` : undefined}
className="min-w-0 text-xs text-[var(--oh-muted)]"
>
{hint}
</span>
)}
</div>
<input
ref={ref}
data-testid={testId}
onChange={(e) => onChange?.(e.target.value)}
onKeyDown={onKeyDown}
onBlur={onBlur}
name={name}
disabled={isDisabled}
type={type}
defaultValue={defaultValue}
value={value}
placeholder={placeholder}
min={min}
max={max}
step={step}
required={required}
pattern={pattern}
title={title}
aria-label={ariaLabel}
aria-describedby={errorId ?? ariaDescribedBy}
aria-invalid={!!error || ariaInvalid}
className={cn(
formControlSettingsFieldClassName,
"disabled:bg-[var(--oh-surface-raised)] disabled:border-[var(--oh-border-subtle)]",
error && "border-red-500",
inputClassName,
)}
/>
{error && (
<p
id={errorId}
role="alert"
data-testid={testId ? `${testId}-error` : undefined}
className="text-xs text-red-400 -mt-1"
>
{error}
</p>
)}
</label>
);
},
);
|