import React from 'react'; import PropTypes from 'prop-types'; import classnames from 'classnames'; import { AccountStore } from 'mailspring-exports'; import { Menu, ButtonDropdown, InjectedComponentSet } from 'mailspring-component-kit'; export default class AccountContactField extends React.Component { static displayName = 'AccountContactField'; static propTypes = { value: PropTypes.object, accounts: PropTypes.array, session: PropTypes.object.isRequired, draft: PropTypes.object.isRequired, onChange: PropTypes.func.isRequired, }; _onChooseContact = contact => { this.props.onChange({ from: [contact] }); this.props.session.ensureCorrectAccount(); this._dropdownComponent.toggleDropdown(); }; _renderAccountSelector() { if (!this.props.value) { return ; } const label = this.props.value.toString(); const multipleAccounts = this.props.accounts.length > 1; const hasAliases = this.props.accounts[0] && this.props.accounts[0].aliases.length > 0; if (multipleAccounts || hasAliases) { return ( { this._dropdownComponent = cm; }} bordered={false} primaryItem={{label}} menu={this._renderAccounts(this.props.accounts)} /> ); } return this._renderAccountSpan(label); } _renderAccountSpan = label => { return ( {label} ); }; _renderMenuItem = contact => { const className = classnames({ contact: true, 'is-alias': contact.isAlias, }); return {contact.toString()}; }; _renderAccounts(accounts) { const items = AccountStore.aliasesFor(accounts); return ( contact.id} itemContent={this._renderMenuItem} onSelect={this._onChooseContact} /> ); } _renderFromFieldComponents = () => { const { draft, session, accounts } = this.props; return ( ); }; render() { return ( From: {this._renderAccountSelector()} {this._renderFromFieldComponents()} ); } }