All files / Components Input.js

100% Statements 20/20
94.44% Branches 34/36
100% Functions 2/2
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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            4x                           59x   59x   59x                   59x                                               59x 59x 1x         58x 1x             57x 1x             56x 7x 49x 2x           47x     59x                                         4x                           4x                      
import React, { useContext } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { WebAppsUXContext } from '../Context/index';
import Loader from './Loader';
 
const Input = props => {
    const {
        id,
        name,
        label,
        action,
        actionLocation,
        helpText,
        error,
        state,
        wrapperClassName,
        labelClassName,
        inputClassName,
        ...attributes
    } = props;
 
    const { theme } = useContext(WebAppsUXContext);
 
    const labelClasses = classNames(
        'block',
        'mb-2',
        'text-sm',
        'font-medium',
        'text-gray-700',
        'dark:text-gray-300',
        labelClassName,
    )
 
    const inputClasses = classNames(
        'bg-transparent',
        'border-2',
        'outline-none',
        'text-sm',
        'rounded-lg',
        'block',
        'w-full',
        'p-2.5',
        'focus:bg-gray-50',
        'dark:focus:bg-gray-700',
        'dark:placeholder-gray-400',
        'transition-colors',
        'focus:ring-0',
        (theme === undefined) ? '' : `focus:border-${theme}-600`,
        (theme === undefined) ? '' : `dark:focus:border-${theme}-500`,
        (state === '') ? 'border-gray-400 text-gray-900 dark:text-white dark:border-gray-600' : '',
        (state === 'error') ? 'border-red-500 text-red-500 focus:border-red-500 dark:focus:border-red-500' : '',
        (state === 'saved') ? 'border-green-500 text-green-500 focus:border-green-500 dark:focus:border-green-500' : '',
        (state === 'saving') ? 'border-orange-500 focus:border-orange-500 dark:focus:border-orange-500' : '',
        (actionLocation === 'left') ? 'pl-10' : '',
        inputClassName,
    )
 
    const Append = () => {
        if (state === 'saving') {
            return (
                <div className={`flex absolute inset-y-0 ${actionLocation}-0 items-center p${actionLocation.charAt(0)}-3`}>
                    <Loader type="circle" height="5" width="5" color="orange" />
                </div>
            )
        } else if (state === 'saved') {
            return (
                <div className={`flex absolute inset-y-0 ${actionLocation}-0 items-center p${actionLocation.charAt(0)}-3`}>
                    <svg xmlns="http://www.w3.org/2000/svg" className="h-5 w-5 text-green-500" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" />
                    </svg>
                </div>
            )
        } else if (state === 'error') {
            return (
                <div className={`flex absolute inset-y-0 ${actionLocation}-0 items-center p${actionLocation.charAt(0)}-3`}>
                    <svg xmlns="http://www.w3.org/2000/svg" className="h-5 w-5 text-red-500" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
                    </svg>
                </div>
            )
        } else if (action && actionLocation === 'none') {
            return action;
        } else if (action !== undefined) {
            return (
                <div className={`flex absolute inset-y-0 ${actionLocation}-0 items-center p${actionLocation.charAt(0)}-3`}>
                    {action}
                </div>
            )
        }
        return null;
    }
 
    return (
        <div className={wrapperClassName}>
            {
                (label !== '') ? <label htmlFor={id} className={labelClasses}>{label}</label> : null
            }
            <div className="relative">
                <input id={id} name={name} className={inputClasses} {...attributes} />
                <Append />
            </div>
            {
                (helpText !== '' || error !== '')
                    ? (
                        <span className={`text-sm transition-colors ${(state === 'error') ? 'text-red-500' : 'text-gray-500 dark:text-gray-400'}`}>
                            {(state === 'error') ? error : helpText}
                        </span>
                    ) : null
            }
        </div>
    );
}
 
Input.propTypes = {
    id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
    name: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
    label: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
    action: PropTypes.object,
    actionLocation: PropTypes.oneOf(['right', 'left', 'none']),
    helpText: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
    error: PropTypes.string,
    state: PropTypes.oneOf(['', 'saving', 'error', 'saved']),
    wrapperClassName: PropTypes.oneOfType([PropTypes.string, PropTypes.array, PropTypes.object]),
    labelClassName: PropTypes.oneOfType([PropTypes.string, PropTypes.array, PropTypes.object]),
    inputClassName: PropTypes.oneOfType([PropTypes.string, PropTypes.array, PropTypes.object]),
}
 
Input.defaultProps = {
    error: '',
    state: '',
    label: '',
    actionLocation: 'right',
    helpText: '',
    wrapperClassName: 'mb-6',
    labelClassName: '',
    inputClassName: '',
}
 
export default Input;