Files
bflows-bandi-fe/src/components/FormFieldRepeaterCriteria/index.js
Vitalii Kiiko 87684bc76b - added bando preview page;
- added bando form preview;
2024-08-27 17:02:16 +02:00

148 lines
5.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import React, { useRef, useEffect, useState } from 'react';
import { classNames } from 'primereact/utils';
import { __ } from '@wordpress/i18n';
import { head } from 'ramda';
// components
import { InputText } from 'primereact/inputtext';
import { Button } from 'primereact/button';
import { Menu } from 'primereact/menu';
import { Dropdown } from 'primereact/dropdown';
import { InputNumber } from 'primereact/inputnumber';
const FormFieldRepeaterCriteria = ({
data,
setDataFn,
fieldName,
options = [],
errors,
register,
label,
infoText,
config = {}
}) => {
const forMenu = useRef(null);
const [stateFieldData, setStateFieldData] = useState([]);
const [threshold, setThreshold] = useState(0);
const menuItems = [
{
type: 'existing',
label: __('Esistente', 'gepafin'),
command: (data) => {
setStateFieldData([...stateFieldData, { id: null, value: '', status: data.item.type }]);
}
},
{
type: 'new',
label: __('Nuovo', 'gepafin'),
command: (data) => {
setStateFieldData([...stateFieldData, { id: null, value: '', status: data.item.type }]);
}
}
]
const removeItem = (index) => {
const newData = stateFieldData.toSpliced(index, 1);
setStateFieldData(newData);
}
const selectItem = (e, index) => {
const targetedOption = head(options.filter(o => o.value === e.value));
if (targetedOption) {
const newData = stateFieldData.map((o, i) => {
if (i === index) {
o.value = targetedOption.value;
o.score = targetedOption.score;
o.id = targetedOption.id;
}
return o;
});
console.log('newData', newData)
setStateFieldData(newData);
}
}
const onInputChange = (value, index, name) => {
const newData = stateFieldData.map((o, i) => {
if (i === index) {
o[name] = value;
}
return o;
})
setStateFieldData(newData);
}
const onThresholdChange = (value) => {
setThreshold(value);
setDataFn('threshold', value, { shouldValidate: true });
}
const properField = (item, i) => {
return item.status === 'new'
? <InputText value={item.value} onInput={(e) => onInputChange(e.target.value, i, 'value')}/>
: <Dropdown value={item.value}
onChange={(e) => selectItem(e, i)}
optionDisabled={(opt) => usedExistingValues.includes(opt.value)}
options={options} optionLabel="value"/>
}
const usedExistingValues = stateFieldData
.filter(o => o.status === 'existing')
.map(o => o.value);
useEffect(() => {
const storeFieldData = data[fieldName] ?? [];
const newData = storeFieldData.map(o => ({ ...o, status: o.id ? 'existing' : 'new' }));
setStateFieldData(newData);
setThreshold(data['threshold'])
register(fieldName, config)
register('threshold', {
required: __('È obbligatorio', 'gepafin')
})
}, [])
useEffect(() => {
setDataFn(fieldName, [...stateFieldData], { shouldValidate: true });
}, [stateFieldData])
return (
<div className={classNames(['appForm__field', 'formfieldrepeater'])}>
<label htmlFor={fieldName} className={classNames({ 'p-error': errors[fieldName] || errors['threshold'] })}>
{label}
</label>
<div className="appForm__oneCol">
<label htmlFor="criterionThreshold">{__('Punteggio minimo per lammissione', 'gepafin')}</label>
<InputNumber inputId="criterionThreshold"
value={threshold}
showButtons
onValueChange={(e) => onThresholdChange(e.value)}/>
</div>
{stateFieldData.map((o, i) => <div key={i}
className={classNames('appForm__field', 'appForm__repeaterItem')}>
<div className="appForm__twoCols">
<div>
<label>{__('Nome criterio di valutazione', 'gepafin')}</label>
<div className="p-inputgroup flex-1">
{properField(o, i)}
<Button icon="pi pi-times" className="p-button-danger" onClick={() => removeItem(i)}/>
</div>
{o.status === 'new' && infoText ? <small>{infoText}</small> : null}
</div>
<div>
<label htmlFor="criterionMin">{__('Punteggio', 'gepafin')}</label>
<InputNumber inputId="criterionMin"
value={o.score}
showButtons
onValueChange={(e) => onInputChange(e.value, i, 'score')}/>
</div>
</div>
</div>)}
<Menu model={menuItems} popup ref={forMenu} id="aimedForMenu"/>
<Button type="button" iconPos="right" label={__('Aggiungi', 'gepafin')}
icon="pi pi-chevron-down" onClick={(event) => forMenu.current.toggle(event)}
aria-controls="aimedForMenu" aria-haspopup/>
</div>
)
}
export default FormFieldRepeaterCriteria;