2018-03-26 23:32:43 +02:00
|
|
|
// @flow
|
2019-02-22 06:01:59 +01:00
|
|
|
import * as React from 'react';
|
2019-10-07 22:02:32 +02:00
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
import Button from 'component/button';
|
2019-02-13 17:27:20 +01:00
|
|
|
import { FormField } from 'component/common/form';
|
2018-10-13 17:49:47 +02:00
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
type Props = {
|
|
|
|
type: string,
|
2019-06-28 19:00:29 +02:00
|
|
|
currentPath?: ?string,
|
2019-10-07 22:02:32 +02:00
|
|
|
onFileChosen: WebFile => void,
|
2019-06-28 09:27:55 +02:00
|
|
|
label?: string,
|
|
|
|
placeholder?: string,
|
2018-07-18 17:46:21 +02:00
|
|
|
fileLabel?: string,
|
2018-06-08 06:05:45 +02:00
|
|
|
directoryLabel?: string,
|
2019-10-07 22:02:32 +02:00
|
|
|
accept?: string,
|
2019-10-11 02:37:18 +02:00
|
|
|
error?: string,
|
|
|
|
disabled?: boolean,
|
2018-03-26 23:32:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class FileSelector extends React.PureComponent<Props> {
|
|
|
|
static defaultProps = {
|
|
|
|
type: 'file',
|
|
|
|
};
|
|
|
|
|
2019-09-27 18:22:15 +02:00
|
|
|
fileInput: React.ElementRef<any>;
|
2019-02-22 06:01:59 +01:00
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
constructor() {
|
|
|
|
super();
|
2019-02-22 06:01:59 +01:00
|
|
|
this.fileInput = React.createRef();
|
2019-10-07 22:02:32 +02:00
|
|
|
this.handleFileInputSelection = this.handleFileInputSelection.bind(this);
|
|
|
|
this.fileInputButton = this.fileInputButton.bind(this);
|
2018-03-26 23:32:43 +02:00
|
|
|
}
|
|
|
|
|
2019-10-07 22:02:32 +02:00
|
|
|
handleFileInputSelection = () => {
|
2019-09-27 18:22:15 +02:00
|
|
|
const { files } = this.fileInput.current;
|
|
|
|
if (!files) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-07 22:02:32 +02:00
|
|
|
const file = files[0];
|
2019-09-27 18:22:15 +02:00
|
|
|
if (this.props.onFileChosen) {
|
2019-10-07 22:02:32 +02:00
|
|
|
this.props.onFileChosen(file);
|
2019-09-27 18:22:15 +02:00
|
|
|
}
|
2019-10-07 22:02:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
fileInputButton = () => {
|
|
|
|
this.fileInput.current.click();
|
|
|
|
};
|
2019-02-22 06:01:59 +01:00
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
input: ?HTMLInputElement;
|
|
|
|
|
|
|
|
render() {
|
2019-10-11 02:37:18 +02:00
|
|
|
const { type, currentPath, label, fileLabel, directoryLabel, placeholder, accept, error, disabled } = this.props;
|
2018-04-02 15:59:23 +02:00
|
|
|
|
2019-06-28 09:27:55 +02:00
|
|
|
const buttonLabel = type === 'file' ? fileLabel || __('Choose File') : directoryLabel || __('Choose Directory');
|
2019-10-07 22:02:32 +02:00
|
|
|
const placeHolder = currentPath || placeholder;
|
2018-03-26 23:32:43 +02:00
|
|
|
return (
|
2019-02-22 06:01:59 +01:00
|
|
|
<React.Fragment>
|
|
|
|
<FormField
|
2019-06-28 09:27:55 +02:00
|
|
|
label={label}
|
2019-03-18 06:06:41 +01:00
|
|
|
webkitdirectory="true"
|
|
|
|
className="form-field--copyable"
|
2019-10-11 02:37:18 +02:00
|
|
|
error={error}
|
|
|
|
disabled={disabled}
|
2019-03-18 06:06:41 +01:00
|
|
|
type="text"
|
|
|
|
readOnly="readonly"
|
2019-10-07 22:02:32 +02:00
|
|
|
value={placeHolder || __('Choose a file')}
|
2019-10-11 02:37:18 +02:00
|
|
|
inputButton={
|
|
|
|
<Button button="primary" disabled={disabled} onClick={this.fileInputButton} label={buttonLabel} />
|
|
|
|
}
|
2019-10-07 22:02:32 +02:00
|
|
|
/>
|
|
|
|
<input
|
|
|
|
type={'file'}
|
|
|
|
style={{ display: 'none' }}
|
|
|
|
accept={accept}
|
|
|
|
ref={this.fileInput}
|
|
|
|
onChange={() => this.handleFileInputSelection()}
|
|
|
|
webkitdirectory={type === 'openDirectory' ? 'True' : null}
|
2019-02-22 06:01:59 +01:00
|
|
|
/>
|
|
|
|
</React.Fragment>
|
2018-03-26 23:32:43 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FileSelector;
|