当前位置:   article > 正文

antd的联级选择器异步调用编辑回显_自己的通用react+antd控件记录-通用图片墙

antd的联级选择器异步调用编辑回显_自己的通用react+antd控件记录-通用图片墙

基于antd的图片墙封装。

调用方法:

<PicturesWall maxNumber={1} showRemoveIcon={false}/>

效果:

3ce26ac99e6d411daad6d5ff68b6b8a6.png

代码:

  1. /**
  2. * 图片墙
  3. * 控件
  4. * @authors 缥缈潇潇
  5. * @date 2020-01-03
  6. */import React, {useState, useEffect} from 'react';
  7. import {Upload, message, Modal} from 'antd';
  8. import {RcFile, UploadFile} from "antd/lib/upload/interface";
  9. import './index.less';
  10. import {CommIcon} from '..';
  11. import {FileDownLoad, FileUpload} from "@/services/fileUploadService";
  12. interface ShowIcon {
  13. /**是否显示删除按钮*/
  14. showRemoveIcon?: boolean;
  15. /**是否显示预览按钮*/
  16. showPreviewIcon?: boolean;
  17. /**是否显示下载按钮*/
  18. showDownloadIcon?: boolean;
  19. }
  20. interface IProps {
  21. /**完成返回事件*/
  22. onChange?: (value: any) => void;
  23. /** 当前值,父组件为form时通过initialValue传入 */
  24. value?: any;
  25. /** 固定写死的初始值 */
  26. defaultValue?: any;
  27. /**业务名称,用于文件上传的时候区分业务类型,接口处可能用于区分文件归属文件夹*/
  28. businessName?: string;
  29. /**最大接受图片数量,默认10*/
  30. maxNumber?: number;
  31. /**预览片样式*/
  32. style?: any;
  33. /**文件最大大小,默认2M*/
  34. maxSize?: number;
  35. /**按钮,默认全显示*/
  36. showIcon?: ShowIcon;
  37. }
  38. const PicturesWall = ({
  39. value, defaultValue, onChange, businessName, maxNumber = 10, style = {width: '100%'}, maxSize = 2, showIcon = {
  40. showRemoveIcon: true,
  41. showPreviewIcon: true,
  42. showDownloadIcon: true,
  43. }
  44. }: IProps) => {
  45. const [previewVisible, setPreviewVisible] = useState(false);
  46. const [fileList, setFileList] = useState(value || defaultValue || []);
  47. const [previewImage, setPreviewImage] = useState('');
  48. /**
  49. * 上传前事件
  50. * @param file 文件
  51. *
  52. * */
  53. const beforeUpload = (file: RcFile, fileList: RcFile[]) => {
  54. if (fileList.length > (maxNumber || 10)) {
  55. message.error('最多只能上传' + maxNumber + '张图片');
  56. return false;
  57. }
  58. let maxPicSize = maxSize ? maxSize : 2;
  59. const isJPG = file.type === 'image/jpg';
  60. const isPNG = file.type === 'image/png';
  61. const isJPEG = file.type === 'image/jpeg';
  62. if (!(isJPG || isPNG || isJPEG)) {
  63. message.error('只能上传jpg、png或者jpeg格式!');
  64. }
  65. const isLt = file.size / 1024 / 1024 < maxPicSize;
  66. if (!isLt) {
  67. message.error('最大只能上传' + maxPicSize + 'M大小的图片!');
  68. }
  69. return (isJPG || isPNG || isJPEG) && isLt;
  70. }
  71. /**
  72. * 关闭预览
  73. *
  74. * */
  75. const handleCancel = () => {
  76. setPreviewVisible(false)
  77. };
  78. /**
  79. * 打开预览
  80. * @param file 文件
  81. *
  82. * */
  83. const handlePreview = async (file: any) => {
  84. if (!file.url && !file.preview) {
  85. file.preview = file.thumbUrl;
  86. }
  87. setPreviewVisible(true);
  88. setPreviewImage(file.url || file.thumbUrl);
  89. };
  90. /**
  91. * 上传事件
  92. * @param info 返回参数
  93. *
  94. * */
  95. const handleChange = (info: any) => {
  96. let newFileList: any = [];
  97. const fileList = info.fileList;
  98. if (info.file.status) {
  99. if (info.file.status === 'done') {
  100. message.success(`${info.file.name} 上传成功`);
  101. } else if (info.file.status === 'error') {
  102. message.error(`${info.file.name} 上传失败`);
  103. }
  104. let ids = new Array<string>();
  105. let uploadOk = false;
  106. fileList.map((file: any) => {
  107. // 上传页面上传的
  108. if (file.response) {
  109. if (file.response.status === "200") {
  110. newFileList.push({
  111. ...file,
  112. id: file.response.entity.file_id,
  113. file_id: file.response.entity.file_id,
  114. path: file.response.entity.path,
  115. url: file.response.entity.path,
  116. thumbUrl: file.response.entity.path
  117. });
  118. ids.push(file.response.entity.file_id);
  119. uploadOk = true;
  120. }
  121. } else {
  122. // 编辑时回显的(回显时讲文件id查询出来放入uid字段)
  123. // 防止传递进来的列表中有id,返回后没有。
  124. newFileList.push({
  125. ...file,
  126. id: file.fileId,
  127. fileId: file.fileId,
  128. });
  129. ids.push(file.fileId);
  130. }
  131. });
  132. setFileList(newFileList);
  133. if (onChange && uploadOk) {
  134. onChange(ids.join(","));
  135. }
  136. }
  137. }
  138. const getUrl = async (fileIds: string) => {
  139. let files = new Array<any>();
  140. if (fileIds) {
  141. const ids = fileIds.split(",");
  142. for (let i = 0; i < ids.length; i++) {
  143. const res = await FileDownLoad({fileId: ids[i]});
  144. if (res.entity) {
  145. files.push({
  146. ...res.entity,
  147. name: res.entity.fileName,
  148. uid: ids[i],
  149. file_id: ids[i],
  150. url: res.entity.accessPath,
  151. path: res.entity.accessPath,
  152. thumbUrl: res.entity.accessPath,
  153. })
  154. }
  155. }
  156. }
  157. setFileList(files);
  158. }
  159. useEffect(() => { //加载图片
  160. getUrl(value || defaultValue || '');
  161. }, [value, defaultValue]);
  162. /**
  163. * 没有图片时候的显示图标
  164. * */
  165. const uploadButton = (
  166. <div style={{backgroundColor: "rgba(217, 217, 217, "}}>
  167. <CommIcon style={{fontSize: '30px', color: 'rgba(217, 217, 217, 1)'}} type={'iconimage'}/>
  168. </div>
  169. );
  170. return (
  171. <div className="clearfix picturesWall">
  172. <Upload
  173. name="uploadFile"
  174. listType="picture-card"
  175. accept='.jpg,.png,.jpeg'
  176. fileList={fileList}
  177. onPreview={handlePreview}
  178. onChange={handleChange}
  179. beforeUpload={beforeUpload}
  180. showUploadList={showIcon}
  181. multiple={false}
  182. customRequest={async (options: any) => {
  183. const formData = new FormData();
  184. formData.append('localFlag', "false");
  185. formData.append('privateFlag', "false");
  186. formData.append('uploadFile', options.file);
  187. formData.append('sysSign', process.env.sys_sign_no);
  188. formData.append('catalogNo', businessName || 'picWall');
  189. FileUpload('', formData).then((res: any) => {
  190. if (res.status === "200") {
  191. options.onSuccess(res, options.file)
  192. }
  193. })
  194. }}
  195. >
  196. {fileList.length >= maxNumber ? null : uploadButton}
  197. </Upload>
  198. <Modal visible={previewVisible} footer={null} onCancel={handleCancel}>
  199. <img alt="example" style={style} src={previewImage}/>
  200. </Modal>
  201. </div>
  202. );
  203. }
  204. export default React.memo(PicturesWall);

样式文件

:global {
.picturesWall {
.ant-upload-list-picture-card .ant-upload-list-item-thumbnail, .ant-upload-list-picture-card .ant-upload-list-item-thumbnail img {width: 100px !important;height: 100px !important;
}
}
}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/151841
推荐阅读
相关标签
  

闽ICP备14008679号