-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract input-container logic into reusable mixin (#6574)
- Loading branch information
1 parent
bf9b0b3
commit 02bc61a
Showing
2 changed files
with
65 additions
and
52 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
packages/input-container/src/vaadin-input-container-mixin.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* @license | ||
* Copyright (c) 2021 - 2023 Vaadin Ltd. | ||
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/ | ||
*/ | ||
|
||
/** | ||
* @polymerMixin | ||
*/ | ||
export const InputContainerMixin = (superClass) => | ||
class InputContainerMixinClass extends superClass { | ||
static get properties() { | ||
return { | ||
/** | ||
* If true, the user cannot interact with this element. | ||
*/ | ||
disabled: { | ||
type: Boolean, | ||
reflectToAttribute: true, | ||
}, | ||
|
||
/** | ||
* Set to true to make this element read-only. | ||
*/ | ||
readonly: { | ||
type: Boolean, | ||
reflectToAttribute: true, | ||
}, | ||
|
||
/** | ||
* Set to true when the element is invalid. | ||
*/ | ||
invalid: { | ||
type: Boolean, | ||
reflectToAttribute: true, | ||
}, | ||
}; | ||
} | ||
|
||
/** @protected */ | ||
ready() { | ||
super.ready(); | ||
|
||
this.addEventListener('pointerdown', (event) => { | ||
if (event.target === this) { | ||
// Prevent direct clicks to the input container from blurring the input | ||
event.preventDefault(); | ||
} | ||
}); | ||
|
||
this.addEventListener('click', (event) => { | ||
if (event.target === this) { | ||
// The vaadin-input-container element was directly clicked, | ||
// focus any focusable child element from the default slot | ||
this.shadowRoot | ||
.querySelector('slot:not([name])') | ||
.assignedNodes({ flatten: true }) | ||
.forEach((node) => node.focus && node.focus()); | ||
} | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters