2
0

TextInput.jsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. import React from 'react'
  15. import PropTypes from 'prop-types'
  16. import styled from 'styled-components'
  17. import Palette from '../../styleUtils/Palette'
  18. import StyleProps from '../../styleUtils/StyleProps'
  19. import starImage from './images/star.svg'
  20. const Wrapper = styled.div`
  21. position: relative;
  22. `
  23. const getInputWidth = props => {
  24. if (props.width) {
  25. return props.width
  26. }
  27. if (props.large) {
  28. return `${StyleProps.inputSizes.large.width}px`
  29. }
  30. return `${StyleProps.inputSizes.regular.width}px`
  31. }
  32. const Input = styled.input`
  33. width: ${props => getInputWidth(props)};
  34. height: ${StyleProps.inputSizes.regular.height}px;
  35. line-height: ${StyleProps.inputSizes.regular.height}px;
  36. border-radius: ${StyleProps.borderRadius};
  37. background-color: #FFF;
  38. border: 1px solid ${props => props.highlight ? Palette.alert : Palette.grayscale[3]};
  39. color: ${Palette.black};
  40. padding: 0 ${props => props.customRequired ? '29px' : '8px'} 0 16px;
  41. font-size: inherit;
  42. transition: all ${StyleProps.animations.swift};
  43. box-sizing: border-box;
  44. &:hover {
  45. border-color: ${props => props.highlight ? Palette.alert : Palette.primary};
  46. }
  47. &:focus {
  48. border-color: ${props => props.highlight ? Palette.alert : Palette.primary};
  49. outline: none;
  50. }
  51. &:disabled {
  52. color: ${Palette.grayscale[3]};
  53. border-color: ${Palette.grayscale[0]};
  54. background-color: ${Palette.grayscale[0]};
  55. }
  56. &::placeholder {
  57. color: ${Palette.grayscale[3]};
  58. }
  59. `
  60. const Required = styled.div`
  61. display: ${props => props.show ? 'block' : 'none'};
  62. position: absolute;
  63. right: 12px;
  64. top: 13px;
  65. width: 8px;
  66. height: 8px;
  67. background: url('${starImage}') center no-repeat;
  68. `
  69. const TextInput = ({ _ref, required, ...props }) => {
  70. return (
  71. <Wrapper>
  72. <Input innerRef={_ref} type="text" customRequired={required} {...props} />
  73. <Required show={required} />
  74. </Wrapper>
  75. )
  76. }
  77. TextInput.propTypes = {
  78. _ref: PropTypes.func,
  79. required: PropTypes.bool,
  80. }
  81. export default TextInput