This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Product Collection: Add columns control to product collection block e…
…ditor settings (#9466) * Add columns control to product collection block editor settings - `InspectorControls` from './inspector-controls' is now imported in `edit.tsx` and used in the returned JSX of `Edit` function. - A new file `columns-control.tsx` is added under 'product-collection' block's 'inspector-controls' directory which exports a `ColumnsControl` component. This component uses `RangeControl` from '@wordpress/components' to control the number of columns in the product collection display layout when the layout type is 'flex'. - The types file (`types.ts`) for 'product-collection' block is updated. The `Attributes` interface is renamed to `ProductCollectionAttributes` and the `ProductCollectionContext` interface is removed. The `ProductCollectionAttributes` now includes 'queryContext', 'templateSlug', and 'displayLayout' properties. * Refactor: Simplify Fallback Return in ColumnsControl Component This commit simplifies the fallback return value of the ColumnsControl component. Instead of returning an empty fragment (<> </>), it now returns null when the condition isn't met. This change improves readability and aligns with best practices for conditional rendering in React.
- Loading branch information
1 parent
229a43d
commit a1f836d
Showing
6 changed files
with
86 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
assets/js/blocks/product-collection/inspector-controls/columns-control.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { RangeControl } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { BlockEditProps } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { ProductCollectionAttributes } from '../types'; | ||
|
||
const ColumnsControl = ( | ||
props: BlockEditProps< ProductCollectionAttributes > | ||
) => { | ||
const { type, columns } = props.attributes.displayLayout; | ||
const showColumnsControl = type === 'flex'; | ||
|
||
return showColumnsControl ? ( | ||
<RangeControl | ||
label={ __( 'Columns', 'woo-gutenberg-products-block' ) } | ||
value={ columns } | ||
onChange={ ( value: number ) => | ||
props.setAttributes( { | ||
displayLayout: { | ||
...props.attributes.displayLayout, | ||
columns: value, | ||
}, | ||
} ) | ||
} | ||
min={ 2 } | ||
max={ Math.max( 6, columns ) } | ||
/> | ||
) : null; | ||
}; | ||
|
||
export default ColumnsControl; |
29 changes: 29 additions & 0 deletions
29
assets/js/blocks/product-collection/inspector-controls/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import type { BlockEditProps } from '@wordpress/blocks'; | ||
import { InspectorControls } from '@wordpress/block-editor'; | ||
import { PanelBody } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { ProductCollectionAttributes } from '../types'; | ||
import ColumnsControl from './columns-control'; | ||
|
||
const ProductCollectionInspectorControls = ( | ||
props: BlockEditProps< ProductCollectionAttributes > | ||
) => { | ||
return ( | ||
<InspectorControls> | ||
<PanelBody | ||
title={ __( 'Settings', 'woo-gutenberg-products-block' ) } | ||
> | ||
<ColumnsControl { ...props } /> | ||
</PanelBody> | ||
</InspectorControls> | ||
); | ||
}; | ||
|
||
export default ProductCollectionInspectorControls; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters