DropdownButton.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 styled, { css } from 'styled-components'
  16. import arrowImage from './images/arrow'
  17. import { ThemePalette, ThemeProps } from '../../../Theme'
  18. const getLabelColor = (props: any) => {
  19. if (props.disabled) {
  20. return ThemePalette.grayscale[3]
  21. }
  22. if (props.primary || props.secondary) {
  23. return 'white'
  24. }
  25. return ThemePalette.black
  26. }
  27. const Label = styled.div<any>`
  28. color: ${(props: any) => getLabelColor(props)};
  29. margin: 0 32px 0 ${(props: any) => (props.embedded ? 0 : 16)}px;
  30. overflow: hidden;
  31. text-overflow: ellipsis;
  32. white-space: nowrap;
  33. flex-grow: 1;
  34. ${(props: any) => (props.useBold ? `font-weight: ${ThemeProps.fontWeights.medium};` : '')}
  35. ${(props: any) => (props.centered ? 'text-align: center;' : '')}
  36. `
  37. const getBackgroundColor = (props: any) => {
  38. if (props.embedded) {
  39. return 'white'
  40. }
  41. if (props.disabled) {
  42. return ThemePalette.grayscale[0]
  43. }
  44. if (props.secondary) {
  45. return ThemePalette.secondaryLight
  46. }
  47. if (props.primary) {
  48. return ThemePalette.primary
  49. }
  50. return 'white'
  51. }
  52. const getArrowColor = (props: any) => {
  53. if (props.disabled) {
  54. return ThemePalette.grayscale[3]
  55. }
  56. if (props.primary || props.secondary) {
  57. return 'white'
  58. }
  59. if (props.outline) {
  60. return ThemePalette.primary
  61. }
  62. return ThemePalette.black
  63. }
  64. const getWidth = (props: any) => {
  65. if (props.large) {
  66. return ThemeProps.inputSizes.large.width - 2
  67. }
  68. if (props.width) {
  69. return props.width - 2
  70. }
  71. return ThemeProps.inputSizes.regular.width - 2
  72. }
  73. const borderColor = (props: any) => {
  74. if (props.highlight) {
  75. return ThemePalette.alert
  76. }
  77. if (props.disabled) {
  78. return ThemePalette.grayscale[0]
  79. }
  80. if (props.primary) {
  81. return ThemePalette.primary
  82. }
  83. if (props.secondary) {
  84. return ThemePalette.secondaryLight
  85. }
  86. if (props.outline) {
  87. return ThemePalette.primary
  88. }
  89. return ThemePalette.grayscale[3]
  90. }
  91. const backgroundHover = (props: any) => {
  92. if (props.disabled || props.embedded) {
  93. return ''
  94. }
  95. if (props.secondary) {
  96. return ThemePalette.secondaryLight
  97. }
  98. return ThemePalette.primary
  99. }
  100. const Wrapper = styled.div<any>`
  101. display: flex;
  102. align-items: center;
  103. position: relative;
  104. width: ${(props: any) => getWidth(props)}px;
  105. height: ${(props: any) => (props.large ? ThemeProps.inputSizes.large.height - 2
  106. : ThemeProps.inputSizes.regular.height - 2)}px;
  107. border: 1px solid ${props => borderColor(props)};
  108. border-radius: ${ThemeProps.borderRadius};
  109. cursor: ${(props: any) => (props.disabled ? 'default' : 'pointer')};
  110. transition: all ${ThemeProps.animations.swift};
  111. background: ${props => getBackgroundColor(props)};
  112. ${(props: any) => (props.embedded ? css`
  113. border: 0;
  114. width: calc(100% + 8px);
  115. ` : '')}
  116. #dropdown-arrow-image {stroke: ${(props: any) => getArrowColor(props)};}
  117. &:hover {
  118. #dropdown-arrow-image {stroke: ${(props: any) => (props.disabled ? '' : props.embedded ? '' : 'white')};}
  119. background: ${(props: any) => backgroundHover(props)};
  120. }
  121. &:hover ${Label} {
  122. color: ${(props: any) => (props.disabled ? '' : props.embedded ? '' : 'white')};
  123. }
  124. ${(props: any) => (props.disabledLoading ? ThemeProps.animations.disabledLoading : '')}
  125. `
  126. const Arrow = styled.div<any>`
  127. position: absolute;
  128. right: 8px;
  129. top: 8px;
  130. display: flex;
  131. width: 16px;
  132. height: 16px;
  133. justify-content: center;
  134. align-items: center;
  135. `
  136. type Props = {
  137. value: string,
  138. onClick?: (event: Event) => void,
  139. customRef?: (ref: HTMLElement) => void,
  140. ref?: (ref: HTMLElement) => void,
  141. arrowRef?: (ref: HTMLElement) => void,
  142. className?: string,
  143. disabled?: boolean,
  144. disabledLoading?: boolean,
  145. 'data-test-id'?: string,
  146. embedded?: boolean,
  147. highlight?: boolean,
  148. secondary?: boolean,
  149. centered?: boolean,
  150. outline?: boolean,
  151. primary?: boolean,
  152. width?: number,
  153. useBold?: boolean,
  154. onMouseDown?: () => void,
  155. onMouseUp?: () => void,
  156. }
  157. class DropdownButton extends React.Component<Props> {
  158. render() {
  159. const disabled = this.props.disabled || this.props.disabledLoading
  160. return (
  161. <Wrapper
  162. data-test-id={this.props['data-test-id'] || 'dropdownButton'}
  163. // eslint-disable-next-line react/jsx-props-no-spreading
  164. {...this.props}
  165. disabled={disabled}
  166. disabledLoading={this.props.disabledLoading}
  167. ref={(e: HTMLElement) => {
  168. if (this.props.customRef) {
  169. this.props.customRef(e)
  170. } else if (this.props.ref) {
  171. this.props.ref(e)
  172. }
  173. }}
  174. onClick={(e: Event) => {
  175. if (!disabled && this.props.onClick) this.props.onClick(e)
  176. }}
  177. >
  178. <Label
  179. // eslint-disable-next-line react/jsx-props-no-spreading
  180. {...this.props}
  181. onClick={() => { }}
  182. ref={() => { }}
  183. data-test-id="dropdownButton-value"
  184. disabled={disabled}
  185. >
  186. {this.props.value}
  187. </Label>
  188. <Arrow
  189. // eslint-disable-next-line react/jsx-props-no-spreading
  190. {...this.props}
  191. ref={(ref: HTMLElement) => { if (this.props.arrowRef) this.props.arrowRef(ref) }}
  192. onClick={() => { }}
  193. data-test-id=""
  194. disabled={disabled}
  195. dangerouslySetInnerHTML={{ __html: arrowImage }}
  196. />
  197. </Wrapper>
  198. )
  199. }
  200. }
  201. export default DropdownButton