Skip to content

Commit ac7acf0

Browse files
wzc520pyfmmiownag
authored andcommitted
docs(attachments): add high-quality preview example for image file (ant-design#960)
1 parent 6eb6fe3 commit ac7acf0

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

components/attachments/demo/with-sender.tsx

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ const Demo = () => {
1212

1313
const senderRef = React.useRef<GetRef<typeof Sender>>(null);
1414

15+
React.useEffect(() => {
16+
// Clear all created object URLs when the component is unmounted
17+
return () => {
18+
items.forEach((item) => {
19+
if (item.url?.startsWith('blob:')) {
20+
URL.revokeObjectURL(item.url);
21+
}
22+
});
23+
};
24+
}, []);
25+
1526
const senderHeader = (
1627
<Sender.Header
1728
title="Attachments"
@@ -27,7 +38,23 @@ const Demo = () => {
2738
// Mock not real upload file
2839
beforeUpload={() => false}
2940
items={items}
30-
onChange={({ fileList }) => setItems(fileList)}
41+
onChange={({ file, fileList }) => {
42+
const updatedFileList = fileList.map((item) => {
43+
if (item.uid === file.uid && file.status !== 'removed' && item.originFileObj) {
44+
// clear URL
45+
if (item.url?.startsWith('blob:')) {
46+
URL.revokeObjectURL(item.url);
47+
}
48+
// create new preview URL
49+
return {
50+
...item,
51+
url: URL.createObjectURL(item.originFileObj),
52+
};
53+
}
54+
return item;
55+
});
56+
setItems(updatedFileList);
57+
}}
3158
placeholder={(type) =>
3259
type === 'drop'
3360
? {

0 commit comments

Comments
 (0)