forked from forcedotcom/commerce-on-lightning-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearchFacetItem.js
More file actions
119 lines (110 loc) · 3.36 KB
/
searchFacetItem.js
File metadata and controls
119 lines (110 loc) · 3.36 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
111
112
113
114
115
116
117
118
119
/*
* 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 { LightningElement, api } from 'lwc';
import { FacetUiTypeEnum, FACETVALUE_TOGGLE_EVT } from './constants';
/**
* @typedef {import('../searchResults/searchResults').DistinctFacetValue} DistinctFacetValue
*/
/**
* An event fired when the facet value has been toggled.
* @event SearchFacetItem#facetvaluetoggle
* @property {object} detail CustomEvent details
* @property {string} detail.id
* The ID of the facet value
* @property {boolean} detail.checked
* Whether the facet value has been checked or unchecked
*/
/**
* Wrapper component for facet values
* @fires SearchFacetItem#facetvaluetoggle
*/
export default class SearchFacetItem extends LightningElement {
static renderMode = 'light';
/**
* Gets or sets the facet value
* @type {?DistinctFacetValue}
*/
@api
value;
/**
* The type of facet being displayed ("radio" or "checkbox")
* @type {string}
*/
@api
type = FacetUiTypeEnum.CHECKBOX;
/**
* Indicates whether we should focus on the facet value when it's first rendered
* @type {boolean}
*/
@api
focusOnInit = false;
/**
* Whether the facet item has been rendered at least once.
* @type {boolean}
* @private
*/
hasRenderedAtLeastOnce = false;
/**
* Whether the component has acquired focus when it was initially displayed.
* @type {boolean}
* @private
*/
hasInitiallyFocused = false;
/**
* Focuses on the facet value on inital rendering if flag is true
* @private
*/
renderedCallback() {
const lightningInputElement = this.querySelector('lightning-input');
if (!this.hasRenderedAtLeastOnce && !this.hasInitiallyFocused && this.focusOnInit) {
lightningInputElement?.focus();
this.hasInitiallyFocused = true;
}
if (this.type === FacetUiTypeEnum.CHECKBOX) {
lightningInputElement.setAttribute('disabled', this.value?.productCount === 0 ? 'true' : 'false');
}
this.hasRenderedAtLeastOnce = true;
}
disconnectedCallback() {
this.hasInitiallyFocused = false;
this.hasRenderedAtLeastOnce = false;
}
/**
* Handler for the 'keyup' event fired from facetItem
* @param {CustomEvent} event The event object
*/
handleKeyPress(event) {
if (event.code === 'Space') {
this.handleFacetValueToggle(event);
}
}
/**
* Handler for the 'click' event fired from inputFacet
* @param {CustomEvent} event The event object
* @fires SearchFacetItem#facetvaluetoggle
*/
handleFacetValueToggle(event) {
const element = event.target;
if (!element.disabled) {
event.preventDefault();
element.checked = !element.checked;
element.focus();
}
this.dispatchEvent(
new CustomEvent(FACETVALUE_TOGGLE_EVT, {
bubbles: true,
composed: true,
cancelable: true,
detail: {
id: element.dataset.id,
checked: element.checked,
},
})
);
}
}