frontend/src/components/Dropdown.tsx

138 lines
3.5 KiB
TypeScript
Raw Normal View History

import React, { useState, useContext, useEffect, MouseEvent, ChangeEvent } from 'react';
2020-08-17 23:56:34 +02:00
import ClickAwayListener from '@material-ui/core/ClickAwayListener';
import { coursesContext } from '../contexts/CoursesProvider';
2020-10-21 20:42:18 +02:00
import { Course } from '../types';
2020-08-17 23:56:34 +02:00
import styled from 'styled-components';
2020-10-21 20:42:18 +02:00
const DropdownContainer = styled.div`
2020-10-30 00:42:40 +01:00
position: absolute;
left: 280px;
top: 65px;
z-index: 99;
min-width: 70%;
max-height: 420px;
border-radius:3px;
overflow-y: auto;
box-shadow: 0.05em 0.2em 0.6em rgba(0,0,0,.2);
scroll-snap-type: y mandatory;
scroll-behavior: smooth;
2020-10-08 20:21:52 +02:00
::-webkit-scrollbar-track {
border-radius: 10px;
background-color: #f5f5f5;
}
::-webkit-scrollbar {
width: 12px;
background-color: #f5f5f5;
}
::-webkit-scrollbar-thumb {
border-radius: 10px;
2020-10-30 00:42:40 +01:00
background-color: black;
2020-10-08 20:21:52 +02:00
border: 1px solid;
}
`;
2020-10-21 20:42:18 +02:00
const CourseContainer = styled.div`
2020-08-17 23:56:34 +02:00
padding: 5px;
padding-left: 20px;
2020-10-30 00:42:40 +01:00
background-color: #f2f4f7;
2020-08-17 23:56:34 +02:00
font-size: 18px;
2020-10-30 00:42:40 +01:00
font-weight: 500;
scroll-snap-align: end;
2020-08-17 23:56:34 +02:00
:hover {
2020-10-30 00:42:40 +01:00
background-color: #ECEEF4;
2020-08-17 23:56:34 +02:00
cursor: pointer;
}
`;
2020-10-30 00:42:40 +01:00
const Input = styled.input`
background-color: #F1F2F5;
font-size: 20px;
height: 100%;
width: 100%;
border: none;
&:focus {
outline: none;
}
`
2020-08-17 23:56:34 +02:00
2020-08-20 18:14:28 +02:00
interface DropdownProps {
clearInput: boolean;
2020-08-23 16:02:52 +02:00
handleClearInput: () => void;
2020-08-20 18:14:28 +02:00
}
2020-08-23 16:02:52 +02:00
export const Dropdown = ({ clearInput, handleClearInput }: DropdownProps) => {
2020-08-17 23:56:34 +02:00
2020-10-22 23:04:00 +02:00
const [open, setOpen] = useState(false);
const [input, setInput] = useState('');
2020-08-17 23:56:34 +02:00
//courses - choosenCourses
const [filteredCourses, setFilteredCourses] = useState<Array<Course>>([]);
const { courses, basket, addToBasket } = useContext(coursesContext)!;
2020-08-17 23:56:34 +02:00
useEffect(() => {
const filterCourses = (input: string) => {
const choosenCoursesNames = basket.map(({ name }) => name.trim());
2020-08-17 23:56:34 +02:00
const filteredCourses = courses.filter(
2020-10-22 23:04:00 +02:00
({ name }) =>
name
.toLowerCase()
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.includes(
input
.toLowerCase()
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, ''),
) && !choosenCoursesNames.includes(name),
2020-08-17 23:56:34 +02:00
);
setFilteredCourses(filteredCourses);
};
filterCourses(input);
}, [input, open, basket]);
2020-08-17 23:56:34 +02:00
2020-08-23 16:02:52 +02:00
useEffect(() => {
clearInput && (setInput(''), handleClearInput());
2020-08-23 16:02:52 +02:00
}, [clearInput]);
2020-08-20 18:14:28 +02:00
const handleChange = (event: ChangeEvent<HTMLInputElement>) => setInput(event.target.value);
2020-08-17 23:56:34 +02:00
const handleClick = () => setOpen(true);
const handleClickAway = () => setOpen(false);
const onCourseClick = async (event: MouseEvent) => {
const target = event.currentTarget;
if (target.id && target.textContent) {
const course = filteredCourses.find(({ id }) => id.toString() === target.id)!;
console.log('added course is');
console.log(course);
2020-08-23 16:10:10 +02:00
addToBasket(course);
2020-08-17 23:56:34 +02:00
setOpen(false);
}
};
return (
2020-10-30 00:42:40 +01:00
// <ClickAwayListener onClickAway={handleClickAway}>
<>
2020-08-17 23:56:34 +02:00
<Input
2020-10-30 00:42:40 +01:00
placeholder="Wyszukaj przedmiot..."
2020-08-17 23:56:34 +02:00
onChange={handleChange}
onClick={handleClick}
value={input}
/>
{open && (
2020-10-21 20:42:18 +02:00
<DropdownContainer>
2020-08-17 23:56:34 +02:00
{filteredCourses.map(({ name, id }, index) => (
2020-10-21 20:42:18 +02:00
<CourseContainer key={index} id={id.toString()} onClick={onCourseClick}>
2020-08-17 23:56:34 +02:00
<p>{name} </p>
2020-10-21 20:42:18 +02:00
</CourseContainer>
2020-08-17 23:56:34 +02:00
))}
2020-10-21 20:42:18 +02:00
</DropdownContainer>
2020-08-17 23:56:34 +02:00
)}
2020-10-30 00:42:40 +01:00
</>
// </ClickAwayListener>
2020-08-17 23:56:34 +02:00
);
};