| 1 |
import { useRef } from 'react'; |
| 2 |
|
| 3 |
import Button from 'elementor-app/ui/molecules/button'; |
| 4 |
|
| 5 |
import { arrayToClassName, isOneOf } from 'elementor-app/utils/utils.js'; |
| 6 |
|
| 7 |
import './upload-file.scss'; |
| 8 |
|
| 9 |
export default function UploadFile( props ) { |
| 10 |
const fileInput = useRef( null ), |
| 11 |
baseClassName = 'e-app-upload-file', |
| 12 |
classes = [ baseClassName, props.className ]; |
| 13 |
|
| 14 |
// For 'wp-media' type. |
| 15 |
let frame; |
| 16 |
|
| 17 |
return ( |
| 18 |
<div className={ arrayToClassName( classes ) }> |
| 19 |
<input |
| 20 |
ref={ fileInput } |
| 21 |
type="file" |
| 22 |
accept={ props.filetypes.map( ( type ) => '.' + type ).join( ', ' ) } |
| 23 |
className="e-app-upload-file__input" |
| 24 |
onChange={ ( event ) => { |
| 25 |
const file = event.target.files[ 0 ]; |
| 26 |
|
| 27 |
if ( file && isOneOf( file.type, props.filetypes ) ) { |
| 28 |
props.onFileSelect( file, event, 'browse' ); |
| 29 |
} else { |
| 30 |
fileInput.current.value = ''; |
| 31 |
|
| 32 |
props.onError( { |
| 33 |
id: 'file_not_allowed', |
| 34 |
message: __( 'This file type is not allowed', 'elementor' ), |
| 35 |
} ); |
| 36 |
} |
| 37 |
} } |
| 38 |
/> |
| 39 |
|
| 40 |
<Button |
| 41 |
className="e-app-upload-file__button" |
| 42 |
text={ props.text } |
| 43 |
variant={ props.variant } |
| 44 |
color={ props.color } |
| 45 |
size="lg" |
| 46 |
hideText={ props.isLoading } |
| 47 |
icon={ props.isLoading ? 'eicon-loading eicon-animation-spin' : '' } |
| 48 |
onClick={ () => { |
| 49 |
if ( props.onFileChoose ) { |
| 50 |
props.onFileChoose(); |
| 51 |
} |
| 52 |
if ( ! props.isLoading ) { |
| 53 |
if ( props.onButtonClick ) { |
| 54 |
props.onButtonClick(); |
| 55 |
} |
| 56 |
|
| 57 |
if ( 'file-explorer' === props.type ) { |
| 58 |
fileInput.current.click(); |
| 59 |
} else if ( 'wp-media' === props.type ) { |
| 60 |
if ( frame ) { |
| 61 |
frame.open(); |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
// Initialize the WP Media frame. |
| 66 |
frame = wp.media( { |
| 67 |
multiple: false, |
| 68 |
library: { |
| 69 |
type: [ 'image', 'image/svg+xml' ], |
| 70 |
}, |
| 71 |
} ); |
| 72 |
|
| 73 |
frame.on( 'select', () => { |
| 74 |
if ( props.onWpMediaSelect ) { |
| 75 |
props.onWpMediaSelect( frame ); |
| 76 |
} |
| 77 |
} ); |
| 78 |
|
| 79 |
frame.open(); |
| 80 |
} |
| 81 |
} |
| 82 |
} } |
| 83 |
/> |
| 84 |
</div> |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
UploadFile.propTypes = { |
| 89 |
className: PropTypes.string, |
| 90 |
type: PropTypes.string, |
| 91 |
onWpMediaSelect: PropTypes.func, |
| 92 |
text: PropTypes.string, |
| 93 |
onFileSelect: PropTypes.func, |
| 94 |
isLoading: PropTypes.bool, |
| 95 |
filetypes: PropTypes.array.isRequired, |
| 96 |
onError: PropTypes.func, |
| 97 |
variant: PropTypes.string, |
| 98 |
color: PropTypes.string, |
| 99 |
onButtonClick: PropTypes.func, |
| 100 |
onFileChoose: PropTypes.func, |
| 101 |
}; |
| 102 |
|
| 103 |
UploadFile.defaultProps = { |
| 104 |
className: '', |
| 105 |
type: 'file-explorer', |
| 106 |
text: __( 'Select File', 'elementor' ), |
| 107 |
onError: () => {}, |
| 108 |
variant: 'contained', |
| 109 |
color: 'primary', |
| 110 |
}; |
| 111 |
|