2019-01-31 22:55:54 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2019-06-27 11:13:02 +00:00
|
|
|
import { RangeControl, ToggleControl } from '@wordpress/components';
|
2019-01-31 22:55:54 +00:00
|
|
|
|
2023-06-14 14:07:28 +00:00
|
|
|
interface ClampProps {
|
|
|
|
( number: number, boundOne: number, boundTwo?: number ): number;
|
|
|
|
}
|
|
|
|
|
|
|
|
const clamp: ClampProps = ( number, boundOne, boundTwo ) => {
|
2023-04-28 10:29:45 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2023-06-14 14:07:28 +00:00
|
|
|
interface GridLayoutControlProps {
|
|
|
|
columns: number;
|
|
|
|
rows: number;
|
|
|
|
setAttributes: ( attributes: Record< string, unknown > ) => void;
|
|
|
|
alignButtons: boolean;
|
|
|
|
minColumns?: number;
|
|
|
|
maxColumns?: number;
|
|
|
|
minRows?: number;
|
|
|
|
maxRows?: 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,
|
2023-06-14 14:07:28 +00:00
|
|
|
}: GridLayoutControlProps ) => {
|
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
|
2023-12-12 22:12:36 +00:00
|
|
|
label={ __( 'Columns', 'woocommerce' ) }
|
2019-01-31 22:55:54 +00:00
|
|
|
value={ columns }
|
2023-06-14 14:07:28 +00:00
|
|
|
onChange={ ( value: number ) => {
|
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
|
2023-12-12 22:12:36 +00:00
|
|
|
label={ __( 'Rows', 'woocommerce' ) }
|
2019-01-31 22:55:54 +00:00
|
|
|
value={ rows }
|
2023-06-14 14:07:28 +00:00
|
|
|
onChange={ ( value: number ) => {
|
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',
|
2023-12-12 22:12:36 +00:00
|
|
|
'woocommerce'
|
2020-01-29 10:28:40 +00:00
|
|
|
) }
|
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.',
|
2023-12-12 22:12:36 +00:00
|
|
|
'woocommerce'
|
2019-09-05 15:09:31 +00:00
|
|
|
)
|
2019-09-09 10:52:48 +00:00
|
|
|
: __(
|
2020-01-29 10:28:40 +00:00
|
|
|
'The last inner block will follow other content.',
|
2023-12-12 22:12:36 +00:00
|
|
|
'woocommerce'
|
2019-09-09 10:52:48 +00:00
|
|
|
)
|
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
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default GridLayoutControl;
|