InputRow.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import React, { type ChangeEvent, Component } from "react";
  2. import Tooltip from "@material-ui/core/Tooltip";
  3. import styled from "styled-components";
  4. type PropsType = {
  5. label?: string;
  6. info?: string;
  7. type: string;
  8. value: string | number;
  9. setValue?: (x: string | number) => void;
  10. unit?: string;
  11. placeholder?: string;
  12. width?: string;
  13. disabled?: boolean;
  14. isRequired?: boolean;
  15. className?: string;
  16. maxLength?: number;
  17. hasError?: boolean;
  18. };
  19. type StateType = {
  20. readOnly: boolean;
  21. };
  22. export default class InputRow extends Component<PropsType, StateType> {
  23. state = {
  24. readOnly: true,
  25. };
  26. handleChange = (e: ChangeEvent<HTMLInputElement>) => {
  27. if (this.props.type === "number") {
  28. this.props.setValue(parseFloat(e.target.value));
  29. } else {
  30. this.props.setValue(e.target.value);
  31. }
  32. };
  33. render() {
  34. const { label, value, type, unit, placeholder, width, info } = this.props;
  35. return (
  36. <StyledInputRow className={this.props.className}>
  37. {(label || info) && (
  38. <Label>
  39. {label}
  40. {info && (
  41. <Tooltip
  42. title={
  43. <div
  44. style={{
  45. fontFamily: "Work Sans, sans-serif",
  46. fontSize: "12px",
  47. fontWeight: "normal",
  48. padding: "5px 6px",
  49. }}
  50. >
  51. {info}
  52. </div>
  53. }
  54. placement="top"
  55. >
  56. <StyledInfoTooltip>
  57. <i className="material-icons">help_outline</i>
  58. </StyledInfoTooltip>
  59. </Tooltip>
  60. )}
  61. {this.props.isRequired && <Required>{" *"}</Required>}
  62. </Label>
  63. )}
  64. <InputWrapper hasError={this.props.hasError} width={width}>
  65. <Input
  66. readOnly={this.state.readOnly}
  67. onFocus={() => { this.setState({ readOnly: false }); }}
  68. disabled={this.props.disabled}
  69. placeholder={placeholder}
  70. width={width}
  71. type={type}
  72. value={value}
  73. onChange={this.handleChange}
  74. maxLength={this.props.maxLength}
  75. />
  76. {unit ? <Unit>{unit}</Unit> : null}
  77. </InputWrapper>
  78. </StyledInputRow>
  79. );
  80. }
  81. }
  82. const Required = styled.div`
  83. margin-left: 8px;
  84. color: #fc4976;
  85. `;
  86. const Unit = styled.div`
  87. padding: 0 10px;
  88. background: #ffffff05;
  89. height: 35px;
  90. display: flex;
  91. align-items: center;
  92. justify-content: center;
  93. border-left: 1px solid #ffffff55;
  94. `;
  95. const InputWrapper = styled.div`
  96. display: flex;
  97. margin-bottom: -1px;
  98. align-items: center;
  99. overflow: hidden;
  100. border: 1px solid
  101. ${(props: { width: string; hasError: boolean }) =>
  102. props.hasError ? "red" : "#ffffff55"};
  103. border-radius: 5px;
  104. ${(props: { width: string; hasError: boolean }) => {
  105. if (props.width) {
  106. return `width:${props.width};`;
  107. }
  108. }}
  109. `;
  110. const Input = styled.input<{ disabled: boolean; width: string }>`
  111. outline: none;
  112. border: none;
  113. font-size: 13px;
  114. background: ${props => props.theme.fg};
  115. cursor: ${(props) => (props.disabled ? "not-allowed" : "")};
  116. width: ${(props) => (props.width ? props.width : "100%")};
  117. color: ${(props) => (props.disabled ? "#ffffff44" : "white")};
  118. padding: 5px 10px;
  119. height: 35px;
  120. `;
  121. const Label = styled.div`
  122. color: #ffffff;
  123. margin-bottom: 10px;
  124. display: flex;
  125. align-items: center;
  126. font-size: 13px;
  127. font-family: "Work Sans", sans-serif;
  128. `;
  129. const StyledInputRow = styled.div`
  130. margin-bottom: 15px;
  131. margin-top: 22px;
  132. `;
  133. const StyledInfoTooltip = styled.div`
  134. display: inline-block;
  135. position: relative;
  136. margin-right: 2px;
  137. > i {
  138. display: flex;
  139. align-items: center;
  140. position: absolute;
  141. top: -10px;
  142. font-size: 10px;
  143. color: #858faaaa;
  144. cursor: pointer;
  145. :hover {
  146. color: #aaaabb;
  147. }
  148. }
  149. `;