PluginProbe
Elementor Website Builder – more than just a page builder / 3.8.0-beta1
Elementor Website Builder – more than just a page builder v3.8.0-beta1
4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 All 457 releases
elementor / app / assets / js / molecules / upload-file.js

upload-file.js in Elementor Website Builder – more than just a page builder 3.8.0-beta1, at app/assets/js/molecules/upload-file.js

111 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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