Skip to content

Commit abef636

Browse files
committed
Expanded example app
1 parent 865015f commit abef636

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src-sample/main/default/lwc/sampleLookupContainer/sampleLookupContainer.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77
checked={isMultiEntry}
88
onchange={handleLookupTypeChange}
99
></lightning-input>
10+
<lightning-input
11+
if:true={isMultiEntry}
12+
type="number"
13+
label="Max selected items"
14+
min="1"
15+
value={maxSelectionSize}
16+
onchange={handleMaxSelectionSizeChange}
17+
required
18+
></lightning-input>
1019

1120
<c-lookup
1221
selection={initialSelection}
@@ -20,6 +29,7 @@
2029
>
2130
</c-lookup>
2231

32+
<lightning-button label="Clear" onclick={handleClear}></lightning-button>&nbsp;
2333
<lightning-button variant="brand" label="Submit" onclick={handleSubmit}></lightning-button>
2434
</div>
2535
</lightning-card>

src-sample/main/default/lwc/sampleLookupContainer/sampleLookupContainer.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export default class SampleLookupContainer extends LightningElement {
99
@api notifyViaAlerts = false;
1010

1111
isMultiEntry = false;
12+
maxSelectionSize = 2;
1213
initialSelection = [
1314
{
1415
id: 'na',
@@ -40,7 +41,11 @@ export default class SampleLookupContainer extends LightningElement {
4041
}
4142

4243
handleSelectionChange() {
43-
this.errors = [];
44+
this.checkForErrors();
45+
}
46+
47+
handleMaxSelectionSizeChange(event) {
48+
this.maxSelectionSize = event.target.value;
4449
}
4550

4651
handleSubmit() {
@@ -50,15 +55,21 @@ export default class SampleLookupContainer extends LightningElement {
5055
}
5156
}
5257

58+
handleClear() {
59+
this.initialSelection = [];
60+
this.errors = [];
61+
}
62+
5363
checkForErrors() {
64+
this.errors = [];
5465
const selection = this.template.querySelector('c-lookup').getSelection();
66+
// Custom validation rule
67+
if (this.isMultiEntry && selection.length > this.maxSelectionSize) {
68+
this.errors.push({ message: `You may only select up to ${this.maxSelectionSize} items.` });
69+
}
70+
// Enforcing required field
5571
if (selection.length === 0) {
56-
this.errors = [
57-
{ message: 'You must make a selection before submitting!' },
58-
{ message: 'Please make a selection and try again.' }
59-
];
60-
} else {
61-
this.errors = [];
72+
this.errors.push({ message: 'Please make a selection.' });
6273
}
6374
}
6475

0 commit comments

Comments
 (0)