Skip to content

Commit

Permalink
feat(img): add ionError event (#17134)
Browse files Browse the repository at this point in the history
resolves #16947
  • Loading branch information
pickfire authored and liamdebeasi committed Mar 27, 2019
1 parent 352797e commit 04f931f
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 6 deletions.
3 changes: 2 additions & 1 deletion angular/src/directives/proxies.ts
Expand Up @@ -294,11 +294,12 @@ export declare interface IonImg extends StencilComponents<'IonImg'> {}
@Component({ selector: 'ion-img', changeDetection: 0, template: '<ng-content></ng-content>', inputs: ['alt', 'src'] })
export class IonImg {
ionImgDidLoad!: EventEmitter<CustomEvent>;
ionError!: EventEmitter<CustomEvent>;
protected el: HTMLElement;
constructor(c: ChangeDetectorRef, r: ElementRef) {
c.detach();
this.el = r.nativeElement;
proxyOutputs(this, this.el, ['ionImgDidLoad']);
proxyOutputs(this, this.el, ['ionImgDidLoad', 'ionError']);
}
}
proxyInputs(IonImg, ['alt', 'src']);
Expand Down
1 change: 1 addition & 0 deletions core/api.txt
Expand Up @@ -380,6 +380,7 @@ ion-header,prop,translucent,boolean,false,false,false
ion-img,shadow
ion-img,prop,alt,string | undefined,undefined,false,false
ion-img,prop,src,string | undefined,undefined,false,false
ion-img,event,ionError,void,true
ion-img,event,ionImgDidLoad,void,true

ion-infinite-scroll-content,none
Expand Down
6 changes: 5 additions & 1 deletion core/src/components.d.ts
Expand Up @@ -1546,7 +1546,11 @@ export namespace Components {
*/
'alt'?: string;
/**
* Emitted when the img src is loaded
* Emitted when the img fails to load
*/
'onIonError'?: (event: CustomEvent<void>) => void;
/**
* Emitted when the img src has been set
*/
'onIonImgDidLoad'?: (event: CustomEvent<void>) => void;
/**
Expand Down
13 changes: 12 additions & 1 deletion core/src/components/img/img.tsx
Expand Up @@ -13,6 +13,8 @@ export class Img implements ComponentInterface {

@State() loadSrc?: string;

@State() loadError?: () => void;

/**
* This attribute defines the alternative text describing the image.
* Users will see this text displayed if the image URL is wrong,
Expand All @@ -29,9 +31,12 @@ export class Img implements ComponentInterface {
this.addIO();
}

/** Emitted when the img src is loaded */
/** Emitted when the img src has been set */
@Event() ionImgDidLoad!: EventEmitter<void>;

/** Emitted when the img fails to load */
@Event() ionError!: EventEmitter<void>;

componentDidLoad() {
this.addIO();
}
Expand Down Expand Up @@ -60,10 +65,15 @@ export class Img implements ComponentInterface {
}

private load() {
this.loadError = this.onError;
this.loadSrc = this.src;
this.ionImgDidLoad.emit();
}

private onError = () => {
this.ionError.emit();
}

private removeIO() {
if (this.io) {
this.io.disconnect();
Expand All @@ -77,6 +87,7 @@ export class Img implements ComponentInterface {
src={this.loadSrc}
alt={this.alt}
decoding="async"
onError={this.loadError}
/>
);
}
Expand Down
7 changes: 4 additions & 3 deletions core/src/components/img/readme.md
Expand Up @@ -79,9 +79,10 @@ export default Example

## Events

| Event | Description | Type |
| --------------- | ---------------------------------- | ------------------- |
| `ionImgDidLoad` | Emitted when the img src is loaded | `CustomEvent<void>` |
| Event | Description | Type |
| --------------- | ------------------------------------- | ------------------- |
| `ionError` | Emitted when the img fails to load | `CustomEvent<void>` |
| `ionImgDidLoad` | Emitted when the img src has been set | `CustomEvent<void>` |


----------------------------------------------
Expand Down
11 changes: 11 additions & 0 deletions core/src/components/img/test/basic/index.html
Expand Up @@ -31,6 +31,13 @@
<f></f>
<f></f>
<ion-img id="hidden" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAB4AAAARQAQMAAAA2ut43AAAABlBMVEXMzMz////TjRV2AAAEPUlEQVR42u3RMREAMBDDsPAn/eXhavOq87Ztu7u7+6eBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBf2hvgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgatgn4GBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGr7S0wMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAw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DAwMDAwMDAwMHC1vQUGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgaugn0GBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGjvYD5WuYrpZqdmcAAAAASUVORK5CYII="></ion-img>
<f></f>
<f></f>
<f></f>
<f></f>
<f></f>
<f></f>
<ion-img id="hidden" src="data:image/png;base64,"></ion-img>

</ion-content>

Expand Down Expand Up @@ -63,6 +70,10 @@
document.body.addEventListener('ionImgDidLoad', (event) => {
console.log('image did load', event.target);
});

document.body.addEventListener('ionError', (event) => {
console.error('image error', event.target);
});
</script>
</body>

Expand Down

0 comments on commit 04f931f

Please sign in to comment.