All files / components OurAddDropdownForm.js

100% Statements 88/88
100% Branches 63/63
100% Functions 13/13
100% Lines 86/86

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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238          64x 64x 1x   64x 1x     64x                 64x 55x 55x 16x   55x                           9x 9x 9x   9x           64x                       135x 232x 12x   220x 217x   3x       135x     135x 43x 43x 97x 94x     43x 43x 33x         135x 135x 135x 135x     135x       135x                   135x 135x   17x 17x 17x 17x   17x 51x 18x     18x 12x 12x         18x 5x 5x     18x       17x 12x     17x 5x 5x   12x   17x     135x 13x   12x 12x 12x     13x 1x     135x 5x 2x 2x 2x   3x     135x                       24x     4x             68x 68x 68x 4x 4x 3x 3x 3x   4x   68x                                                            
import React from 'react';
import { useState, useEffect } from 'react';
import { Form } from 'react-bootstrap';
 
function DropdownOption({ label, isSelected, onClickFunc, testid, rawKey, isGhost }) {
    const [isHovered, setIsHovered] = useState(false);
    const handleMouseEnter = () => {
        setIsHovered(true);
    };
    const handleMouseLeave = () => {
        setIsHovered(false);
    };
    // Stryker disable all
    const divStyle = {
        backgroundColor: isHovered ? 'lightgreen' : 'white',
        transition: 'background-color 0.25s ease',
        'paddingLeft': '2px',
        'paddingRight': '2px',
        'cursor': 'pointer',
    };
    // Stryker restore all
    let mainOption;
    if (!isSelected) {
        let altStyle = divStyle;
        if(isGhost){
            altStyle['backgroundColor'] = 'lightgreen';
        }
        mainOption = (
            <div
                key={rawKey}
                data-testid={testid}
                style={altStyle}
                onMouseEnter={handleMouseEnter}
                onMouseLeave={handleMouseLeave}
                onClick={onClickFunc}
            >
                {label}
            </div>
        );
    } else {
        // Stryker disable all
        let altStyle = divStyle;
        altStyle['backgroundColor'] = 'green';
        altStyle['color'] = 'white';
        // Stryker restore all
        mainOption = (
            <div key={rawKey} data-testid={testid} style={altStyle} onClick={onClickFunc}>
                {label}
            </div>
        );
    }
    return <div>{mainOption}</div>;
}
 
export default function OurAddDropdownForm({
    content,
    label,
    basis = null,
    testId = 'testid',
    autocomplete=true,
    onChangeFunc = null,
}) {
    // Stryker disable all
    content.sort((a, b) => {
        if (a.label < b.label) {
            return -1;
        }
        if (a.label > b.label) {
            return 1;
        }
        return 0;
    });
    // Stryker restore all
 
    const [filteredContent, changeFilteredContent] = useState(content);
 
    // Stryker disable all
    useEffect(() => {
        const fixedContent = [];
        for (let i = 0; i < content.length; ++i) {
            if (!autocomplete || !basis || content[i].label.startsWith(basis.label)) {
                fixedContent.push(content[i]);
            }
        }
        changeFilteredContent(fixedContent);
        if(fixedContent.length > 0){
            changeGhostContent(fixedContent[0]);
        }
    }, [content, basis, autocomplete]);
    // Stryker restore all
 
    const [selectedContent, changeSelectedContent] = useState(basis);
    const [userTypedContent, changeUserTypContent] = useState(basis !== null ? basis.label : "");
    const [showingDropdown, changeShowingDropdown] = useState(false);
    const [validationStyle, changeValidationStyle] = useState(autocomplete ? {} : {cursor: 'pointer', 'caretColor': 'transparent'});
    // the user can press enter to autocomplete
    // Stryker disable next-line all : there might be a good test for this but since showingDropdown doesn't render anything on fixedContent.length === 0 this might be harder to test
    const [ghostContent, changeGhostContent] = useState(filteredContent.length > 0 ? filteredContent[0] : null);
    
 
    // Stryker disable all
    const optionWrapperStyle = {
        position: 'absolute',
        left: '26px',
        'marginTop': '4px',
        'borderRadius': '2px',
        'overflowY': 'scroll',
        height: '200px',
    };
    // Stryker restore all
 
    let count = 0;
    const filterPrefix = (prefix) => {
        // filter the dropdown to only have the matching prefix options
        const prefixedContent = [];
        let isValidSelection = false;
        let overflow = true;
        let first = true;
 
        for(let i = 0; i < content.length; ++i){
            if(content[i].label.startsWith(prefix)){
                overflow = false;
                // the first element will be "semihovered" i.e. the user can press enter
                // autofill the text automatically
                if(first){
                    changeGhostContent(content[i]);
                    first = false;
                }
 
                // a direct match is found
                // NOTE: THIS ASSUMES THAT ALL CONTENT IS UNIQUE
                if(content[i].label === prefix){
                    changeSelectedContent(content[i]);
                    isValidSelection = true;
                }
 
                prefixedContent.push(content[i]);
            }
        }
        // if there was no direct match then there is no selectedContent
        if(!isValidSelection){
            changeSelectedContent(null);
        } 
        // no matches or prefix matches
        if(overflow){
            changeValidationStyle({color:"red"});
            changeGhostContent(null);
        } else {
            changeValidationStyle({});
        }
        changeFilteredContent(prefixedContent);
    };
 
    const internalOnChange = (event) => {
        if(autocomplete){
            // grab the userText 
            const newSelectedContent = event.target.value;
            changeUserTypContent(newSelectedContent);
            filterPrefix(newSelectedContent);
        }
 
        if (onChangeFunc) {
            onChangeFunc(event);
        }
    };
    const fillGhost = (event) => {
        if(event.key === "Enter" && ghostContent !== null) {
            changeUserTypContent(ghostContent.label);
            filterPrefix(ghostContent.label);
            changeShowingDropdown(false);
        } else {
            changeShowingDropdown(true);
        }
    };
    return (
        <div>
            {label}
            {content.length !== 0 && (
                <div>
                    <Form.Control
                        data-testid={`${testId}-test-dropdown-form`}
                        type="text"
                        value={autocomplete ? userTypedContent : (selectedContent !== null ? selectedContent.label : "") }
                        onChange={internalOnChange}
                        style={validationStyle}
                        onFocus={() => {
                            changeShowingDropdown(true);
                        }}
                        onClick={() => {
                            changeShowingDropdown(true);
                        }}
                        onKeyDown={autocomplete ? fillGhost : null}
                    />
                    {showingDropdown && (
                        <div data-testid = {`${testId}-wrapper`} style={optionWrapperStyle}>
                            {filteredContent.map((obj) => {
                                const key = obj.key;
                                const innerLabel = obj.label;
                                const select = () => {
                                    changeSelectedContent(obj);
                                    if(autocomplete){
                                        changeUserTypContent(innerLabel);
                                        changeGhostContent(obj);
                                        filterPrefix(innerLabel);
                                    }
                                    changeShowingDropdown(false);
                                };
                                return (
                                    <DropdownOption
                                        testid={`${testId}-dropdown-form-option-${count++}`}
                                        label={innerLabel}
                                        isSelected={selectedContent && key === selectedContent.key}
                                        isGhost={autocomplete ? (key === ghostContent.key) : false}
                                        rawKey = {key}
                                        // Stryker disable next-line all
                                        key ={`${key}-dropdown-option`}
                                        onClickFunc={select}
                                    ></DropdownOption>
                                );
                            })}
                        </div>
                    )}
                </div>
            )}
            {content.length === 0 && (
                <div>
                    <Form.Control
                        data-testid={`${testId}-test-dropdown-form`}
                        type="text"
                        disabled={true}
                        style={{ cursor: 'not-allowed' }}
                    />
                </div>
            )}
        </div>
    );
}