File size: 2,659 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
import { ToggleControl } from '@wordpress/components';
import clsx from 'clsx';
import { Children, Component } from 'react';
import InfoPopover from 'calypso/components/info-popover';

import './style.scss';

class PluginAction extends Component {
	handleAction = ( event ) => {
		if ( ! this.props.disabledInfo ) {
			this.props.action();
		} else {
			this.infoPopover.handleClick( event );
		}
	};

	disabledInfoLabelRef = ( ref ) => {
		this.disabledInfoLabel = ref;
	};

	renderLabel() {
		if ( ! this.props.label ) {
			return null;
		}

		return (
			/* eslint-disable jsx-a11y/click-events-have-key-events */
			/* eslint-disable jsx-a11y/no-static-element-interactions */
			<span
				className={ clsx( 'plugin-action__label', { hide: this.props.hideLabel } ) }
				ref={ this.disabledInfoLabelRef }
				onClick={ this.handleAction }
			>
				<span className="plugin-action__label-text">{ this.props.label }</span>
				<span className="plugin-action__label-disabled-info">{ this.renderDisabledInfo() }</span>
			</span>
			/* eslint-enable jsx-a11y/click-events-have-key-events */
			/* eslint-enable jsx-a11y/no-static-element-interactions */
		);
	}

	infoPopoverRef = ( ref ) => {
		this.infoPopover = ref;
	};

	renderDisabledInfo() {
		if ( ! this.props.disabledInfo ) {
			return null;
		}

		return (
			<InfoPopover
				className="plugin-action__disabled-info"
				position="bottom left"
				popoverName={ 'Plugin Action Disabled' + this.props.label }
				gaEventCategory="Plugins"
				ref={ this.infoPopoverRef }
				ignoreContext={ this.disabledInfoLabel }
			>
				{ this.props.disabledInfo }
			</InfoPopover>
		);
	}

	renderToggle() {
		return (
			<>
				<ToggleControl
					onChange={ this.props.action }
					checked={ this.props.status }
					disabled={ this.props.inProgress || this.props.disabled || !! this.props.disabledInfo }
					id={ this.props.htmlFor }
					label={ this.renderLabel() }
					aria-label={ this.props.label }
					__nextHasNoMarginBottom
				/>
				{ this.props.toggleExtraContent }
			</>
		);
	}

	renderChildren() {
		return (
			<span className="plugin-action__children">
				{ this.props.children }
				{ this.renderLabel() }
			</span>
		);
	}

	renderInner() {
		if ( 0 < Children.count( this.props.children ) ) {
			return this.renderChildren();
		}

		return this.renderToggle();
	}

	render() {
		const additionalClasses = {
			'is-disabled': this.props.disabled,
			'has-disabled-info': !! this.props.disabledInfo,
		};

		return (
			<div className={ clsx( 'plugin-action', additionalClasses, this.props.className ) }>
				{ this.renderInner() }
			</div>
		);
	}
}

export default PluginAction;