import { Dropdown } from "react-hartan"
function App() {
return (
<>
<Dropdown></Dropdown>
</>
)
}
export default App
This is the code for Dropdown component.
import useDropdown from "../../Hooks/Dropdown"
import dropdownStyle from "./Dropdown.module.css"
import PropTypes from "prop-types"
const list = [
"Item 1",
"Item 2",
"Item 3",
"Item 4"
];
function onClickItem(val){
console.log(val);
}
export default function Dropdown({ listItem = list, buttonText = "Dropdown", onClickFunction = onClickItem, id, userDropdownStyle, userDropdownListStyle, userDropdownListItemStyle, userButtonStyle }) {
const [isOpen, setIsOpen, handleDropdownButtonClick, dropdownText] = useDropdown(buttonText, onClickFunction);
return (
<section className={`${dropdownStyle.dropdown} ${userDropdownStyle}`}>
<button className={`${dropdownStyle.dropdownBtn} ${userButtonStyle}`} onClick={() => setIsOpen(!isOpen)}>{dropdownText}
{
isOpen ? (
<span>
<svg xmlns="http://www.w3.org/2000/svg" height="14" width="12.25" viewBox="0 0 448 512"><path d="M201.4 137.4c12.5-12.5 32.8-12.5 45.3 0l160 160c12.5 12.5 12.5 32.8 0 45.3s-32.8 12.5-45.3 0L224 205.3 86.6 342.6c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3l160-160z" /></svg>
</span>
) :
(
<span>
<svg xmlns="http://www.w3.org/2000/svg" height="14" width="12.25" viewBox="0 0 448 512"><path fill="#000000" d="M201.4 342.6c12.5 12.5 32.8 12.5 45.3 0l160-160c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0L224 274.7 86.6 137.4c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3l160 160z" /></svg>
</span>
)
}
</button>
<ul className={`${dropdownStyle.dropdownList} ${userDropdownListStyle}`}>
{
isOpen && (
listItem.map((listText, id) => {
return <li className={`${dropdownStyle.dropdownListItem} ${userDropdownListItemStyle}`} key={id} onClick={handleDropdownButtonClick}><span>{listText}</span></li>
})
)
}
</ul>
</section>
)
}
Dropdown.propTypes = {
listItem: PropTypes.arrayOf(PropTypes.node),
buttonText: PropTypes.node,
onClickFunction: PropTypes.func,
id: PropTypes.string,
userDropdownStyle: PropTypes.string,
userButtonStyle: PropTypes.string,
userDropdownList: PropTypes.string,
userDropdownListItem: PropTypes.string
};
.dropdown {
width: 16rem;
}
/* Opening/Closing Dropdown button */
.dropdownBtn {
background-color: #1e90ff;
padding: .75rem 1.25rem;
display: flex;
justify-content: space-between;
align-items: center;
border-radius: .5rem;
width: 100%;
font-size: 1.5rem;
font-weight: 600;
cursor: pointer;
border: 2px solid transparent;
}
/* dropdown icon change on open/close */
.dropdownBtn span {
display: flex;
justify-content: center;
align-items: center;
}
/* dropdown list container */
.dropdownList {
background-color: #1e90ff;
margin-top: .5rem;
border-radius: .5rem;
}
/* each item inside the dropdown list container */
.dropdownListItem {
list-style: none;
padding: .75rem 1rem;
cursor: pointer;
}
/* span text of each item inside the dropdown list container */
.dropdownListItem span {
text-decoration: none;
color: #000000;
font-weight: 500;
font-size: 1.5rem;
transition: .1s;
}