- updated delega form logic;
This commit is contained in:
@@ -2,11 +2,11 @@ import React, { useEffect, useState, useRef } from 'react';
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { isEmpty } from 'ramda';
|
||||
|
||||
import FileUploadService from '../../service/file-upload-service';
|
||||
|
||||
import { FileUpload } from 'primereact/fileupload';
|
||||
import { Tag } from 'primereact/tag';
|
||||
import { Button } from 'primereact/button';
|
||||
|
||||
// api
|
||||
import CompanyService from '../../service/company-service';
|
||||
|
||||
const FileuploadDelega = ({
|
||||
@@ -21,7 +21,7 @@ const FileuploadDelega = ({
|
||||
companyId = 0,
|
||||
disabled = false
|
||||
}) => {
|
||||
const [stateFieldData, setStateFieldData] = useState({});
|
||||
const [stateFieldData, setStateFieldData] = useState([]);
|
||||
const [acceptFormats, setAcceptFormats] = useState('');
|
||||
const inputRef = useRef();
|
||||
|
||||
@@ -35,9 +35,7 @@ const FileuploadDelega = ({
|
||||
|
||||
const callback = (data) => {
|
||||
if (data.status === 'SUCCESS') {
|
||||
setStateFieldData(data.data);
|
||||
const files = inputRef.current.getFiles();
|
||||
inputRef.current.setUploadedFiles(files);
|
||||
setStateFieldData([data.data]);
|
||||
inputRef.current.setFiles([]);
|
||||
}
|
||||
}
|
||||
@@ -47,11 +45,12 @@ const FileuploadDelega = ({
|
||||
}
|
||||
|
||||
const itemTemplate = (file) => {
|
||||
let fileName = file.fileName ? file.fileName : file.name;
|
||||
return (
|
||||
<div className="appForm__fileUploadItem">
|
||||
<div>
|
||||
<span className="appForm__fileUploadItemName">
|
||||
{file.fileName}
|
||||
{fileName}
|
||||
</span>
|
||||
</div>
|
||||
<div>
|
||||
@@ -90,6 +89,7 @@ const FileuploadDelega = ({
|
||||
setStateFieldData(prevState => {
|
||||
const newFiles = prevState.filter(o => o.id !== id);
|
||||
inputRef.current.setUploadedFiles(newFiles);
|
||||
console.log('dCallback - newFiles', newFiles)
|
||||
return newFiles;
|
||||
});
|
||||
}
|
||||
@@ -104,6 +104,13 @@ const FileuploadDelega = ({
|
||||
}
|
||||
|
||||
const onBeforeSelect = (e) => {
|
||||
const files = inputRef.current.getFiles();
|
||||
const uploadedfiles = inputRef.current.getUploadedFiles();
|
||||
|
||||
if (!multiple && (uploadedfiles.length > 0 || files.length > 0)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (e.originalEvent.target.files) {
|
||||
return !isEmpty(e.originalEvent.target.files)
|
||||
? validateFileInputType(e.originalEvent.target.files)
|
||||
@@ -127,10 +134,7 @@ const FileuploadDelega = ({
|
||||
|
||||
useEffect(() => {
|
||||
if (inputRef.current) {
|
||||
const properValue = multiple
|
||||
? defaultValue
|
||||
: !isEmpty(defaultValue) ? [defaultValue] : [];
|
||||
inputRef.current.setUploadedFiles(properValue);
|
||||
inputRef.current.setUploadedFiles(defaultValue);
|
||||
}
|
||||
}, [defaultValue]);
|
||||
|
||||
@@ -141,10 +145,9 @@ const FileuploadDelega = ({
|
||||
|
||||
useEffect(() => {
|
||||
if (inputRef.current) {
|
||||
inputRef.current.setUploadedFiles(multiple ? stateFieldData : [stateFieldData]);
|
||||
inputRef.current.setUploadedFiles(stateFieldData);
|
||||
}
|
||||
|
||||
setDataFn(fieldName, multiple ? [...stateFieldData] : stateFieldData, { shouldValidate: true });
|
||||
setDataFn(fieldName, stateFieldData, { shouldValidate: true });
|
||||
}, [stateFieldData])
|
||||
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user