TextInput.jsx 4.6 KB

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