-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_current_usecase_example.tsx
100 lines (87 loc) · 2.25 KB
/
my_current_usecase_example.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*eslint-disable */
import { IonRow, IonCol, IonIcon } from '@ionic/react';
/*eslint-enable */
import React, { useRef, useEffect } from 'react';
import styled from 'styled-components';
import IonGridNoPadding from '../utilities/IonGridNoPadding';
import { cloudUploadOutline } from 'ionicons/icons';
import { useUploadFiles } from '../hooks/useUploadFiles';
import { animated, useSprings } from 'react-spring';
interface Props {}
const AttachFiles: React.FC<Props> = () => {
const [state, uploadFiles] = useUploadFiles('test');
const inputRef = useRef<any>();
useEffect(() => {
console.log(state);
}, [state]);
const springs = useSprings(
state.length,
state.map((file) => ({
height: file.loadingPercent ? `${file.loadingPercent}%` : '0%',
}))
);
return (
<IonGridNoPadding>
<IonRow>
{state.map((file, index) => (
<IonCol size='auto'>
<ImageContainer incomplete={!file.downloadURL}>
<Overlay style={springs[index]} />
<PreviewImage src={file.localFileId} />
</ImageContainer>
</IonCol>
))}
<IonCol size='auto'>
<InputArea onClick={() => inputRef?.current?.click()}>
<IonIcon icon={cloudUploadOutline} />
</InputArea>
<input
ref={inputRef}
onChange={() => uploadFiles(inputRef?.current?.files)}
type='file'
name='images'
accept='image/*'
multiple
hidden
/>
</IonCol>
</IonRow>
</IonGridNoPadding>
);
};
export default AttachFiles;
const Overlay = styled(animated.div)`
background: rgba(5, 146, 255, 0.3);
width: 100%;
z-index: 2;
position: absolute;
bottom: 0;
`;
const ImageContainer = styled.div<{ incomplete: boolean }>`
position: relative;
margin: auto;
width: 6rem;
height: 6rem;
object-fit: contain;
border: ${(props) => (props.incomplete ? 'none' : '2px solid var(--ion-color-success)')};
display: flex;
justify-content: center;
align-items: center;
background: var(--ion-color-light);
`;
const PreviewImage = styled.img`
max-width: 5.5rem;
max-height: 5.5rem;
object-fit: contain;
`;
const InputArea = styled.button`
position: relative;
margin: auto;
width: 6rem;
height: 6rem;
background: none;
border: 1.5px dashed var(--ion-color-medium);
display: flex;
justify-content: center;
align-items: center;
`;