ValuesForm.tsx 5.0 KB

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