forked from parse-community/parse-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileInput.react.js
More file actions
74 lines (70 loc) · 2.05 KB
/
FileInput.react.js
File metadata and controls
74 lines (70 loc) · 2.05 KB
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
/*
* Copyright (c) 2016-present, Parse, LLC
* All rights reserved.
*
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
import React from 'react';
import { escape } from 'lib/StringEscaping';
import styles from 'components/FileInput/FileInput.scss';
export default class FileInput extends React.Component {
handleChange(e) {
const file = e.target.files[0];
this.props.onChange(file);
}
renderLabel() {
if (!this.props.value) {
return null;
}
if (typeof this.props.value === 'string') {
return <span className={styles.label}>{escape(this.props.value)}</span>;
}
if (this.props.value.name && !this.props.value.url) {
return <span className={styles.label}>{escape(this.props.value.name)}</span>;
}
if (this.props.value.name && this.props.value.url) {
return (
<a href={this.props.value.url} target="_blank" className={styles.label} rel="noreferrer">
{escape(this.props.value.name)}
</a>
);
}
}
render() {
const inputProps = {
type: 'file',
value: '',
disabled: this.props.disabled,
onChange: this.handleChange.bind(this),
};
if (this.props.accept) {
inputProps.accept = this.props.accept;
}
const label = this.props.buttonText ? null : this.renderLabel();
const buttonStyles = [styles.button];
if (this.props.disabled || this.props.uploading) {
buttonStyles.push(styles.disabled);
}
if (label) {
buttonStyles.push(styles.withLabel);
}
return (
<div className={styles.input}>
<div className={buttonStyles.join(' ')}>
{this.props.uploading ? (
<div className={styles.spinner}></div>
) : this.props.buttonText ? (
<span>{this.props.buttonText}</span>
) : label ? (
<span>Change file</span>
) : (
<span>Upload a file</span>
)}
<input {...inputProps} />
</div>
{label}
</div>
);
}
}