FormWrapper.tsx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. import React, { Component } from "react";
  2. import styled from "styled-components";
  3. import { Section, FormElement } from "shared/types";
  4. import { Context } from "shared/Context";
  5. import TabRegion from "components/TabRegion";
  6. import ValuesForm from "components/values-form/ValuesForm";
  7. import _ from "lodash";
  8. import SaveButton from "../SaveButton";
  9. type PropsType = {
  10. showStateDebugger?: boolean;
  11. formData: any;
  12. onSubmit?: (formValues: any) => void;
  13. saveValuesStatus?: string | null;
  14. isInModal?: boolean;
  15. renderTabContents?: (currentTab: string) => any;
  16. tabOptions?: any[];
  17. // TabRegion props to pass through
  18. color?: string;
  19. addendum?: any;
  20. // overrideValues?: any;
  21. };
  22. type StateType = {
  23. currentTab: string;
  24. tabOptions: { value: string; label: string }[];
  25. metaState: any;
  26. requiredFields: string[];
  27. };
  28. export default class FormWrapper extends Component<PropsType, StateType> {
  29. state = {
  30. currentTab: "",
  31. tabOptions: [] as { value: string; label: string }[],
  32. metaState: {} as any,
  33. requiredFields: [] as string[],
  34. };
  35. updateTabs = (resetState?: boolean) => {
  36. if (resetState) {
  37. let tabOptions = [] as { value: string; label: string }[];
  38. let tabs = this.props.formData?.tabs;
  39. let requiredFields = [] as string[];
  40. let metaState: any = {};
  41. if (tabs) {
  42. tabs.forEach((tab: any, i: number) => {
  43. if (tab.name && tab.label) {
  44. // If a tab is valid, first extract state
  45. tab.sections.forEach((section: Section, i: number) => {
  46. section.contents.forEach((item: FormElement, i: number) => {
  47. // If no name is assigned use values.yaml variable as identifier
  48. let key = item.name || item.variable;
  49. let def =
  50. item.settings && item.settings.unit
  51. ? `${item.settings.default}${item.settings.unit}`
  52. : item.settings?.default;
  53. def = (item.value && item.value[0]) || def;
  54. if (item.type === "checkbox") {
  55. def = item.value && item.value[0];
  56. }
  57. // Handle add to list of required fields
  58. if (item.required && key) {
  59. requiredFields.push(key);
  60. }
  61. let value: any = def;
  62. switch (item.type) {
  63. case "checkbox":
  64. value = def || false;
  65. break;
  66. case "string-input":
  67. value = def || "";
  68. break;
  69. case "string-input-password":
  70. value = def || item.settings.default;
  71. case "array-input":
  72. value = def || [];
  73. break;
  74. case "env-key-value-array":
  75. value = def || {};
  76. break;
  77. case "key-value-array":
  78. value = def || {};
  79. break;
  80. case "number-input":
  81. value = def.toString() ? def : "";
  82. break;
  83. case "select":
  84. value = def || item.settings.options[0].value;
  85. break;
  86. case "provider-select":
  87. let providerMap: any = {
  88. gke: "gcp",
  89. eks: "aws",
  90. doks: "do",
  91. };
  92. def = providerMap[this.context.currentCluster.service];
  93. value = def || "aws";
  94. break;
  95. case "base-64":
  96. value = def || "";
  97. case "base-64-password":
  98. value = def || "";
  99. default:
  100. }
  101. if (value !== null && value !== undefined) {
  102. metaState[key] = { value };
  103. }
  104. });
  105. });
  106. tabOptions.push({ value: tab.name, label: tab.label });
  107. }
  108. });
  109. }
  110. if (this.props.tabOptions?.length > 0) {
  111. tabOptions = tabOptions.concat(this.props.tabOptions);
  112. }
  113. if (tabOptions.length > 0) {
  114. this.setState({
  115. tabOptions: tabOptions,
  116. currentTab: tabOptions[0].value,
  117. metaState,
  118. requiredFields: requiredFields,
  119. });
  120. } else {
  121. this.setState({ tabOptions });
  122. }
  123. } else {
  124. // TODO: refactor by consolidating w/ above
  125. // Handle change only to external tabs (e.g. DevOps mode toggle)
  126. let tabOptions = [] as { value: string; label: string }[];
  127. let tabs = this.props.formData?.tabs;
  128. if (tabs) {
  129. tabs.forEach((tab: any, i: number) => {
  130. if (tab.name && tab.label) {
  131. tabOptions.push({ value: tab.name, label: tab.label });
  132. }
  133. });
  134. }
  135. if (this.props.tabOptions?.length > 0) {
  136. tabOptions = tabOptions.concat(this.props.tabOptions);
  137. }
  138. this.setState({ tabOptions });
  139. }
  140. };
  141. componentDidMount() {
  142. this.updateTabs();
  143. }
  144. componentDidUpdate(prevProps: any) {
  145. if (
  146. !_.isEqual(prevProps.tabOptions, this.props.tabOptions) ||
  147. !_.isEqual(prevProps.formData, this.props.formData)
  148. ) {
  149. this.updateTabs();
  150. }
  151. }
  152. isSet = (value: any) => {
  153. if (
  154. value === null ||
  155. value === undefined ||
  156. value === "" ||
  157. value === false
  158. ) {
  159. return false;
  160. }
  161. return true;
  162. };
  163. isDisabled = () => {
  164. let requiredMissing = false;
  165. this.state.requiredFields.forEach((requiredKey: string, i: number) => {
  166. if (!this.isSet(this.state.metaState[requiredKey]?.value)) {
  167. requiredMissing = true;
  168. }
  169. });
  170. return requiredMissing;
  171. };
  172. renderTabContents = () => {
  173. let tabs = this.props.formData?.tabs;
  174. if (tabs) {
  175. let matchedTab = null as any;
  176. tabs.forEach((tab: any, i: number) => {
  177. if (tab.name === this.state.currentTab) {
  178. matchedTab = tab;
  179. }
  180. });
  181. if (matchedTab) {
  182. return (
  183. <ValuesForm
  184. metaState={this.state.metaState}
  185. setMetaState={(key: string, value: any) => {
  186. let metaState: any = this.state.metaState;
  187. metaState[key] = { value };
  188. this.setState({ metaState });
  189. }}
  190. sections={matchedTab.sections}
  191. />
  192. );
  193. }
  194. }
  195. // If no form tabs match, check against external tabs
  196. if (this.props.renderTabContents) {
  197. return this.props.renderTabContents(this.state.currentTab);
  198. }
  199. return <div>No matched tabs found.</div>;
  200. };
  201. renderStateDebugger = () => {
  202. if (this.props.showStateDebugger) {
  203. return (
  204. <>
  205. <StateDisplay>
  206. <Header>FormWrapper State</Header>
  207. <ScrollWrapper>
  208. {JSON.stringify(this.state.metaState, undefined, 2)}
  209. </ScrollWrapper>
  210. </StateDisplay>
  211. </>
  212. );
  213. }
  214. };
  215. handleSubmit = () => {
  216. // Extract metaState values
  217. let submissionValues: any = {};
  218. Object.keys(this.state.metaState).forEach((key: string, i: number) => {
  219. submissionValues[key] = this.state.metaState[key]?.value;
  220. });
  221. this.props.onSubmit && this.props.onSubmit(submissionValues);
  222. };
  223. showSaveButton = (): boolean => {
  224. // Check if current tab is among non-form tab options{
  225. let nonFormTabValues = this.props.tabOptions?.map((tab: any, i: number) => {
  226. return tab.value;
  227. });
  228. if (nonFormTabValues && nonFormTabValues.includes(this.state.currentTab)) {
  229. return false;
  230. }
  231. return true;
  232. };
  233. renderContents = (showSave: boolean) => {
  234. return (
  235. <>
  236. <TabRegion
  237. options={this.state.tabOptions}
  238. currentTab={this.state.currentTab}
  239. setCurrentTab={(x: string) => this.setState({ currentTab: x })}
  240. addendum={this.props.addendum}
  241. color={this.props.color}
  242. >
  243. {this.renderTabContents()}
  244. </TabRegion>
  245. {showSave && (
  246. <SaveButton
  247. disabled={this.isDisabled()}
  248. text="Deploy"
  249. onClick={this.handleSubmit}
  250. status={
  251. this.isDisabled()
  252. ? "Missing required fields"
  253. : this.props.saveValuesStatus
  254. }
  255. makeFlush={!this.props.isInModal}
  256. />
  257. )}
  258. {this.renderStateDebugger()}
  259. </>
  260. );
  261. };
  262. render() {
  263. let showSave = this.showSaveButton();
  264. return (
  265. <>
  266. {this.props.isInModal ? (
  267. <StyledValuesWrapper showSave={showSave}>
  268. {this.renderContents(showSave)}
  269. </StyledValuesWrapper>
  270. ) : (
  271. <PaddedWrapper>
  272. <StyledValuesWrapper showSave={showSave}>
  273. {this.renderContents(showSave)}
  274. </StyledValuesWrapper>
  275. </PaddedWrapper>
  276. )}
  277. </>
  278. );
  279. }
  280. }
  281. FormWrapper.contextType = Context;
  282. const Spacer = styled.div`
  283. width: 100%;
  284. height: 200px;
  285. background: red;
  286. position: relative;
  287. `;
  288. const TabWrapper = styled.div`
  289. min-height: 100px;
  290. display: flex;
  291. align-items: center;
  292. justify-content: center;
  293. `;
  294. const ScrollWrapper = styled.div`
  295. padding: 20px;
  296. overflow-y: auto;
  297. max-height: 300px;
  298. padding-top: 15px;
  299. `;
  300. const Header = styled.div`
  301. width: 100%;
  302. height: 40px;
  303. color: #ffffff;
  304. font-weight: 500;
  305. padding-left: 17px;
  306. background: #00000022;
  307. display: flex;
  308. align-items: center;
  309. `;
  310. const StateDisplay = styled.pre`
  311. width: 100%;
  312. font-size: 13px;
  313. display:
  314. overflow: hidden;
  315. border-radius: 5px;
  316. position: relative;
  317. line-height: 1.5em;
  318. color: #aaaabb;
  319. background: #ffffff11;
  320. `;
  321. const StyledValuesWrapper = styled.div<{ showSave: boolean }>`
  322. width: 100%;
  323. padding: 0;
  324. height: ${(props) => (props.showSave ? "calc(100% - 55px)" : "100%")};
  325. `;
  326. const PaddedWrapper = styled.div`
  327. padding-bottom: 65px;
  328. position: relative;
  329. `;