-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathFormField.tsx
More file actions
120 lines (109 loc) · 2.93 KB
/
Copy pathFormField.tsx
File metadata and controls
120 lines (109 loc) · 2.93 KB
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
import React, { useId } from 'react';
import './FormField.css';
interface FormFieldProps {
id: string;
name: string;
label: string;
type?: 'text' | 'password' | 'email' | 'tel' | 'number';
value: string;
onChange: (value: string) => void;
onBlur?: () => void;
placeholder?: string;
error?: string;
helpText?: string;
formatText?: string;
describedBy?: string;
required?: boolean;
disabled?: boolean;
autoComplete?: string;
maxLength?: number;
className?: string;
}
export const FormField: React.FC<FormFieldProps> = ({
id,
name,
label,
type = 'text',
value,
onChange,
onBlur,
placeholder,
error,
helpText,
formatText,
describedBy = '',
required = false,
disabled = false,
autoComplete,
maxLength,
className = '',
}) => {
const helpId = `${id}-help`;
const errorId = `${id}-error`;
const formatId = `${id}-format`;
// Combine aria-describedby from props and computed IDs
const getAriaDescribedBy = (): string => {
const ids: string[] = [];
// Add help text ID if helpText exists
if (helpText) ids.push(helpId);
// Add format text ID if formatText exists
if (formatText) ids.push(formatId);
// Add error ID if error exists
if (error) ids.push(errorId);
// Add any additional IDs from props
if (describedBy) {
const propIds = describedBy.split(' ');
propIds.forEach((id) => {
if (id && !ids.includes(id)) {
ids.push(id);
}
});
}
return ids.length > 0 ? ids.join(' ') : undefined;
};
return (
<div className={`form-field ${className} ${error ? 'form-field--error' : ''}`}>
<label htmlFor={id} className="form-field__label">
{label}
{required && <span className="form-field__required" aria-hidden="true">*</span>}
</label>
<div className="form-field__input-wrapper">
<input
id={id}
name={name}
type={type}
value={value}
onChange={(e) => onChange(e.target.value)}
onBlur={onBlur}
placeholder={placeholder}
disabled={disabled}
required={required}
autoComplete={autoComplete}
maxLength={maxLength}
className="form-field__input"
aria-describedby={getAriaDescribedBy()}
aria-invalid={!!error}
aria-required={required}
/>
</div>
{/* Help text - always present but hidden from screen readers unless referenced */}
{helpText && (
<div id={helpId} className="form-field__help">
{helpText}
</div>
)}
{/* Format text - visible format hint */}
{formatText && (
<div id={formatId} className="form-field__format">
{formatText}
</div>
)}
{/* Error message - shown when error exists */}
{error && (
<div id={errorId} className="form-field__error" role="alert">
{error}
</div>
)}
</div>
);
};