add start of user search dropdown in compose view
parent
887ba53902
commit
e4b36fe7f7
@ -0,0 +1,73 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { PropsData as ConversationListItemPropsType } from './ConversationListItem';
|
||||||
|
|
||||||
|
import { LocalizerType } from '../types/Util';
|
||||||
|
import classNames from 'classnames';
|
||||||
|
|
||||||
|
export type PropsData = {
|
||||||
|
contacts: Array<ConversationListItemPropsType>;
|
||||||
|
regionCode: string;
|
||||||
|
searchTerm: string;
|
||||||
|
selectedContact: Number;
|
||||||
|
onContactSelected: any;
|
||||||
|
};
|
||||||
|
|
||||||
|
type PropsHousekeeping = {
|
||||||
|
i18n: LocalizerType;
|
||||||
|
};
|
||||||
|
|
||||||
|
type Props = PropsData & PropsHousekeeping;
|
||||||
|
|
||||||
|
export class UserSearchResults extends React.Component<Props> {
|
||||||
|
public constructor(props: Props) {
|
||||||
|
super(props);
|
||||||
|
}
|
||||||
|
|
||||||
|
public render() {
|
||||||
|
const { contacts, i18n, searchTerm } = this.props;
|
||||||
|
|
||||||
|
const haveContacts = contacts && contacts.length;
|
||||||
|
const noResults = !haveContacts;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="module-search-results">
|
||||||
|
{noResults ? (
|
||||||
|
<div className="module-search-results__no-results">
|
||||||
|
{i18n('noSearchResults', [searchTerm])}
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
|
{haveContacts ? this.renderContacts(contacts) : null}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private renderContacts(items: Array<ConversationListItemPropsType>) {
|
||||||
|
return (
|
||||||
|
<div className="contacts-dropdown">
|
||||||
|
{items.map((contact, index) => this.renderContact(contact, index))}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private renderContact(contact: ConversationListItemPropsType, index: Number) {
|
||||||
|
const { profileName, phoneNumber } = contact;
|
||||||
|
const { selectedContact } = this.props;
|
||||||
|
|
||||||
|
const shortenedPubkey = window.shortenPubkey(phoneNumber);
|
||||||
|
const rowContent = `${profileName} ${shortenedPubkey}`;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className={classNames(
|
||||||
|
'contacts-dropdown-row',
|
||||||
|
selectedContact === index && 'contacts-dropdown-row-selected'
|
||||||
|
)}
|
||||||
|
key={contact.phoneNumber}
|
||||||
|
onClick={() => this.props.onContactSelected(contact.phoneNumber)}
|
||||||
|
role="button"
|
||||||
|
>
|
||||||
|
{rowContent}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,96 @@
|
|||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
import { UserSearchResults } from '../UserSearchResults';
|
||||||
|
import { SessionSearchInput } from './SessionSearchInput';
|
||||||
|
|
||||||
|
import { PropsData as SearchResultsProps } from '../SearchResults';
|
||||||
|
|
||||||
|
export interface Props {
|
||||||
|
searchTerm: string;
|
||||||
|
placeholder: string;
|
||||||
|
searchResults?: SearchResultsProps;
|
||||||
|
updateSearch: (searchTerm: string) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface State {
|
||||||
|
selectedContact: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class UserSearchDropdown extends React.Component<Props, State> {
|
||||||
|
private readonly updateSearchBound: (searchedString: string) => void;
|
||||||
|
|
||||||
|
public constructor(props: Props) {
|
||||||
|
super(props);
|
||||||
|
this.updateSearchBound = this.updateSearch.bind(this);
|
||||||
|
this.handleNavigation = this.handleNavigation.bind(this);
|
||||||
|
this.handleContactSelected = this.handleContactSelected.bind(this);
|
||||||
|
this.state = {
|
||||||
|
selectedContact: 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public handleNavigation(e: any) {
|
||||||
|
const { selectedContact } = this.state;
|
||||||
|
const { searchResults } = this.props;
|
||||||
|
// arrow up/down button should select next/previous list element
|
||||||
|
if (
|
||||||
|
e.keyCode === 38 &&
|
||||||
|
selectedContact > 0 &&
|
||||||
|
searchResults &&
|
||||||
|
searchResults.contacts.length > 0
|
||||||
|
) {
|
||||||
|
this.setState(prevState => ({
|
||||||
|
selectedContact: +prevState.selectedContact - 1,
|
||||||
|
}));
|
||||||
|
} else if (
|
||||||
|
e.keyCode === 40 &&
|
||||||
|
searchResults &&
|
||||||
|
selectedContact < searchResults.contacts.length - 1
|
||||||
|
) {
|
||||||
|
this.setState(prevState => ({
|
||||||
|
selectedContact: +prevState.selectedContact + 1,
|
||||||
|
}));
|
||||||
|
} else if (
|
||||||
|
e.key === 'Enter' &&
|
||||||
|
searchResults &&
|
||||||
|
searchResults.contacts.length > 0
|
||||||
|
) {
|
||||||
|
this.handleContactSelected(
|
||||||
|
searchResults.contacts[selectedContact].phoneNumber
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public render() {
|
||||||
|
const { searchResults, placeholder } = this.props;
|
||||||
|
const { selectedContact } = this.state;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<SessionSearchInput
|
||||||
|
searchString={this.props.searchTerm}
|
||||||
|
onChange={this.updateSearchBound}
|
||||||
|
placeholder={placeholder}
|
||||||
|
handleNavigation={this.handleNavigation}
|
||||||
|
/>
|
||||||
|
{searchResults && (
|
||||||
|
<UserSearchResults
|
||||||
|
{...searchResults}
|
||||||
|
i18n={window.i18n}
|
||||||
|
selectedContact={selectedContact}
|
||||||
|
onContactSelected={this.handleContactSelected}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public updateSearch(data: string) {
|
||||||
|
this.setState({ selectedContact: 0 });
|
||||||
|
this.props.updateSearch(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public handleContactSelected(key: string) {
|
||||||
|
this.updateSearch(key);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue