import { Gridicon, Popover } from '@automattic/components';
import debugModule from 'debug';
import { localize } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { Component, createRef } from 'react';
import ReactDom from 'react-dom';
import AsyncLoad from 'calypso/components/async-load';
import InfiniteList from 'calypso/components/infinite-list';
import PopoverMenuItem from 'calypso/components/popover-menu/item';
import UserItem from 'calypso/components/user';
import { hasTouch } from 'calypso/lib/touch-detect';
import 'calypso/components/popover-menu/style.scss';
/**
* Module variables
*/
const debug = debugModule( 'calypso:author-selector' );
class AuthorSwitcherShell extends Component {
static propTypes = {
users: PropTypes.array,
allowSingleUser: PropTypes.bool,
popoverPosition: PropTypes.string,
ignoreContext: PropTypes.shape( { getDOMNode: PropTypes.func } ),
transformAuthor: PropTypes.func,
};
state = {
showAuthorMenu: false,
};
authorSelectorSearchRef = createRef();
authorSelectorToggleRef = createRef();
authorSelectorChevronRef = createRef();
render() {
const { users } = this.props;
if ( ! this.userCanSelectAuthor() ) {
return { this.props.children };
}
return (
{ this.props.children }
{ ( this.props.search || users.length > 10 ) && (
) }
{ ! users.length && this.props.search && ! this.props.isLoading ? (
this.noUsersFound()
) : (
) }
);
}
setListContext = ( infiniteListInstance ) => {
this.setState( {
listContext: ReactDom.findDOMNode( infiniteListInstance ),
} );
};
userCanSelectAuthor() {
const { users } = this.props;
if ( this.props.search ) {
return true;
}
// no user choice
if ( ! users || ! users.length || ( ! this.props.allowSingleUser && users.length === 1 ) ) {
return false;
}
return true;
}
toggleShowAuthor = () => {
this.setState( {
showAuthorMenu: ! this.state.showAuthorMenu,
} );
};
onClose = ( event ) => {
const toggleElement = ReactDom.findDOMNode( this.authorSelectorToggleRef.current );
if ( event && toggleElement.contains( event.target ) ) {
// let toggleShowAuthor() handle this case
return;
}
this.setState( {
showAuthorMenu: false,
} );
this.props.updateSearch( '' );
};
renderAuthor = ( rawAuthor ) => {
const { transformAuthor } = this.props;
const author = transformAuthor ? transformAuthor( rawAuthor ) : rawAuthor;
const authorGUID = this.getAuthorItemGUID( author );
return (
);
};
noUsersFound() {
return (
{ this.props.translate( 'No matching users found.' ) }
);
}
selectAuthor = ( author ) => {
debug( 'assign author:', author );
if ( this.props.onSelect ) {
this.props.onSelect( author );
}
this.setState( {
showAuthorMenu: false,
} );
this.props.updateSearch( '' );
};
getAuthorItemGUID = ( author ) => {
return 'author-item-' + author.ID;
};
renderLoadingAuthors = () => {
return (
);
};
}
export default localize( AuthorSwitcherShell );