Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. // @flow
  2. import React, { memo } from "react";
  3. import Downshift from "downshift";
  4. import styled from "@emotion/styled";
  5. import { css } from "@emotion/core";
  6. import theme from "../../../theme";
  7.  
  8. type ListItemsprops = {
  9. label: string,
  10. value: any,
  11. props: any
  12. }
  13. type Props = {
  14. bg: string,
  15. listItems: Array<ListItemsprops>,
  16. icon?: Node,
  17. clsNameButton: string,
  18. clsNameMenu: string,
  19. clsNameMenuItem: string,
  20. onChange: function
  21. };
  22.  
  23. export const DropdownMenu = ({
  24. icon,
  25. listItems,
  26. bg = "rgba(255,255,255,0)",
  27. clsNameButton = "",
  28. clsNameMenu = "",
  29. clsNameMenuItem = "",
  30. onChange = fn => fn
  31. }: Props) => {
  32. return (
  33. <Downshift
  34. onChange={onChange}
  35. itemToString={item => (item ? item.value : '')}
  36. >
  37. {({
  38. getItemProps,
  39. getMenuProps,
  40. isOpen,
  41. highlightedIndex,
  42. selectedItem,
  43. getToggleButtonProps,
  44. getRootProps
  45. }) => {
  46. return (
  47. <Box {...getRootProps()}>
  48. <MenuToggle bg={bg} {...getToggleButtonProps()} className={clsNameButton}>
  49. {icon ? icon : (<><i /><i /><i /></>)}
  50. </MenuToggle>
  51. {isOpen
  52. ? (
  53. <Menu {...getMenuProps()} className={clsNameMenu}>
  54. {
  55. listItems
  56. .map((item, index) => (
  57. <MenuItem
  58. {...getItemProps({
  59. key: item.label,
  60. index,
  61. item,
  62. style: {
  63. color: highlightedIndex === index ? theme.colors.links : 'inherit',
  64. fontWeight: selectedItem === item ? '500' : '400',
  65. },
  66. })}
  67. className={clsNameMenuItem}
  68. {...item.props}
  69. >
  70. {item.value}
  71. </MenuItem>
  72. ))
  73. }
  74. </Menu>
  75. ) : null}
  76. </Box>
  77. )
  78. }}
  79. </Downshift>
  80. );
  81. };
  82.  
  83. const Box = styled.div`
  84. position: relative;
  85. width: 1em;
  86. height: 1em;
  87. z-index: 2;
  88. `;
  89. const MenuToggle = styled.div`
  90. width: 3rem;
  91. height: 3rem;
  92. border-radius: 3rem;
  93. cursor: pointer;
  94. position: absolute;
  95. left: 50%;
  96. top: 50%;
  97. transform: translate(-50%, -50%);
  98. display: flex;
  99. flex-direction: column;
  100. align-items: center;
  101. justify-content: center;
  102. background: ${({ bg }) => bg};
  103.  
  104. i {
  105. display: block;
  106. width: .4rem;
  107. height: .4rem;
  108. border-radius: .3rem;
  109. margin: 0.2rem auto;
  110. background: ${theme.colors.primary};
  111. opacity: 0.5;
  112. }
  113.  
  114. &:hover i {
  115. opacity: 0.75;
  116. }
  117. `;
  118. const Menu = styled.ul`
  119. position: absolute;
  120. top: 100%;
  121. right: 0;
  122. background: #fff;
  123. border-radius: 0.5rem;
  124. box-shadow: 0.5rem 0.5rem 0.5rem rgba(0, 0, 0, 0.15);
  125. padding: 1.2rem 0;
  126. margin: 1rem 0 0;
  127. min-width: 20rem;
  128. list-style: none;
  129. `;
  130. const MenuItem = styled.li`
  131. padding: .6rem 2.4rem;
  132. color: inherit;
  133.  
  134. &:hover {
  135. color: ${theme.colors.links}
  136. }
  137. `;
  138.  
  139. export default memo(DropdownMenu);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement