mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-21 13:01:01 +08:00
feat(chips): finished Component
This commit is contained in:

committed by
Brandy Carney

parent
421f637f96
commit
0dece7252b
@ -12,7 +12,7 @@ $chip-margin: 2px 0 !default;
|
|||||||
|
|
||||||
$chip-label-color: rgba(0, 0, 0, .87) !default;
|
$chip-label-color: rgba(0, 0, 0, .87) !default;
|
||||||
$chip-icon-color: rgba(0, 0, 0, .87) !default;
|
$chip-icon-color: rgba(0, 0, 0, .87) !default;
|
||||||
$chip-remove-color: rgba(137, 137, 137, .54) !default;
|
$chip-remove-icon-color: rgba(137, 137, 137, .54) !default;
|
||||||
|
|
||||||
ion-chip {
|
ion-chip {
|
||||||
display: inline-flex;
|
display: inline-flex;
|
||||||
@ -87,6 +87,6 @@ ion-chip {
|
|||||||
|
|
||||||
font-size: 1.4em;
|
font-size: 1.4em;
|
||||||
|
|
||||||
color: $chip-remove-color;
|
color: $chip-remove-icon-color;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { Directive, ElementRef, Renderer, Attribute } from '@angular/core';
|
import { Component, ElementRef, Renderer, Attribute } from '@angular/core';
|
||||||
|
|
||||||
import { Config } from '../../config/config';
|
import { Config } from '../../config/config';
|
||||||
|
|
||||||
@ -10,10 +10,13 @@ import { Config } from '../../config/config';
|
|||||||
* @see {@link /docs/v2/components/#chips Chips Component Docs}
|
* @see {@link /docs/v2/components/#chips Chips Component Docs}
|
||||||
**/
|
**/
|
||||||
|
|
||||||
@Directive({
|
@Component({
|
||||||
selector: 'ion-chip'
|
selector: 'ion-chip',
|
||||||
|
template: '<span class="hide"><ng-content></ng-content></span><ion-label></ion-label><button clear dark (click)="deleteChip()" *ngIf="deletable"><ion-icon name="close-circle"></ion-icon></button>'
|
||||||
})
|
})
|
||||||
|
|
||||||
export class Chip {
|
export class Chip {
|
||||||
|
private deletable: boolean = false;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
config: Config,
|
config: Config,
|
||||||
@ -25,23 +28,28 @@ export class Chip {
|
|||||||
this._readAttrs(element);
|
this._readAttrs(element);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
ngOnInit() {
|
||||||
|
this._fixContent(this._elementRef.nativeElement);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
private _readAttrs(element: HTMLElement) {
|
private _readAttrs(element: HTMLElement) {
|
||||||
|
|
||||||
let elementAttrs = element.attributes;
|
let elementAttrs = element.attributes;
|
||||||
let attrName: string;
|
let attrName: string;
|
||||||
|
|
||||||
console.info("mushroom");
|
|
||||||
|
|
||||||
for (let i = 0, l = elementAttrs.length; i < l; i++) {
|
for (let i = 0, l = elementAttrs.length; i < l; i++) {
|
||||||
if (elementAttrs[i].value !== '') continue;
|
if (elementAttrs[i].value !== '') continue;
|
||||||
|
|
||||||
attrName = elementAttrs[i].name;
|
attrName = elementAttrs[i].name;
|
||||||
|
|
||||||
// Ignore attributes item-left, item-right
|
if (attrName === 'deletable') {
|
||||||
if (attrName.indexOf('item') === -1) {
|
this._setDeletable();
|
||||||
this._setClass(attrName);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -49,7 +57,55 @@ export class Chip {
|
|||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
private _setClass(color: string) {
|
private deleteChip() {
|
||||||
this._renderer.setElementClass(this._elementRef.nativeElement, 'chip-' + color, true);
|
this._elementRef.nativeElement.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
private _setDeletable() {
|
||||||
|
this.deletable = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
private _fixContent(element: HTMLElement) {
|
||||||
|
// Take the first text node and add it to the label.
|
||||||
|
// Take the first icon or avatar and add it to the chip.
|
||||||
|
// Discard everything else.
|
||||||
|
|
||||||
|
let childNodes: NodeList = element.childNodes;
|
||||||
|
let innerNode: Node = childNodes[0];
|
||||||
|
let labelNode: Node = childNodes[1];
|
||||||
|
let addedNodes: NodeList = innerNode.childNodes;
|
||||||
|
element.removeChild(innerNode);
|
||||||
|
|
||||||
|
let missing = {image: true, text: true};
|
||||||
|
let childNode: Node;
|
||||||
|
let imageNode: Node;
|
||||||
|
let text: string;
|
||||||
|
for (let i = 0, l = addedNodes.length; i < l; i++) {
|
||||||
|
childNode = addedNodes[i];
|
||||||
|
if (childNode.nodeType === 3 && missing.text) {
|
||||||
|
text = childNode.textContent.trim();
|
||||||
|
if (text !== '') {
|
||||||
|
labelNode.textContent = text;
|
||||||
|
missing.text = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (childNode.nodeType === 1 && missing.image) {
|
||||||
|
name = childNode.nodeName;
|
||||||
|
if (childNode.nodeName === 'ION-ICON' || childNode.nodeName === 'ION-AVATAR') {
|
||||||
|
missing.image = false;
|
||||||
|
imageNode = childNode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (imageNode) {
|
||||||
|
element.insertBefore(imageNode, element.firstChild);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,12 +11,12 @@
|
|||||||
|
|
||||||
<p>
|
<p>
|
||||||
<ion-chip>
|
<ion-chip>
|
||||||
<ion-label>Default</ion-label>
|
Default
|
||||||
</ion-chip>
|
</ion-chip>
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
<ion-chip>
|
<ion-chip>
|
||||||
<ion-label>Another Longer Text</ion-label>
|
Another With Longer Text
|
||||||
</ion-chip>
|
</ion-chip>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
@ -10,33 +10,38 @@
|
|||||||
<ion-content padding style="text-align:center">
|
<ion-content padding style="text-align:center">
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
<ion-chip>
|
<ion-chip deletable>
|
||||||
<ion-label>Default</ion-label>
|
Default
|
||||||
<button ion-button icon-only clear dark (click)="deleteClicked()">
|
|
||||||
<ion-icon name="close-circle"></ion-icon>
|
|
||||||
</button>
|
|
||||||
</ion-chip>
|
</ion-chip>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
<ion-chip>
|
<ion-chip deletable>
|
||||||
<ion-icon name="pin" primary></ion-icon>
|
<ion-icon name="pin" primary></ion-icon>
|
||||||
<ion-label>Primary</ion-label>
|
With Icon
|
||||||
<button ion-button icon-only clear dark (click)="deleteClicked()">
|
|
||||||
<ion-icon name="close-circle"></ion-icon>
|
|
||||||
</button>
|
|
||||||
</ion-chip>
|
</ion-chip>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
<ion-chip>
|
<ion-chip deletable>
|
||||||
<ion-avatar>
|
<ion-avatar>
|
||||||
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAAAAACH5BAAAAAAALAAAAAABAAEAAAICTAEAOw==">
|
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAAAAACH5BAAAAAAALAAAAAABAAEAAAICTAEAOw==">
|
||||||
</ion-avatar>
|
</ion-avatar>
|
||||||
<ion-label>Default</ion-label>
|
With Avatar
|
||||||
<button ion-button icon-only clear dark (click)="deleteClicked()">
|
</ion-chip>
|
||||||
<ion-icon name="close-circle"></ion-icon>
|
</p>
|
||||||
</button>
|
|
||||||
|
<p>
|
||||||
|
<ion-chip deletable>
|
||||||
|
<b>This Should Not Be Seen</b>
|
||||||
|
|
||||||
|
With Junk Elements
|
||||||
|
<ion-icon name="pin" primary></ion-icon>
|
||||||
|
<ion-avatar>
|
||||||
|
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAAAAACH5BAAAAAAALAAAAAABAAEAAAICTAEAOw==">
|
||||||
|
</ion-avatar>
|
||||||
|
<hr>
|
||||||
|
This Should Not Be Seen Either
|
||||||
</ion-chip>
|
</ion-chip>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
@ -12,37 +12,37 @@
|
|||||||
<p>
|
<p>
|
||||||
<ion-chip>
|
<ion-chip>
|
||||||
<ion-icon name="pin"></ion-icon>
|
<ion-icon name="pin"></ion-icon>
|
||||||
<ion-label>Default</ion-label>
|
Default
|
||||||
</ion-chip>
|
</ion-chip>
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
<ion-chip>
|
<ion-chip>
|
||||||
<ion-icon name="pin" primary></ion-icon>
|
<ion-icon name="pin" primary></ion-icon>
|
||||||
<ion-label>Primary</ion-label>
|
Primary
|
||||||
</ion-chip>
|
</ion-chip>
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
<ion-chip>
|
<ion-chip>
|
||||||
<ion-icon name="pin" secondary></ion-icon>
|
<ion-icon name="pin" secondary></ion-icon>
|
||||||
<ion-label>Secondary</ion-label>
|
Secondary
|
||||||
</ion-chip>
|
</ion-chip>
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
<ion-chip>
|
<ion-chip>
|
||||||
<ion-icon name="pin" danger></ion-icon>
|
<ion-icon name="pin" danger></ion-icon>
|
||||||
<ion-label>Danger</ion-label>
|
Danger
|
||||||
</ion-chip>
|
</ion-chip>
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
<ion-chip>
|
<ion-chip>
|
||||||
<ion-icon name="pin" light></ion-icon>
|
<ion-icon name="pin" light></ion-icon>
|
||||||
<ion-label>Light</ion-label>
|
Light
|
||||||
</ion-chip>
|
</ion-chip>
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
<ion-chip>
|
<ion-chip>
|
||||||
<ion-icon name="pin" dark></ion-icon>
|
<ion-icon name="pin" dark></ion-icon>
|
||||||
<ion-label>Dark</ion-label>
|
Dark
|
||||||
</ion-chip>
|
</ion-chip>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
<ion-avatar>
|
<ion-avatar>
|
||||||
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAAAAACH5BAAAAAAALAAAAAABAAEAAAICTAEAOw==">
|
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAAAAACH5BAAAAAAALAAAAAABAAEAAAICTAEAOw==">
|
||||||
</ion-avatar>
|
</ion-avatar>
|
||||||
<ion-label>Default</ion-label>
|
Default
|
||||||
</ion-chip>
|
</ion-chip>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user