Spaces:
Running

File size: 3,554 Bytes
7e79d39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useId } from 'react';
import { cn } from '@/shared/lib/cn';

const BASE_CONTROL =
  'w-full rounded-xl bg-surface-2 border border-line-strong text-ink-1 placeholder:text-ink-3 text-xs sm:text-sm outline-none transition focus:border-accent focus-visible:ring-2 focus-visible:ring-accent/40 disabled:opacity-60';

interface InputProps extends React.InputHTMLAttributes<HTMLInputElement> {
  label?: string;
  helperText?: string;
  error?: string;
  leftIcon?: React.ReactNode;
}

export function Input({
  label,
  helperText,
  error,
  leftIcon,
  id,
  className,
  ...rest
}: InputProps) {
  const autoId = useId();
  const fieldId = id || autoId;

  return (
    <div className="space-y-1.5">
      {label && (
        <label htmlFor={fieldId} className="block text-xs font-medium text-ink-2">
          {label}
        </label>
      )}
      <div className="relative">
        {leftIcon && (
          <span
            className="absolute left-3 top-1/2 -translate-y-1/2 text-ink-3 pointer-events-none"
            aria-hidden="true"
          >
            {leftIcon}
          </span>
        )}
        <input
          id={fieldId}
          aria-invalid={error ? 'true' : undefined}
          className={cn(
            BASE_CONTROL,
            'min-h-[42px] py-2',
            leftIcon ? 'pl-9 pr-3' : 'px-3',
            error && 'border-rose-500/70 focus:border-rose-500',
            className,
          )}
          {...rest}
        />
      </div>
      {error && (
        <p role="alert" className="text-[11px] text-rose-500">
          {error}
        </p>
      )}
      {!error && helperText && <p className="text-[11px] text-ink-3">{helperText}</p>}
    </div>
  );
}

interface TextareaProps extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {
  label?: string;
  helperText?: string;
  error?: string;
}

export function Textarea({
  label,
  helperText,
  error,
  id,
  className,
  ...rest
}: TextareaProps) {
  const autoId = useId();
  const fieldId = id || autoId;

  return (
    <div className="space-y-1.5">
      {label && (
        <label htmlFor={fieldId} className="block text-xs font-medium text-ink-2">
          {label}
        </label>
      )}
      <textarea
        id={fieldId}
        aria-invalid={error ? 'true' : undefined}
        className={cn(
          BASE_CONTROL,
          'px-3 py-2.5 leading-relaxed',
          error && 'border-rose-500/70 focus:border-rose-500',
          className,
        )}
        {...rest}
      />
      {error && (
        <p role="alert" className="text-[11px] text-rose-500">
          {error}
        </p>
      )}
      {!error && helperText && <p className="text-[11px] text-ink-3">{helperText}</p>}
    </div>
  );
}

interface SelectProps extends React.SelectHTMLAttributes<HTMLSelectElement> {
  label?: string;
  error?: string;
}

export function Select({ label, error, id, className, children, ...rest }: SelectProps) {
  const autoId = useId();
  const fieldId = id || autoId;

  return (
    <div className="space-y-1.5">
      {label && (
        <label htmlFor={fieldId} className="block text-xs font-medium text-ink-2">
          {label}
        </label>
      )}
      <select
        id={fieldId}
        aria-invalid={error ? 'true' : undefined}
        className={cn(BASE_CONTROL, 'min-h-[42px] px-3 py-2', className)}
        {...rest}
      >
        {children}
      </select>
      {error && (
        <p role="alert" className="text-[11px] text-rose-500">
          {error}
        </p>
      )}
    </div>
  );
}