- saving progress;
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
import React, { useMemo, useState, useEffect } from 'react';
|
||||
import { klona } from 'klona';
|
||||
import { useForm } from 'react-hook-form';
|
||||
|
||||
// tools
|
||||
import uniqid from '../../../../helpers/uniqid';
|
||||
|
||||
// components
|
||||
import FormField from '../../../../components/FormField';
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { Button } from 'primereact/button';
|
||||
|
||||
const RepeaterFields = ({ sourceId, sourceName }) => {
|
||||
const [items, setItems] = useState([]);
|
||||
const [chosen, setChosen] = useState({});
|
||||
const [formInitialData, setFormInitialData] = useState({});
|
||||
const {
|
||||
control,
|
||||
handleSubmit,
|
||||
formState: { errors },
|
||||
setValue,
|
||||
register,
|
||||
trigger,
|
||||
getValues
|
||||
} = useForm({
|
||||
defaultValues: useMemo(() => {
|
||||
return formInitialData;
|
||||
}, [formInitialData]), mode: 'onChange'
|
||||
});
|
||||
|
||||
const onSubmit = () => {
|
||||
};
|
||||
|
||||
const doUpdateAmendment = () => {
|
||||
trigger();
|
||||
let formValues = klona(getValues());
|
||||
console.log('formValues', formValues);
|
||||
}
|
||||
|
||||
const addNew = () => {
|
||||
const uid = uniqid('f');
|
||||
const newItem = {
|
||||
fieldId: uid,
|
||||
name: '',
|
||||
file: {},
|
||||
}
|
||||
setItems([...items, newItem]);
|
||||
setChosen(newItem);
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
}, [chosen])
|
||||
console.log('items', items);
|
||||
return (
|
||||
<div className="fieldsRepeater">
|
||||
<form className="appForm" onSubmit={handleSubmit(onSubmit)}>
|
||||
{items
|
||||
? items.map(o => <div key={o.fieldId}>
|
||||
<div className="fieldsRepeater__Heading">
|
||||
<span>{o.name}</span>
|
||||
</div>
|
||||
{chosen.fieldId === o.fieldId
|
||||
? <div className="fieldsRepeater__fields">
|
||||
<FormField
|
||||
type="textinput"
|
||||
fieldName="name"
|
||||
label={__('Titolo del file', 'gepafin')}
|
||||
control={control}
|
||||
errors={errors}
|
||||
defaultValue={formInitialData['name']}
|
||||
config={{ required: __('È obbligatorio', 'gepafin') }}
|
||||
/>
|
||||
<FormField
|
||||
type="fileupload"
|
||||
setDataFn={setValue}
|
||||
saveFormCallback={doUpdateAmendment}
|
||||
fieldName="file"
|
||||
label={__('File', 'gepafin')}
|
||||
control={control}
|
||||
register={register}
|
||||
errors={errors}
|
||||
defaultValue={formInitialData['file'] ? formInitialData['file'] : []}
|
||||
accept={[]}
|
||||
source={sourceName}
|
||||
sourceId={sourceId}
|
||||
multiple={false}
|
||||
/>
|
||||
</div> : null}
|
||||
</div>
|
||||
) : null}
|
||||
</form>
|
||||
<Button
|
||||
type="button"
|
||||
disabled={false}
|
||||
onClick={addNew}
|
||||
label={__('Aggiungi nuovo', 'gepafin')}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default RepeaterFields;
|
||||
Reference in New Issue
Block a user