DropdownInput.jsx 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 { observer } from 'mobx-react'
  17. import styled from 'styled-components'
  18. import DropdownLink from '../DropdownLink'
  19. import TextInput from '../../atoms/TextInput'
  20. import Palette from '../../styleUtils/Palette'
  21. import StyleProps from '../../styleUtils/StyleProps'
  22. import arrowImage from './images/arrow'
  23. const Wrapper = styled.div`
  24. display: flex;
  25. align-items: center;
  26. border: 1px solid ${props => props.disabled ? Palette.grayscale[0] : props.highlight ? Palette.alert : Palette.grayscale[3]};
  27. border-radius: ${StyleProps.borderRadius};
  28. height: ${StyleProps.inputSizes.regular.height - 2}px;
  29. `
  30. const linkButtonStyle = props => {
  31. return {
  32. width: '60px',
  33. height: '14px',
  34. padding: '8px',
  35. background: props.disabled ? Palette.grayscale[0] : Palette.grayscale[1],
  36. borderTopLeftRadius: StyleProps.borderRadius,
  37. borderBottomLeftRadius: StyleProps.borderRadius,
  38. justifyContent: 'center',
  39. }
  40. }
  41. type ItemType = {
  42. label: string,
  43. value: string,
  44. [string]: any,
  45. }
  46. type Props = {
  47. items: ItemType[],
  48. selectedItem: string,
  49. onItemChange: (item: ItemType) => void,
  50. inputValue: string,
  51. onInputChange: (value: string) => void,
  52. placeholder?: string,
  53. highlight?: boolean,
  54. disabled?: boolean,
  55. }
  56. type State = {}
  57. @observer
  58. class DropdownInput extends React.Component<Props, State> {
  59. render() {
  60. return (
  61. <Wrapper highlight={this.props.highlight} disabled={this.props.disabled}>
  62. <DropdownLink
  63. linkButtonStyle={linkButtonStyle(this.props)}
  64. items={this.props.items}
  65. selectedItem={this.props.selectedItem}
  66. onChange={this.props.onItemChange}
  67. listWidth="auto"
  68. secondary
  69. disabled={this.props.disabled}
  70. arrowImage={arrowImage}
  71. />
  72. <TextInput
  73. embedded
  74. width="146px"
  75. style={{ paddingLeft: '8px', height: '30px' }}
  76. value={this.props.inputValue}
  77. onChange={e => { this.props.onInputChange(e.target.value) }}
  78. placeholder={this.props.placeholder}
  79. disabled={this.props.disabled}
  80. />
  81. </Wrapper>
  82. )
  83. }
  84. }
  85. export default DropdownInput