ValuesForm.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import React, { Component } from 'react';
  2. import styled from 'styled-components';
  3. import _ from 'lodash';
  4. import { Section, FormElement } from '../../shared/types';
  5. import { Context } from '../../shared/Context';
  6. import api from '../../shared/api';
  7. import CheckboxRow from './CheckboxRow';
  8. import InputRow from './InputRow';
  9. import Base64InputRow from './Base64InputRow';
  10. import SelectRow from './SelectRow';
  11. import Helper from './Helper';
  12. import Heading from './Heading';
  13. import ExpandableResource from '../ExpandableResource';
  14. import VeleroForm from '../forms/VeleroForm';
  15. let dummySections = [
  16. {
  17. "name":"section_one",
  18. "show_if":"",
  19. "contents":[
  20. {
  21. "type":"heading",
  22. "label":"Polyphia",
  23. },
  24. {
  25. "type":"subtitle",
  26. "label":"Tim Hendrix",
  27. },
  28. {
  29. "type":"velero-create-backup",
  30. "label":"Tim Hendrix",
  31. },
  32. ]
  33. }
  34. ];
  35. type PropsType = {
  36. sections?: Section[],
  37. metaState?: any,
  38. setMetaState?: any,
  39. };
  40. type StateType = any;
  41. export default class ValuesForm extends Component<PropsType, StateType> {
  42. getInputValue = (item: FormElement) => {
  43. let key = item.name || item.variable;
  44. let value = this.props.metaState[key];
  45. if (item.settings && item.settings.unit && value) {
  46. value = value.split(item.settings.unit)[0]
  47. }
  48. return value;
  49. }
  50. renderSection = (section: Section) => {
  51. return section.contents.map((item: FormElement, i: number) => {
  52. // If no name is assigned use values.yaml variable as identifier
  53. let key = item.name || item.variable;
  54. switch (item.type) {
  55. case 'heading':
  56. return <Heading key={i}>{item.label}</Heading>;
  57. case 'subtitle':
  58. return <Helper key={i}>{item.label}</Helper>;
  59. case 'resource-list':
  60. if (Array.isArray(item.value)) {
  61. return (
  62. <ResourceList>
  63. {
  64. item.value.map((resource: any, i: number) => {
  65. return (
  66. <ExpandableResource
  67. key={i}
  68. resource={resource}
  69. isLast={i === item.value.length - 1}
  70. roundAllCorners={true}
  71. />
  72. );
  73. })
  74. }
  75. </ResourceList>
  76. );
  77. }
  78. case 'checkbox':
  79. return (
  80. <CheckboxRow
  81. key={i}
  82. checked={this.props.metaState[key]}
  83. toggle={() => this.props.setMetaState({ [key]: !this.props.metaState[key] })}
  84. label={item.label}
  85. />
  86. );
  87. case 'array-input':
  88. return (
  89. <InputRow
  90. key={i}
  91. isRequired={item.required}
  92. type='text'
  93. value={this.getInputValue(item)}
  94. setValue={(x: string) => {
  95. this.props.setMetaState({ [key]: [x] });
  96. }}
  97. label={item.label}
  98. unit={item.settings ? item.settings.unit : null}
  99. />
  100. );
  101. case 'string-input':
  102. return (
  103. <InputRow
  104. key={i}
  105. isRequired={item.required}
  106. type='text'
  107. value={this.getInputValue(item)}
  108. setValue={(x: string) => {
  109. if (item.settings && item.settings.unit && x !== '') {
  110. x = x + item.settings.unit;
  111. }
  112. this.props.setMetaState({ [key]: x });
  113. }}
  114. label={item.label}
  115. unit={item.settings ? item.settings.unit : null}
  116. />
  117. );
  118. case 'number-input':
  119. return (
  120. <InputRow
  121. key={i}
  122. isRequired={item.required}
  123. type='number'
  124. value={this.getInputValue(item)}
  125. setValue={(x: number) => {
  126. let val: string | number = x;
  127. if (Number.isNaN(x)) {
  128. val = ''
  129. }
  130. // Convert to string if unit is set
  131. if (item.settings && item.settings.unit) {
  132. val = x.toString();
  133. val = val + item.settings.unit;
  134. }
  135. this.props.setMetaState({ [key]: val });
  136. }}
  137. label={item.label}
  138. unit={item.settings ? item.settings.unit : null}
  139. />
  140. );
  141. case 'select':
  142. return (
  143. <SelectRow
  144. key={i}
  145. value={this.props.metaState[key]}
  146. setActiveValue={(val) => this.props.setMetaState({ [key]: val })}
  147. options={item.settings.options}
  148. dropdownLabel=''
  149. label={item.label}
  150. />
  151. );
  152. case 'velero-create-backup':
  153. return (
  154. <VeleroForm
  155. />
  156. );
  157. case 'base-64':
  158. return (
  159. <Base64InputRow
  160. key={i}
  161. isRequired={item.required}
  162. type='text'
  163. value={this.getInputValue(item)}
  164. setValue={(x: string) => {
  165. if (item.settings && item.settings.unit && x !== '') {
  166. x = x + item.settings.unit;
  167. }
  168. this.props.setMetaState({ [key]: btoa(x) });
  169. }}
  170. label={item.label}
  171. unit={item.settings ? item.settings.unit : null}
  172. />
  173. );
  174. case 'base-64-password':
  175. return (
  176. <Base64InputRow
  177. key={i}
  178. isRequired={item.required}
  179. type='password'
  180. value={this.getInputValue(item)}
  181. setValue={(x: string) => {
  182. if (item.settings && item.settings.unit && x !== '') {
  183. x = x + item.settings.unit;
  184. }
  185. this.props.setMetaState({ [key]: btoa(x) });
  186. }}
  187. label={item.label}
  188. unit={item.settings ? item.settings.unit : null}
  189. />
  190. );
  191. default:
  192. }
  193. });
  194. }
  195. renderFormContents = () => {
  196. if (this.props.metaState) {
  197. return this.props.sections.map((section: Section, i: number) => {
  198. // Hide collapsible section if deciding field is false
  199. if (section.show_if) {
  200. if (!this.props.metaState[section.show_if]) {
  201. return null;
  202. }
  203. }
  204. return (
  205. <div key={i}>
  206. {this.renderSection(section)}
  207. </div>
  208. );
  209. });
  210. }
  211. }
  212. render() {
  213. return (
  214. <StyledValuesForm>
  215. <DarkMatter />
  216. {this.renderFormContents()}
  217. </StyledValuesForm>
  218. );
  219. }
  220. }
  221. ValuesForm.contextType = Context;
  222. const ResourceList = styled.div`
  223. margin-bottom: 15px;
  224. margin-top: 20px;
  225. border-radius: 5px;
  226. overflow: hidden;
  227. `;
  228. const DarkMatter = styled.div`
  229. margin-top: 0px;
  230. `;
  231. const StyledValuesForm = styled.div`
  232. width: 100%;
  233. height: 100%;
  234. background: #ffffff11;
  235. color: #ffffff;
  236. padding: 0px 35px 25px;
  237. position: relative;
  238. border-radius: 5px;
  239. font-size: 13px;
  240. overflow: auto;
  241. `;