forked from forcedotcom/commerce-on-lightning-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearchInputFacet.js
More file actions
110 lines (100 loc) · 2.83 KB
/
searchInputFacet.js
File metadata and controls
110 lines (100 loc) · 2.83 KB
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
/*
* Copyright (c) 2023, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* For full license text, see the LICENSE file in the repo
* root or https://opensource.org/licenses/apache-2-0/
*/
import { api, LightningElement } from 'lwc';
import { NUM_FACETVALUES_ALWAYS_DISPLAYED, FACETVALUE_SHOW_MORE_LIMIT } from './constants';
import generateLabel from './inputFacetLabelGenerator';
/**
* @typedef {import('../searchResults/searchResults').DistinctFacetValue} DistinctFacetValue
*/
export default class SearchInputFacet extends LightningElement {
static renderMode = 'light';
/**
* The values of the facet
* @type {?DistinctFacetValue[]}
*/
@api
values;
/**
* Gets the defaulted / normalized sequence of facet values to display.
* @type {DistinctFacetValue[]}
*/
get normalizedValues() {
return this.values || [];
}
/**
* The type of facet being displayed
* @type {?string}
*/
@api
type;
/**
* The facet name for the values being displayed
* @type {?string}
*/
@api
facetName;
/**
* Determines whether we show all the facet's values or not
* @type {boolean}
* @private
*/
_expanded = false;
/**
* Gets the defaulted / normalized sequence of facet values to display.
* Only show the first 6 values if
* @type {DistinctFacetValue[]}
* @readonly
* @private
*/
get displayedValues() {
let facetValues = Array.from(this.normalizedValues);
if (this.displayShowMore && !this._expanded) {
facetValues = facetValues.slice(0, NUM_FACETVALUES_ALWAYS_DISPLAYED);
} else if (this.displayShowMore && this._expanded) {
facetValues = facetValues.map((facetValue, index) => ({
...facetValue,
focusOnInit: index === NUM_FACETVALUES_ALWAYS_DISPLAYED,
}));
}
return facetValues;
}
/**
* Gets whether we should display a "Show More" button or not
* @type {boolean}
* @readonly
* @private
*/
get displayShowMore() {
return this.normalizedValues.length > FACETVALUE_SHOW_MORE_LIMIT;
}
/**
* Gets the label for the 'Show More' or 'Show Less' button
* @type {string}
* @readonly
* @private
*/
get showMoreOrLessLabel() {
return generateLabel(this._expanded);
}
/**
* Gets the aria label for the 'Show More' or 'Show Less' button
* @type {string}
* @readonly
* @private
*/
get facetAriaLabel() {
return generateLabel(this._expanded, this.facetName);
}
/**
* Handle the 'click' event fired from the 'Show More' or 'Show Less' button
* @private
*/
handleShowMoreOrLess() {
this._expanded = !this._expanded;
}
}