ValuesForm.tsx 5.3 KB

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