import './App.css'; import type { IElixFormsComponentFormState } from './IElixFormsComponentFormState'; import type { IElixFormsComponentProperties } from './IElixFormsComponentProperties'; import type { IElixFormsComponentCustomFormFieldFactory } from './IElixFormsComponentCustomFormFieldFactory'; import type { ElixFormsCheckboxOption, ElixFormsDropdownOption, ElixFormsRadioOption } from './ElixFormsTypes'; import { ElixFormsElement } from './ElixFormsElement'; import { QueryParamHelper } from './QueryParamHelper'; import { Component, type JSX } from 'react'; import React from 'react'; export default abstract class ElixFormsComponentAbstract extends Component { constructor(props: IElixFormsComponentProperties) { super(props); this.state = { 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[] = []; this.mandatoryFormFieldNames.forEach(paramName => { 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 { isDarkTheme, userDisplayName, moduleName, headerTitle, headerHeroImageSrc: headerTitleImageSrc, cardTitle, cardDescription, alertInfoTitle, alertInfoMessage, instructionsTitle, instructionsMessage, submitText, } = this.props; const queryParams = new URLSearchParams(window.location.search); const missingMandatoryParams = this.mandatoryFormFieldNames.filter(paramName => !queryParams.has(paramName)); const hasAuthenticationError = missingMandatoryParams.length > 0; return (
{userDisplayName && }
{headerTitleImageSrc && Hero Image} {headerTitle && (

{headerTitle}

)}
{cardTitle &&

{cardTitle}

} {cardDescription &&

{cardDescription}

} {(alertInfoTitle || alertInfoMessage) &&
{alertInfoTitle &&
{alertInfoTitle}
} {alertInfoMessage &&
{alertInfoMessage}
}
} {hasAuthenticationError ? (
Accesso non autorizzato!
) : ( <> {this.renderExtraContentPre()} {(instructionsTitle || instructionsMessage) &&
{instructionsTitle &&
{instructionsTitle}
} {instructionsMessage &&
{instructionsMessage}
}
}
{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 currentValue = this.state.formData[paramName] !== undefined ? this.state.formData[paramName] : (this.checkboxValues.get(paramName)?.join(',').trim() ?? ''); const selectedValues = (currentValue || '').split(',').filter(Boolean).map((value: string) => Number(value)); return new ElixFormsElement( , <>
{values.map((entry, entryIndex) => { const id = `${paramName}_${entryIndex}`; const isChecked = selectedValues.includes(entryIndex); return (
{ const currentCheckboxValues = this.checkboxValues.get(paramName) ?? []; const checkboxIndex = currentCheckboxValues.indexOf(entryIndex); const nextValues = [...currentCheckboxValues]; if (event.target.checked && checkboxIndex === -1) { nextValues.push(entryIndex); } else if (!event.target.checked && checkboxIndex !== -1) { nextValues.splice(checkboxIndex, 1); } this.checkboxValues.set(paramName, nextValues); const newValue = nextValues.join(',').trim(); this.setState({ formData: { ...this.state.formData, [paramName]: newValue } }); }} />
); })}
).render(); } private createRadioInput(paramName: string, label: string, values: Array, required: boolean = false): JSX.Element { const currentValue = this.state.formData[paramName] !== undefined ? String(this.state.formData[paramName]) : String(QueryParamHelper.getOptionFromQuery(paramName) ?? ''); return new ElixFormsElement( {label},
{values.map((entry) => { const id = `${paramName}_${entry.value}`; return (
{ this.setState({ formData: { ...this.state.formData, [paramName]: String(entry.value) } }); }} required={required} />
); })}
).render(); } private createBooleanInput(paramName: string, label: string, required: boolean = false): JSX.Element { const currentValue = this.state.formData[paramName] !== undefined ? String(this.state.formData[paramName]) : String(QueryParamHelper.getOptionFromQuery(paramName) ?? ''); return new ElixFormsElement( {label},
{[{ value: 'true', label: 'Sì' }, { value: 'false', label: 'No' }].map((val) => { const id = `${paramName}_${val.value}`; return (
{ this.setState({ formData: { ...this.state.formData, [paramName]: val.value } }); }} required={required} />
); })}
).render(); } private createTextAreaInput(paramName: string, label: string, required: boolean = false): JSX.Element { const currentValue = this.state.formData[paramName] !== undefined ? String(this.state.formData[paramName]) : (QueryParamHelper.getDecodedTextFromQuery(paramName) ?? ''); return new ElixFormsElement( ,