import * as React from 'react'; import classnames from 'classnames'; import styles from './BrowseInput.sass'; import IReactComponentProps from '../../../common/structures/IReactComponentProps'; import { TextButton, TextButtonPropSize } from '../../buttons/TextButton/TextButton'; import untildify from 'untildify'; import { FunctionGeneric } from '../../../common/structures/Generics'; const { ipcRenderer } = require('electron'); interface IProps extends IReactComponentProps { defaultPath?: string; dialogProperties?: string[]; dialogTitle?: string; isFormInput?: boolean; isInline?: boolean; onChange?: FunctionGeneric; placeholder?: string; value?: string; } interface IState { value: string | undefined; } export default class BrowseInput extends React.Component { constructor (props: IProps) { super(props); this.state = { value: this.props.value || '', }; this.browseFolder = this.browseFolder.bind(this); } UNSAFE_componentWillReceiveProps (nextProps: IProps) { if ('value' in nextProps) { this.setState({ value: nextProps.value }); } } async browseFolder () { const { canceled, filePaths } = await ipcRenderer.invoke( 'ui:open-dialog-in-current-window', { defaultPath: untildify(this.state.value! || this.props.defaultPath!), properties: this.props.dialogProperties, title: this.props.dialogTitle, } ); if (canceled || !filePaths) { return; } const value = filePaths[0]; if (this.props.onChange && this.props.onChange.call(this, value) === false) { return false; } this.setState({ value }); } render () { return (
{this.state.value || this.props.placeholder} Browse
); } }