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

65 lines
1.4 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-05-12 12:15:59 +02:00
class FileSelector extends React.Component {
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
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"],
},
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"
className="file-selector__choose-button"
onClick={() => this.handleButtonClick()}
>
{this.props.type == "file"
? __("Choose File")
: __("Choose Directory")}
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-06-06 23:19:12 +02:00
{this.state.path ? 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;