1
1
import classNames from "classnames" ;
2
2
import { ProgressBar } from "./ProgressBar" ;
3
3
import { UploadInfo } from "./UploadInfo" ;
4
- import { createElement , ReactElement , useCallback , MouseEvent , KeyboardEvent } from "react" ;
4
+ import { createElement , ReactElement , useCallback , MouseEvent , KeyboardEvent , ReactNode } from "react" ;
5
5
import { FileStatus , FileStore } from "../stores/FileStore" ;
6
6
import { observer } from "mobx-react-lite" ;
7
7
import fileSize from "filesize.js" ;
8
8
import { FileIcon } from "./FileIcon" ;
9
- import { useTranslationsStore } from "../utils/useTranslationsStore" ;
9
+ import { FileUploaderContainerProps } from "../../typings/FileUploaderProps" ;
10
+ import { ActionsBar } from "./ActionsBar" ;
10
11
11
12
interface FileEntryContainerProps {
12
13
store : FileStore ;
14
+ actions ?: FileUploaderContainerProps [ "customButtons" ] ;
13
15
}
14
16
15
- export const FileEntryContainer = observer ( ( { store } : FileEntryContainerProps ) : ReactElement => {
16
- const onRemove = useCallback ( ( ) => {
17
- store . remove ( ) ;
18
- } , [ store ] ) ;
17
+ export const FileEntryContainer = observer ( ( { store, actions } : FileEntryContainerProps ) : ReactElement | null => {
18
+ const defaultAction = actions ?. find ( a => {
19
+ return a . buttonIsDefault ;
20
+ } ) ;
21
+
22
+ const defaultListAction = defaultAction ?. buttonActionFile ?? defaultAction ?. buttonActionImage ;
23
+
24
+ const onDefaultAction = useCallback ( ( ) => {
25
+ store . executeAction ( defaultListAction ) ;
26
+ } , [ store , defaultListAction ] ) ;
27
+
28
+ if ( store . fileStatus === "missing" ) {
29
+ return null ;
30
+ }
19
31
20
32
return (
21
33
< FileEntry
@@ -25,10 +37,8 @@ export const FileEntryContainer = observer(({ store }: FileEntryContainerProps):
25
37
mimeType = { store . mimeType }
26
38
fileStatus = { store . fileStatus }
27
39
errorMessage = { store . errorDescription }
28
- canRemove = { store . canRemove }
29
- onRemove = { onRemove }
30
- canDownload = { store . canDownload }
31
- downloadUrl = { store . downloadUrl }
40
+ defaultAction = { defaultListAction && store . canExecute ( defaultListAction ) ? onDefaultAction : undefined }
41
+ actions = { < ActionsBar actions = { actions } store = { store } /> }
32
42
/>
33
43
) ;
34
44
} ) ;
@@ -42,44 +52,32 @@ interface FileEntryProps {
42
52
fileStatus : FileStatus ;
43
53
errorMessage ?: string ;
44
54
45
- canRemove : boolean ;
46
- onRemove : ( ) => void ;
55
+ defaultAction ?: ( ) => void ;
47
56
48
- canDownload : boolean ;
49
- downloadUrl ?: string ;
57
+ actions ?: ReactNode ;
50
58
}
51
59
52
60
function FileEntry ( props : FileEntryProps ) : ReactElement {
53
- const translations = useTranslationsStore ( ) ;
61
+ const { defaultAction } = props ;
54
62
55
- const { canDownload, downloadUrl, onRemove } = props ;
56
-
57
- const onViewClick = useCallback (
63
+ const onClick = useCallback (
58
64
( e : MouseEvent < HTMLDivElement > ) => {
59
65
e . stopPropagation ( ) ;
60
66
e . preventDefault ( ) ;
61
- onDownloadClick ( downloadUrl ) ;
67
+ defaultAction ?. ( ) ;
62
68
} ,
63
- [ downloadUrl ]
69
+ [ defaultAction ]
64
70
) ;
65
71
66
72
const onKeyDown = useCallback (
67
73
( e : KeyboardEvent < HTMLDivElement > ) => {
68
74
if ( e . code === "Enter" || e . code === "Space" ) {
69
75
e . stopPropagation ( ) ;
70
76
e . preventDefault ( ) ;
71
- onDownloadClick ( downloadUrl ) ;
77
+ defaultAction ?. ( ) ;
72
78
}
73
79
} ,
74
- [ downloadUrl ]
75
- ) ;
76
-
77
- const onRemoveClick = useCallback (
78
- ( e : MouseEvent < HTMLButtonElement > ) => {
79
- e . stopPropagation ( ) ;
80
- onRemove ( ) ;
81
- } ,
82
- [ onRemove ]
80
+ [ defaultAction ]
83
81
) ;
84
82
85
83
return (
@@ -89,9 +87,9 @@ function FileEntry(props: FileEntryProps): ReactElement {
89
87
invalid : props . fileStatus === "validationError"
90
88
} ) }
91
89
title = { props . title }
92
- tabIndex = { canDownload ? 0 : undefined }
93
- onClick = { canDownload ? onViewClick : undefined }
94
- onKeyDown = { canDownload ? onKeyDown : undefined }
90
+ tabIndex = { ! ! defaultAction ? 0 : undefined }
91
+ onClick = { ! ! defaultAction ? onClick : undefined }
92
+ onKeyDown = { ! ! defaultAction ? onKeyDown : undefined }
95
93
>
96
94
< div className = { "entry-details" } >
97
95
< div
@@ -111,19 +109,7 @@ function FileEntry(props: FileEntryProps): ReactElement {
111
109
< div className = { "entry-details-main-size" } > { props . size !== - 1 && fileSize ( props . size ) } </ div >
112
110
</ div >
113
111
114
- < div className = { "entry-details-actions" } >
115
- { downloadUrl && < div className = { "download-icon" } /> }
116
- < button
117
- className = { classNames ( "action-button" , {
118
- disabled : ! props . canRemove
119
- } ) }
120
- onClick = { onRemoveClick }
121
- role = { "button" }
122
- title = { translations . get ( "removeButtonTextMessage" ) }
123
- >
124
- < div className = { "remove-icon" } />
125
- </ button >
126
- </ div >
112
+ { props . actions }
127
113
</ div >
128
114
< div className = { "entry-progress" } >
129
115
< ProgressBar visible = { props . fileStatus === "uploading" } indeterminate />
@@ -134,11 +120,3 @@ function FileEntry(props: FileEntryProps): ReactElement {
134
120
</ div >
135
121
) ;
136
122
}
137
-
138
- function onDownloadClick ( fileUrl : string | undefined ) : void {
139
- if ( ! fileUrl ) {
140
- return ;
141
- }
142
- const url = `${ fileUrl } &target=window` ;
143
- window . open ( url , "mendix_file" ) ;
144
- }
0 commit comments