diff --git a/scelta-carriera/src/App.jsx b/scelta-carriera/src/App.jsx index beb7d03..e77aa28 100644 --- a/scelta-carriera/src/App.jsx +++ b/scelta-carriera/src/App.jsx @@ -91,6 +91,11 @@ function App() { <> {formFieldFactory.createTextInput("name", "Name", true)} {formFieldFactory.createTextAreaInput("description", "Description")} + {formFieldFactory.createNumberInput("number", "Number")} + {formFieldFactory.createBooleanInput("boolean", "Boolean")} + {formFieldFactory.createRadioInput("radio", "Radio", [{ "value": 1, "label": "Opzione 1" }, { "value": 2, "label": "Opzione 2" }, { "value": 3, "label": "Altra opzione" }])} + {formFieldFactory.createDropdownInput("dropdown", "Dropdown", [{ "value": 0, "label": "Not applicable" }, { "value": 1, "label": "Goal 1: No poverty" }, { "value": 2, "label": "Goal 2: Zero hunger" }, { "value": 3, "label": "Goal 3: Good health and well-being" }])} + {formFieldFactory.createCheckboxInput("checkbox", "Checkbox", [{ "value": 0, "label": "Check 1" }, { "value": 1, "label": "Check 2" }, { "value": 2, "label": "Check 3" }])} ) }; diff --git a/scelta-carriera/src/components/ElixFormsComponentAbstract.tsx b/scelta-carriera/src/components/ElixFormsComponentAbstract.tsx index abb75bf..39be596 100644 --- a/scelta-carriera/src/components/ElixFormsComponentAbstract.tsx +++ b/scelta-carriera/src/components/ElixFormsComponentAbstract.tsx @@ -6,6 +6,7 @@ import { QueryParamHelper } from './QueryParamHelper'; import { Component, FormEvent, JSX } from 'react'; import React from 'react'; import { IElixFormsComponentCustomFormFieldFactory } from './IElixFormsComponentCustomFormFieldFactory'; +import { ElixFormsCheckboxOption, ElixFormsDropdownOption, ElixFormsRadioOption } from './ElixFormsTypes'; export default abstract class ElixFormsComponentAbstract extends Component { constructor(props: IElixFormsComponentProperties) { @@ -61,15 +62,15 @@ export default abstract class ElixFormsComponentAbstract extends Component this.createBooleanInput(name, label, required), - createCheckboxInput: (name: string, label: string, options: Map, required: boolean = false) => + createCheckboxInput: (name: string, label: string, options: Array, required: boolean = false) => this.createCheckboxInput(name, label, options, required), - createDropdownInput: (name: string, label: string, options: Map, required: boolean = false) => + 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: Map, required: boolean = false) => + 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), @@ -108,7 +109,7 @@ export default abstract class ElixFormsComponentAbstract extends Component, required: boolean = false) : JSX.Element { + private createCheckboxInput(paramName: string, label: string, values: Array, required: boolean = false) : JSX.Element { const checkedValues = QueryParamHelper.getCheckedFromQuery(paramName); if (!this.checkboxValues.get(paramName)) { @@ -118,17 +119,17 @@ export default abstract class ElixFormsComponentAbstract extends Component { - const id = `${paramName}_${num}`; + values.forEach((entry, entryIndex) => { + const id = `${paramName}_${entryIndex}`; checkboxes.push( - { + { const currentCheckboxValues = this.checkboxValues.get(paramName)!; - const index = currentCheckboxValues.indexOf(num) ?? -1; - if (checked && index === -1) { - currentCheckboxValues.push(num); - } else if (!checked && index !== -1) { - currentCheckboxValues.splice(index, 1); + const checkboxIndex = currentCheckboxValues.indexOf(entryIndex) ?? -1; + if (checked && checkboxIndex === -1) { + currentCheckboxValues.push(entryIndex); + } else if (!checked && checkboxIndex !== -1) { + currentCheckboxValues.splice(checkboxIndex, 1); } this.setState({ formData: { ...this.state.formData, [paramName]: currentCheckboxValues.join(',').trim() ?? '' } }, () => console.log(`Checkbox ${id} changed to ${checked}. Current values for ${paramName}: ${this.state.formData[paramName]}`)); @@ -146,19 +147,20 @@ export default abstract class ElixFormsComponentAbstract extends Component, required: boolean = false) : JSX.Element { + private createRadioInput(paramName: string, label: string, values: Array, required: boolean = false) : JSX.Element { const radioValue = QueryParamHelper.getOptionFromQuery(paramName); const radios: FluentUI.IChoiceGroupOption[] = []; - values.forEach((val, num) => { - const id = `${paramName}_${num}`; + values.forEach((entry, _) => { + const id = `${paramName}_${entry.value}`; radios.push( { key: id, - text: val, + text: entry.label, id: id, name: paramName, - value: num + value: entry.value, + className: 'isiportalPartialAdminFormFieldRadio' } ); }); @@ -187,7 +189,8 @@ export default abstract class ElixFormsComponentAbstract extends Component{label}, - + ).render(); } @@ -216,7 +219,7 @@ export default abstract class ElixFormsComponentAbstract extends Component{label}, - + ).render(); } @@ -229,20 +232,34 @@ export default abstract class ElixFormsComponentAbstract extends Component{label}, - + ).render(); } - private createDropdownInput(paramName: string, label: string, values: Map, required: boolean = false): JSX.Element { + private createDropdownInput(paramName: string, label: string, values: Array, required: boolean = false): JSX.Element { const selectedValue = QueryParamHelper.getOptionFromQuery(paramName); - const options = Array.from(values).map(([num, val]) => ({ - key: num.toString(), - text: val - })); + const options: FluentUI.IDropdownOption[] = []; + + values.forEach(entry => { + const id = `${paramName}_${entry.value}`; + options.push( + { + key: id, + text: entry.label + } + ); + }); return new ElixFormsElement( {label}, - + ).render(); } @@ -257,19 +274,25 @@ export default abstract class ElixFormsComponentAbstract extends Component(field.options.map((o: any) => [o.value, o.label])), field.required ?? false); + 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 Map(field.options.map((o: any) => [o.value, o.label])), field.required ?? false); + return this.createCheckboxInput(field.key, field.label, + new Array().concat(...field.options.map((o: any) => [[o.value, o.label]])), + field.required ?? false); case "number": return this.createNumberInput(field.key, field.label, field.required ?? false); case "dropdown": - return this.createDropdownInput(field.key, field.label, new Map(field.options.map((o: any) => [o.value, o.label])), field.required ?? false); + return this.createDropdownInput(field.key, field.label, + new Array().concat(...field.options.map((o: any) => [[o.value, o.label]])), + field.required ?? false); default: return <>; diff --git a/scelta-carriera/src/components/ElixFormsTypes.tsx b/scelta-carriera/src/components/ElixFormsTypes.tsx new file mode 100644 index 0000000..11f629a --- /dev/null +++ b/scelta-carriera/src/components/ElixFormsTypes.tsx @@ -0,0 +1,3 @@ +export type ElixFormsCheckboxOption = { value: number, label: string }; +export type ElixFormsDropdownOption = { value: number, label: string }; +export type ElixFormsRadioOption = { value: number, label: string }; \ No newline at end of file diff --git a/scelta-carriera/src/components/IElixFormsComponentCustomFormFieldFactory.ts b/scelta-carriera/src/components/IElixFormsComponentCustomFormFieldFactory.ts index c4cce9f..26aba2d 100644 --- a/scelta-carriera/src/components/IElixFormsComponentCustomFormFieldFactory.ts +++ b/scelta-carriera/src/components/IElixFormsComponentCustomFormFieldFactory.ts @@ -1,12 +1,13 @@ import { JSX } from "react"; +import { ElixFormsCheckboxOption, ElixFormsDropdownOption, ElixFormsRadioOption } from "./ElixFormsTypes"; export type IElixFormsComponentCustomFormFieldFactory = { createBooleanInput: (name: string, label: string, required?: boolean) => JSX.Element; - createCheckboxInput: (name: string, label: string, options: Map, required?: boolean) => JSX.Element; - createDropdownInput: (name: string, label: string, options: Map, required?: boolean) => JSX.Element; + createCheckboxInput: (name: string, label: string, options: Array, required?: boolean) => JSX.Element; + createDropdownInput: (name: string, label: string, options: Array, required?: boolean) => JSX.Element; createHiddenInput: (name: string) => JSX.Element; createNumberInput: (name: string, label: string, required?: boolean) => JSX.Element; - createRadioInput: (name: string, label: string, options: Map, required?: boolean) => JSX.Element; + createRadioInput: (name: string, label: string, options: Array, required?: boolean) => JSX.Element; createTextInput: (name: string, label: string, required?: boolean) => JSX.Element; createTextAreaInput: (name: string, label: string, required?: boolean) => JSX.Element; };