Commit 7493438e authored by zhangwenshuai's avatar zhangwenshuai

update upload

parent 49acd331
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
display: flex; display: flex;
vertical-align: middle; vertical-align: middle;
align-items: center; align-items: center;
user-select: none;
} }
.moreBtn { .moreBtn {
......
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
align-items: center; align-items: center;
border: 1px solid @primaryColor; border: 1px solid @primaryColor;
min-height: 32px; min-height: 32px;
padding: 0 @paddingLg;
user-select: none;
&.disabled { &.disabled {
background: @disabledColor; background: @disabledColor;
border-color: #d9d9d9; border-color: #d9d9d9;
......
import React, { useEffect, useRef, useState } from 'react'; import React, { useEffect, useState } from 'react';
import { Modal, Tooltip, Button, Icon } from 'antd'; import { Modal } from 'antd';
import classNames from 'classnames'; import classNames from 'classnames';
import { ApolloUploadProps } from '../editInterface'; import { ApolloUploadProps } from '../editInterface';
import { antiAssign } from '../../../../utils/utils'; import { antiAssign } from '../../../../utils/utils';
import s from './index.less';
import { onBlurFn } from '../onBlurFn';
import Upload from '../../extra/upload'; import Upload from '../../extra/upload';
import s from './index.less';
export const ApolloUpload = (props: ApolloUploadProps) => { export const ApolloUpload = (props: ApolloUploadProps) => {
const { const { isMultiple, onEmitChange, onChange, disabled, origin, value } = props;
isMultiple, onEmitChange, onChange, disabled, origin, tableId,
} = props;
const selfProps = antiAssign(props, [ const selfProps = antiAssign(props, [
'onChange', 'onChange',
'value', 'value',
...@@ -25,7 +22,6 @@ export const ApolloUpload = (props: ApolloUploadProps) => { ...@@ -25,7 +22,6 @@ export const ApolloUpload = (props: ApolloUploadProps) => {
'origin', 'origin',
'disableOptions', 'disableOptions',
'rowId', 'rowId',
'onBlurFn',
'getInstanceDetail', 'getInstanceDetail',
]); ]);
selfProps.disabled = !!props.disabled; selfProps.disabled = !!props.disabled;
...@@ -35,75 +31,35 @@ export const ApolloUpload = (props: ApolloUploadProps) => { ...@@ -35,75 +31,35 @@ export const ApolloUpload = (props: ApolloUploadProps) => {
selfProps.multiple = false; selfProps.multiple = false;
selfProps.maxLength = 1; selfProps.maxLength = 1;
} }
const [value, setValue] = useState<any>([]); const [curValue, setCurValue] = useState<any>(value);
useEffect(() => {
setValue(props.value);
}, [props.value]);
const changeValue = (value: any) => { const changeValue = (value: any) => {
setValue(value); setCurValue(value);
if (origin === 'editForm') { if (typeof onChange === 'function') {
if (typeof onChange === 'function') { onChange(value);
onChange(value);
}
// onBlurFn({ ...props, value });
}
};
const [visible, setVisible] = useState(false);
const refHelper = useRef(visible);
const toggleUploadDialog = (flag: boolean) => {
refHelper.current = flag;
setVisible(flag);
};
// 点击Modal中上传时回调用浏览器弹框,触发onBlur,而此时并不希望触发onBlur,因此在组件内自定义监听
// useEffect(() => {
// document.addEventListener('click', onBlur, false);
// if (origin !== 'editForm') {
// toggleUploadDialog(true);
// }
// return () => {
// document.removeEventListener('click', onBlur, false);
// };
// }, []);
const clearEdit = () => {
// 清除所有dom的编辑状态
const doms = document.querySelectorAll(`.cellUnit.table_${tableId}`);
if (doms) {
doms.forEach((item) => {
item.setAttribute('data-editing-cell', '0');
});
}
};
const onBlur = () => {
// 当弹框显示时,不触发emit事件
if (refHelper.current) {
return;
} }
if (typeof onEmitChange === 'function') { if (origin === 'editForm') {
onEmitChange(props.value); onBlur(value);
} }
clearEdit();
}; };
const onOk = () => { const onBlur = (value: any) => {
if (typeof onEmitChange === 'function') { if (typeof onEmitChange === 'function') {
onEmitChange(value); onEmitChange(value);
} }
if (typeof onChange === 'function') {
onChange(value);
}
clearEdit();
toggleUploadDialog(false);
}; };
const onCancel = () => { const [visible, setVisible] = useState(false);
if (typeof onEmitChange === 'function') { // 点击Modal中上传时回调用浏览器弹框,触发onBlur,而此时并不希望触发onBlur,因此在组件内自定义监听
onEmitChange(props.value); useEffect(() => {
if (origin !== 'editForm') {
setVisible(true);
} }
clearEdit(); }, []);
toggleUploadDialog(false); const onOk = () => {
onBlur(curValue);
setVisible(false);
}; };
const onClickContainer = (e: any) => { const onCancel = () => {
e.stopPropagation(); // 阻止事件冒泡 onBlur(value);
e.nativeEvent.stopImmediatePropagation(); setVisible(false);
}; };
let containerClass = s.container; let containerClass = s.container;
if (disabled) { if (disabled) {
...@@ -113,29 +69,23 @@ export const ApolloUpload = (props: ApolloUploadProps) => { ...@@ -113,29 +69,23 @@ export const ApolloUpload = (props: ApolloUploadProps) => {
containerClass = classNames(containerClass, s.editForm); containerClass = classNames(containerClass, s.editForm);
return ( return (
<div className={containerClass}> <div className={containerClass}>
<Upload {...selfProps} value={value} listType="picture-card" onChange={changeValue} /> <Upload {...selfProps} value={curValue} listType="picture-card" onChange={changeValue} />
</div> </div>
); );
} }
return ( return (
<div className={containerClass} onClick={onClickContainer}> <div className={containerClass}>
{value {curValue &&
&& value.map((item: any, i: number) => { curValue.map((item: any, i: number) => {
return ( return (
<div key={i} className={s.picContainer}> <div key={i} className={s.picContainer}>
<Tooltip placement="bottom" title={item.name}> <img alt="" className={s.pic} src={item.thumbUrl} />
<img
alt=""
className={origin === 'editForm' ? classNames(s.pic, s.editForm) : s.pic}
src={item.thumbUrl}
/>
</Tooltip>
</div> </div>
); );
})} })}
<Modal visible={visible} title="文件上传" onOk={onOk} onCancel={onCancel}> <Modal visible={visible} title="文件上传" onOk={onOk} onCancel={onCancel}>
<div className={s.uploadContainer}> <div className={s.uploadContainer}>
<Upload {...selfProps} value={props.value} onChange={changeValue} /> <Upload {...selfProps} value={curValue} onChange={changeValue} />
</div> </div>
</Modal> </Modal>
</div> </div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment