File size: 5,014 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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 <span>{ this.props.children }</span>;
		}

		return (
			<span>
				<span
					className="author-selector__author-toggle"
					onClick={ this.toggleShowAuthor }
					onKeyDown={ this.toggleShowAuthor }
					role="button"
					tabIndex={ -1 }
					ref={ this.authorSelectorToggleRef }
				>
					{ this.props.children }
					<Gridicon ref={ this.authorSelectorChevronRef } icon="chevron-down" size={ 18 } />
				</span>
				<Popover
					isVisible={ this.state.showAuthorMenu }
					onClose={ this.onClose }
					position={ this.props.popoverPosition }
					context={ this.authorSelectorChevronRef.current }
					onKeyDown={ this.onKeyDown }
					className="author-selector__popover popover"
					ignoreContext={ this.props.ignoreContext }
				>
					{ ( this.props.search || users.length > 10 ) && (
						<AsyncLoad
							require="@automattic/search"
							compact
							onSearch={ this.props.updateSearch }
							placeholder={ this.props.translate( 'Find Author…', { context: 'search label' } ) }
							delaySearch
							// eslint-disable-next-line jsx-a11y/no-autofocus
							autoFocus={ ! hasTouch() }
						/>
					) }
					{ ! users.length && this.props.search && ! this.props.isLoading ? (
						this.noUsersFound()
					) : (
						<InfiniteList
							items={ users }
							key={ this.props.listKey }
							className="author-selector__infinite-list"
							ref={ this.setListContext }
							context={ this.state.listContext }
							fetchingNextPage={ this.props.isFetchingNextPage }
							guessedItemHeight={ 42 }
							lastPage={ ! this.props.hasNextPage }
							fetchNextPage={ this.props.fetchNextPage }
							getItemRef={ this.getAuthorItemGUID }
							renderLoadingPlaceholders={ this.renderLoadingAuthors }
							renderItem={ this.renderAuthor }
						/>
					) }
				</Popover>
			</span>
		);
	}

	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 (
			<PopoverMenuItem
				className="author-selector__menu-item"
				onClick={ this.selectAuthor.bind( this, author ) }
				focusOnHover={ false }
				key={ authorGUID }
				tabIndex="-1"
			>
				<UserItem user={ author } />
			</PopoverMenuItem>
		);
	};

	noUsersFound() {
		return (
			<div className="author-selector__no-users">
				{ this.props.translate( 'No matching users found.' ) }
			</div>
		);
	}

	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 (
			<PopoverMenuItem disabled key="author-item-placeholder">
				<UserItem />
			</PopoverMenuItem>
		);
	};
}

export default localize( AuthorSwitcherShell );