forked from ant-design/ant-design
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add crop image demo for Upload (ant-design#24174)
* chore: add antd-img-crop * docs: add crop image demo for upload * fix: move antd-img-crop to devDependencies * fix: fix eslint warning * test: update snapshot
- Loading branch information
1 parent
f546c8b
commit bfd5644
Showing
3 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
--- | ||
order: 14 | ||
title: | ||
zh-CN: 上传前裁切图片 | ||
en-US: Crop image before uploading | ||
--- | ||
|
||
## zh-CN | ||
|
||
配合 [antd-img-crop](https://github.com/nanxiaobei/antd-img-crop) 实现上传前裁切图片。 | ||
|
||
## en-US | ||
|
||
Use [antd-img-crop](https://github.com/nanxiaobei/antd-img-crop) to crop image before uploading. | ||
|
||
```jsx | ||
import React, { useState } from 'react'; | ||
import { Upload } from 'antd'; | ||
import ImgCrop from 'antd-img-crop'; | ||
|
||
const Demo = () => { | ||
const [fileList, setFileList] = useState([ | ||
{ | ||
uid: '-1', | ||
name: 'image.png', | ||
status: 'done', | ||
url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png', | ||
}, | ||
]); | ||
|
||
const onChange = ({ fileList: newFileList }) => { | ||
setFileList(newFileList); | ||
}; | ||
|
||
const onPreview = async file => { | ||
let src = file.url; | ||
if (!src) { | ||
src = await new Promise(resolve => { | ||
const reader = new FileReader(); | ||
reader.readAsDataURL(file.originFileObj); | ||
reader.onload = () => resolve(reader.result); | ||
}); | ||
} | ||
const image = new Image(); | ||
image.src = src; | ||
const imgWindow = window.open(src); | ||
imgWindow.document.write(image.outerHTML); | ||
}; | ||
|
||
return ( | ||
<ImgCrop rotate> | ||
<Upload | ||
action="https://www.mocky.io/v2/5cc8019d300000980a055e76" | ||
listType="picture-card" | ||
fileList={fileList} | ||
onChange={onChange} | ||
onPreview={onPreview} | ||
> | ||
{fileList.length < 5 && '+ Upload'} | ||
</Upload> | ||
</ImgCrop> | ||
); | ||
}; | ||
|
||
ReactDOM.render(<Demo />, mountNode); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters