import React, { useState } from "react"; import "../../Sass/_forms.scss"; import ErrorBlock from "./ErrorBlock"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faEye, faEyeSlash } from "@fortawesome/free-solid-svg-icons"; export enum InputType { button = "button", checkbox = "checkbox", color = "color", date = "date", datetimelocal = "datetime-local", email = "email", file = "file", hidden = "hidden", image = "image", month = "month", number = "number", password = "password", radio = "radio", range = "range", reset = "reset", search = "search", submit = "submit", tel = "tel", text = "text", textarea = "textarea", time = "time", url = "url", week = "week", } export interface InputProps { includeLabel?: boolean; name: string; label: string; error: string; placeHolder?: string; readOnly?: boolean; type: InputType; value?: string | number | readonly string[] | undefined; defaultValue?: string | number | readonly string[] | undefined; min?: number; max?: number; step?: number; hidden?: boolean; autoComplete?: string; onChange?: (e: React.ChangeEvent) => void; maxLength?: number; } function Input(props: InputProps) { const { includeLabel, name, label, error, placeHolder, readOnly, type, value, defaultValue, maxLength, hidden, autoComplete, onChange, ...rest } = props; let showValue = value; let checked: boolean = false; let divClassName = "form-group"; let labelClassName = "label"; let className = "form-control"; let flexClassName = ""; const [showPasswordIcon, setShowPasswordIcon] = useState(faEyeSlash); if (type === InputType.checkbox) { checked = value === String(true); showValue = undefined; divClassName = "form-check"; className = "form-check-input"; labelClassName += " form-check-label"; flexClassName += "checkbox"; } if (type === InputType.checkbox) { divClassName += " allignedCheckBox"; } const renderType = type === InputType.password && showPasswordIcon === faEye ? InputType.text : type; const divEyeIconClassName = readOnly ? "fullHeight disabledIcon" : "fullHeight"; if (type === InputType.password) { flexClassName += "flex"; } return ( ); } export default Input;