import React from 'react'; import ReactDOM from 'react-dom'; import {Utils} from 'nylas-exports'; import {InjectedComponentSet} from 'nylas-component-kit'; export default class CollapsedParticipants extends React.Component { static displayName = "CollapsedParticipants"; static propTypes = { // Arrays of Contact objects. to: React.PropTypes.array, cc: React.PropTypes.array, bcc: React.PropTypes.array, } static defaultProps = { to: [], cc: [], bcc: [], } constructor(props = {}) { super(props); this._keyPrefix = Utils.generateTempId(); this.state = { numToDisplay: 999, numRemaining: 0, numBccRemaining: 0, } } componentDidMount() { this._setNumHiddenParticipants(); } componentWillReceiveProps() { // Always re-evaluate the hidden participant count when the participant set changes this.setState({ numToDisplay: 999, numRemaining: 0, numBccRemaining: 0, }); } shouldComponentUpdate(nextProps, nextState) { return !Utils.isEqualReact(nextProps, this.props) || !Utils.isEqualReact(nextState, this.state); } componentDidUpdate() { this._setNumHiddenParticipants(); } _setNumHiddenParticipants() { const $wrap = ReactDOM.findDOMNode(this.refs.participantsWrap) const $regulars = Array.from($wrap.getElementsByClassName("regular-contact")); const $bccs = Array.from($wrap.getElementsByClassName("bcc-contact")); const availableSpace = $wrap.getBoundingClientRect().width; let numRemaining = this.props.to.length + this.props.cc.length; let numBccRemaining = this.props.bcc.length; let numToDisplay = 0; let widthAccumulator = 0; for (const $p of $regulars) { widthAccumulator += $p.getBoundingClientRect().width; if (widthAccumulator >= availableSpace) { break; } numRemaining -= 1; numToDisplay += 1; } for (const $p of $bccs) { widthAccumulator += $p.getBoundingClientRect().width; if (widthAccumulator >= availableSpace) { break; } numBccRemaining -= 1; numToDisplay += 1; } this.setState({numToDisplay, numRemaining, numBccRemaining}); } _renderNumRemaining() { let str = null; if (this.state.numRemaining === 0 && this.state.numBccRemaining === 0) { return null; } else if (this.state.numRemaining > 0 && this.state.numBccRemaining === 0) { str = `${this.state.numRemaining} more`; } else if (this.state.numRemaining === 0 && this.state.numBccRemaining > 0) { str = `${this.state.numBccRemaining} Bcc`; } else if (this.state.numRemaining > 0 && this.state.numBccRemaining > 0) { str = `${this.state.numRemaining + this.state.numBccRemaining} more (${this.state.numBccRemaining} Bcc)`; } return (