-
Notifications
You must be signed in to change notification settings - Fork 10.8k
/
attribute-filter.js
295 lines (278 loc) · 7.25 KB
/
attribute-filter.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
* External dependencies
*/
import PropTypes from 'prop-types';
import { SelectControl as Select, Spinner } from '@wordpress/components';
import classnames from 'classnames';
import {
createElement,
Fragment,
useEffect,
useState,
} from '@wordpress/element';
import apiFetch from '@wordpress/api-fetch';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import Search from '../search';
import SelectControl from '../select-control';
import {
backwardsCompatibleCreateInterpolateElement as createInterpolateElement,
textContent,
} from './utils';
const getScreenReaderText = ( {
attributeTerms,
config,
filter,
selectedAttribute,
selectedAttributeTerm,
} ) => {
if (
! attributeTerms ||
attributeTerms.length === 0 ||
! selectedAttribute ||
selectedAttribute.length === 0 ||
selectedAttributeTerm === ''
) {
return '';
}
const rule = Array.isArray( config.rules )
? config.rules.find(
( configRule ) => configRule.value === filter.rule
) || {}
: {};
const attributeName = selectedAttribute[ 0 ].label;
const termObject = attributeTerms.find(
( { key } ) => key === selectedAttributeTerm
);
const attributeTerm = termObject && termObject.label;
if ( ! attributeName || ! attributeTerm ) {
return '';
}
const filterStr = createInterpolateElement(
/* eslint-disable-next-line max-len */
/* translators: Sentence fragment describing a product attribute match. Example: "Color Is Not Blue" - attribute = Color, equals = Is Not, value = Blue */
__( '<attribute/> <equals/> <value/>', 'woocommerce' ),
{
attribute: <Fragment>{ attributeName }</Fragment>,
equals: <Fragment>{ rule.label }</Fragment>,
value: <Fragment>{ attributeTerm }</Fragment>,
}
);
return textContent(
createInterpolateElement( config.labels.title, {
filter: <Fragment>{ filterStr }</Fragment>,
rule: <Fragment />,
title: <Fragment />,
} )
);
};
const AttributeFilter = ( props ) => {
const { className, config, filter, isEnglish, onFilterChange } = props;
const { rule, value } = filter;
const { labels, rules } = config;
const [ selectedAttribute, setSelectedAttribute ] = useState( [] );
// Set selected attribute from filter value (in query string).
useEffect( () => {
if (
! selectedAttribute.length &&
Array.isArray( value ) &&
value[ 0 ]
) {
apiFetch( {
path: `/wc-analytics/products/attributes/${ value[ 0 ] }`,
} )
.then( ( { id, name } ) => [
{
key: id.toString(),
label: name,
},
] )
.then( setSelectedAttribute );
}
}, [ value, selectedAttribute ] );
const [ attributeTerms, setAttributeTerms ] = useState( [] );
// Fetch all product attributes on mount.
useEffect( () => {
if ( ! selectedAttribute.length ) {
return;
}
setAttributeTerms( false );
apiFetch( {
path: `/wc-analytics/products/attributes/${ selectedAttribute[ 0 ].key }/terms?per_page=100`,
} )
.then( ( terms ) =>
terms.map( ( { id, name } ) => ( {
key: id.toString(),
label: name,
} ) )
)
.then( setAttributeTerms );
}, [ selectedAttribute ] );
const [ selectedAttributeTerm, setSelectedAttributeTerm ] = useState(
Array.isArray( value ) ? value[ 1 ] || '' : ''
);
const screenReaderText = getScreenReaderText( {
attributeTerms,
config,
filter,
selectedAttribute,
selectedAttributeTerm,
} );
/*eslint-disable jsx-a11y/no-noninteractive-tabindex*/
return (
<fieldset
className="woocommerce-filters-advanced__line-item"
tabIndex="0"
>
<legend className="screen-reader-text">{ labels.add || '' }</legend>
<div
className={ classnames(
'woocommerce-filters-advanced__fieldset',
{
'is-english': isEnglish,
}
) }
>
{ createInterpolateElement( labels.title, {
title: <span className={ className } />,
rule: (
<Select
className={ classnames(
className,
'woocommerce-filters-advanced__rule'
) }
options={ rules }
value={ rule }
onChange={ ( selectedValue ) =>
onFilterChange( {
property: 'rule',
value: selectedValue,
} )
}
aria-label={ labels.rule }
/>
),
filter: (
<div
className={ classnames(
className,
'woocommerce-filters-advanced__attribute-fieldset'
) }
>
{ ! Array.isArray( value ) ||
! value.length ||
selectedAttribute.length ? (
<Search
className="woocommerce-filters-advanced__input woocommerce-search"
onChange={ ( [ attr ] ) => {
setSelectedAttribute(
attr ? [ attr ] : []
);
setSelectedAttributeTerm( '' );
onFilterChange( {
property: 'value',
value: [ attr && attr.key ].filter(
Boolean
),
} );
} }
type="attributes"
placeholder={ __(
'Attribute name',
'woocommerce'
) }
multiple={ false }
selected={ selectedAttribute }
inlineTags
aria-label={ __(
'Attribute name',
'woocommerce'
) }
/>
) : (
<Spinner />
) }
{ selectedAttribute.length > 0 &&
( attributeTerms.length ? (
<Fragment>
<span className="woocommerce-filters-advanced__attribute-field-separator">
=
</span>
<SelectControl
className="woocommerce-filters-advanced__input woocommerce-search"
placeholder={ __(
'Attribute value',
'woocommerce'
) }
inlineTags
isSearchable
multiple={ false }
showAllOnFocus
options={ attributeTerms }
selected={ selectedAttributeTerm }
onChange={ ( term ) => {
// Clearing the input using delete/backspace causes an empty array to be passed here.
if (
typeof term !== 'string'
) {
term = '';
}
setSelectedAttributeTerm(
term
);
onFilterChange( {
property: 'value',
value: [
selectedAttribute[ 0 ]
.key,
term,
].filter( Boolean ),
} );
} }
/>
</Fragment>
) : (
<Spinner />
) ) }
</div>
),
} ) }
</div>
{ screenReaderText && (
<span className="screen-reader-text">{ screenReaderText }</span>
) }
</fieldset>
);
/*eslint-enable jsx-a11y/no-noninteractive-tabindex*/
};
AttributeFilter.propTypes = {
/**
* The configuration object for the single filter to be rendered.
*/
config: PropTypes.shape( {
labels: PropTypes.shape( {
rule: PropTypes.string,
title: PropTypes.string,
filter: PropTypes.string,
} ),
rules: PropTypes.arrayOf( PropTypes.object ),
input: PropTypes.object,
} ).isRequired,
/**
* The activeFilter handed down by AdvancedFilters.
*/
filter: PropTypes.shape( {
key: PropTypes.string,
rule: PropTypes.string,
value: PropTypes.arrayOf(
PropTypes.oneOfType( [ PropTypes.string, PropTypes.number ] )
),
} ).isRequired,
/**
* Function to be called on update.
*/
onFilterChange: PropTypes.func.isRequired,
};
export default AttributeFilter;