Dropdown.jsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. componentWillUpdate() {
  107. this.buttonRect = this.buttonRef.getBoundingClientRect()
  108. }
  109. componentDidUpdate() {
  110. this.updateListPosition()
  111. }
  112. componentWillUnmount() {
  113. window.removeEventListener('mousedown', this.handlePageClick, false)
  114. }
  115. getLabel(item) {
  116. let labelField = this.props.labelField || 'label'
  117. if (item === null || item === undefined) {
  118. return this.props.noSelectionMessage
  119. }
  120. return (item[labelField] !== null && item[labelField] !== undefined && item[labelField].toString()) || item.toString()
  121. }
  122. updateListPosition() {
  123. if (!this.state.showDropdownList || !this.listRef || !this.buttonRef) {
  124. return
  125. }
  126. let buttonHeight = this.buttonRef.offsetHeight
  127. let tipHeight = 8
  128. let listTop = this.buttonRect.top + buttonHeight + tipHeight
  129. let listHeight = this.listRef.offsetHeight
  130. if (listTop + listHeight > window.innerHeight) {
  131. listTop = window.innerHeight - listHeight - 10
  132. this.tipRef.style.display = 'none'
  133. } else {
  134. this.tipRef.style.display = 'block'
  135. }
  136. this.listRef.style.top = `${listTop + window.pageYOffset}px`
  137. this.listRef.style.left = `${this.buttonRect.left}px`
  138. }
  139. handlePageClick() {
  140. if (!this.itemMouseDown) {
  141. this.setState({ showDropdownList: false })
  142. }
  143. }
  144. handleButtonClick() {
  145. if (this.props.disabled) {
  146. return
  147. }
  148. this.setState({ showDropdownList: !this.state.showDropdownList })
  149. }
  150. handleItemClick(item) {
  151. this.setState({ showDropdownList: false, firstItemHover: false })
  152. if (this.props.onChange) {
  153. this.props.onChange(item)
  154. }
  155. }
  156. handleItemMouseEnter(index) {
  157. if (index === 0) {
  158. this.setState({ firstItemHover: true })
  159. }
  160. }
  161. handleItemMouseLeave(index) {
  162. if (index === 0) {
  163. this.setState({ firstItemHover: false })
  164. }
  165. }
  166. renderList() {
  167. if (!this.props.items || this.props.items.length === 0 || !this.state.showDropdownList) {
  168. return null
  169. }
  170. let selectedLabel = this.getLabel(this.props.selectedItem)
  171. let list = ReactDOM.createPortal((
  172. <List {...this.props} innerRef={ref => { this.listRef = ref }}>
  173. <Tip innerRef={ref => { this.tipRef = ref }} primary={this.state.firstItemHover} />
  174. <ListItems>
  175. {this.props.items.map((item, i) => {
  176. let label = this.getLabel(item)
  177. let listItem = (
  178. <ListItem
  179. key={label}
  180. onMouseDown={() => { this.itemMouseDown = true }}
  181. onMouseUp={() => { this.itemMouseDown = false }}
  182. onMouseEnter={() => { this.handleItemMouseEnter(i) }}
  183. onMouseLeave={() => { this.handleItemMouseLeave(i) }}
  184. onClick={() => { this.handleItemClick(item) }}
  185. selected={label === selectedLabel}
  186. >{label}
  187. </ListItem>
  188. )
  189. return listItem
  190. })}
  191. </ListItems>
  192. </List>
  193. ), document.body)
  194. return list
  195. }
  196. render() {
  197. let buttonValue = () => {
  198. if (this.props.items && this.props.items.length) {
  199. return this.getLabel(this.props.selectedItem)
  200. }
  201. return this.props.noItemsMessage || ''
  202. }
  203. return (
  204. <Wrapper
  205. className={this.props.className}
  206. onMouseDown={() => { this.itemMouseDown = true }}
  207. onMouseUp={() => { this.itemMouseDown = false }}
  208. >
  209. <DropdownButton
  210. {...this.props}
  211. innerRef={ref => { this.buttonRef = ref }}
  212. onMouseDown={() => { this.itemMouseDown = true }}
  213. onMouseUp={() => { this.itemMouseDown = false }}
  214. value={buttonValue()}
  215. onClick={() => this.handleButtonClick()}
  216. />
  217. {this.renderList()}
  218. </Wrapper>
  219. )
  220. }
  221. }
  222. export default Dropdown