Skip to content

Commit

Permalink
Allow typing for profs and majors
Browse files Browse the repository at this point in the history
  • Loading branch information
jacquelinecai committed Dec 20, 2024
1 parent 85d07fb commit 311f155
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 93 deletions.
102 changes: 57 additions & 45 deletions client/src/modules/Course/Components/MultiSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,82 +11,94 @@ const MultiSelect = ({
placeholder,
onChange
}: SelectProps) => {
const [highlightedIndex, setHighlightedIndex] = useState<number>(0);
const [searchTerm, setSearchTerm] = useState<string>('');
const [open, setOpen] = useState<boolean>(false);

// helpers

const selected = (option: string) => {
return value.includes(option);
};

// logic controls:
const filteredOptions = searchTerm.length !== 0
? options.filter((option) =>
option.toLowerCase().includes(searchTerm.toLowerCase())
)
: options;

const handleDropdown = () => {
setOpen(!open);
};

const handleSelect = (option: string) => {
if (value.includes(option)) {
onChange(value.filter((selection) => selection !== option));
} else {
if (!value.includes(option)) {
onChange([...value, option]);
}

setSearchTerm('');
setOpen(false);
};

const handleDelete = (option: string) => {
onChange(value.filter((opt: string) => opt !== option));
};

const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setSearchTerm(e.target.value);
if (!open) setOpen(true);
};

const handleBlur = (e: React.FocusEvent) => {
if (!e.currentTarget.contains(e.relatedTarget)) {
setOpen(false);
}
};

return (
<div
className={styles.select}
tabIndex={0}
onBlur={() => setOpen(false)}
onBlur={handleBlur}
onClick={handleDropdown}
>
<div className={styles.values}>
{value.length > 0 ? (
value.map((selection) => (
<div className={styles.value} key={selection}>
{' '}
{selection}{' '}
<img
onClick={() => handleDelete(selection)}
src={closeIcon}
alt="close"
/>
</div>
))
) : (
<div className={styles.placeholder}> {placeholder} </div>
)}
{value.map((selection) => (
<div className={styles.value} key={selection}>
{selection}
<img
onClick={(e) => {
e.stopPropagation();
handleDelete(selection);
}}
src={closeIcon}
alt="close"
/>
</div>
))}
<input
type="text"
className={styles.searchInput}
value={searchTerm}
onChange={handleInputChange}
placeholder={value.length === 0 ? placeholder : ''}
/>
<img
className={styles.dropdownicon}
src={dropdownIcon}
alt="drop-down-icon"
alt="dropdown"
/>
</div>
{open && (
<ul className={styles.options}>
{options.map((option, index) => (
<li
className={`${styles.option} ${
selected(option) && styles.selected
} ${index === highlightedIndex && styles.highlighted}`}
key={option}
onClick={(e) => {
e.stopPropagation();
handleSelect(option);
}}
onMouseEnter={() => setHighlightedIndex(index)}
>
{' '}
{option}{' '}
</li>
))}
{filteredOptions.length > 0 ? (
filteredOptions.map((option) => (
<li
className={styles.option}
key={option}
onClick={(e) => {
e.stopPropagation();
handleSelect(option);
}}
>
{option}
</li>
))
) : (
<li className={styles.noOptions}>No options found</li>
)}
</ul>
)}
</div>
Expand Down
84 changes: 36 additions & 48 deletions client/src/modules/Course/Styles/Select.module.css
Original file line number Diff line number Diff line change
@@ -1,107 +1,89 @@
.select {
width: 100%;
max-width: 456px;

border: 1px solid var(--clr-blue-300);
border-radius: 10px;
padding: 10px;

padding: 8px 10px;
display: flex;
align-items: center;
gap: 0.5em;

outline: none;

position: relative;

outline: none;
cursor: pointer;
}

.placeholder {
color: rgb(161, 161, 161);
position: absolute;
top: 50%;
left: 10px;
transform: translateY(-50%);
pointer-events: none;
font-size: 14px;
}

.values {
width: 100%;
flex: 1;
display: flex;
flex-flow: row wrap;
flex-wrap: wrap;
align-items: center;
gap: 0.5em;
overflow-x: auto;
}

.single-value {
.searchInput {
flex: 1;
border: none;
outline: none;
font-size: 14px;
padding: 4px 0;
min-height: 24px;
}

.value {
background: rgb(238, 238, 238);
padding: 6px;
padding: 6px 8px;
border-radius: 4px;

display: flex;
flex-flow: row nowrap;
align-items: center;
gap: 8px;

font-size: 14px;
}

.value > img {
.value img {
width: 10px;
/* margin-bottom: -1px; */
cursor: pointer;
}

.dropdownicon {
margin-left: auto;
width: 12px;
cursor: pointer;
}

.select:focus {
border: 3px solid var(--clr-blue-400);
padding: 8px;
.select:focus-within {
border: 2px solid var(--clr-blue-400);
}

.options {
display: flex;
flex-flow: column nowrap;

flex-direction: column;
position: absolute;
margin: 0;
padding: 8px;
list-style-type: none;
max-height: 200px;
overflow-y: auto;
border: 0.5px solid rgb(225, 225, 225);
border-radius: 4%;
width: 100%;
left: 0;
top: calc(100% + 0.55em);
background: #fff;
z-index: 101;
}

.gradeoptions {
display: flex;
flex-flow: column nowrap;

position: absolute;
margin: 0;
padding: 8px;
list-style-type: none;
max-height: 200px;
overflow-y: auto;
border: 0.5px solid rgb(225, 225, 225);
border-radius: 4%;
border: 1px solid var(--clr-blue-300);
border-radius: 10px;
width: 100%;
left: 0;
bottom: calc(100% + 0.55em);
top: calc(100% + 5px);
background: #fff;
z-index: 101;
z-index: 10;
}

.option {
padding: 0.8em 1em;
cursor: pointer;

font-size: 14px;
border-radius: 5px;
}

Expand All @@ -112,3 +94,9 @@
.option.highlighted {
background-color: var(--clr-blue-200);
}

.noOptions {
padding: 0.8em 1em;
color: rgb(161, 161, 161);
text-align: center;
}

0 comments on commit 311f155

Please sign in to comment.