Skip to content

Commit 0cd649d

Browse files
committed
feat: focus function
1 parent 748789b commit 0cd649d

File tree

5 files changed

+20
-0
lines changed

5 files changed

+20
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ Sometimes, you may want to pass extra data from the lookup component to Apex. To
277277

278278
| Function | Description |
279279
| ---------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
280+
| `focus()` | Places focus on the component an opens the search dropdown (unless this is a single selection lookup with a selection). |
280281
| `blur()` | Removes focus from the component and closes the search results list. |
281282
| `getSelection()` | Gets the current lookup selection as an array of `LookupSearchResult`. |
282283
| `setDefaultResults(results)` | Allows to set optional default items returned when search has no result (ex: recent items).<br/>`results` is an array of `LookupSearchResult`. |

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
</c-lookup>
4141

4242
<div class="slds-var-m-top_large">
43+
<lightning-button label="Focus" onclick={handleFocus}></lightning-button>&nbsp;
4344
<lightning-button label="Clear" onclick={handleClear}></lightning-button>&nbsp;
4445
<lightning-button variant="brand" label="Submit" onclick={handleSubmit}></lightning-button>
4546
</div>

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ export default class SampleLookupContainer extends LightningElement {
107107
this.errors = [];
108108
}
109109

110+
handleFocus() {
111+
this.template.querySelector('c-lookup').focus();
112+
}
113+
110114
checkForErrors() {
111115
this.errors = [];
112116
const selection = this.template.querySelector('c-lookup').getSelection();

src/main/default/lwc/lookup/__tests__/lookupExposedFunctions.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ describe('c-lookup exposed functions', () => {
6161
expect(listItemEls.length).toBe(SAMPLE_SEARCH_ITEMS.length);
6262
});
6363

64+
it('focuses', async () => {
65+
// Create lookup
66+
const lookupEl = createLookupElement();
67+
lookupEl.focus();
68+
69+
// Verify focus
70+
expect(document.activeElement).toEqual(lookupEl);
71+
});
72+
6473
it('blurs removes focus and closes dropdown', async () => {
6574
jest.useFakeTimers();
6675

src/main/default/lwc/lookup/lookup.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ export default class Lookup extends NavigationMixin(LightningElement) {
144144
}
145145
}
146146

147+
@api
148+
focus() {
149+
this.template.querySelector('input')?.focus();
150+
}
151+
147152
@api
148153
blur() {
149154
this.template.querySelector('input')?.blur();

0 commit comments

Comments
 (0)