| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- import React, { Component } from 'react';
- import styled from 'styled-components';
- import _ from 'lodash';
- import { Section, FormElement } from '../../shared/types';
- import { Context } from '../../shared/Context';
- import api from '../../shared/api';
- import CheckboxRow from './CheckboxRow';
- import InputRow from './InputRow';
- import Base64InputRow from './Base64InputRow';
- import SelectRow from './SelectRow';
- import Helper from './Helper';
- import Heading from './Heading';
- import ExpandableResource from '../ExpandableResource';
- import VeleroForm from '../forms/VeleroForm';
- let dummySections = [
- {
- "name":"section_one",
- "show_if":"",
- "contents":[
- {
- "type":"heading",
- "label":"Polyphia",
- },
- {
- "type":"subtitle",
- "label":"Tim Hendrix",
- },
- {
- "type":"velero-create-backup",
- "label":"Tim Hendrix",
- },
- ]
- }
- ];
- type PropsType = {
- sections?: Section[],
- metaState?: any,
- setMetaState?: any,
- };
- type StateType = any;
- export default class ValuesForm extends Component<PropsType, StateType> {
- getInputValue = (item: FormElement) => {
- let key = item.name || item.variable;
- let value = this.props.metaState[key];
- if (item.settings && item.settings.unit && value) {
- value = value.split(item.settings.unit)[0]
- }
- return value;
- }
- renderSection = (section: Section) => {
- return section.contents.map((item: FormElement, i: number) => {
- // If no name is assigned use values.yaml variable as identifier
- let key = item.name || item.variable;
- switch (item.type) {
- case 'heading':
- return <Heading key={i}>{item.label}</Heading>;
- case 'subtitle':
- return <Helper key={i}>{item.label}</Helper>;
- case 'resource-list':
- if (Array.isArray(item.value)) {
- return (
- <ResourceList>
- {
- item.value.map((resource: any, i: number) => {
- return (
- <ExpandableResource
- key={i}
- resource={resource}
- isLast={i === item.value.length - 1}
- roundAllCorners={true}
- />
- );
- })
- }
- </ResourceList>
- );
- }
- case 'checkbox':
- return (
- <CheckboxRow
- key={i}
- checked={this.props.metaState[key]}
- toggle={() => this.props.setMetaState({ [key]: !this.props.metaState[key] })}
- label={item.label}
- />
- );
- case 'array-input':
- return (
- <InputRow
- key={i}
- isRequired={item.required}
- type='text'
- value={this.getInputValue(item)}
- setValue={(x: string) => {
- this.props.setMetaState({ [key]: [x] });
- }}
- label={item.label}
- unit={item.settings ? item.settings.unit : null}
- />
- );
- case 'string-input':
- return (
- <InputRow
- key={i}
- isRequired={item.required}
- type='text'
- value={this.getInputValue(item)}
- setValue={(x: string) => {
- if (item.settings && item.settings.unit && x !== '') {
- x = x + item.settings.unit;
- }
- this.props.setMetaState({ [key]: x });
- }}
- label={item.label}
- unit={item.settings ? item.settings.unit : null}
- />
- );
- case 'number-input':
- return (
- <InputRow
- key={i}
- isRequired={item.required}
- type='number'
- value={this.getInputValue(item)}
- setValue={(x: number) => {
- let val: string | number = x;
- if (Number.isNaN(x)) {
- val = ''
- }
- // Convert to string if unit is set
- if (item.settings && item.settings.unit) {
- val = x.toString();
- val = val + item.settings.unit;
- }
-
- this.props.setMetaState({ [key]: val });
- }}
- label={item.label}
- unit={item.settings ? item.settings.unit : null}
- />
- );
- case 'select':
- return (
- <SelectRow
- key={i}
- value={this.props.metaState[key]}
- setActiveValue={(val) => this.props.setMetaState({ [key]: val })}
- options={item.settings.options}
- dropdownLabel=''
- label={item.label}
- />
- );
- case 'velero-create-backup':
- return (
- <VeleroForm
- />
- );
- case 'base-64':
- return (
- <Base64InputRow
- key={i}
- isRequired={item.required}
- type='text'
- value={this.getInputValue(item)}
- setValue={(x: string) => {
- if (item.settings && item.settings.unit && x !== '') {
- x = x + item.settings.unit;
- }
- this.props.setMetaState({ [key]: btoa(x) });
- }}
- label={item.label}
- unit={item.settings ? item.settings.unit : null}
- />
- );
- case 'base-64-password':
- return (
- <Base64InputRow
- key={i}
- isRequired={item.required}
- type='password'
- value={this.getInputValue(item)}
- setValue={(x: string) => {
- if (item.settings && item.settings.unit && x !== '') {
- x = x + item.settings.unit;
- }
- this.props.setMetaState({ [key]: btoa(x) });
- }}
- label={item.label}
- unit={item.settings ? item.settings.unit : null}
- />
- );
- default:
- }
- });
- }
- renderFormContents = () => {
- if (this.props.metaState) {
- return this.props.sections.map((section: Section, i: number) => {
- // Hide collapsible section if deciding field is false
- if (section.show_if) {
- if (!this.props.metaState[section.show_if]) {
- return null;
- }
- }
- return (
- <div key={i}>
- {this.renderSection(section)}
- </div>
- );
- });
- }
- }
- render() {
- return (
- <StyledValuesForm>
- <DarkMatter />
- {this.renderFormContents()}
- </StyledValuesForm>
- );
- }
- }
- ValuesForm.contextType = Context;
- const ResourceList = styled.div`
- margin-bottom: 15px;
- margin-top: 20px;
- border-radius: 5px;
- overflow: hidden;
- `;
- const DarkMatter = styled.div`
- margin-top: 0px;
- `;
- const StyledValuesForm = styled.div`
- width: 100%;
- height: 100%;
- background: #ffffff11;
- color: #ffffff;
- padding: 0px 35px 25px;
- position: relative;
- border-radius: 5px;
- font-size: 13px;
- overflow: auto;
- `;
|