Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | 45x 8x 45x 13x 45x 45x 35x 35x 35x 35x 35x 10x 10x 10x 10x 10x | import { useId } from 'react';
import type { InputHTMLAttributes, ReactNode, TextareaHTMLAttributes } from 'react';
import { CloseIcon } from '@/components/icons';
/**
* Reusable text field. Styling lives in globals.css (`.field*`) so the
* interactive states and error styling are CSS-driven; these components only
* compose class names.
*
* Two forms share the same label + supporting-text chrome:
* - `Input`: single-line `<input>` (44px tall).
* - `Textarea`: multi-line `<textarea>` (80px tall, resizes vertically).
*
* Pass `error` (a message) to switch to the error styling; it renders in place
* of `supportingText`. `iconLeft` is a 20px leading adornment; `onClear` adds a
* trailing clear (×) button. `required` shows a `*` and sets the DOM attribute.
*/
type FieldChrome = {
label?: string;
required?: boolean;
/** Help text under the field; hidden when `error` is present. */
supportingText?: ReactNode;
/** Error message; its presence switches the field to the error styling. */
error?: ReactNode;
/** 20px leading adornment (icon). */
iconLeft?: ReactNode;
/** When provided, renders a trailing clear (×) button. */
onClear?: () => void;
/** Class for the outer `.field` wrapper. */
containerClassName?: string;
};
function FieldLabel({ htmlFor, label, required }: { htmlFor: string; label?: string; required?: boolean }) {
if (!label) return null;
return (
<label className="field__label" htmlFor={htmlFor}>
{label}
{required && (
<span className="field__required" aria-hidden="true">
*
</span>
)}
</label>
);
}
function FieldSupporting({
error,
supportingText,
id,
}: {
error?: ReactNode;
supportingText?: ReactNode;
id: string;
}) {
if (!error && !supportingText) return null;
return (
<span className={error ? 'field__supporting field__supporting--error' : 'field__supporting'} id={id}>
{error || supportingText}
</span>
);
}
function FieldBox({
textarea = false,
error,
iconLeft,
onClear,
children,
}: Pick<FieldChrome, 'error' | 'iconLeft' | 'onClear'> & { textarea?: boolean; children: ReactNode }) {
const classes = ['field__box', textarea ? 'field__box--textarea' : '', error ? 'field__box--error' : '']
.filter(Boolean)
.join(' ');
return (
<div className={classes}>
{iconLeft && (
<span className="field__adornment" aria-hidden="true">
{iconLeft}
</span>
)}
{children}
{onClear && (
<button type="button" className="field__adornment field__clear" onClick={onClear} aria-label="Clear">
<CloseIcon />
</button>
)}
</div>
);
}
interface InputProps extends Omit<InputHTMLAttributes<HTMLInputElement>, 'id'>, FieldChrome {
id?: string;
}
export function Input({
label,
required,
supportingText,
error,
iconLeft,
onClear,
containerClassName,
className,
id,
...rest
}: InputProps) {
const generatedId = useId();
const inputId = id ?? generatedId;
const describedById = `${inputId}-desc`;
const hasDesc = Boolean(error || supportingText);
return (
<div className={['field', containerClassName].filter(Boolean).join(' ')}>
<FieldLabel htmlFor={inputId} label={label} required={required} />
<FieldBox error={error} iconLeft={iconLeft} onClear={onClear}>
<input
id={inputId}
className={['field__control', className].filter(Boolean).join(' ')}
required={required}
aria-invalid={error ? true : undefined}
aria-describedby={hasDesc ? describedById : undefined}
{...rest}
/>
</FieldBox>
<FieldSupporting error={error} supportingText={supportingText} id={describedById} />
</div>
);
}
interface TextareaProps extends Omit<TextareaHTMLAttributes<HTMLTextAreaElement>, 'id'>, FieldChrome {
id?: string;
}
export function Textarea({
label,
required,
supportingText,
error,
iconLeft,
onClear,
containerClassName,
className,
id,
rows = 3,
...rest
}: TextareaProps) {
const generatedId = useId();
const inputId = id ?? generatedId;
const describedById = `${inputId}-desc`;
const hasDesc = Boolean(error || supportingText);
return (
<div className={['field', containerClassName].filter(Boolean).join(' ')}>
<FieldLabel htmlFor={inputId} label={label} required={required} />
<FieldBox textarea error={error} iconLeft={iconLeft} onClear={onClear}>
<textarea
id={inputId}
className={['field__control', className].filter(Boolean).join(' ')}
required={required}
rows={rows}
aria-invalid={error ? true : undefined}
aria-describedby={hasDesc ? describedById : undefined}
{...rest}
/>
</FieldBox>
<FieldSupporting error={error} supportingText={supportingText} id={describedById} />
</div>
);
}
|