File size: 3,940 Bytes
b64de39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Input - Reusable text input with label, validation, and error display.
 *
 * Supports all standard HTML input types, optional leading/trailing
 * icons, helper text, and inline error messages.
 */

import { forwardRef } from "react";
import PropTypes from "prop-types";

const Input = forwardRef(function Input(
  {
    id,
    name,
    type = "text",
    label,
    placeholder,
    value,
    defaultValue,
    onChange,
    onBlur,
    disabled = false,
    readOnly = false,
    required = false,
    error,
    helperText,
    leadingIcon,
    trailingIcon,
    className = "",
    inputClassName = "",
    ...rest
  },
  ref,
) {
  const inputId = id || name;
  const hasError = Boolean(error);
  const describedBy = [];
  if (hasError) {
    describedBy.push(`${inputId}-error`);
  }
  if (helperText && !hasError) {
    describedBy.push(`${inputId}-helper`);
  }

  const ringColor = hasError
    ? "border-danger-500 focus:ring-danger-500 focus:border-danger-500"
    : "border-neutral-300 focus:ring-primary-500 focus:border-primary-500";

  const inputClasses = [
    "block w-full rounded-lg border px-3 py-2 text-sm text-neutral-900",
    "placeholder:text-neutral-400",
    "transition-colors duration-150",
    "focus:outline-none focus:ring-2 focus:ring-offset-0",
    "disabled:bg-neutral-100 disabled:cursor-not-allowed",
    "read-only:bg-neutral-50",
    ringColor,
    leadingIcon ? "pl-10" : "",
    trailingIcon ? "pr-10" : "",
    inputClassName,
  ]
    .filter(Boolean)
    .join(" ");

  return (
    <div className={`w-full ${className}`}>
      {label && (
        <label
          htmlFor={inputId}
          className="block text-sm font-medium text-neutral-700 mb-1"
        >
          {label}
          {required && (
            <span className="text-danger-500 ml-0.5" aria-hidden="true">
              *
            </span>
          )}
        </label>
      )}

      <div className="relative">
        {leadingIcon && (
          <div className="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3 text-neutral-400">
            {leadingIcon}
          </div>
        )}

        <input
          ref={ref}
          id={inputId}
          name={name}
          type={type}
          placeholder={placeholder}
          value={value}
          defaultValue={defaultValue}
          onChange={onChange}
          onBlur={onBlur}
          disabled={disabled}
          readOnly={readOnly}
          required={required}
          aria-invalid={hasError}
          aria-describedby={
            describedBy.length > 0 ? describedBy.join(" ") : undefined
          }
          className={inputClasses}
          {...rest}
        />

        {trailingIcon && (
          <div className="absolute inset-y-0 right-0 flex items-center pr-3 text-neutral-400">
            {trailingIcon}
          </div>
        )}
      </div>

      {hasError && (
        <p
          id={`${inputId}-error`}
          className="mt-1 text-sm text-danger-600"
          role="alert"
        >
          {error}
        </p>
      )}

      {helperText && !hasError && (
        <p
          id={`${inputId}-helper`}
          className="mt-1 text-sm text-neutral-500"
        >
          {helperText}
        </p>
      )}
    </div>
  );
});

Input.propTypes = {
  id: PropTypes.string,
  name: PropTypes.string,
  type: PropTypes.string,
  label: PropTypes.string,
  placeholder: PropTypes.string,
  value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
  defaultValue: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
  onChange: PropTypes.func,
  onBlur: PropTypes.func,
  disabled: PropTypes.bool,
  readOnly: PropTypes.bool,
  required: PropTypes.bool,
  error: PropTypes.string,
  helperText: PropTypes.string,
  leadingIcon: PropTypes.node,
  trailingIcon: PropTypes.node,
  className: PropTypes.string,
  inputClassName: PropTypes.string,
};

export default Input;