|
| 1 | +import React, { useState } from "react"; |
| 2 | + |
| 3 | +/* |
| 4 | + * This is a nested comoponenet in the FieldTemplate, and |
| 5 | + * does all the work for rendering the different optins in their |
| 6 | + * respective places. |
| 7 | + * |
| 8 | + * props: |
| 9 | + * |
| 10 | + * data -> The data to render - namely, the data as defined in the schema |
| 11 | + */ |
| 12 | +export const IPythonFormGroup = (props: { |
| 13 | + properties: any; |
| 14 | + selecteditem: string; |
| 15 | + mainprops: any; |
| 16 | + handleAdditionalProperties: any; |
| 17 | +}) => { |
| 18 | + const fg: any = generateFormGroupMap(props.properties); |
| 19 | + return ( |
| 20 | + <div> |
| 21 | + {fg[props.selecteditem].map((index: number) => ( |
| 22 | + <div className="property-wrapper" key={index}> |
| 23 | + {props.properties[index].content} |
| 24 | + </div> |
| 25 | + ))} |
| 26 | + </div> |
| 27 | + ); |
| 28 | +}; |
| 29 | + |
| 30 | +export const KeyValueWidget = (props: any) => { |
| 31 | + const [key, setKey] = useState(props.value.name); |
| 32 | + const [val, setVal] = useState(props.formData[props.value.name]); |
| 33 | + return ( |
| 34 | + <div> |
| 35 | + <input |
| 36 | + type="string" |
| 37 | + value={key} |
| 38 | + onChange={(e: React.ChangeEvent<HTMLInputElement>) => { |
| 39 | + props.handleKey(key, e.target.value); |
| 40 | + setKey(e.target.value); |
| 41 | + }} |
| 42 | + /> |
| 43 | + <input |
| 44 | + type="string" |
| 45 | + value={val} |
| 46 | + onChange={(e: React.ChangeEvent<HTMLInputElement>) => { |
| 47 | + props.handleVal(key, e.target.value); |
| 48 | + setVal(e.target.value); |
| 49 | + }} |
| 50 | + /> |
| 51 | + </div> |
| 52 | + ); |
| 53 | +}; |
| 54 | + |
| 55 | +/* |
| 56 | + * This function is a field |
| 57 | + * that is rendered for the environment variable. |
| 58 | + */ |
| 59 | +export const EnvVarForm = (props: any) => { |
| 60 | + /* |
| 61 | + * Function to handle a change of key values in a key value store |
| 62 | + */ |
| 63 | + const handleKeyChange = (key: string, newkey: string) => { |
| 64 | + props.formData[newkey] = props.formData[key]; |
| 65 | + delete props.formData[key]; |
| 66 | + console.log(props.formData); |
| 67 | + }; |
| 68 | + |
| 69 | + /* |
| 70 | + * Function to handle any changing values |
| 71 | + */ |
| 72 | + const handleValueChange = (key: string, newvalue: string) => { |
| 73 | + props.formData[key] = newvalue; |
| 74 | + console.log(props.formData); |
| 75 | + }; |
| 76 | + |
| 77 | + const widget: (props: any) => JSX.Element = props.uiSchema["ui:widget"]; |
| 78 | + const formData = props.formData; |
| 79 | + return ( |
| 80 | + <div> |
| 81 | + {props.properties.map((item: any) => |
| 82 | + widget( |
| 83 | + (props = { |
| 84 | + value: item, |
| 85 | + formData: formData, |
| 86 | + handleKey: handleKeyChange, |
| 87 | + handleVal: handleValueChange, |
| 88 | + }) |
| 89 | + ) |
| 90 | + )} |
| 91 | + <button |
| 92 | + type="button" |
| 93 | + onClick={(e: any) => { |
| 94 | + alert(props.onAddClick); |
| 95 | + }} |
| 96 | + > |
| 97 | + Add New |
| 98 | + </button> |
| 99 | + </div> |
| 100 | + ); |
| 101 | +}; |
| 102 | + |
| 103 | +/* |
| 104 | + * Grab the location of the element in the array, |
| 105 | + * returning a value of the postions in the array. |
| 106 | + */ |
| 107 | +function generateFormGroupMap(dataArr: any) { |
| 108 | + const GeneralSettingsArray: Array<number> = []; |
| 109 | + const LaunchArgumentsArray: Array<number> = []; |
| 110 | + const EnvironmentVariableArray: Array<number> = []; |
| 111 | + const ComputeParametersArray: Array<number> = []; |
| 112 | + const MetadataArray: Array<number> = []; |
| 113 | + dataArr.forEach((element: any) => { |
| 114 | + if ( |
| 115 | + element.name == "display_name" || |
| 116 | + element.name == "interrupt_mode" || |
| 117 | + element.name == "language" |
| 118 | + ) { |
| 119 | + GeneralSettingsArray.push(dataArr.indexOf(element)); |
| 120 | + } else if (element.name == "argv") { |
| 121 | + LaunchArgumentsArray.push(dataArr.indexOf(element)); |
| 122 | + } else if (element.name == "env") { |
| 123 | + EnvironmentVariableArray.push(dataArr.indexOf(element)); |
| 124 | + } else if (element.name == "parameters") { |
| 125 | + ComputeParametersArray.push(dataArr.indexOf(element)); |
| 126 | + } else if (element.name == "metadata") { |
| 127 | + MetadataArray.push(dataArr.indexOf(element)); |
| 128 | + } else { |
| 129 | + console.log("oopsies"); |
| 130 | + } |
| 131 | + }); |
| 132 | + return { |
| 133 | + ["General Settings"]: GeneralSettingsArray, |
| 134 | + ["Launch Arguments"]: LaunchArgumentsArray, |
| 135 | + ["Environment Variables"]: EnvironmentVariableArray, |
| 136 | + ["Compute Parameters"]: ComputeParametersArray, |
| 137 | + ["Metadata"]: MetadataArray, |
| 138 | + }; |
| 139 | +} |
0 commit comments