ArrayInput.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import React from "react";
  2. import styled from "styled-components";
  3. import {
  4. ArrayInputField,
  5. ArrayInputFieldState,
  6. GetFinalVariablesFunction,
  7. } from "../types";
  8. import useFormField from "../hooks/useFormField";
  9. const ArrayInput: React.FC<ArrayInputField> = (props) => {
  10. const { state, variables, setVars } = useFormField<ArrayInputFieldState>(
  11. props.id,
  12. {
  13. initVars: {
  14. [props.variable]: props.value && props.value[0] ? props.value[0] : [],
  15. },
  16. }
  17. );
  18. if (state == undefined) return <></>;
  19. const renderDeleteButton = (values: string[], i: number) => {
  20. if (!props.isReadOnly) {
  21. return (
  22. <DeleteButton
  23. onClick={() => {
  24. setVars((prev) => {
  25. return {
  26. [props.variable]: prev[props.variable]
  27. .slice(0, i)
  28. .concat(prev[props.variable].slice(i + 1)),
  29. };
  30. });
  31. }}
  32. >
  33. <i className="material-icons">cancel</i>
  34. </DeleteButton>
  35. );
  36. }
  37. };
  38. const renderInputList = (values: string[]) => {
  39. return (
  40. <>
  41. {values?.map((value: string, i: number) => {
  42. return (
  43. <InputWrapper>
  44. <Input
  45. placeholder=""
  46. width="270px"
  47. value={value}
  48. onChange={(e: any) => {
  49. e.persist();
  50. setVars((prev) => {
  51. return {
  52. [props.variable]: prev[props.variable]?.map(
  53. (t: string, j: number) => {
  54. return i == j ? e.target.value : t;
  55. }
  56. ),
  57. };
  58. });
  59. }}
  60. disabled={props.isReadOnly}
  61. />
  62. {renderDeleteButton(values, i)}
  63. </InputWrapper>
  64. );
  65. })}
  66. </>
  67. );
  68. };
  69. return (
  70. <StyledInputArray>
  71. <Label>{props.label}</Label>
  72. {variables[props.variable] === 0 ? (
  73. <></>
  74. ) : (
  75. renderInputList(variables[props.variable])
  76. )}
  77. <AddRowButton
  78. onClick={() => {
  79. setVars((prev) => {
  80. return {
  81. [props.variable]: [...prev[props.variable], ""],
  82. };
  83. });
  84. }}
  85. >
  86. <i className="material-icons">add</i> Add Row
  87. </AddRowButton>
  88. </StyledInputArray>
  89. );
  90. };
  91. export default ArrayInput;
  92. export const getFinalVariablesForArrayInput: GetFinalVariablesFunction = (
  93. vars,
  94. props: ArrayInputField
  95. ) => {
  96. return vars[props.variable]
  97. ? {}
  98. : {
  99. [props.variable]: props.value ? props.value[0] : [],
  100. };
  101. };
  102. const AddRowButton = styled.div`
  103. display: flex;
  104. align-items: center;
  105. margin-top: 5px;
  106. width: 270px;
  107. font-size: 13px;
  108. color: #aaaabb;
  109. height: 30px;
  110. border-radius: 3px;
  111. cursor: pointer;
  112. background: #ffffff11;
  113. :hover {
  114. background: #ffffff22;
  115. }
  116. > i {
  117. color: #ffffff44;
  118. font-size: 16px;
  119. margin-left: 8px;
  120. margin-right: 10px;
  121. display: flex;
  122. align-items: center;
  123. justify-content: center;
  124. }
  125. `;
  126. const DeleteButton = styled.div`
  127. width: 15px;
  128. height: 15px;
  129. display: flex;
  130. align-items: center;
  131. margin-left: 8px;
  132. margin-top: -3px;
  133. justify-content: center;
  134. > i {
  135. font-size: 17px;
  136. color: #ffffff44;
  137. display: flex;
  138. align-items: center;
  139. justify-content: center;
  140. cursor: pointer;
  141. :hover {
  142. color: #ffffff88;
  143. }
  144. }
  145. `;
  146. const InputWrapper = styled.div`
  147. display: flex;
  148. align-items: center;
  149. `;
  150. const Input = styled.input`
  151. outline: none;
  152. border: none;
  153. margin-bottom: 5px;
  154. font-size: 13px;
  155. background: #ffffff11;
  156. border: 1px solid #ffffff55;
  157. border-radius: 3px;
  158. width: ${(props: { disabled?: boolean; width: string }) =>
  159. props.width ? props.width : "270px"};
  160. color: ${(props: { disabled?: boolean; width: string }) =>
  161. props.disabled ? "#ffffff44" : "white"};
  162. padding: 5px 10px;
  163. height: 35px;
  164. `;
  165. const Label = styled.div`
  166. color: #ffffff;
  167. margin-bottom: 10px;
  168. `;
  169. const StyledInputArray = styled.div`
  170. margin-bottom: 15px;
  171. margin-top: 22px;
  172. `;