InputRow.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import React, { ChangeEvent, Component } from 'react';
  2. import styled from 'styled-components';
  3. type PropsType = {
  4. label?: string,
  5. type: string,
  6. value: string | number,
  7. setValue: (x: string | number) => void,
  8. unit?: string,
  9. placeholder?: string,
  10. width?: string,
  11. disabled?: boolean,
  12. isRequired?: boolean,
  13. };
  14. type StateType = {
  15. readOnly: boolean
  16. };
  17. export default class InputRow extends Component<PropsType, StateType> {
  18. state = {
  19. readOnly: true
  20. }
  21. handleChange = (e: ChangeEvent<HTMLInputElement>) => {
  22. if (this.props.type === 'number') {
  23. this.props.setValue(parseInt(e.target.value));
  24. } else {
  25. this.props.setValue(e.target.value);
  26. }
  27. }
  28. render() {
  29. let { label, value, type, unit, placeholder, width } = this.props;
  30. return (
  31. <StyledInputRow>
  32. <Label>{label} <Required>{this.props.isRequired ? ' *' : null}</Required></Label>
  33. <InputWrapper>
  34. <Input
  35. readOnly={this.state.readOnly} onFocus={() => this.setState({ readOnly: false })}
  36. disabled={this.props.disabled}
  37. placeholder={placeholder}
  38. width={width}
  39. type={type}
  40. value={value}
  41. onChange={this.handleChange}
  42. />
  43. {unit ? <Unit>{unit}</Unit> : null}
  44. </InputWrapper>
  45. </StyledInputRow>
  46. );
  47. }
  48. }
  49. const Required = styled.div`
  50. margin-left: 8px;
  51. color: #fc4976;
  52. `;
  53. const Unit = styled.div`
  54. margin-left: 8px;
  55. `;
  56. const InputWrapper = styled.div`
  57. display: flex;
  58. margin-bottom: -1px;
  59. align-items: center;
  60. `;
  61. const Input = styled.input`
  62. outline: none;
  63. border: none;
  64. font-size: 13px;
  65. background: #ffffff11;
  66. border: 1px solid #ffffff55;
  67. border-radius: 3px;
  68. width: ${(props: { disabled: boolean, width: string }) => props.width ? props.width : '270px'};
  69. color: ${(props: { disabled: boolean, width: string }) => props.disabled ? '#ffffff44' : 'white'};
  70. padding: 5px 10px;
  71. height: 35px;
  72. `;
  73. const Label = styled.div`
  74. color: #ffffff;
  75. margin-bottom: 10px;
  76. display: flex;
  77. align-items: center;
  78. font-size: 13px;
  79. font-family: 'Work Sans', sans-serif;
  80. `;
  81. const StyledInputRow = styled.div`
  82. margin-bottom: 15px;
  83. margin-top: 20px;
  84. `;