import React from "react"; const { remote } = require("electron"); class FileSelector extends React.PureComponent { static propTypes = { type: React.PropTypes.oneOf(["file", "directory"]), initPath: React.PropTypes.string, onFileChosen: React.PropTypes.func, }; static defaultProps = { type: "file", }; constructor(props) { super(props); this._inputElem = null; } componentWillMount() { this.setState({ path: this.props.initPath || null, }); } handleButtonClick() { remote.dialog.showOpenDialog( { properties: this.props.type == "file" ? ["openFile"] : ["openDirectory", "createDirectory"], }, paths => { if (!paths) { // User hit cancel, so do nothing return; } const path = paths[0]; this.setState({ path: path, }); if (this.props.onFileChosen) { this.props.onFileChosen(path); } } ); } render() { return (
{" "} { this._inputElem = input; }} onFocus={() => { this._inputElem.select(); }} readOnly="readonly" value={this.state.path || __("No File Chosen")} />
); } } export default FileSelector;