Dropdown.jsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import React from 'react'
  15. import PropTypes from 'prop-types'
  16. import styled from 'styled-components'
  17. import ReactDOM from 'react-dom'
  18. import { DropdownButton } from 'components'
  19. import Palette from '../../styleUtils/Palette'
  20. import StyleProps from '../../styleUtils/StyleProps'
  21. const Wrapper = styled.div`
  22. position: relative;
  23. `
  24. const getWidth = props => {
  25. if (props.large) {
  26. return StyleProps.inputSizes.large.width - 2
  27. }
  28. if (props.width) {
  29. return props.width - 2
  30. }
  31. return StyleProps.inputSizes.regular.width - 2
  32. }
  33. const List = styled.div`
  34. position: absolute;
  35. background: white;
  36. cursor: pointer;
  37. width: ${props => getWidth(props)}px;
  38. border: 1px solid ${Palette.grayscale[3]};
  39. border-radius: ${StyleProps.borderRadius};
  40. z-index: 1000;
  41. `
  42. const ListItems = styled.div`
  43. max-height: 400px;
  44. overflow: auto;
  45. `
  46. const Tip = styled.div`
  47. position: absolute;
  48. width: 10px;
  49. height: 10px;
  50. background: ${props => props.primary ? Palette.primary : 'white'};
  51. border-top: 1px solid ${Palette.grayscale[3]};
  52. border-left: 1px solid ${Palette.grayscale[3]};
  53. border-bottom: 1px solid ${props => props.primary ? Palette.primary : 'white'};
  54. border-right: 1px solid ${props => props.primary ? Palette.primary : 'white'};
  55. transform: rotate(45deg);
  56. right: 8px;
  57. top: -6px;
  58. z-index: 11;
  59. transition: all ${StyleProps.animations.swift};
  60. `
  61. const ListItem = styled.div`
  62. position: relative;
  63. color: ${Palette.grayscale[4]};
  64. padding: 8px 16px;
  65. transition: all ${StyleProps.animations.swift};
  66. ${props => props.selected ? `font-weight: ${StyleProps.fontWeights.medium};` : ''}
  67. &:first-child {
  68. border-top-left-radius: ${StyleProps.borderRadius};
  69. border-top-right-radius: ${StyleProps.borderRadius};
  70. }
  71. &:last-child {
  72. border-bottom-left-radius: ${StyleProps.borderRadius};
  73. border-bottom-right-radius: ${StyleProps.borderRadius};
  74. }
  75. &:hover {
  76. background: ${Palette.primary};
  77. color: white;
  78. }
  79. `
  80. class Dropdown extends React.Component {
  81. static propTypes = {
  82. selectedItem: PropTypes.any,
  83. items: PropTypes.array,
  84. labelField: PropTypes.string,
  85. className: PropTypes.string,
  86. onChange: PropTypes.func,
  87. noItemsMessage: PropTypes.string,
  88. noSelectionMessage: PropTypes.string,
  89. disabled: PropTypes.bool,
  90. width: PropTypes.number,
  91. }
  92. static defaultProps = {
  93. noSelectionMessage: 'Select an item',
  94. }
  95. constructor() {
  96. super()
  97. this.state = {
  98. showDropdownList: false,
  99. }
  100. this.handlePageClick = this.handlePageClick.bind(this)
  101. }
  102. componentDidMount() {
  103. window.addEventListener('mousedown', this.handlePageClick, false)
  104. this.buttonRect = this.buttonRef.getBoundingClientRect()
  105. }
  106. componentDidUpdate() {
  107. this.updateListPosition()
  108. }
  109. componentWillUnmount() {
  110. window.removeEventListener('mousedown', this.handlePageClick, false)
  111. }
  112. getLabel(item) {
  113. let labelField = this.props.labelField || 'label'
  114. if (item === null || item === undefined) {
  115. return this.props.noSelectionMessage
  116. }
  117. return (item[labelField] !== null && item[labelField] !== undefined && item[labelField].toString()) || item.toString()
  118. }
  119. updateListPosition() {
  120. if (!this.state.showDropdownList || !this.listRef || !this.buttonRef) {
  121. return
  122. }
  123. let buttonHeight = this.buttonRef.offsetHeight
  124. let tipHeight = 8
  125. let listTop = this.buttonRect.top + buttonHeight + tipHeight
  126. let listHeight = this.listRef.offsetHeight
  127. if (listTop + listHeight > window.innerHeight) {
  128. listTop = window.innerHeight - listHeight - 10
  129. this.tipRef.style.display = 'none'
  130. } else {
  131. this.tipRef.style.display = 'block'
  132. }
  133. this.listRef.style.top = `${listTop}px`
  134. this.listRef.style.left = `${this.buttonRect.left}px`
  135. }
  136. handlePageClick() {
  137. if (!this.itemMouseDown) {
  138. this.setState({ showDropdownList: false })
  139. }
  140. }
  141. handleButtonClick() {
  142. if (this.props.disabled) {
  143. return
  144. }
  145. this.setState({ showDropdownList: !this.state.showDropdownList })
  146. }
  147. handleItemClick(item) {
  148. this.setState({ showDropdownList: false, firstItemHover: false })
  149. if (this.props.onChange) {
  150. this.props.onChange(item)
  151. }
  152. }
  153. handleItemMouseEnter(index) {
  154. if (index === 0) {
  155. this.setState({ firstItemHover: true })
  156. }
  157. }
  158. handleItemMouseLeave(index) {
  159. if (index === 0) {
  160. this.setState({ firstItemHover: false })
  161. }
  162. }
  163. renderList() {
  164. if (!this.props.items || this.props.items.length === 0 || !this.state.showDropdownList) {
  165. return null
  166. }
  167. let selectedLabel = this.getLabel(this.props.selectedItem)
  168. let list = ReactDOM.createPortal((
  169. <List {...this.props} innerRef={ref => { this.listRef = ref }}>
  170. <Tip innerRef={ref => { this.tipRef = ref }} primary={this.state.firstItemHover} />
  171. <ListItems>
  172. {this.props.items.map((item, i) => {
  173. let label = this.getLabel(item)
  174. let listItem = (
  175. <ListItem
  176. key={label}
  177. onMouseDown={() => { this.itemMouseDown = true }}
  178. onMouseUp={() => { this.itemMouseDown = false }}
  179. onMouseEnter={() => { this.handleItemMouseEnter(i) }}
  180. onMouseLeave={() => { this.handleItemMouseLeave(i) }}
  181. onClick={() => { this.handleItemClick(item) }}
  182. selected={label === selectedLabel}
  183. >{label}
  184. </ListItem>
  185. )
  186. return listItem
  187. })}
  188. </ListItems>
  189. </List>
  190. ), document.body)
  191. return list
  192. }
  193. render() {
  194. let buttonValue = () => {
  195. if (this.props.items && this.props.items.length) {
  196. return this.getLabel(this.props.selectedItem)
  197. }
  198. return this.props.noItemsMessage || ''
  199. }
  200. return (
  201. <Wrapper
  202. className={this.props.className}
  203. onMouseDown={() => { this.itemMouseDown = true }}
  204. onMouseUp={() => { this.itemMouseDown = false }}
  205. >
  206. <DropdownButton
  207. {...this.props}
  208. innerRef={ref => { this.buttonRef = ref }}
  209. onMouseDown={() => { this.itemMouseDown = true }}
  210. onMouseUp={() => { this.itemMouseDown = false }}
  211. value={buttonValue()}
  212. onClick={() => this.handleButtonClick()}
  213. />
  214. {this.renderList()}
  215. </Wrapper>
  216. )
  217. }
  218. }
  219. export default Dropdown