- fixed fileupload;
This commit is contained in:
@@ -14,6 +14,7 @@ import { mimeTypes } from '../../../../configData';
|
||||
const Fileupload = ({
|
||||
fieldName,
|
||||
setDataFn,
|
||||
control,
|
||||
label,
|
||||
errors,
|
||||
register,
|
||||
@@ -21,7 +22,7 @@ const Fileupload = ({
|
||||
config = {},
|
||||
infoText = null,
|
||||
accept = ['image/*'],
|
||||
doctype = 'images',
|
||||
doctype = 'document',
|
||||
maxSize = 100000000,
|
||||
emptyText = __('Trascina qui il tuo file', 'gepafin'),
|
||||
chooseLabel = __('Aggiungi file', 'gepafin'),
|
||||
@@ -29,7 +30,7 @@ const Fileupload = ({
|
||||
sourceId = 0,
|
||||
source = 'application',
|
||||
disabled = false,
|
||||
maxFileSize = 100000000
|
||||
saveFormCallback = () => {}
|
||||
}) => {
|
||||
const [stateFieldData, setStateFieldData] = useState([]);
|
||||
const [acceptFormats, setAcceptFormats] = useState('');
|
||||
@@ -51,7 +52,9 @@ const Fileupload = ({
|
||||
setStateFieldData(data.data);
|
||||
const files = inputRef.current.getFiles();
|
||||
inputRef.current.setUploadedFiles(files);
|
||||
setDataFn(fieldName, data.data, { shouldValidate: true });
|
||||
inputRef.current.setFiles([]);
|
||||
saveFormCallback();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,11 +63,12 @@ const Fileupload = ({
|
||||
}
|
||||
|
||||
const itemTemplate = (file) => {
|
||||
let fileName = file.fileName ? file.fileName : file.name;
|
||||
return (
|
||||
<div className="appForm__fileUploadItem">
|
||||
<div>
|
||||
<span className="appForm__fileUploadItemName">
|
||||
{file.name}
|
||||
{fileName}
|
||||
</span>
|
||||
</div>
|
||||
<div>
|
||||
@@ -72,7 +76,9 @@ const Fileupload = ({
|
||||
{!file.id ? <Tag value={__('In attesa', 'gepafin')} severity="warning"></Tag> : null}
|
||||
</div>
|
||||
<div>
|
||||
<Button icon="pi pi-times" severity="danger"
|
||||
<Button icon="pi pi-times"
|
||||
severity="danger"
|
||||
type="button"
|
||||
disabled={disabled}
|
||||
aria-label={__('Anulla', 'gepafin')}
|
||||
onClick={() => onTemplateRemove(file)}/>
|
||||
@@ -101,6 +107,8 @@ const Fileupload = ({
|
||||
setStateFieldData(prevState => {
|
||||
const newFiles = prevState.filter(o => o.id !== id);
|
||||
inputRef.current.setUploadedFiles(newFiles);
|
||||
setDataFn(fieldName, newFiles, { shouldValidate: true });
|
||||
saveFormCallback();
|
||||
return newFiles;
|
||||
});
|
||||
}
|
||||
@@ -154,11 +162,9 @@ const Fileupload = ({
|
||||
|
||||
useEffect(() => {
|
||||
if (inputRef.current) {
|
||||
const properValue = multiple
|
||||
? defaultValue
|
||||
: !isEmpty(defaultValue) ? [defaultValue] : [];
|
||||
inputRef.current.setUploadedFiles(properValue);
|
||||
inputRef.current.setUploadedFiles(defaultValue);
|
||||
}
|
||||
setStateFieldData(defaultValue);
|
||||
}, [defaultValue]);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -166,18 +172,12 @@ const Fileupload = ({
|
||||
setAcceptFormats(accept.join(',').replace(/\*/g, '.\*').replace(/,/g, '|'));
|
||||
}, [accept]);
|
||||
|
||||
useEffect(() => {
|
||||
if (inputRef.current) {
|
||||
inputRef.current.setUploadedFiles(stateFieldData);
|
||||
}
|
||||
setDataFn(fieldName, [...stateFieldData], { shouldValidate: true });
|
||||
}, [stateFieldData])
|
||||
|
||||
return (
|
||||
sourceId || sourceId === 0
|
||||
? <>
|
||||
<label htmlFor={fieldName} className={classNames({ 'p-error': errors[fieldName] })}>
|
||||
{label}{config.required || config.isRequired ? <span className="appForm__field--required">*</span> : null}
|
||||
{label}{config.required || config.isRequired ?
|
||||
<span className="appForm__field--required">*</span> : null}
|
||||
{acceptFormats ? ' (' + getPropeMimeLabels(accept) + ')' : null}
|
||||
</label>
|
||||
<FileUpload
|
||||
|
||||
Reference in New Issue
Block a user