ValuesForm.tsx 4.7 KB

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