import PropTypes from 'prop-types'; import React, { Component } from 'react'; import FileBrowserModal from 'Components/FileBrowser/FileBrowserModal'; import Icon from 'Components/Icon'; import { icons } from 'Helpers/Props'; import AutoSuggestInput from './AutoSuggestInput'; import FormInputButton from './FormInputButton'; import styles from './PathInput.css'; class PathInput extends Component { // // Lifecycle constructor(props, context) { super(props, context); this._node = document.getElementById('portal-root'); this.state = { value: props.value, isFileBrowserModalOpen: false }; } componentDidUpdate(prevProps) { const { value } = this.props; if (prevProps.value !== value) { this.setState({ value }); } } // // Control getSuggestionValue({ path }) { return path; } renderSuggestion({ path }, { query }) { const lastSeparatorIndex = query.lastIndexOf('\\') || query.lastIndexOf('/'); if (lastSeparatorIndex === -1) { return ( {path} ); } return ( {path.substr(0, lastSeparatorIndex)} {path.substr(lastSeparatorIndex)} ); } // // Listeners onInputChange = ({ value }) => { this.setState({ value }); }; onInputKeyDown = (event) => { if (event.key === 'Tab') { event.preventDefault(); const path = this.props.paths[0]; if (path) { this.props.onChange({ name: this.props.name, value: path.path }); if (path.type !== 'file') { this.props.onFetchPaths(path.path); } } } }; onInputBlur = () => { this.props.onChange({ name: this.props.name, value: this.state.value }); this.props.onClearPaths(); }; onSuggestionsFetchRequested = ({ value }) => { this.props.onFetchPaths(value); }; onSuggestionsClearRequested = () => { // Required because props aren't always rendered, but no-op // because we don't want to reset the paths after a path is selected. }; onSuggestionSelected = (event, { suggestionValue }) => { this.props.onFetchPaths(suggestionValue); }; onFileBrowserOpenPress = () => { this.setState({ isFileBrowserModalOpen: true }); }; onFileBrowserModalClose = () => { this.setState({ isFileBrowserModalOpen: false }); }; // // Render render() { const { className, name, paths, includeFiles, hasFileBrowser, onChange, ...otherProps } = this.props; const { value, isFileBrowserModalOpen } = this.state; return (