TextInput.jsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 * as React from 'react'
  16. import styled, { css } from 'styled-components'
  17. import Palette from '../../styleUtils/Palette'
  18. import StyleProps from '../../styleUtils/StyleProps'
  19. import closeImage from './images/close.svg'
  20. import requiredImage from './images/required.svg'
  21. const Wrapper = styled.div`
  22. position: relative;
  23. `
  24. const Required = styled.div`
  25. position: absolute;
  26. width: 8px;
  27. height: 8px;
  28. right: -16px;
  29. top: 12px;
  30. background: url('${requiredImage}') center no-repeat;
  31. `
  32. const getInputWidth = props => {
  33. if (props.width) {
  34. return typeof props.width === 'number' ? `${props.width}px` : props.width
  35. }
  36. if (props.large) {
  37. return `${StyleProps.inputSizes.large.width}px`
  38. }
  39. return `${StyleProps.inputSizes.regular.width}px`
  40. }
  41. const borderColor = (props, defaultColor = Palette.grayscale[3]) => props.highlight ? Palette.alert : defaultColor
  42. const Input = styled.input`
  43. width: ${props => getInputWidth(props)};
  44. height: ${props => props.height || `${StyleProps.inputSizes.regular.height}px`};
  45. line-height: ${props => props.lineHeight || 'normal'};
  46. border-radius: ${StyleProps.borderRadius};
  47. background-color: #FFF;
  48. border: ${props => props.embedded ? 0 : css`1px solid ${props => borderColor(props)}`};
  49. border-top-left-radius: ${props => props.embedded ? 0 : StyleProps.borderRadius};
  50. border-top-right-radius: ${StyleProps.borderRadius};
  51. border-bottom-left-radius: ${props => props.embedded ? 0 : StyleProps.borderRadius};
  52. border-bottom-right-radius: ${StyleProps.borderRadius};
  53. color: ${Palette.black};
  54. padding: 0 8px 0 ${props => props.embedded ? 0 : '16px'};
  55. font-size: inherit;
  56. transition: all ${StyleProps.animations.swift};
  57. box-sizing: border-box;
  58. &:hover {
  59. border-color: ${props => borderColor(props, props.disablePrimary ? null : Palette.primary)};
  60. }
  61. &:focus {
  62. border-color: ${props => borderColor(props, props.disablePrimary ? null : Palette.primary)};
  63. outline: none;
  64. }
  65. &:disabled {
  66. color: ${Palette.grayscale[3]};
  67. border-color: ${Palette.grayscale[0]};
  68. background-color: ${Palette.grayscale[0]};
  69. }
  70. &::placeholder {
  71. color: ${Palette.grayscale[3]};
  72. }
  73. `
  74. const Close = styled.div`
  75. display: ${props => props.show ? 'block' : 'none'};
  76. width: 16px;
  77. height: 16px;
  78. background: url('${closeImage}') center no-repeat;
  79. position: absolute;
  80. top: 8px;
  81. right: 8px;
  82. cursor: pointer;
  83. `
  84. type Props = {
  85. _ref?: (ref: HTMLElement) => void,
  86. disabled?: boolean,
  87. highlight?: boolean,
  88. large?: boolean,
  89. onChange?: (e: SyntheticInputEvent<EventTarget>) => void,
  90. placeholder?: string,
  91. type?: string,
  92. value?: string,
  93. showClose?: boolean,
  94. onCloseClick?: () => void,
  95. embedded?: boolean,
  96. height?: string,
  97. 'data-test-id'?: string,
  98. required?: boolean,
  99. }
  100. const TextInput = (props: Props) => {
  101. const { _ref, value, onChange, showClose, onCloseClick } = props
  102. let input
  103. return (
  104. <Wrapper data-test-id={props['data-test-id'] || 'textInput'}>
  105. <Input
  106. innerRef={ref => { input = ref; if (_ref) _ref(ref) }}
  107. type="text"
  108. value={value}
  109. onChange={onChange}
  110. data-test-id="textInput-input"
  111. {...props}
  112. />
  113. {props.required ? <Required /> : null}
  114. <Close
  115. data-test-id="textInput-close"
  116. show={showClose && value !== '' && value !== undefined}
  117. onClick={() => {
  118. input.focus()
  119. // $FlowIgnore
  120. if (onChange) onChange({ target: { value: '' } })
  121. if (onCloseClick) onCloseClick()
  122. }}
  123. />
  124. </Wrapper>
  125. )
  126. }
  127. export default TextInput