2018-03-26 14:32:43 -07:00
|
|
|
// @flow
|
2019-02-22 00:01:59 -05:00
|
|
|
import * as React from 'react';
|
2020-10-15 19:06:05 -04:00
|
|
|
import { remote } from 'electron';
|
2018-03-26 14:32:43 -07:00
|
|
|
import Button from 'component/button';
|
2019-02-13 12:27:20 -04:00
|
|
|
import { FormField } from 'component/common/form';
|
2018-10-13 22:49:47 +07:00
|
|
|
|
2018-03-26 14:32:43 -07:00
|
|
|
type Props = {
|
|
|
|
type: string,
|
2019-06-28 13:00:29 -04:00
|
|
|
currentPath?: ?string,
|
2019-10-07 16:02:32 -04:00
|
|
|
onFileChosen: WebFile => void,
|
2019-06-28 03:27:55 -04:00
|
|
|
label?: string,
|
|
|
|
placeholder?: string,
|
2019-10-07 16:02:32 -04:00
|
|
|
accept?: string,
|
2019-10-10 20:37:18 -04:00
|
|
|
error?: string,
|
|
|
|
disabled?: boolean,
|
2020-06-30 01:51:15 -04:00
|
|
|
autoFocus?: boolean,
|
2018-03-26 14:32:43 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class FileSelector extends React.PureComponent<Props> {
|
2020-06-30 01:51:15 -04:00
|
|
|
static defaultProps = {
|
|
|
|
autoFocus: false,
|
|
|
|
};
|
|
|
|
|
2020-01-16 08:46:04 +13:00
|
|
|
componentDidUpdate(prevProps: Props) {
|
|
|
|
// If the form has just been cleared,
|
|
|
|
// clear the file input
|
|
|
|
if (prevProps.currentPath && !this.props.currentPath) {
|
|
|
|
this.fileInput.current.value = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-26 14:32:43 -07:00
|
|
|
static defaultProps = {
|
|
|
|
type: 'file',
|
|
|
|
};
|
|
|
|
|
2019-09-27 12:22:15 -04:00
|
|
|
fileInput: React.ElementRef<any>;
|
2019-02-22 00:01:59 -05:00
|
|
|
|
2018-03-26 14:32:43 -07:00
|
|
|
constructor() {
|
|
|
|
super();
|
2019-02-22 00:01:59 -05:00
|
|
|
this.fileInput = React.createRef();
|
2019-10-07 16:02:32 -04:00
|
|
|
this.handleFileInputSelection = this.handleFileInputSelection.bind(this);
|
|
|
|
this.fileInputButton = this.fileInputButton.bind(this);
|
2018-03-26 14:32:43 -07:00
|
|
|
}
|
|
|
|
|
2019-10-07 16:02:32 -04:00
|
|
|
handleFileInputSelection = () => {
|
2019-09-27 12:22:15 -04:00
|
|
|
const { files } = this.fileInput.current;
|
|
|
|
if (!files) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-07 16:02:32 -04:00
|
|
|
const file = files[0];
|
2020-10-15 19:06:05 -04:00
|
|
|
|
2019-09-27 12:22:15 -04:00
|
|
|
if (this.props.onFileChosen) {
|
2019-10-07 16:02:32 -04:00
|
|
|
this.props.onFileChosen(file);
|
2019-09-27 12:22:15 -04:00
|
|
|
}
|
2019-10-07 16:02:32 -04:00
|
|
|
};
|
|
|
|
|
2020-10-15 19:06:05 -04:00
|
|
|
handleDirectoryInputSelection = () => {
|
|
|
|
remote.dialog.showOpenDialog({ properties: ['openDirectory'] }).then(result => {
|
|
|
|
const path = result && result.filePaths[0];
|
|
|
|
if (path) {
|
|
|
|
// $FlowFixMe
|
|
|
|
this.props.onFileChosen({ path });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-10-07 16:02:32 -04:00
|
|
|
fileInputButton = () => {
|
|
|
|
this.fileInput.current.click();
|
|
|
|
};
|
2019-02-22 00:01:59 -05:00
|
|
|
|
2018-03-26 14:32:43 -07:00
|
|
|
input: ?HTMLInputElement;
|
|
|
|
|
|
|
|
render() {
|
2020-07-23 13:02:07 -04:00
|
|
|
const { type, currentPath, label, placeholder, accept, error, disabled, autoFocus = false } = this.props;
|
2019-10-07 16:02:32 -04:00
|
|
|
const placeHolder = currentPath || placeholder;
|
2019-12-09 13:51:00 -05:00
|
|
|
|
2018-03-26 14:32:43 -07:00
|
|
|
return (
|
2019-02-22 00:01:59 -05:00
|
|
|
<React.Fragment>
|
|
|
|
<FormField
|
2019-06-28 03:27:55 -04:00
|
|
|
label={label}
|
2019-03-18 01:06:41 -04:00
|
|
|
webkitdirectory="true"
|
|
|
|
className="form-field--copyable"
|
2019-10-10 20:37:18 -04:00
|
|
|
error={error}
|
|
|
|
disabled={disabled}
|
2019-03-18 01:06:41 -04:00
|
|
|
type="text"
|
|
|
|
readOnly="readonly"
|
2019-10-07 16:02:32 -04:00
|
|
|
value={placeHolder || __('Choose a file')}
|
2019-10-10 20:37:18 -04:00
|
|
|
inputButton={
|
2020-06-30 01:51:15 -04:00
|
|
|
<Button
|
|
|
|
autoFocus={autoFocus}
|
|
|
|
button="secondary"
|
|
|
|
disabled={disabled}
|
2020-10-15 19:06:05 -04:00
|
|
|
onClick={type === 'openDirectory' ? this.handleDirectoryInputSelection : this.fileInputButton}
|
2020-07-23 13:02:07 -04:00
|
|
|
label={__('Browse')}
|
2020-06-30 01:51:15 -04:00
|
|
|
/>
|
2019-10-10 20:37:18 -04:00
|
|
|
}
|
2019-10-07 16:02:32 -04:00
|
|
|
/>
|
|
|
|
<input
|
|
|
|
type={'file'}
|
|
|
|
style={{ display: 'none' }}
|
|
|
|
accept={accept}
|
|
|
|
ref={this.fileInput}
|
2020-10-15 19:06:05 -04:00
|
|
|
onChange={() => (type === 'openDirectory' ? () => {} : this.handleFileInputSelection())}
|
2019-10-07 16:02:32 -04:00
|
|
|
webkitdirectory={type === 'openDirectory' ? 'True' : null}
|
2019-02-22 00:01:59 -05:00
|
|
|
/>
|
|
|
|
</React.Fragment>
|
2018-03-26 14:32:43 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FileSelector;
|