InputRow.tsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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(parseFloat(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 && (
  33. <Label>
  34. {label} <Required>{this.props.isRequired ? " *" : null}</Required>
  35. </Label>
  36. )}
  37. <InputWrapper>
  38. <Input
  39. readOnly={this.state.readOnly}
  40. onFocus={() => this.setState({ readOnly: false })}
  41. disabled={this.props.disabled}
  42. placeholder={placeholder}
  43. width={width}
  44. type={type}
  45. value={value}
  46. onChange={this.handleChange}
  47. />
  48. {unit ? <Unit>{unit}</Unit> : null}
  49. </InputWrapper>
  50. </StyledInputRow>
  51. );
  52. }
  53. }
  54. const Required = styled.div`
  55. margin-left: 8px;
  56. color: #fc4976;
  57. `;
  58. const Unit = styled.div`
  59. margin-left: 8px;
  60. `;
  61. const InputWrapper = styled.div`
  62. display: flex;
  63. margin-bottom: -1px;
  64. align-items: center;
  65. `;
  66. const Input = styled.input<{ disabled: boolean; width: string }>`
  67. outline: none;
  68. border: none;
  69. font-size: 13px;
  70. background: #ffffff11;
  71. border: 1px solid #ffffff55;
  72. cursor: ${(props) => (props.disabled ? "not-allowed" : "")};
  73. border-radius: 3px;
  74. width: ${(props) => (props.width ? props.width : "270px")};
  75. color: ${(props) => (props.disabled ? "#ffffff44" : "white")};
  76. padding: 5px 10px;
  77. height: 35px;
  78. `;
  79. const Label = styled.div`
  80. color: #ffffff;
  81. margin-bottom: 10px;
  82. display: flex;
  83. align-items: center;
  84. font-size: 13px;
  85. font-family: "Work Sans", sans-serif;
  86. `;
  87. const StyledInputRow = styled.div`
  88. margin-bottom: 15px;
  89. margin-top: 22px;
  90. `;