Skip to content

Commit

Permalink
feat: add a method to programatically focus and open the dropdown ele…
Browse files Browse the repository at this point in the history
…ment (#867)

* feat: add a method to programatically focus and open the dropdown element

* test: adds missing test for focuselement method

* fix: fix typo in method description

* fix: add missing folder to CODE_STYLE url in CONTRIBUTING doc
  • Loading branch information
NOMADE55 authored Dec 5, 2024
1 parent 388af60 commit bef2cc5
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
- **Code Review Process**: We will review your PR and provide feedback. Once the PR is ready for merge, it will be merged by the maintainers.

## Style Guides
- **Code Style**: Please read our [Code Style](/~https://github.com/scania-digital-design-system/tegel/blob/main/CODE_STYLE.md) before contributing.
- **Code Style**: Please read our [Code Style](/~https://github.com/scania-digital-design-system/tegel/blob/main/.github/CODE_STYLE.md) before contributing.

## Testing your changes locally
To properly test your changes' behaviors in a project, reference your local Tegel instance instead of the published version on the registry
Expand Down
23 changes: 14 additions & 9 deletions packages/core/src/components/dropdown/dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ export class TdsDropdown {
this.handleChange();
}

/** Method that forces focus on the input element. */
@Method()
async focusElement() {
this.focusInputElement();
this.handleFocus({});
}

/** Method for setting the value of the Dropdown.
*
* Single selection example:
Expand Down Expand Up @@ -472,7 +479,13 @@ export class TdsDropdown {
};

private handleFocus = (event) => {
this.open = true;
this.filterFocus = true;
if (this.multiselect) {
this.inputElement.value = '';
}
this.tdsFocus.emit(event);
this.handleFilter({ target: { value: '' } });
};

private handleBlur = (event) => {
Expand Down Expand Up @@ -542,15 +555,7 @@ export class TdsDropdown {
}
this.handleBlur(event);
}}
onFocus={(event) => {
this.open = true;
this.filterFocus = true;
if (this.multiselect) {
this.inputElement.value = '';
}
this.handleFocus(event);
this.handleFilter({ target: { value: '' } });
}}
onFocus={(event) => this.handleFocus(event)}
onKeyDown={(event) => {
if (event.key === 'Escape') {
this.open = false;
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/components/dropdown/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ Type: `Promise<void>`



### `focusElement() => Promise<void>`

Method that forces focus on the input element.

#### Returns

Type: `Promise<void>`



### `removeValue(oldValue: string) => Promise<{ value: string; label: string; }[]>`

Method for removing a selected value in the Dropdown.
Expand Down
20 changes: 20 additions & 0 deletions packages/core/src/components/dropdown/test/default/dropdown.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,24 @@ test.describe.parallel('tds-dropdown-default', () => {
await expect(dropdownButtonElement).toBeVisible();
await expect(dropdownListElementTwoButton).toBeVisible();
});
test('focusElement() method focus and opens the dropdown-list', async ({ page }) => {
await page.goto(componentTestPath);
const dropdownButton = page.getByRole('button').first();
const dropdownListElementOne = page
.locator('tds-dropdown-option')
.filter({ hasText: 'Option 1' });
await expect(dropdownListElementOne).toBeHidden();

await page.evaluate(() => {
const dropdownnew = document.querySelector('tds-dropdown');
dropdownnew.focusElement();
});

/* before clicking dropdownlist should not be visible, the button should be */
await expect(dropdownButton).toBeVisible();
await expect(dropdownListElementOne).toBeVisible();

/* checks diff on screenshot */
await expect(page).toHaveScreenshot({ maxDiffPixels: 0 });
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit bef2cc5

Please sign in to comment.