AutocompleteInput.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 * as React from 'react'
  15. import styled, { css } from 'styled-components'
  16. import arrowImage from './images/arrow'
  17. import TextInput from '../TextInput'
  18. import Palette from '../../styleUtils/Palette'
  19. import StyleProps from '../../styleUtils/StyleProps'
  20. const getWidth = (props: any) => {
  21. if (props.width) {
  22. return props.width - 2
  23. }
  24. if (props.large) {
  25. return StyleProps.inputSizes.large.width - 2
  26. }
  27. return StyleProps.inputSizes.regular.width - 2
  28. }
  29. const getBorder = (props: any) => {
  30. if (props.embedded) {
  31. return css`border: none;`
  32. }
  33. return css`border: 1px solid ${props.highlight ? Palette.alert : props.disabled ? Palette.grayscale[0] : Palette.grayscale[3]};`
  34. }
  35. const Wrapper = styled.div<any>`
  36. display: flex;
  37. align-items: center;
  38. position: relative;
  39. width: ${props => (props.embedded ? 'calc(100% + 8px)' : `${getWidth(props)}px`)};
  40. height: ${props => (props.large ? StyleProps.inputSizes.large.height - 2
  41. : StyleProps.inputSizes.regular.height - 2)}px;
  42. ${props => getBorder(props)}
  43. border-radius: ${StyleProps.borderRadius};
  44. cursor: ${props => (props.disabled ? 'default' : 'pointer')};
  45. transition: all ${StyleProps.animations.swift};
  46. background: ${props => (props.disabled && !props.embedded ? Palette.grayscale[0] : 'white')};
  47. #dropdown-arrow-image {stroke: ${props => (props.disabled ? Palette.grayscale[3] : Palette.black)};}
  48. ${props => (props.focus ? css`border-color: ${Palette.primary};` : '')}
  49. ${props => (props.disabledLoading ? StyleProps.animations.disabledLoading : '')}
  50. `
  51. const Arrow = styled.div<any>`
  52. position: absolute;
  53. top: 8px;
  54. right: 8px;
  55. width: 16px;
  56. height: 16px;
  57. display: flex;
  58. justify-content: center;
  59. align-items: center;
  60. `
  61. type Props = {
  62. value: string,
  63. customRef?: (ref: HTMLElement) => void,
  64. ref?: (ref: HTMLElement) => void,
  65. onChange: (value: string) => void,
  66. disabled?: boolean,
  67. disabledLoading?: boolean,
  68. width?: number,
  69. large?: boolean,
  70. onFocus?: () => void,
  71. onBlur?: () => void,
  72. onInputKeyDown?: (e: React.KeyboardEvent<HTMLInputElement>) => void,
  73. highlight?: boolean,
  74. embedded?: boolean,
  75. }
  76. type State = {
  77. textInputFocus: boolean,
  78. }
  79. class AutocompleteInput extends React.Component<Props, State> {
  80. state: State = {
  81. textInputFocus: false,
  82. }
  83. textInputRef: HTMLElement | undefined
  84. render() {
  85. const disabled = this.props.disabled || this.props.disabledLoading
  86. return (
  87. <Wrapper
  88. large={this.props.large}
  89. width={this.props.width}
  90. focus={this.state.textInputFocus}
  91. highlight={this.props.highlight}
  92. disabled={disabled}
  93. disabledLoading={this.props.disabledLoading}
  94. embedded={this.props.embedded}
  95. ref={(e: HTMLElement) => {
  96. if (this.props.customRef) {
  97. this.props.customRef(e)
  98. } else if (this.props.ref) {
  99. this.props.ref(e)
  100. }
  101. }}
  102. >
  103. <TextInput
  104. data-test-id="acInput-text"
  105. disabled={disabled}
  106. value={this.props.value}
  107. onChange={e => { this.props.onChange(e.target.value) }}
  108. embedded
  109. width={this.props.embedded ? '100%' : this.props.width ? `${this.props.width - 40}px` : '180px'}
  110. style={{
  111. marginLeft: this.props.embedded ? 0 : '16px',
  112. paddingRight: this.props.embedded ? '32px' : '8px',
  113. }}
  114. height="29px"
  115. lineHeight="30px"
  116. placeholder="Type to search ..."
  117. onFocus={() => {
  118. if (this.props.onFocus) { this.props.onFocus() } this.setState({ textInputFocus: true })
  119. }}
  120. onBlur={() => {
  121. if (this.props.onBlur) { this.props.onBlur() } this.setState({ textInputFocus: false })
  122. }}
  123. _ref={(ref: HTMLElement | undefined) => { this.textInputRef = ref }}
  124. onInputKeyDown={this.props.onInputKeyDown}
  125. />
  126. <Arrow
  127. data-test-id="acInput-arrow"
  128. disabled={disabled}
  129. dangerouslySetInnerHTML={{ __html: arrowImage }}
  130. onClick={() => { if (this.textInputRef) this.textInputRef.focus() }}
  131. />
  132. </Wrapper>
  133. )
  134. }
  135. }
  136. export default AutocompleteInput