added some more widget types to the form builder. w/ burnettk

This commit is contained in:
jasquat 2022-11-19 20:55:27 -05:00
parent 4652ee2792
commit 56126eb670
2 changed files with 58 additions and 5 deletions

View File

@ -107,4 +107,6 @@ export interface FormField {
id: string; id: string;
title: string; title: string;
required: boolean; required: boolean;
type: string;
enum: string[];
} }

View File

@ -1,6 +1,6 @@
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
// @ts-ignore // @ts-ignore
import { Button, ButtonSet, Form, Stack, TextInput } from '@carbon/react'; import { Button, Select, SelectItem, TextInput } from '@carbon/react';
import { useParams } from 'react-router-dom'; import { useParams } from 'react-router-dom';
import { FormField } from '../interfaces'; import { FormField } from '../interfaces';
import { modifyProcessModelPath, slugifyString } from '../helpers'; import { modifyProcessModelPath, slugifyString } from '../helpers';
@ -8,18 +8,25 @@ import HttpService from '../services/HttpService';
export default function JsonSchemaFormBuilder() { export default function JsonSchemaFormBuilder() {
const params = useParams(); const params = useParams();
const formFieldTypes = ['textbox', 'checkbox', 'select'];
const [formTitle, setFormTitle] = useState<string>(''); const [formTitle, setFormTitle] = useState<string>('');
const [formDescription, setFormDescription] = useState<string>(''); const [formDescription, setFormDescription] = useState<string>('');
const [formId, setFormId] = useState<string>(''); const [formId, setFormId] = useState<string>('');
const [formFields, setFormFields] = useState<FormField[]>([]); const [formFields, setFormFields] = useState<FormField[]>([]);
const [showNewFormField, setShowNewFormField] = useState<boolean>(false); const [showNewFormField, setShowNewFormField] = useState<boolean>(false);
const [formFieldSelectOptions, setFormFieldSelectOptions] =
useState<string>('');
const [formIdHasBeenUpdatedByUser, setFormIdHasBeenUpdatedByUser] = const [formIdHasBeenUpdatedByUser, setFormIdHasBeenUpdatedByUser] =
useState<boolean>(false); useState<boolean>(false);
const [formFieldIdHasBeenUpdatedByUser, setFormFieldIdHasBeenUpdatedByUser] = const [formFieldIdHasBeenUpdatedByUser, setFormFieldIdHasBeenUpdatedByUser] =
useState<boolean>(false); useState<boolean>(false);
const [showFormFieldSelectTextField, setShowFormFieldSelectTextField] =
useState<boolean>(false);
const [formFieldId, setFormFieldId] = useState<string>(''); const [formFieldId, setFormFieldId] = useState<string>('');
const [formFieldTitle, setFormFieldTitle] = useState<string>(''); const [formFieldTitle, setFormFieldTitle] = useState<string>('');
const [formFieldType, setFormFieldType] = useState<string>('');
const modifiedProcessModelId = modifyProcessModelPath( const modifiedProcessModelId = modifyProcessModelPath(
`${params.process_model_id}` `${params.process_model_id}`
@ -34,11 +41,21 @@ export default function JsonSchemaFormBuilder() {
properties: {}, properties: {},
required: [], required: [],
}; };
formFields.forEach((formField: FormField) => { formFields.forEach((formField: FormField) => {
(formJson.properties as any)[formField.id] = { let jsonSchemaFieldType = 'string';
type: 'string', if (formField.type === 'checkbox') {
jsonSchemaFieldType = 'boolean';
}
const formJsonObject: any = {
type: jsonSchemaFieldType,
title: formField.title, title: formField.title,
}; };
if (formField.type === 'select') {
formJsonObject.enum = formField.enum;
}
(formJson.properties as any)[formField.id] = formJsonObject;
}); });
return JSON.stringify(formJson, null, 2); return JSON.stringify(formJson, null, 2);
@ -70,11 +87,13 @@ export default function JsonSchemaFormBuilder() {
setFormTitle(newFormTitle); setFormTitle(newFormTitle);
}; };
const createNewFormField = () => { const addFormField = () => {
const newFormField: FormField = { const newFormField: FormField = {
id: formFieldId, id: formFieldId,
title: formFieldTitle, title: formFieldTitle,
required: false, required: false,
type: formFieldType,
enum: formFieldSelectOptions.split(','),
}; };
setFormFieldIdHasBeenUpdatedByUser(false); setFormFieldIdHasBeenUpdatedByUser(false);
@ -82,6 +101,16 @@ export default function JsonSchemaFormBuilder() {
setFormFields([...formFields, newFormField]); setFormFields([...formFields, newFormField]);
}; };
const handleFormFieldTypeChange = (event: any) => {
setFormFieldType(event.srcElement.value);
if (event.srcElement.value === 'select') {
setShowFormFieldSelectTextField(true);
} else {
setShowFormFieldSelectTextField(false);
}
};
const newFormFieldComponent = () => { const newFormFieldComponent = () => {
if (showNewFormField) { if (showNewFormField) {
return ( return (
@ -105,7 +134,26 @@ export default function JsonSchemaFormBuilder() {
setFormFieldId(event.srcElement.value); setFormFieldId(event.srcElement.value);
}} }}
/> />
<Button onClick={createNewFormField}>Add Field</Button> <Select
id="form-field-type"
labelText="Type"
onChange={handleFormFieldTypeChange}
>
{formFieldTypes.map((fft: string) => {
return <SelectItem text={fft} value={fft} />;
})}
</Select>
{showFormFieldSelectTextField ? (
<TextInput
id="json-form-field-select-options"
name="select-options"
labelText="Select Options"
onChange={(event: any) => {
setFormFieldSelectOptions(event.srcElement.value);
}}
/>
) : null}
<Button onClick={addFormField}>Add Field</Button>
</> </>
); );
} }
@ -184,6 +232,9 @@ export default function JsonSchemaFormBuilder() {
onClick={() => { onClick={() => {
setFormFieldId(''); setFormFieldId('');
setFormFieldTitle(''); setFormFieldTitle('');
setFormFieldType('');
setFormFieldSelectOptions('');
setShowFormFieldSelectTextField(false);
setShowNewFormField(true); setShowNewFormField(true);
}} }}
> >