-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
127 additions
and
29 deletions.
There are no files selected for viewing
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,16 @@ | ||
--- | ||
"@patternfly/pfe-core": minor | ||
--- | ||
**Decorators**: Added `@listen`. Use it to attach element event listeners to | ||
class methods. | ||
|
||
```ts | ||
@customElement('custom-input') | ||
class CustomInput extends LitElement { | ||
@property({ type: Boolean }) dirty = false; | ||
@listen('keyup', { once: true }) | ||
protected onKeyup() { | ||
this.dirty = true; | ||
} | ||
} | ||
``` |
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,21 @@ | ||
--- | ||
"@patternfly/pfe-core": minor | ||
--- | ||
**Decorators**: Added `@observes`. Use it to add property change callback by | ||
decorating them with the name of the property to observe | ||
|
||
```ts | ||
@customElement('custom-button') | ||
class CustomButton extends LitElement { | ||
#internals = this.attachInternals(); | ||
|
||
@property({ type: Boolean }) disabled = false; | ||
|
||
@observes('disabled') | ||
protected disabledChanged() { | ||
this.#internals.ariaDisabled = | ||
this.disabled ? 'true' | ||
: this.ariaDisabled ?? 'false'; | ||
} | ||
} | ||
``` |
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
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,26 @@ | ||
import type { LitElement } from 'lit'; | ||
|
||
/** | ||
* Listens for a given event on the custom element. | ||
* equivalent to calling `this.addEventListener` in the constructor | ||
* @param type event type e.g. `click` | ||
* @param options event listener options object e.g. `{ passive: true }` | ||
*/ | ||
export function listen(type: string, options?: EventListenerOptions) { | ||
return function( | ||
proto: LitElement, | ||
methodName: string, | ||
) { | ||
const origConnected = proto.connectedCallback; | ||
const origDisconnected = proto.disconnectedCallback; | ||
const listener = (proto as any)[methodName] as EventListener; | ||
proto.connectedCallback = function() { | ||
origConnected(); | ||
this.addEventListener(type, listener, options); | ||
}; | ||
proto.disconnectedCallback = function() { | ||
origDisconnected(); | ||
this.removeEventListener(type, listener, options); | ||
}; | ||
}; | ||
} |
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,30 @@ | ||
import type { ReactiveElement } from 'lit'; | ||
|
||
import type { ChangeCallback } from '@patternfly/pfe-core/controllers/property-observer-controller.js'; | ||
|
||
/** | ||
* Observes changes on the given property and calls the decorated method | ||
* with the old and new values when it changes. | ||
* @param propertyName property to react to | ||
*/ | ||
export function observes<T extends ReactiveElement>(propertyName: string & keyof T) { | ||
return function(proto: T, methodName: string) { | ||
const method = proto[methodName as keyof T] as ChangeCallback<T>; | ||
if (typeof method !== 'function') { | ||
throw new Error('@observes must decorate a class method'); | ||
} | ||
const descriptor = Object.getOwnPropertyDescriptor(proto, propertyName); | ||
Object.defineProperty(proto, propertyName, { | ||
...descriptor, | ||
configurable: true, | ||
set(this: T, newVal: T[keyof T]) { | ||
const oldVal = this[propertyName as keyof T]; | ||
// first, call any pre-existing setters, e.g. `@property` | ||
descriptor?.set?.call(this, newVal); | ||
method.call(this, oldVal, newVal); | ||
}, | ||
}); | ||
}; | ||
} | ||
|
||
|
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
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
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
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