ValuesForm.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import React, { Component } from 'react';
  2. import styled from 'styled-components';
  3. import { FormYAML, Section, FormElement } from '../../shared/types';
  4. import SaveButton from '../SaveButton';
  5. import CheckboxRow from './CheckboxRow';
  6. import InputRow from './InputRow';
  7. import SelectRow from './SelectRow';
  8. type PropsType = {
  9. formData?: FormYAML
  10. };
  11. type StateType = any;
  12. export default class ValuesForm extends Component<PropsType, StateType> {
  13. // Initialize corresponding state fields for form blocks
  14. componentDidMount() {
  15. let formState: any = {};
  16. this.props.formData.Sections.forEach((section: Section, i: number) => {
  17. section.Contents.forEach((item: FormElement, i: number) => {
  18. // If no name is assigned use values.yaml variable as identifier
  19. let key = item.Name || item.Variable;
  20. let def = item.Settings.Default;
  21. switch (item.Type) {
  22. case 'checkbox':
  23. formState[key] = def ? def : false;
  24. break;
  25. case 'string-input':
  26. formState[key] = def ? def : '';
  27. break;
  28. case 'number-input':
  29. formState[key] = def ? def : '';
  30. break;
  31. case 'select':
  32. formState[key] = def ? def : item.Settings.Options[0].Value;
  33. default:
  34. }
  35. });
  36. });
  37. this.setState(formState);
  38. }
  39. renderSection = (section: Section) => {
  40. return section.Contents.map((item: FormElement, i: number) => {
  41. // If no name is assigned use values.yaml variable as identifier
  42. let key = item.Name || item.Variable;
  43. switch (item.Type) {
  44. case 'heading':
  45. return <Heading key={i}>{item.Label}</Heading>
  46. case 'subtitle':
  47. return <Helper key={i}>{item.Label}</Helper>
  48. case 'checkbox':
  49. return (
  50. <CheckboxRow
  51. key={i}
  52. checked={this.state[key]}
  53. toggle={() => this.setState({ [key]: !this.state[key] })}
  54. label={item.Label}
  55. />
  56. );
  57. case 'string-input':
  58. return (
  59. <InputRow
  60. key={i}
  61. type={'text'}
  62. value={this.state[key]}
  63. setValue={(x: string) => this.setState({ [key]: x })}
  64. label={item.Label}
  65. unit={item.Settings ? item.Settings.Unit : null}
  66. />
  67. );
  68. case 'number-input':
  69. return (
  70. <InputRow
  71. key={i}
  72. type={'number'}
  73. value={this.state[key]}
  74. setValue={(x: string) => this.setState({ [key]: parseInt(x) })}
  75. label={item.Label}
  76. unit={item.Settings ? item.Settings.Unit : null}
  77. />
  78. );
  79. case 'select':
  80. return (
  81. <SelectRow
  82. key={i}
  83. value={this.state[key]}
  84. setActiveValue={(val) => this.setState({ [key]: val })}
  85. options={item.Settings.Options}
  86. dropdownLabel=''
  87. label={item.Label}
  88. />
  89. );
  90. default:
  91. }
  92. });
  93. }
  94. renderFormContents = () => {
  95. if (this.state) {
  96. return this.props.formData.Sections.map((section: Section, i: number) => {
  97. // Hide collapsible section if deciding field is false
  98. if (section.ShowIf) {
  99. if (!this.state[section.ShowIf]) {
  100. return null;
  101. }
  102. }
  103. return (
  104. <div key={i}>
  105. {this.renderSection(section)}
  106. </div>
  107. );
  108. });
  109. }
  110. }
  111. render() {
  112. return (
  113. <Wrapper>
  114. <StyledValuesForm>
  115. <DarkMatter />
  116. {this.renderFormContents()}
  117. </StyledValuesForm>
  118. <SaveButton
  119. text='Deploy'
  120. onClick={() => console.log(this.state)}
  121. status={null}
  122. />
  123. </Wrapper>
  124. );
  125. }
  126. }
  127. const DarkMatter = styled.div`
  128. margin-top: 0px;
  129. `;
  130. const Wrapper = styled.div`
  131. width: 100%;
  132. height: 100%;
  133. `;
  134. const Helper = styled.div`
  135. color: #aaaabb;
  136. line-height: 1.6em;
  137. font-size: 13px;
  138. margin-bottom: 15px;
  139. margin-top: 20px;
  140. `;
  141. const Heading = styled.div`
  142. color: white;
  143. font-weight: 500;
  144. font-size: 16px;
  145. margin-top: 30px;
  146. margin-bottom: 5px;
  147. `;
  148. const StyledValuesForm = styled.div`
  149. width: 100%;
  150. height: 100%;
  151. background: #ffffff11;
  152. color: #ffffff;
  153. padding: 0px 35px 25px;
  154. position: relative;
  155. border-radius: 5px;
  156. font-size: 13px;
  157. overflow: auto;
  158. `;