InputRow.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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-right: 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. margin-right: 8px;
  72. height: 30px;
  73. `;
  74. const Label = styled.div`
  75. color: #ffffff;
  76. margin-bottom: 10px;
  77. display: flex;
  78. align-items: center;
  79. font-size: 13px;
  80. font-family: 'Work Sans', sans-serif;
  81. `;
  82. const StyledInputRow = styled.div`
  83. margin-bottom: 15px;
  84. margin-top: 20px;
  85. `;