import './App.css' import * as FluentUI from '@fluentui/react'; import type { IElixFormsComponentFormState } from './IElixFormsComponentFormState'; import type { IElixFormsComponentProperties } from './IElixFormsComponentProperties'; import { ElixFormsElement } from './ElixFormsElement'; import { QueryParamHelper } from './QueryParamHelper'; import { Component, type FormEvent, type JSX, useState } from 'react'; import React from 'react'; import type { IElixFormsComponentCustomFormFieldFactory } from './IElixFormsComponentCustomFormFieldFactory'; import type { ElixFormsCheckboxOption, ElixFormsDropdownOption, ElixFormsRadioOption } from './ElixFormsTypes'; export default abstract class ElixFormsComponentAbstract extends Component { constructor(props: IElixFormsComponentProperties) { super(props); this.state = { formData: {} }; // const [formData, setFormData] = useState({}); // const handleAnyInputChange = (name: string) => (event: any, valueOrOption: any) => { //ChangeEvent | SyntheticEvent // let value: any = null; // // Fluent UI Dropdown passes (event, option) // if (valueOrOption && valueOrOption.key !== undefined) { // value = valueOrOption.key; // } // // Fluent UI TextField passes (event, newValue) // else if (typeof valueOrOption === "string") { // value = valueOrOption; // } // // Fluent UI Checkbox passes (event, checked) // else if (typeof valueOrOption === "boolean") { // value = valueOrOption; // } // // Native HTML inputs // else if (event?.target) { // value = event.target.type === "checkbox" // ? event.target.checked // : event.target.value; // } // setFormData(prev => ({ // ...prev, // [name]: value // })); // console.log(`Input ${name} changed to ${value}. Current form data:`, JSON.stringify(formData)); // }; } private checkboxValues = new Map(); private mandatoryFormFieldNames: string[] = [ "RWE2_MODULE_ID", "RWE2_REQUEST_ID", "custom-workflow-back-url", "custom-workflow-generic-id", "custom-workflow-current-tabrel-genid", "custom-workflow-source-field", "crc", "MODULE_TESTMODE_KEY", "ELANG" ]; private createMandatoryFormFields(queryParams: URLSearchParams): JSX.Element { const hiddenInputs: JSX.Element[] = []; // Create a hidden input for each mandatory Elix parameter this.mandatoryFormFieldNames.forEach(paramName => { const value = queryParams.get(paramName) ?? ""; // Create the input element hiddenInputs.push(this.createHiddenInput(paramName)); }); return <>{hiddenInputs}; } private createAdditionalFormFields(): React.ReactElement { const schema = JSON.parse(this.props.additionalFieldsJson || "[]"); return ( <> {schema.map((field: { key: React.Key | null | undefined; }) => (
{this.renderField(field)}
))} ); } protected createCustomFormFields(formFieldFactory: IElixFormsComponentCustomFormFieldFactory): JSX.Element { return <>; } private renderCustomFormFields(): JSX.Element { const formFieldFactory = { createBooleanInput: (name: string, label: string, required: boolean = false) => this.createBooleanInput(name, label, required), createCheckboxInput: (name: string, label: string, options: Array) => this.createCheckboxInput(name, label, options), createDropdownInput: (name: string, label: string, options: Array, required: boolean = false) => this.createDropdownInput(name, label, options, required), createHiddenInput: (name: string) => this.createHiddenInput(name), createNumberInput: (name: string, label: string, required: boolean = false) => this.createNumberInput(name, label, required), createRadioInput: (name: string, label: string, options: Array, required: boolean = false) => this.createRadioInput(name, label, options, required), createTextInput: (name: string, label: string, required: boolean = false) => this.createTextInput(name, label, required), createTextAreaInput: (name: string, label: string, required: boolean = false) => this.createTextAreaInput(name, label, required), }; return this.createCustomFormFields(formFieldFactory); } protected renderExtraContentPre(): JSX.Element | null { return null; } protected renderExtraContentPost(): JSX.Element | null { return null; } public override render(): React.ReactElement { const { description, isDarkTheme, environmentMessage, hasTeamsContext, userDisplayName, additionalFieldsJson, headerTitle, pageTitle, pageDescription, submitDescription, heroImageSrc } = this.props; const queryParams = new URLSearchParams(window.location.search); const missingMandatoryParams = this.mandatoryFormFieldNames.filter(paramName => !queryParams.has(paramName)); const hasAuthenticationError = missingMandatoryParams.length > 0; return (

elixForms

Logo Università: di Parma
{headerTitle && (

{headerTitle}

)}
{pageTitle &&

{pageTitle}

} {heroImageSrc && Hero Image} {pageDescription &&

{pageDescription}

} {environmentMessage &&
{environmentMessage}
} {description &&
{description}
} {hasAuthenticationError ? ( Accesso non autorizzato. Parametri obbligatori mancanti ({missingMandatoryParams.join(', ')}). ) : ( <> {this.renderExtraContentPre()}
{this.createMandatoryFormFields(queryParams)} {this.createAdditionalFormFields()} {this.renderCustomFormFields()}
{this.renderExtraContentPost()} )}
powered by elixForms
versione 1.24.0
); } private createCheckboxInput(paramName: string, label: string, values: Array): JSX.Element { const checkedValues = QueryParamHelper.getCheckedFromQuery(paramName); if (!this.checkboxValues.get(paramName)) { this.checkboxValues.set(paramName, checkedValues); } const checkboxes: React.ReactElement[] = []; const stackTokens = { childrenGap: 10 }; values.forEach((entry, entryIndex) => { const id = `${paramName}_${entryIndex}`; checkboxes.push( { const currentCheckboxValues = this.checkboxValues.get(paramName)!; const checkboxIndex = currentCheckboxValues.indexOf(entryIndex); if (checked && checkboxIndex === -1) { currentCheckboxValues.push(entryIndex); } else if (!checked && checkboxIndex !== -1) { currentCheckboxValues.splice(checkboxIndex, 1); } const newValue = currentCheckboxValues.join(',').trim(); this.setState({ formData: { ...this.state.formData, [paramName]: newValue } }, () => { console.log(`Checkbox ${id} changed to ${checked}. Current values for ${paramName}: ${this.state.formData[paramName]}`); }); }} /> ); }); const currentValue = this.state.formData[paramName] !== undefined ? this.state.formData[paramName] : (this.checkboxValues.get(paramName)?.join(',').trim() ?? ''); return new ElixFormsElement( <>{label}, <> {checkboxes} ).render(); } private createRadioInput(paramName: string, label: string, values: Array, required: boolean = false): JSX.Element { const radioValue = QueryParamHelper.getOptionFromQuery(paramName); const radios: FluentUI.IChoiceGroupOption[] = []; values.forEach((entry, _) => { const id = `${paramName}_${entry.value}`; radios.push( { key: id, text: entry.label, id: id, name: paramName, value: entry.value, className: 'isiportalPartialAdminFormFieldRadio' } ); }); return new ElixFormsElement( <>, { const value = option?.value ?? ''; this.setState({ formData: { ...this.state.formData, [paramName]: value } }, () => console.log(`Radio ${option?.id} changed to ${value}. Current value for ${paramName}: ${this.state.formData[paramName]}`) ); }} /> ).render(); } private createBooleanInput(paramName: string, label: string, required: boolean = false): JSX.Element { const boolValue = QueryParamHelper.getOptionFromQuery(paramName); const radios: FluentUI.IChoiceGroupOption[] = []; [{ "value": "true", "label": "Sì" }, { "value": "false", "label": "No" }].forEach((val) => { const id = `${paramName}_${val.value}`; radios.push( { key: id, text: val.label, id: id, name: paramName, value: val.value, className: 'isiportalPartialAdminFormFieldRadio' } ); }); return new ElixFormsElement( <>, { const value = option?.value ?? ''; this.setState({ formData: { ...this.state.formData, [paramName]: value } }, () => console.log(`Boolean ${option?.id} changed to ${value}. Current value for ${paramName}: ${this.state.formData[paramName]}`) ); }} /> ).render(); } private createTextAreaInput(paramName: string, label: string, required: boolean = false): JSX.Element { const textareaValue = QueryParamHelper.getDecodedTextFromQuery(paramName) ?? ""; return new ElixFormsElement( {label}, { this.setState({ formData: { ...this.state.formData, [paramName]: value } }, () => console.log(`TextArea ${paramName} changed to ${value}. Current value for ${paramName}: ${this.state.formData[paramName]}`) ); }} /> ).render(); } private createTextInput(paramName: string, label: string, required: boolean = false): JSX.Element { const textValue = QueryParamHelper.getDecodedTextFromQuery(paramName) ?? ""; return new ElixFormsElement( {label}, { this.setState({ formData: { ...this.state.formData, [paramName]: value } }, () => console.log(`TextInput ${paramName} changed to ${value}. Current value for ${paramName}: ${this.state.formData[paramName]}`) ); }} /> ).render(); } private createHiddenInput(paramName: string): JSX.Element { const textValue = QueryParamHelper.getDecodedTextFromQuery(paramName) ?? ""; return ; } private createNumberInput(paramName: string, label: string, required: boolean = false): JSX.Element { const textValue = QueryParamHelper.getDecodedTextFromQuery(paramName) ?? ""; return new ElixFormsElement( {label}, { this.setState({ formData: { ...this.state.formData, [paramName]: value } }, () => console.log(`NumberInput ${paramName} changed to ${value}. Current value for ${paramName}: ${this.state.formData[paramName]}`) ); }} /> ).render(); } private createDropdownInput(paramName: string, label: string, values: Array, required: boolean = false): JSX.Element { const queryValue = QueryParamHelper.getOptionFromQuery(paramName); const currentValue = this.state.formData[paramName] !== undefined ? this.state.formData[paramName] : (queryValue?.toString() ?? ''); const options: FluentUI.IDropdownOption[] = []; values.forEach(entry => { options.push( { key: entry.value.toString(), text: entry.label } ); }); return new ElixFormsElement( {label}, <> { if (option) { const value = option.key.toString(); this.setState({ formData: { ...this.state.formData, [paramName]: value } }, () => console.log(`Dropdown ${paramName} changed to ${value}. Current value for ${paramName}: ${this.state.formData[paramName]}`) ); } }} /> ).render(); } private renderField(field: any): JSX.Element { //const value = this.state.formData[field.key] || ""; switch (field.type) { case "text": return this.createTextInput(field.key, field.label, field.required ?? false); case "textarea": return this.createTextAreaInput(field.key, field.label, field.required ?? false); case "radio": return this.createRadioInput(field.key, field.label, new Array().concat(...field.options.map((o: any) => [[o.value, o.label]])), field.required ?? false); case "boolean": return this.createBooleanInput(field.key, field.label, field.required ?? false); case "checkbox": return this.createCheckboxInput(field.key, field.label, new Array().concat(...field.options.map((o: any) => [[o.value, o.label, o.required ?? false]]))); case "number": return this.createNumberInput(field.key, field.label, field.required ?? false); case "dropdown": return this.createDropdownInput(field.key, field.label, new Array().concat(...field.options.map((o: any) => [[o.value, o.label]])), field.required ?? false); default: return <>; } } }