All files / Components Select.js

100% Statements 18/18
100% Branches 34/34
100% Functions 2/2
100% Lines 18/18

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 141 142 143 144            1x                             10x   10x   10x                   10x                                                     10x 10x 2x         8x 2x             6x 2x             4x 2x           2x     10x                                             1x                           1x                      
import React, { useContext } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import Loader from './Loader';
import { WebAppsUXContext } from '../Context';
 
const Select = props => {
    const {
        id,
        name,
        label,
        action,
        actionLocation,
        helpText,
        error,
        state,
        wrapperClassName,
        labelClassName,
        selectClassName,
        children,
        ...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 selectClasses = classNames(
        'bg-transparent',
        'border-2',
        'border-gray-400',
        'text-gray-900',
        'outline-none',
        'text-sm',
        'rounded-lg',
        'block',
        'w-full',
        'p-2.5',
        'focus:bg-gray-50',
        'dark:focus:bg-gray-700',
        'dark:border-gray-600',
        'dark:placeholder-gray-400',
        'dark:text-white',
        'transition-colors',
        'ring-0',
        `focus:border-${theme}-600`,
        `dark:focus:border-${theme}-500`,
        (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' : '',
        selectClassName,
    )
 
    const Append = () => {
        if (state === 'saving') {
            return (
                <div className={`flex absolute inset-y-0 ${(actionLocation === 'right') ? 'right-5' : 'left-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 === 'right') ? 'right-5' : 'left-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 === 'right') ? 'right-5' : 'left-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 !== undefined) {
            return (
                <div className={`flex absolute inset-y-0 ${(actionLocation === 'right') ? 'right-5' : 'left-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">
                <select id={id} name={name} className={selectClasses} {...attributes}>
                    {children}
                </select>
                <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>
    );
}
 
Select.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']),
    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]),
    selectClassName: PropTypes.oneOfType([PropTypes.string, PropTypes.array, PropTypes.object]),
}
 
Select.defaultProps = {
    error: '',
    state: '',
    label: '',
    actionLocation: 'right',
    helpText: '',
    wrapperClassName: 'mb-6',
    labelClassName: '',
    selectClassName: '',
}
 
export default Select;