import React, { useEffect, useRef, useState } from "react"; import styled from "styled-components"; import { ContainerWidget, TextAreaWidget, TextFieldWidget } from "./index.style"; export const SuccessIcon = ` `; export const ErrorIcon = ` `; export interface TextFieldProps { // Label Text Properties label?: string labelFontSize?: number labelFontWeight?: string labelFontFamily?: string labelFontDecoration?: string labelColor?: string // Placeholder Properties placeholder?: string placeholderFontSize?: number placeholderFontWeight?: string placeholderFontFamily?: string placeholderFontDecoration?: string placeholderColor?: string // Input Field Properties type?: string fontSize?: number fontWeight?: string fontFamily?: string fontDecoration?: string color?: string multline?: number // Left Icon leftIcon?: string leftIconSize?: number leftIconColor?: string // Right Icon rightIcon?: string rightIconSize?: number rightIconColor?: string // Container backgroundColor?: string backgroundOpacity?: string borderColor?: string borderWidth?: number borderRadius?: number borderType?: string boxShadow?: string // Supporting Text Properties supportingText?: string supportingTextFontSize?: number supportingTextFontWeight?: string supportingTextFontFamily?: string supportingTextFontDecoration?: string supportingTextColor?: string // Actions onComplete?: () => void // Status isDisabled?: boolean } const WOITextField = (props: TextFieldProps) => { const [input, setInput] = useState(''); const [isError, setError] = useState(false); const [isSuccess, setSuccess] = useState(false); const [focusState, setFocusState] = useState(false); var { type, label, labelFontSize, labelFontWeight, labelFontFamily, labelFontDecoration, labelColor, placeholder, leftIcon, leftIconSize, leftIconColor, rightIcon, rightIconSize, rightIconColor, isDisabled, multline, supportingText, supportingTextFontSize, supportingTextFontWeight, supportingTextFontFamily, supportingTextFontDecoration, supportingTextColor, } = props; // Label const LabelTextWidget = styled.label` display: flex; flex-direction: column; margin: 4px 0px 12px 4px; font-family: ${labelFontFamily ? labelFontFamily : 'Nunito Sans'}; font-size: ${labelFontSize ? labelFontSize + 'px' : '16px'}; font-style: normal; font-weight: ${labelFontWeight ? labelFontWeight : '400'}; line-height: normal; text-decoration: ${labelFontDecoration ? labelFontDecoration : 'normal'}; color: ${labelColor ? labelColor : '#000000'}; `; // Left Icon Widget const LeftIconWidget = styled.div` position: absolute; left: 3%; top: ${multline ? '20%' : '50%'}; transform: ${multline ? 'translate(-20%, -50%)' : 'translate(-8%, -50%)'}; height: ${leftIconSize}px; width: ${leftIconSize}px; opacity: ${isDisabled ? 0.5 : 1}; cursor: ${isDisabled ? 'not-allowed' : 'auto'}; svg path{ fill: ${isSuccess ? '#00B87C' : isError ? '#ff3333' : focusState ? props.borderColor : leftIconColor} } `; // Right Icon Widget const RightIconWidget = styled.div` position: absolute; right: 3%; top: ${multline ? '20%' : '50%'}; transform: ${multline ? 'translate(-20%, -50%)' : 'translate(-8%, -50%)'}; height: ${rightIconSize}px; width: ${rightIconSize}px; opacity: ${isDisabled ? 0.2 : 1}; cursor: ${isDisabled ? 'not-allowed' : 'auto'}; svg path{ fill: ${isSuccess ? '#00B87C' : isError ? '#ff3333' : focusState ? props.borderColor : rightIconColor} } `; // Supporting Text Widget const SupportingTextWidget = styled.p` display: flex; flex-direction: column; margin: 12px 0px 0px 4px; font-family: ${supportingTextFontFamily ? supportingTextFontFamily : 'Nunito Sans'}; font-size: ${supportingTextFontSize ? supportingTextFontSize + 'px' : '12px'}; font-style: normal; font-weight: ${supportingTextFontWeight ? supportingTextFontWeight : '400'}; line-height: normal; text-decoration: ${supportingTextFontDecoration ? supportingTextFontDecoration : 'normal'}; color: ${supportingTextColor ? supportingTextColor : '#000000'}; `; useEffect(() => { function handleClick(e: MouseEvent) { if (!isDisabled) { // if the click is outside the input container, // if it has input, then focus and validate // if no input, then just go out of focus if (containerRef.current && (containerRef.current as Element).contains(e.target as Node) && inputRef.current && inputRef.current !== e.target) { if ((inputRef.current as HTMLInputElement).value) { inputValidator((inputRef.current as HTMLInputElement).value); } else { setFocusState(false) } } else { if (containerRef.current && (containerRef.current as Element).contains(e.target as Node)) { setFocusState(true); } else { if (inputRef.current && !(inputRef.current as HTMLInputElement).value) { setFocusState(false); setError(false); setSuccess(false); } setFocusState(false); } } } } document.addEventListener('mousedown', handleClick); return () => document.removeEventListener('mousedown', handleClick); }, [isDisabled]); const containerRef: React.MutableRefObject = useRef(null); const inputRef: React.MutableRefObject = useRef(null); // Input validator const inputValidator = (value: any) => { setInput(value); setFocusState(false); if (value === '') { setError(true); setSuccess(false); } else { setSuccess(true); setError(false); } } return (
{label} {leftIcon ? : null} {multline ? : } {rightIcon ? : null} {supportingText}
); }; export default WOITextField;