InputArray.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import React, { Component } from "react";
  2. import styled from "styled-components";
  3. import InputRow from "./InputRow";
  4. type PropsType = {
  5. label?: string;
  6. values: string[];
  7. setValues: (x: string[]) => void;
  8. width?: string;
  9. };
  10. type StateType = {};
  11. export default class InputArray extends Component<PropsType, StateType> {
  12. dict2arr = (dict: Record<string, any>) => {
  13. let arr = [];
  14. for (let key in dict) {
  15. arr.push(`${key}: ${dict[key]}`);
  16. }
  17. return arr;
  18. };
  19. renderInputList = (values: string[]) => {
  20. return (
  21. <>
  22. {values.map((value: string, i: number) => {
  23. return (
  24. <InputWrapper>
  25. <Input
  26. placeholder=""
  27. width="270px"
  28. value={value}
  29. onChange={(e: any) => {
  30. let v = [...values];
  31. v[i] = e.target.value;
  32. this.props.setValues(v);
  33. }}
  34. />
  35. <DeleteButton
  36. onClick={() => {
  37. let v = [...values];
  38. v.splice(i, 1);
  39. this.props.setValues(v);
  40. }}
  41. >
  42. <i className="material-icons">cancel</i>
  43. </DeleteButton>
  44. </InputWrapper>
  45. );
  46. })}
  47. </>
  48. );
  49. };
  50. render() {
  51. let { values } = this.props;
  52. if (!Array.isArray(values)) {
  53. values = this.dict2arr(values);
  54. }
  55. return (
  56. <StyledInputArray>
  57. <Label>{this.props.label}</Label>
  58. {values.length === 0 ? <></> : this.renderInputList(values)}
  59. <AddRowButton
  60. onClick={() => {
  61. let v = [...values];
  62. v.push("");
  63. this.props.setValues(v);
  64. }}
  65. >
  66. <i className="material-icons">add</i> Add Row
  67. </AddRowButton>
  68. </StyledInputArray>
  69. );
  70. }
  71. }
  72. const AddRowButton = styled.div`
  73. display: flex;
  74. align-items: center;
  75. margin-top: 5px;
  76. width: 270px;
  77. font-size: 13px;
  78. color: #aaaabb;
  79. height: 30px;
  80. border-radius: 3px;
  81. cursor: pointer;
  82. background: #ffffff11;
  83. :hover {
  84. background: #ffffff22;
  85. }
  86. > i {
  87. color: #ffffff44;
  88. font-size: 16px;
  89. margin-left: 8px;
  90. margin-right: 10px;
  91. display: flex;
  92. align-items: center;
  93. justify-content: center;
  94. }
  95. `;
  96. const DeleteButton = styled.div`
  97. width: 15px;
  98. height: 15px;
  99. display: flex;
  100. align-items: center;
  101. margin-left: 8px;
  102. margin-top: -3px;
  103. justify-content: center;
  104. > i {
  105. font-size: 17px;
  106. color: #ffffff44;
  107. display: flex;
  108. align-items: center;
  109. justify-content: center;
  110. cursor: pointer;
  111. :hover {
  112. color: #ffffff88;
  113. }
  114. }
  115. `;
  116. const InputWrapper = styled.div`
  117. display: flex;
  118. align-items: center;
  119. `;
  120. const Input = styled.input`
  121. outline: none;
  122. border: none;
  123. margin-bottom: 5px;
  124. font-size: 13px;
  125. background: #ffffff11;
  126. border: 1px solid #ffffff55;
  127. border-radius: 3px;
  128. width: ${(props: { disabled?: boolean; width: string }) =>
  129. props.width ? props.width : "270px"};
  130. color: ${(props: { disabled?: boolean; width: string }) =>
  131. props.disabled ? "#ffffff44" : "white"};
  132. padding: 5px 10px;
  133. height: 35px;
  134. `;
  135. const Label = styled.div`
  136. color: #ffffff;
  137. margin-bottom: 10px;
  138. `;
  139. const StyledInputArray = styled.div`
  140. margin-bottom: 15px;
  141. margin-top: 22px;
  142. `;