forked from forcedotcom/commerce-on-lightning-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommonModal.js
More file actions
145 lines (135 loc) · 3.93 KB
/
commonModal.js
File metadata and controls
145 lines (135 loc) · 3.93 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
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
/*
* 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 } from 'lwc';
import LightningModal from 'lightning/modal';
/**
* Modal for defining and handling primary and secondary actions.
* The modal will close by default on either action click unless the event
* is cancelled by the consumer.
* @fires CommonModal#primaryactionclick
* @fires CommonModal#secondaryactionclick
* @example
* CommonModal.open({
* label: 'Custom Modal', // <-- Required
* size: 'small', // <-- Optional, defaults to 'medium'
* description: 'This is a custom modal showcase', // <-- Optional
* secondaryActionLabel: 'Cancel', // <-- Required
* primaryActionLabel: 'Submit', // <-- Required
* onprimaryactionclick: () => { // <-- Optional, default just closes the modal
* this.handleNavigateToCart();
* },
* onsecondaryactionclick: () => { // <-- Optional, default just closes the modal
* // ...
* },
* });
* @example Manual `close` handling
* CommonModal.open({
* label: 'Custom Modal',
* secondaryActionLabel: 'Cancel',
* primaryActionLabel: 'Submit',
* onprimaryactionclick: async (event) => {
* event.preventDefault(); // <-- Prevents the standard behaviour, i.e. closing the modal
*
* // Execute custom behavior
* await this.handleNavigateToCart();
* event.detail.close('myReason'); // <-- Close the modal manually with a custom result
* },
* });
*/
export default class CommonModal extends LightningModal {
/**
* Sets the modal's title and assistive device label.
* @type {?string}
*/
@api
label;
/**
* An optional message to display to the customer.
* @type {?string}
*/
@api
message;
/**
* The label for the primary action. If no label is provided, the action is omitted.
* @type {?string}
*/
@api
primaryActionLabel;
/**
* The label for the secondary action. If no label is provided, the action is omitted.
* @type {?string}
*/
@api
secondaryActionLabel;
/**
* Whether to show the primary action.
* @type {boolean}
* @readonly
* @private
*/
get hasPrimaryAction() {
return Boolean(this.primaryActionLabel);
}
/**
* Whether to show the secondary action.
* @type {boolean}
* @readonly
* @private
*/
get hasSecondaryAction() {
return Boolean(this.secondaryActionLabel);
}
/**
* Whether the modal has a message/body.
* @type {boolean}
* @readonly
* @private
*/
get hasMessageText() {
return typeof this.message === 'string' && this.message.trim().length > 0;
}
/**
* Handles the click on the primary action.
* @readonly
* @private
*/
handlePrimaryAction() {
this.handleAction('primary');
}
/**
* Handles the click on the secondary action.
* @readonly
* @private
*/
handleSecondaryAction() {
this.handleAction('secondary');
}
/**
* This method dispatches either the primary or secondary action click event,
* and ensures that the modal automatically closes itself, unless the consumer
* calls {@link CustomEvent.prototype.preventDefault}.
* @param {('primary' | 'secondary')} eventType
* The type of the emitted event
* @private
*/
handleAction(eventType) {
const event = new CustomEvent(`${eventType}actionclick`, {
cancelable: true,
detail: {
close: (result) => {
event.preventDefault();
this.close(result);
},
},
});
this.dispatchEvent(event);
if (!event.defaultPrevented) {
this.close(eventType);
}
}
}