File size: 2,121 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
import clsx from 'clsx';
import PropTypes from 'prop-types';
import { createRef, Component } from 'react';
import Count from '../count';
import TranslatableString from './translatable/proptype';

class SelectDropdownItem extends Component {
	static propTypes = {
		children: PropTypes.oneOfType( [ TranslatableString, PropTypes.node ] ).isRequired,
		compactCount: PropTypes.bool,
		path: PropTypes.string,
		selected: PropTypes.bool,
		onClick: PropTypes.func,
		count: PropTypes.number,
		disabled: PropTypes.bool,
		icon: PropTypes.element,
		ariaLabel: PropTypes.string,
		secondaryIcon: PropTypes.element,
	};

	static defaultProps = {
		selected: false,
	};

	linkRef = createRef();

	// called by the parent `SelectDropdown` component to focus the item on keyboard navigation
	focusLink() {
		this.linkRef.current.focus();
	}

	render() {
		const optionClassName = clsx( 'select-dropdown__item', this.props.className, {
			'is-selected': this.props.selected,
			'is-disabled': this.props.disabled,
			'has-icon': this.props.icon,
		} );

		const label = this.props.value || this.props.children;
		const ariaLabel =
			this.props.ariaLabel ||
			( 'number' === typeof this.props.count ? `${ label } (${ this.props.count })` : label );

		return (
			<li className="select-dropdown__option">
				<a
					ref={ this.linkRef }
					href={ this.props.path }
					className={ optionClassName }
					onClick={ this.props.disabled ? null : this.props.onClick }
					data-bold-text={ label }
					role="menuitem"
					tabIndex="0"
					aria-current={ this.props.selected }
					aria-label={ ariaLabel }
					data-e2e-title={ this.props.e2eTitle }
				>
					<span className="select-dropdown__item-text">
						{ this.props.icon }
						{ this.props.children }
					</span>
					{ 'number' === typeof this.props.count ? (
						<span data-text={ this.props.count } className="select-dropdown__item-count">
							<Count count={ this.props.count } compact={ this.props.compactCount } />
						</span>
					) : (
						<>{ this.props.secondaryIcon }</>
					) }
				</a>
			</li>
		);
	}
}

export default SelectDropdownItem;