only run handler after first render

pull/366/head^2
Cory LaViska 2021-03-11 08:57:33 -05:00
rodzic 7fb1a83788
commit c5614bfc95
2 zmienionych plików z 11 dodań i 2 usunięć

Wyświetl plik

@ -11,6 +11,7 @@ _During the beta period, these restrictions may be relaxed in the event of a mis
- Add touch support to `sl-rating` [#362](https://github.com/shoelace-style/shoelace/pull/362)
- Fixed a bug where the `open` attribute on `sl-details` would prevent it from opening [#357](https://github.com/shoelace-style/shoelace/issues/357)
- Fixed event detail type parsing so component class names are shown instead of `default`
- Improved `@watch` decorator so watch handlers don't run before the first render
## 2.0.0-beta.30

Wyświetl plik

@ -86,6 +86,9 @@ export function tag(tagName: string) {
// @watch decorator
//
// Runs when an observed property changes, e.g. @property or @internalProperty. This will only run after the first
// update, so initial values will not trigger the watch function.
//
// Usage:
//
// @watch('propName') handlePropChange(oldValue, newValue) {
@ -94,10 +97,15 @@ export function tag(tagName: string) {
//
export function watch(propName: string) {
return (protoOrDescriptor: any, name: string): any => {
const update = protoOrDescriptor.update;
const { firstUpdated, update } = protoOrDescriptor;
protoOrDescriptor.firstUpdated = function (changedProps: Map<string, any>) {
firstUpdated.call(this, changedProps);
this.__didFirstUpdate = true;
};
protoOrDescriptor.update = function (changedProps: Map<string, any>) {
if (changedProps.has(propName)) {
if (this.__didFirstUpdate && changedProps.has(propName)) {
const oldValue = changedProps.get(propName);
const newValue = this[propName];