From ab1892a7104204659fb14a8c8944c066b35e2f3f Mon Sep 17 00:00:00 2001 From: rohan09-raj Date: Mon, 19 Feb 2024 20:51:05 +0530 Subject: feat: Add a custom select component - We will use this as default html select component is not very customizable Co-authored-by: Blaster4385 --- .../src/components/CustomSelect/CustomSelect.jsx | 42 ++++++++++ .../CustomSelect/CustomSelect.module.css | 32 ++++++++ client/src/components/Editor/Editor.jsx | 88 ++++++++++----------- client/src/components/Editor/Editor.module.css | 30 +------ client/src/components/Header/Header.jsx | 22 ++++-- client/src/components/Header/Header.module.css | 29 ++++++- client/src/pages/Home/Home.jsx | 1 - client/src/utils/constants.js | 91 ++++++++++++++++------ 8 files changed, 227 insertions(+), 108 deletions(-) create mode 100644 client/src/components/CustomSelect/CustomSelect.jsx create mode 100644 client/src/components/CustomSelect/CustomSelect.module.css diff --git a/client/src/components/CustomSelect/CustomSelect.jsx b/client/src/components/CustomSelect/CustomSelect.jsx new file mode 100644 index 0000000..9969a6d --- /dev/null +++ b/client/src/components/CustomSelect/CustomSelect.jsx @@ -0,0 +1,42 @@ +import React, { useState } from "react"; +import styles from "./CustomSelect.module.css"; + +const CustomSelect = ({ options, onSelect }) => { + const [isOpen, setIsOpen] = useState(false); + const [selectedOption, setSelectedOption] = useState( + options.length > 0 ? options[0] : null + ); + + const toggleDropdown = () => { + setIsOpen(!isOpen); + }; + + const handleOptionClick = (option) => { + setSelectedOption(option); + onSelect(option.value); + setIsOpen(false); + }; + + return ( +
+
+ {selectedOption ? selectedOption.label : "Select an option"} +
+ {isOpen && ( +
+ {options.map((option) => ( +
handleOptionClick(option)} + > + {option.label} +
+ ))} +
+ )} +
+ ); +}; + +export default CustomSelect; diff --git a/client/src/components/CustomSelect/CustomSelect.module.css b/client/src/components/CustomSelect/CustomSelect.module.css new file mode 100644 index 0000000..a3b6520 --- /dev/null +++ b/client/src/components/CustomSelect/CustomSelect.module.css @@ -0,0 +1,32 @@ +.select { + position: relative; + display: inline-block; +} + +.selected__option { + cursor: pointer; + padding: 10px; + border: 1px solid #ccc; + border-radius: 4px; +} + +.options { + position: absolute; + top: 100%; + left: 0; + width: 100%; + border: 1px solid #ccc; + border-top: none; + border-radius: 0 0 4px 4px; + background-color: #ebdbb2; + z-index: 1; +} + +.option { + padding: 10px; + color: #282828; + cursor: pointer; + &:hover { + background-color: #f0f0f0; + } +} diff --git a/client/src/components/Editor/Editor.jsx b/client/src/components/Editor/Editor.jsx index 143abd3..29294a0 100644 --- a/client/src/components/Editor/Editor.jsx +++ b/client/src/components/Editor/Editor.jsx @@ -3,7 +3,8 @@ import { useNavigate, useParams } from "react-router-dom"; import Prism from "prismjs"; import styles from "./Editor.module.css"; import "../prism-themes/prism-gruvbox-dark.css"; -import { SERVER_BASE_URL, SUPPORTED_LANGUAGES } from "../../utils/constants"; +import { SERVER_BASE_URL } from "../../utils/constants"; +import Header from "../Header/Header"; const Editor = () => { const { id } = useParams(); @@ -42,8 +43,8 @@ const Editor = () => { } }; - const handleLanguageChange = (event) => { - setLanguage(event.target.value); + const handleLanguageChange = (value) => { + setLanguage(value); }; useEffect(() => { @@ -71,59 +72,48 @@ const Editor = () => { const lines = useMemo(() => text.split("\n"), [text]); return ( -
- {!id && ( - <> - + <> +
+
+ {!id && ( - - )} + )} -
-
- {lines.map((_, index) => ( -
- {index + 1} -
- ))} -
-
-