// @flow import * as React from 'react'; import Button from 'component/button'; import { FormField } from 'component/common/form'; type Props = { type: string, currentPath?: ?string, onFileChosen: WebFile => void, label?: string, placeholder?: string, fileLabel?: string, directoryLabel?: string, accept?: string, error?: string, disabled?: boolean, }; class FileSelector extends React.PureComponent { static defaultProps = { type: 'file', }; fileInput: React.ElementRef; constructor() { super(); this.fileInput = React.createRef(); this.handleFileInputSelection = this.handleFileInputSelection.bind(this); this.fileInputButton = this.fileInputButton.bind(this); } handleFileInputSelection = () => { const { files } = this.fileInput.current; if (!files) { return; } const file = files[0]; if (this.props.onFileChosen) { this.props.onFileChosen(file); } }; fileInputButton = () => { this.fileInput.current.click(); }; input: ?HTMLInputElement; render() { const { type, currentPath, label, fileLabel, directoryLabel, placeholder, accept, error, disabled } = this.props; const buttonLabel = type === 'file' ? fileLabel || __('Choose File') : directoryLabel || __('Choose Directory'); const placeHolder = currentPath || placeholder; return ( } /> this.handleFileInputSelection()} webkitdirectory={type === 'openDirectory' ? 'True' : null} /> ); } } export default FileSelector;