Skip to content

Commit

Permalink
add: active filters block
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhtungdu committed Dec 12, 2023
1 parent 1c635cd commit 293140f
Show file tree
Hide file tree
Showing 23 changed files with 1,021 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"reusable": false
},
"usesContext": [
"query"
"query",
"queryId"
],
"providesContext": {
"collectionData": "collectionData"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { AttributeSetting } from '@woocommerce/types';
const ATTRIBUTES = getSetting< AttributeSetting[] >( 'attributes', [] );

const template = [
[ 'woocommerce/collection-active-filters', {} ],
[
'core/heading',
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"name": "woocommerce/collection-active-filters",
"version": "1.0.0",
"title": "Collection Active Filters",
"description": "Display the currently active filters.",
"category": "woocommerce",
"keywords": [
"WooCommerce"
],
"textdomain": "woo-gutenberg-products-block",
"apiVersion": 2,
"ancestor": [
"woocommerce/collection-filters"
],
"supports": {
"interactivity": true
},
"usesContext": [
"queryId"
],
"attributes": {
"displayStyle": {
"type": "string",
"default": "list"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* External dependencies
*/
import { InspectorControls } from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import {
PanelBody,
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis
__experimentalToggleGroupControl as ToggleGroupControl,
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
} from '@wordpress/components';

/**
* Internal dependencies
*/
import type { EditProps, BlockAttributes } from '../types';

export const Inspector = ( { attributes, setAttributes }: EditProps ) => {
const { displayStyle } = attributes;

return (
<InspectorControls>
<PanelBody
title={ __(
'Display Settings',
'woo-gutenberg-products-block'
) }
>
<ToggleGroupControl
label={ __(
'Display Style',
'woo-gutenberg-products-block'
) }
value={ displayStyle }
onChange={ ( value: BlockAttributes[ 'displayStyle' ] ) =>
setAttributes( {
displayStyle: value,
} )
}
className="wc-block-active-filter__style-toggle"
>
<ToggleGroupControlOption
value="list"
label={ __( 'List', 'woo-gutenberg-products-block' ) }
/>
<ToggleGroupControlOption
value="chips"
label={ __( 'Chips', 'woo-gutenberg-products-block' ) }
/>
</ToggleGroupControl>
</PanelBody>
</InspectorControls>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* External dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { Label, RemovableChip } from '@woocommerce/blocks-components';
import { Icon, closeSmall } from '@wordpress/icons';

interface RemovableListItemProps {
type: string;
name: string;
prefix?: string | JSX.Element;
showLabel?: boolean;
isLoading?: boolean;
displayStyle: string;
removeCallback?: () => void;
}

export const RemovableListItem = ( {
type,
name,
prefix = '',
removeCallback = () => null,
showLabel = true,
displayStyle,
}: RemovableListItemProps ) => {
const prefixedName = prefix ? (
<>
{ prefix }
&nbsp;
{ name }
</>
) : (
name
);
const removeText = sprintf(
/* translators: %s attribute value used in the filter. For example: yellow, green, small, large. */
__( 'Remove %s filter', 'woo-gutenberg-products-block' ),
name
);

return (
<li
className="wc-block-active-filters__list-item"
key={ type + ':' + name }
>
{ showLabel && (
<span className="wc-block-active-filters__list-item-type">
{ type + ': ' }
</span>
) }
{ displayStyle === 'chips' ? (
<RemovableChip
element="span"
text={ prefixedName }
onRemove={ removeCallback }
radius="large"
ariaLabel={ removeText }
/>
) : (
<span className="wc-block-active-filters__list-item-name">
<button
className="wc-block-active-filters__list-item-remove"
onClick={ removeCallback }
>
<Icon
className="wc-block-components-chip__remove-icon"
icon={ closeSmall }
size={ 16 }
/>
<Label screenReaderLabel={ removeText } />
</button>
{ prefixedName }
</span>
) }
</li>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* External dependencies
*/
import { useBlockProps } from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';
import classNames from 'classnames';
import { Disabled } from '@wordpress/components';

/**
* Internal dependencies
*/
import { EditProps } from './types';
import { Inspector } from './components/inspector';
import { RemovableListItem } from './components/removable-list-item';

const Edit = ( props: EditProps ) => {
const { displayStyle } = props.attributes;

const blockProps = useBlockProps( {
className: 'wc-block-active-filters',
} );

return (
<div { ...blockProps }>
<Inspector { ...props } />
<Disabled>
<ul
className={ classNames( 'wc-block-active-filters__list', {
'wc-block-active-filters__list--chips':
displayStyle === 'chips',
} ) }
>
<RemovableListItem
type={ __( 'Size', 'woo-gutenberg-products-block' ) }
name={ __( 'Small', 'woo-gutenberg-products-block' ) }
displayStyle={ displayStyle }
/>
<RemovableListItem
type={ __( 'Color', 'woo-gutenberg-products-block' ) }
name={ __( 'Blue', 'woo-gutenberg-products-block' ) }
displayStyle={ displayStyle }
/>
</ul>
</Disabled>
</div>
);
};

export default Edit;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* External dependencies
*/
import { store, navigate, getContext } from '@woocommerce/interactivity';

type ActiveFiltersContext = {
queryId: number;
params: string[];
};

store( 'woocommerce/collection-active-filters', {
actions: {
clearAll: () => {
const { params } = getContext< ActiveFiltersContext >();
const url = new URL( window.location.href );
const { searchParams } = url;

params.forEach( ( param ) => searchParams.delete( param ) );
navigate( url.href );
},
},
} );
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* External dependencies
*/
import { registerBlockType } from '@wordpress/blocks';
import { Icon } from '@wordpress/icons';
import { toggle } from '@woocommerce/icons';

/**
* Internal dependencies
*/
import metadata from './block.json';
import Edit from './edit';
import './style.scss';

registerBlockType( metadata, {
icon: {
src: (
<Icon
icon={ toggle }
className="wc-block-editor-components-block-icon"
/>
),
},
edit: Edit,
} );

0 comments on commit 293140f

Please sign in to comment.