AutocompleteInput.jsx 4.2 KB

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