fix(item): add support for rendering buttons inside of an item

TODO document
This commit is contained in:
Brandy Carney
2018-01-31 12:24:33 -05:00
parent bb85a1794e
commit 37b1c03293
4 changed files with 56 additions and 8 deletions

View File

@ -1338,6 +1338,8 @@ declare global {
color?: string; color?: string;
href?: string; href?: string;
mode?: 'ios' | 'md'; mode?: 'ios' | 'md';
onclick?: (this: HTMLElement, ev: MouseEvent) => any;
tappable?: boolean;
} }
} }
} }

View File

@ -37,6 +37,18 @@ export class Item {
*/ */
@Prop() href: string; @Prop() href: string;
/**
* @input {any} Callback function.
* If this property is set, a button tag will be rendered.
*/
@Prop() onclick: (this: HTMLElement, ev: MouseEvent) => any;
/**
* @input {boolean} Whether or not this item should be tappable.
* If true, a button tag will be rendered. Default `true`.
*/
@Prop() tappable = false;
@Listen('ionStyle') @Listen('ionStyle')
itemStyle(ev: UIEvent) { itemStyle(ev: UIEvent) {
ev.stopPropagation(); ev.stopPropagation();
@ -87,8 +99,12 @@ export class Item {
this.hasStyleChange = false; this.hasStyleChange = false;
// TODO add support for button items const clickable = this.href || this.onclick || this.tappable;
const TagType = this.href ? 'a' : 'div';
let TagType = 'div';
if (clickable) {
TagType = this.href ? 'a' : 'button';
}
return ( return (
<TagType class={themedClasses}> <TagType class={themedClasses}>
@ -99,7 +115,7 @@ export class Item {
</div> </div>
<slot name='end'></slot> <slot name='end'></slot>
</div> </div>
{ this.href && this.mode === 'md' && <ion-ripple-effect useTapClick={true} /> } { clickable && this.mode === 'md' && <ion-ripple-effect useTapClick={true} /> }
</TagType> </TagType>
); );
} }

View File

@ -22,6 +22,16 @@ string
#### onclick
#### tappable
boolean
## Attributes ## Attributes
#### color #### color
@ -39,6 +49,16 @@ string
#### onclick
#### tappable
boolean
---------------------------------------------- ----------------------------------------------

View File

@ -28,16 +28,20 @@
a[ion-item] secondary a[ion-item] secondary
</ion-item> </ion-item>
<ion-item onclick="testClick(event)"> <ion-item tappable id="attachClick">
button[ion-item] button[ion-item] tappable click listener
</ion-item> </ion-item>
<ion-item class="activated" onclick="testClick(event)"> <ion-item ONCLICK="testClick(event)">
button[ion-item].activated button[ion-item] ONCLICK
</ion-item>
<ion-item class="activated" onClick="testClick(event)">
button[ion-item].activated onClick
</ion-item> </ion-item>
<ion-item color="danger" onclick="testClick(event)"> <ion-item color="danger" onclick="testClick(event)">
button[ion-item] danger button[ion-item] danger onclick
</ion-item> </ion-item>
<ion-item onclick="testClickOutsize(event)"> <ion-item onclick="testClickOutsize(event)">
@ -134,6 +138,12 @@
</ion-app> </ion-app>
<script> <script>
const attach = document.getElementById('attachClick');
attach.addEventListener('click', (ev) => {
console.log('clicked me!', ev);
});
function testClick(ev) { function testClick(ev) {
console.log('CLICK!', ev.target.tagName, ev.target.textContent.trim()); console.log('CLICK!', ev.target.tagName, ev.target.textContent.trim());
} }