File size: 2,052 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
import { Count, Gridicon, FormLabel } from '@automattic/components';
import clsx from 'clsx';
import { localize } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { Component } from 'react';
import FormInputCheckbox from 'calypso/components/forms/form-checkbox';

import './style.scss';

export class BulkSelect extends Component {
	static displayName = 'BulkSelect';

	static propTypes = {
		id: PropTypes.string,
		totalElements: PropTypes.number.isRequired,
		selectedElements: PropTypes.number.isRequired,
		onToggle: PropTypes.func.isRequired,
	};

	getStateIcon = () => {
		if ( this.hasSomeElementsSelected() ) {
			return <Gridicon className="bulk-select__some-checked-icon" icon="minus-small" size={ 18 } />;
		}
	};

	hasAllElementsSelected = () => {
		return (
			this.props.isChecked ??
			( this.props.selectedElements && this.props.selectedElements === this.props.totalElements )
		);
	};

	hasSomeElementsSelected = () => {
		return (
			this.props.isHalfChecked ??
			( this.props.selectedElements && this.props.selectedElements < this.props.totalElements )
		);
	};

	handleToggleAll = () => {
		const newCheckedState = ! ( this.hasSomeElementsSelected() || this.hasAllElementsSelected() );
		this.props.onToggle( newCheckedState );
	};

	render() {
		const { translate, ariaLabel = translate( 'Select All' ), disabled = false } = this.props;
		const isChecked = this.hasAllElementsSelected();
		const inputClasses = clsx( 'bulk-select__box', {
			// TODO: We might be able to remove this class in favor of the :checked pseudoselector.
			'is-checked': isChecked,
		} );

		return (
			<span className="bulk-select">
				<FormLabel className="bulk-select__container">
					<FormInputCheckbox
						className={ inputClasses }
						checked={ isChecked }
						onChange={ this.handleToggleAll }
						aria-label={ ariaLabel }
						disabled={ disabled }
					/>
					<Count count={ this.props.selectedElements } />
					{ this.getStateIcon() }
				</FormLabel>
			</span>
		);
	}
}

export default localize( BulkSelect );