ValuesForm.tsx 4.8 KB

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