Mailspring/app/internal_packages/thread-search/lib/thread-search-bar.jsx

99 lines
2.7 KiB
React
Raw Normal View History

2017-09-27 02:33:08 +08:00
import React, { Component } from 'react';
import PropTypes from 'prop-types';
2017-09-27 02:46:00 +08:00
import { Menu, SearchBar, ListensToFluxStore } from 'mailspring-component-kit';
import { FocusedPerspectiveStore } from 'mailspring-exports';
2017-09-27 02:33:08 +08:00
import SearchStore from './search-store';
import SearchActions from './search-actions';
class ThreadSearchBar extends Component {
static displayName = 'ThreadSearchBar';
static propTypes = {
query: PropTypes.string,
isSearching: PropTypes.bool,
suggestions: PropTypes.array,
perspective: PropTypes.object,
2017-09-27 02:33:08 +08:00
};
2017-09-27 02:33:08 +08:00
_onSelectSuggestion = suggestion => {
if (suggestion.thread) {
2017-09-27 02:33:08 +08:00
SearchActions.querySubmitted(`"${suggestion.thread.subject}"`);
} else {
SearchActions.querySubmitted(suggestion.value);
}
2017-09-27 02:33:08 +08:00
};
2017-09-27 02:33:08 +08:00
_onSearchQueryChanged = query => {
SearchActions.queryChanged(query);
if (query === '') {
this._onClearSearchQuery();
}
2017-09-27 02:33:08 +08:00
};
2017-09-27 02:33:08 +08:00
_onSubmitSearchQuery = query => {
SearchActions.querySubmitted(query);
2017-09-27 02:33:08 +08:00
};
_onClearSearchQuery = () => {
SearchActions.querySubmitted('');
2017-09-27 02:33:08 +08:00
};
_onClearSearchSuggestions = () => {
2017-09-27 02:33:08 +08:00
SearchActions.searchBlurred();
};
2017-09-27 02:33:08 +08:00
_renderSuggestion = suggestion => {
if (suggestion.contact) {
return <Menu.NameEmailItem name={suggestion.contact.name} email={suggestion.contact.email} />;
}
if (suggestion.thread) {
return suggestion.thread.subject;
}
if (suggestion.customElement) {
2017-09-27 02:33:08 +08:00
return suggestion.customElement;
}
return suggestion.label;
2017-09-27 02:33:08 +08:00
};
_placeholder = () => {
if (this.props.perspective.isInbox()) {
return 'Search all email';
}
2017-09-27 02:33:08 +08:00
return `Search ${this.props.perspective.name || ''}`;
};
render() {
2017-09-27 02:33:08 +08:00
const { query, isSearching, suggestions } = this.props;
return (
<SearchBar
className="thread-search-bar"
placeholder={this._placeholder()}
query={query}
suggestions={suggestions}
isSearching={isSearching}
2017-09-27 02:33:08 +08:00
suggestionKey={suggestion =>
suggestion.label || (suggestion.contact || {}).id || (suggestion.thread || {}).id}
suggestionRenderer={this._renderSuggestion}
onSelectSuggestion={this._onSelectSuggestion}
onSubmitSearchQuery={this._onSubmitSearchQuery}
onSearchQueryChanged={this._onSearchQueryChanged}
onClearSearchQuery={this._onClearSearchQuery}
onClearSearchSuggestions={this._onClearSearchSuggestions}
/>
2017-09-27 02:33:08 +08:00
);
}
}
export default ListensToFluxStore(ThreadSearchBar, {
stores: [SearchStore, FocusedPerspectiveStore],
getStateFromStores() {
return {
query: SearchStore.query(),
suggestions: SearchStore.suggestions(),
isSearching: SearchStore.isSearching(),
perspective: FocusedPerspectiveStore.current(),
};
},
2017-09-27 02:33:08 +08:00
});