AutocompleteInput.jsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.js'
  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 Wrapper = styled.div`
  31. display: flex;
  32. align-items: center;
  33. position: relative;
  34. width: ${props => getWidth(props)}px;
  35. height: ${props => props.large ? StyleProps.inputSizes.large.height - 2
  36. : StyleProps.inputSizes.regular.height - 2}px;
  37. border: 1px solid ${props => props.highlight ? Palette.alert : props.disabled ? Palette.grayscale[0] : Palette.grayscale[3]};
  38. border-radius: ${StyleProps.borderRadius};
  39. cursor: ${props => props.disabled ? 'default' : 'pointer'};
  40. transition: all ${StyleProps.animations.swift};
  41. background: ${props => props.disabled ? Palette.grayscale[0] : 'white'};
  42. #dropdown-arrow-image {stroke: ${props => props.disabled ? Palette.grayscale[0] : Palette.grayscale[4]};}
  43. ${props => props.focus ? css`border-color: ${Palette.primary};` : ''}
  44. `
  45. const Arrow = styled.div`
  46. position: absolute;
  47. top: 0;
  48. right: 0;
  49. width: 30px;
  50. height: 30px;
  51. display: flex;
  52. justify-content: center;
  53. align-items: center;
  54. `
  55. type Props = {
  56. value: string,
  57. customRef?: (ref: HTMLElement) => void,
  58. innerRef?: (ref: HTMLElement) => void,
  59. onChange: (value: string) => void,
  60. onClick?: () => void,
  61. disabled?: boolean,
  62. width?: number,
  63. large?: boolean,
  64. onFocus?: () => void,
  65. highlight?: boolean,
  66. }
  67. type State = {
  68. textInputFocus: boolean,
  69. }
  70. class AutocompleteInput extends React.Component<Props, State> {
  71. state = {
  72. textInputFocus: false,
  73. }
  74. render() {
  75. return (
  76. <Wrapper
  77. large={this.props.large}
  78. width={this.props.width}
  79. focus={this.state.textInputFocus}
  80. highlight={this.props.highlight}
  81. disabled={this.props.disabled}
  82. innerRef={e => {
  83. if (this.props.customRef) {
  84. this.props.customRef(e)
  85. } else if (this.props.innerRef) {
  86. this.props.innerRef(e)
  87. }
  88. }}
  89. >
  90. <TextInput
  91. data-test-id="acInput-text"
  92. disabled={this.props.disabled}
  93. value={this.props.value}
  94. onChange={e => { this.props.onChange(e.target.value) }}
  95. embedded
  96. width={this.props.width ? `${this.props.width - 40}px` : '180px'}
  97. style={{ marginLeft: '16px' }}
  98. height="29px"
  99. lineHeight="30px"
  100. placeholder="Type to search ..."
  101. onFocus={() => { if (this.props.onFocus) { this.props.onFocus() } this.setState({ textInputFocus: true }) }}
  102. onBlur={() => { this.setState({ textInputFocus: false }) }}
  103. />
  104. <Arrow
  105. data-test-id="acInput-arrow"
  106. disabled={this.props.disabled}
  107. dangerouslySetInnerHTML={{ __html: arrowImage }}
  108. onClick={this.props.onClick}
  109. />
  110. </Wrapper>
  111. )
  112. }
  113. }
  114. export default AutocompleteInput