index.js 2.07 KB
Newer Older
Pham Huy committed
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
/**
 * External dependencies
 */
import { __ } from '@wordpress/i18n';
import { clamp, isNaN } from 'lodash';
import { Fragment } from '@wordpress/element';
import PropTypes from 'prop-types';
import { RangeControl, ToggleControl } from '@wordpress/components';
import { MAX_COLUMNS, MIN_COLUMNS, MAX_ROWS, MIN_ROWS } from '@woocommerce/block-settings';

/**
 * A combination of range controls for product grid layout settings.
 */
const GridLayoutControl = ( { columns, rows, setAttributes, alignButtons } ) => {
	return (
		<Fragment>
			<RangeControl
				label={ __( 'Columns', 'woocommerce' ) }
				value={ columns }
				onChange={ ( value ) => {
					const newValue = clamp(
						value,
						MIN_COLUMNS,
						MAX_COLUMNS
					);
					setAttributes( { columns: isNaN( newValue ) ? '' : newValue } );
				} }
				min={ MIN_COLUMNS }
				max={ MAX_COLUMNS }
			/>
			<RangeControl
				label={ __( 'Rows', 'woocommerce' ) }
				value={ rows }
				onChange={ ( value ) => {
					const newValue = clamp(
						value,
						MIN_ROWS,
						MAX_ROWS
					);
					setAttributes( { rows: isNaN( newValue ) ? '' : newValue } );
				} }
				min={ MIN_ROWS }
				max={ MAX_ROWS }
			/>
			<ToggleControl
				label={ __( 'Align Add to Cart buttons', 'woocommerce' ) }
				help={
					alignButtons ?
						__(
							'Buttons are aligned vertically.',
							'woocommerce'
						) :
						__(
							'Buttons follow content.',
							'woocommerce'
						)
				}
				checked={ alignButtons }
				onChange={ () => setAttributes( { alignButtons: ! alignButtons } ) }
			/>
		</Fragment>
	);
};

GridLayoutControl.propTypes = {
	/**
	 * The current columns count.
	 */
	columns: PropTypes.oneOfType( [ PropTypes.number, PropTypes.string ] ).isRequired,
	/**
	 * The current rows count.
	 */
	rows: PropTypes.oneOfType( [ PropTypes.number, PropTypes.string ] ).isRequired,
	/**
	 * Whether or not buttons are aligned horizontally across items.
	 */
	alignButtons: PropTypes.bool.isRequired,
	/**
	 * Callback to update the layout settings.
	 */
	setAttributes: PropTypes.func.isRequired,
};

export default GridLayoutControl;