2019-01-31 22:55:54 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import PropTypes from 'prop-types';
|
2019-06-27 11:13:02 +00:00
|
|
|
import { RangeControl, ToggleControl } from '@wordpress/components';
|
2019-01-31 22:55:54 +00:00
|
|
|
|
2023-04-28 10:29:45 +00:00
|
|
|
const clamp = ( number, boundOne, boundTwo ) => {
|
|
|
|
if ( ! boundTwo ) {
|
|
|
|
return Math.max( number, boundOne ) === boundOne ? number : boundOne;
|
|
|
|
} else if ( Math.min( number, boundOne ) === number ) {
|
|
|
|
return boundOne;
|
|
|
|
} else if ( Math.max( number, boundTwo ) === number ) {
|
|
|
|
return boundTwo;
|
|
|
|
}
|
|
|
|
return number;
|
|
|
|
};
|
|
|
|
|
2019-01-31 22:55:54 +00:00
|
|
|
/**
|
|
|
|
* A combination of range controls for product grid layout settings.
|
2020-09-20 23:54:08 +00:00
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {Object} props Incoming props for the component.
|
|
|
|
* @param {number} props.columns
|
|
|
|
* @param {number} props.rows
|
2020-09-20 23:54:08 +00:00
|
|
|
* @param {function(any):any} props.setAttributes Setter for block attributes.
|
2022-07-19 13:09:46 +00:00
|
|
|
* @param {boolean} props.alignButtons
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {number} props.minColumns
|
|
|
|
* @param {number} props.maxColumns
|
|
|
|
* @param {number} props.minRows
|
|
|
|
* @param {number} props.maxRows
|
2019-01-31 22:55:54 +00:00
|
|
|
*/
|
2019-09-05 15:09:31 +00:00
|
|
|
const GridLayoutControl = ( {
|
|
|
|
columns,
|
|
|
|
rows,
|
|
|
|
setAttributes,
|
|
|
|
alignButtons,
|
2021-04-22 11:37:27 +00:00
|
|
|
minColumns = 1,
|
|
|
|
maxColumns = 6,
|
|
|
|
minRows = 1,
|
|
|
|
maxRows = 6,
|
2019-09-05 15:09:31 +00:00
|
|
|
} ) => {
|
2019-01-31 22:55:54 +00:00
|
|
|
return (
|
2020-12-14 11:54:34 +00:00
|
|
|
<>
|
2019-01-31 22:55:54 +00:00
|
|
|
<RangeControl
|
|
|
|
label={ __( 'Columns', 'woo-gutenberg-products-block' ) }
|
|
|
|
value={ columns }
|
|
|
|
onChange={ ( value ) => {
|
2021-04-22 11:37:27 +00:00
|
|
|
const newValue = clamp( value, minColumns, maxColumns );
|
2019-09-05 15:09:31 +00:00
|
|
|
setAttributes( {
|
2021-01-05 13:58:18 +00:00
|
|
|
columns: Number.isNaN( newValue ) ? '' : newValue,
|
2019-09-05 15:09:31 +00:00
|
|
|
} );
|
2019-01-31 22:55:54 +00:00
|
|
|
} }
|
2021-04-22 11:37:27 +00:00
|
|
|
min={ minColumns }
|
|
|
|
max={ maxColumns }
|
2019-01-31 22:55:54 +00:00
|
|
|
/>
|
|
|
|
<RangeControl
|
|
|
|
label={ __( 'Rows', 'woo-gutenberg-products-block' ) }
|
|
|
|
value={ rows }
|
|
|
|
onChange={ ( value ) => {
|
2021-04-22 11:37:27 +00:00
|
|
|
const newValue = clamp( value, minRows, maxRows );
|
2019-09-05 15:09:31 +00:00
|
|
|
setAttributes( {
|
2021-01-05 13:58:18 +00:00
|
|
|
rows: Number.isNaN( newValue ) ? '' : newValue,
|
2019-09-05 15:09:31 +00:00
|
|
|
} );
|
2019-01-31 22:55:54 +00:00
|
|
|
} }
|
2021-04-22 11:37:27 +00:00
|
|
|
min={ minRows }
|
|
|
|
max={ maxRows }
|
2019-01-31 22:55:54 +00:00
|
|
|
/>
|
2019-06-27 11:13:02 +00:00
|
|
|
<ToggleControl
|
2020-01-29 10:28:40 +00:00
|
|
|
label={ __(
|
2022-09-12 08:39:26 +00:00
|
|
|
'Align the last block to the bottom',
|
2020-01-29 10:28:40 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2019-06-27 11:13:02 +00:00
|
|
|
help={
|
2019-09-05 15:09:31 +00:00
|
|
|
alignButtons
|
|
|
|
? __(
|
2022-09-12 08:39:26 +00:00
|
|
|
'Align the last block to the bottom.',
|
2019-09-05 15:09:31 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
)
|
2019-09-09 10:52:48 +00:00
|
|
|
: __(
|
2020-01-29 10:28:40 +00:00
|
|
|
'The last inner block will follow other content.',
|
2019-09-09 10:52:48 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
)
|
2019-06-27 11:13:02 +00:00
|
|
|
}
|
|
|
|
checked={ alignButtons }
|
2019-09-09 10:52:48 +00:00
|
|
|
onChange={ () =>
|
|
|
|
setAttributes( { alignButtons: ! alignButtons } )
|
|
|
|
}
|
2019-06-27 11:13:02 +00:00
|
|
|
/>
|
2020-12-14 11:54:34 +00:00
|
|
|
</>
|
2019-01-31 22:55:54 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
GridLayoutControl.propTypes = {
|
2021-04-22 11:37:27 +00:00
|
|
|
// The current columns count.
|
2019-09-05 15:09:31 +00:00
|
|
|
columns: PropTypes.oneOfType( [ PropTypes.number, PropTypes.string ] )
|
|
|
|
.isRequired,
|
2021-04-22 11:37:27 +00:00
|
|
|
// The current rows count.
|
2019-09-05 15:09:31 +00:00
|
|
|
rows: PropTypes.oneOfType( [ PropTypes.number, PropTypes.string ] )
|
|
|
|
.isRequired,
|
2021-04-22 11:37:27 +00:00
|
|
|
// Whether or not buttons are aligned horizontally across items.
|
2019-06-27 11:13:02 +00:00
|
|
|
alignButtons: PropTypes.bool.isRequired,
|
2021-04-22 11:37:27 +00:00
|
|
|
// Callback to update the layout settings.
|
2019-01-31 22:55:54 +00:00
|
|
|
setAttributes: PropTypes.func.isRequired,
|
2021-04-22 11:37:27 +00:00
|
|
|
// Min and max constraints.
|
|
|
|
minColumns: PropTypes.number,
|
|
|
|
maxColumns: PropTypes.number,
|
|
|
|
minRows: PropTypes.number,
|
|
|
|
maxRows: PropTypes.number,
|
2019-01-31 22:55:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default GridLayoutControl;
|