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 220
/
block.js
167 lines (157 loc) · 4.1 KB
/
block.js
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { useQueryStateByKey } from '@woocommerce/base-context/hooks';
import { useMemo } from '@wordpress/element';
import classnames from 'classnames';
import PropTypes from 'prop-types';
import { Label } from '@woocommerce/blocks-checkout';
/**
* Internal dependencies
*/
import './style.scss';
import { getAttributeFromTaxonomy } from '../../utils/attributes';
import { formatPriceRange, renderRemovableListItem } from './utils';
import ActiveAttributeFilters from './active-attribute-filters';
/**
* Component displaying active filters.
*
* @param {Object} props Incoming props for the component.
* @param {Object} props.attributes Incoming attributes for the block.
* @param {boolean} props.isEditor Whether or not in the editor context.
*/
const ActiveFiltersBlock = ( {
attributes: blockAttributes,
isEditor = false,
} ) => {
const [ productAttributes, setProductAttributes ] = useQueryStateByKey(
'attributes',
[]
);
const [ minPrice, setMinPrice ] = useQueryStateByKey( 'min_price' );
const [ maxPrice, setMaxPrice ] = useQueryStateByKey( 'max_price' );
const activePriceFilters = useMemo( () => {
if ( ! Number.isFinite( minPrice ) && ! Number.isFinite( maxPrice ) ) {
return null;
}
return renderRemovableListItem( {
type: __( 'Price', 'woo-gutenberg-products-block' ),
name: formatPriceRange( minPrice, maxPrice ),
removeCallback: () => {
setMinPrice( undefined );
setMaxPrice( undefined );
},
displayStyle: blockAttributes.displayStyle,
} );
}, [
minPrice,
maxPrice,
blockAttributes.displayStyle,
setMinPrice,
setMaxPrice,
] );
const activeAttributeFilters = useMemo( () => {
return productAttributes.map( ( attribute ) => {
const attributeObject = getAttributeFromTaxonomy(
attribute.attribute
);
return (
<ActiveAttributeFilters
attributeObject={ attributeObject }
displayStyle={ blockAttributes.displayStyle }
slugs={ attribute.slug }
key={ attribute.attribute }
operator={ attribute.operator }
/>
);
} );
}, [ productAttributes, blockAttributes.displayStyle ] );
const hasFilters = () => {
return (
productAttributes.length > 0 ||
Number.isFinite( minPrice ) ||
Number.isFinite( maxPrice )
);
};
if ( ! hasFilters() && ! isEditor ) {
return null;
}
const TagName = `h${ blockAttributes.headingLevel }`;
const listClasses = classnames( 'wc-block-active-filters__list', {
'wc-block-active-filters__list--chips':
blockAttributes.displayStyle === 'chips',
} );
return (
<>
{ ! isEditor && blockAttributes.heading && (
<TagName>{ blockAttributes.heading }</TagName>
) }
<div className="wc-block-active-filters">
<ul className={ listClasses }>
{ isEditor ? (
<>
{ renderRemovableListItem( {
type: __(
'Size',
'woo-gutenberg-products-block'
),
name: __(
'Small',
'woo-gutenberg-products-block'
),
displayStyle: blockAttributes.displayStyle,
} ) }
{ renderRemovableListItem( {
type: __(
'Color',
'woo-gutenberg-products-block'
),
name: __(
'Blue',
'woo-gutenberg-products-block'
),
displayStyle: blockAttributes.displayStyle,
} ) }
</>
) : (
<>
{ activePriceFilters }
{ activeAttributeFilters }
</>
) }
</ul>
<button
className="wc-block-active-filters__clear-all"
onClick={ () => {
setMinPrice( undefined );
setMaxPrice( undefined );
setProductAttributes( [] );
} }
>
<Label
label={ __(
'Clear All',
'woo-gutenberg-products-block'
) }
screenReaderLabel={ __(
'Clear All Filters',
'woo-gutenberg-products-block'
) }
/>
</button>
</div>
</>
);
};
ActiveFiltersBlock.propTypes = {
/**
* The attributes for this block.
*/
attributes: PropTypes.object.isRequired,
/**
* Whether it's in the editor or frontend display.
*/
isEditor: PropTypes.bool,
};
export default ActiveFiltersBlock;