lbry-desktop/ui/js/component/file-selector.js

87 lines
2 KiB
JavaScript
Raw Normal View History

2017-06-06 23:19:12 +02:00
import React from "react";
2017-05-12 12:15:59 +02:00
2017-06-06 23:19:12 +02:00
const { remote } = require("electron");
2017-06-08 06:42:19 +02:00
class FileSelector extends React.PureComponent {
2017-05-12 12:15:59 +02:00
static propTypes = {
2017-06-06 23:19:12 +02:00
type: React.PropTypes.oneOf(["file", "directory"]),
2017-05-12 12:15:59 +02:00
initPath: React.PropTypes.string,
onFileChosen: React.PropTypes.func,
2017-06-06 23:19:12 +02:00
};
2017-05-12 12:15:59 +02:00
static defaultProps = {
2017-06-06 23:19:12 +02:00
type: "file",
};
2017-05-12 12:15:59 +02:00
2017-10-03 05:04:21 +02:00
constructor(props) {
super(props);
this._inputElem = null;
}
2017-05-12 12:15:59 +02:00
componentWillMount() {
this.setState({
path: this.props.initPath || null,
});
}
handleButtonClick() {
2017-06-06 23:19:12 +02:00
remote.dialog.showOpenDialog(
{
properties: this.props.type == "file"
? ["openFile"]
: ["openDirectory", "createDirectory"],
2017-06-06 23:19:12 +02:00
},
paths => {
if (!paths) {
// User hit cancel, so do nothing
return;
}
2017-05-12 12:15:59 +02:00
2017-06-06 23:19:12 +02:00
const path = paths[0];
this.setState({
path: path,
});
if (this.props.onFileChosen) {
this.props.onFileChosen(path);
}
2017-05-12 12:15:59 +02:00
}
2017-06-06 23:19:12 +02:00
);
2017-05-12 12:15:59 +02:00
}
render() {
return (
<div className="file-selector">
2017-06-06 23:19:12 +02:00
<button
type="button"
2017-10-03 05:04:21 +02:00
className="button-block button-alt file-selector__choose-button"
2017-06-06 23:19:12 +02:00
onClick={() => this.handleButtonClick()}
>
2017-10-03 06:08:25 +02:00
<span className="button__content">
<span className="button-label">
{this.props.type == "file"
? __("Choose File")
: __("Choose Directory")}
</span>
</span>
2017-05-12 12:15:59 +02:00
</button>
2017-06-06 23:19:12 +02:00
{" "}
2017-05-12 12:15:59 +02:00
<span className="file-selector__path">
2017-10-03 05:04:21 +02:00
<input
className="input-copyable"
type="text"
ref={input => {
this._inputElem = input;
}}
onFocus={() => {
this._inputElem.select();
}}
readOnly="readonly"
value={this.state.path || __("No File Chosen")}
/>
2017-05-12 12:15:59 +02:00
</span>
</div>
);
}
2017-06-06 06:21:55 +02:00
}
2017-05-12 12:15:59 +02:00
export default FileSelector;