File size: 6,473 Bytes
65d68f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
"use client";

import { forwardRef, InputHTMLAttributes, useId } from "react";
import { clsx } from "clsx";
import { designSystem } from "@/lib/design-system";

const { mobile } = designSystem; // colors/radius/spacing/typography reserved

// =============================================================================
// Input Types
// =============================================================================

export interface InputProps extends InputHTMLAttributes<HTMLInputElement> {
  label?: string;
  error?: string;
  hint?: string;
  leftIcon?: React.ReactNode;
  rightIcon?: React.ReactNode;
  fullWidth?: boolean;
}

// =============================================================================
// SharedInput Component
// =============================================================================

export const SharedInput = forwardRef<HTMLInputElement, InputProps>(
  (
    {
      label,
      error,
      hint,
      leftIcon,
      rightIcon,
      fullWidth: _fullWidth = true,
      id: providedId,
      className,
      disabled,
      type = "text",
      ...props
    },
    ref,
  ) => {
    const generatedId = useId();
    const inputId = providedId ?? generatedId;
    const messageId = `${inputId}-message`;
    const hasIcon = leftIcon || rightIcon;
    const inputPadding = hasIcon ? "pl-10 pr-10" : "px-4";

    return (
      <div className="w-full" role="presentation">
        {label && (
          <label htmlFor={inputId} className="block mb-2 text-[12px] font-semibold text-[var(--text-primary)]">
            {label}
            {props.required && (
              <span className="text-[var(--accent-red)]" aria-hidden="true">
                *
              </span>
            )}
          </label>
        )}

        <div className="relative">
          {leftIcon && (
            <div className="absolute left-3 top-1/2 -translate-y-1/2 text-[var(--text-muted)] pointer-events-none">
              {leftIcon}
            </div>
          )}

          <input
            ref={ref}
            id={inputId}
            type={type}
            disabled={disabled}
            aria-invalid={error ? true : undefined}
            aria-describedby={error || hint ? messageId : undefined}
            // Critical: 16px font-size on mobile to prevent iOS zoom
            style={{
              fontSize: mobile.minInputFontSize,
            }}
            className={clsx(
              "w-full",
              "h-[44px]", // Fixed height for consistency
              "rounded-lg", // Using lg (16px) for inputs - can be adjusted
              "border",
              "bg-[var(--bg-surface)]",
              "text-[var(--text-primary)]",
              inputPadding,
              "placeholder:text-[var(--text-ghost)]",
              "border-[var(--border-subtle)]",
              "transition-colors",
              "duration-150",
              "focus:outline-none",
              "focus:border-[var(--accent-primary)]",
              "focus:ring-2",
              "focus:ring-[var(--accent-primary)]",
              "focus:ring-opacity-20",
              disabled && "cursor-not-allowed",
              disabled && "opacity-40",
              disabled && "bg-[var(--bg-elevated)]",
              error && "border-[var(--accent-red)]",
              error && "focus:border-[var(--accent-red)]",
              error && "focus:ring-[var(--accent-red)]",
              className,
            )}
            {...props}
          />

          {rightIcon && (
            <div className="absolute right-3 top-1/2 -translate-y-1/2 text-[var(--text-muted)] pointer-events-none">
              {rightIcon}
            </div>
          )}
        </div>

        {error && (
          <p id={messageId} className="mt-1.5 text-[12px] text-[var(--accent-red)]" role="alert">
            {error}
          </p>
        )}

        {hint && !error && (
          <p id={messageId} className="mt-1.5 text-[12px] text-[var(--text-muted)]">{hint}</p>
        )}
      </div>
    );
  },
);

SharedInput.displayName = "SharedInput";

// =============================================================================
// Convenience Input Components
// =============================================================================

// Search input with icon
export function SearchInput({
  placeholder = "Search...",
  className,
  ...props
}: Omit<InputProps, "leftIcon">) {
  return (
    <SharedInput
      type="search"
      placeholder={placeholder}
      className={clsx("pl-9", className)}
      {...props}
    />
  );
}

// Textarea variant
export function SharedTextarea({
  label,
  error,
  hint,
  className,
  rows = 4,
  ...props
}: {
  label?: string;
  error?: string;
  hint?: string;
  className?: string;
  rows?: number;
} & Omit<InputHTMLAttributes<HTMLTextAreaElement>, "type" | "leftIcon" | "rightIcon">) {
  const generatedId = useId();
  const textareaId = props.id ?? generatedId;
  const messageId = `${textareaId}-message`;

  return (
    <div className="w-full" role="presentation">
      {label && (
        <label htmlFor={textareaId} className="block mb-2 text-[12px] font-semibold text-[var(--text-primary)]">
          {label}
        </label>
      )}

      <textarea
        id={textareaId}
        rows={rows}
        aria-invalid={error ? true : undefined}
        aria-describedby={error || hint ? messageId : undefined}
        // Critical: 16px font-size on mobile
        style={{
          fontSize: mobile.minInputFontSize,
        }}
        className={clsx(
          "w-full",
          "p-4",
          "rounded-lg",
          "border",
          "bg-[var(--bg-surface)]",
          "text-[var(--text-primary)]",
          "placeholder:text-[var(--text-ghost)]",
          "border-[var(--border-subtle)]",
          "transition-colors",
          "duration-150",
          "focus:outline-none",
          "focus:border-[var(--accent-primary)]",
          "focus:ring-2",
          "focus:ring-[var(--accent-primary)]",
          "focus:ring-opacity-20",
          "resize-none",
          error && "border-[var(--accent-red)]",
          className,
        )}
        {...props}
      />

      {error && (
        <p id={messageId} className="mt-1.5 text-[12px] text-[var(--accent-red)]" role="alert">
          {error}
        </p>
      )}

      {hint && !error && (
        <p id={messageId} className="mt-1.5 text-[12px] text-[var(--text-muted)]">{hint}</p>
      )}
    </div>
  );
}