# @terminus/ui-chip

> Chip & Chip Collection

Latest version **4.0.0** (published 2020-12-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install @terminus/ui-chip
pnpm add @terminus/ui-chip
yarn add @terminus/ui-chip
bun add @terminus/ui-chip
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.0.0 |
| Published | 2020-12-16 |
| First published | 2020-06-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 492.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 11 |
| Author | @terminus |
| Maintainers | benjamincharity, bmalinconico-terminus, atlwendy, terminus_devops |

## Links

- npm: https://www.npmjs.com/package/@terminus/ui-chip
- Repository: https://github.com/GetTerminus/terminus-oss
- Issues: https://github.com/GetTerminus/terminus-oss/issues
- npm.io page: https://npm.io/package/@terminus/ui-chip

## Dependencies (3)

- [tslib](https://npm.io/package/tslib.md) ^2.0.3
- [@schematics/angular](https://npm.io/package/@schematics/angular.md) 11.0.4
- [@angular-devkit/schematics](https://npm.io/package/@angular-devkit/schematics.md) 11.0.4

## Recent versions

- 4.0.0 (latest) — 2020-12-16
- 4.0.1 (next) — 2021-12-01
- 3.0.7 — 2020-11-13
- 3.0.6 — 2020-11-13
- 3.0.5 — 2020-11-11
- 3.0.4 — 2020-11-11
- 3.0.3 — 2020-11-06
- 3.0.2 — 2020-10-02
- 3.0.1 — 2020-10-01
- 3.0.0 — 2020-09-24
- 2.1.2 — 2020-09-14
- 2.1.1 — 2020-09-11
- 2.1.0 — 2020-09-08
- 2.0.9 — 2020-09-04
- 2.0.8 — 2020-09-03
- … 17 more at https://npm.io/package/@terminus/ui-chip/versions

## README

<h1>Chip & Chip Collection</h1>

[![CI/CD Status][github-action-badge]][github-action-link] [![Codecov][codecov-badge]][codecov-project] [![MIT License][license-image]][license-url]  
[![NPM version][npm-version-image]][npm-package] [![Library size][file-size-badge]][raw-distribution-js]

A collection of individual, keyboard accessible, chips. Useful for displaying choice collections.

NOTE: This component does not support a `FormControl`; it is a simple collection display.

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
## Table of Contents

- [Installation](#installation)
  - [CSS imports](#css-imports)
  - [CSS resources](#css-resources)
- [Usage](#usage)
  - [Using the DOM as value](#using-the-dom-as-value)
  - [Orientation](#orientation)
  - [Removable](#removable)
  - [Selectable](#selectable)
  - [Badge](#badge)
    - [Background color](#background-color)
  - [Events](#events)
    - [Collection events](#collection-events)
    - [Chip events](#chip-events)
- [Test Helpers](#test-helpers)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

## Installation

Using `ng add` command can help getting all the dependencies installed:

```bash
ng add @terminus/ui-chip
```
  
### CSS imports

In your top-level stylesheet, add these imports:

```css
@import '~@terminus/design-tokens/css/library-design-tokens.css';
@import '~@terminus/ui-styles/terminus-ui.css';
```  

### CSS resources

Load the needed font families by adding this link to the `<head>` of your application:

```css
<link href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,400;0,500;0,700;1,400&display=swap" rel="stylesheet">
```

## Usage

Create a collection of chips:

```typescript
myChips = ['one', 'two', 'three'];
```

```html
<ts-chip-collection>
  <ts-chip
    *ngFor="let chip of myChips;"
    [value]="chip"
  >{{ chip }}</ts-chip>
</ts-chip-collection>
```

### Using the DOM as value

For string based collections, the value input can be disregarded as the value will be pulled directly from the DOM.

```html
<ts-chip-collection>
  <ts-chip *ngFor="let chip of ['foo', 'bar', 'baz']">
    {{ chip }}
  </ts-chip>
</ts-chip-collection>
```

### Orientation

A collection of chips can be displayed in a row (`horizontal`) or a column (`vertical`) via the `orientation` input. By
default it displays as a row.

```html
<ts-chip-collection>
  <ts-chip
    *ngFor="let chip of chips"
    orientation="vertical"
  >{{ chip }}</ts-chip>
</ts-chip-collection>
```

### Removable

By default, chips are 'removable'. Since this component does not directly manage the data, when a user attempts to
remove a chip, an event is emitted. The consumer is responsible to use that event to remove the item from the
collection. (See [Events](#events))

```html
<ts-chip-collection>
  <ts-chip
    *ngFor="let chip of myChips"
    (remove)="removeChip($event)"
  >{{ chip }}</ts-chip>
</ts-chip-collection>
```

```typescript
myChips = ['apple', 'banana'];

removeChip(removeEvent: TsChipEvent): void {
  const index = this.myChips.indexOf(removeEvent.chip.value);
  if (index < 0) {
    return;
  }
  this.myChips.splice(index, 1);
}
```

The ability to remove a chip can be disabled per chip or as a collection:

```html
<!-- Disable the chip directly -->
<ts-chip-collection>
  <ts-chip
    *ngFor="let chip of chips"
    [isRemovable]="false"
  >{{ chip }}</ts-chip>
</ts-chip-collection>

<!-- Disable for the entire collection -->
<ts-chip-collection [isRemovable]="false">
  <ts-chip *ngFor="let chip of chips">
    {{ chip }}
  </ts-chip>
</ts-chip-collection>
```

### Selectable

Chips can be selected and will visually show that selection.

The ability to select chips can be disabled per chip or as a collection:

```html
<!-- Disable on the chip directly -->
<ts-chip-collection>
  <ts-chip
    *ngFor="let chip of chips"
    [isSelectable]="false"
  >{{ chip }}</ts-chip>
</ts-chip-collection>

<!-- Disable for the entire collection -->
<ts-chip-collection [isSelectable]="false">
  <ts-chip *ngFor="let chip of chips">
    {{ chip }}
  </ts-chip>
</ts-chip-collection>
```

### Badge

A chip can be used as a badge by placing the `tsChipBadge` directive on a standalone chip:

```html
<ts-chip tsChipBadge>My badge!</ts-chip>
```

This will disable the ability to remove, select or focus the chip.

#### Background color

You can change the badge background color by overriding this CSS custom property:

```css
:root {
  --ts-chip-badge-backgroundColor: #bada55;
}
```

### Events

Since this component does not directly manage the data, we rely on emitting events to alert the consumer for any user
interaction.

#### Collection events

| Event              | Description                                    | Payload                  |
|:-------------------|:-----------------------------------------------|:-------------------------|
| `collectionChange` | Fired when any chips are added or removed      | `TsChipCollectionChange` |
| `removed`          | Fired when a chip is removed                   | `TsChipEvent`            |
| `tabUpdateFocus`   | Fired when the user tabs out of the collection | `void`                   |

#### Chip events

| Event             | Description                                 | Payload                 |
|:------------------|:--------------------------------------------|:------------------------|
| `clicked`         | Fired when the chip is clicked              | `TsChipClickEvent`      |
| `destroyed`       | Fired when the chip is destroyed            | `TsChipEvent`           |
| `blurred`         | Fired when focus leaves the chip            | `void`                  |
| `remove`          | Fired when the chip should be removed       | `TsChipEvent`           |
| `selectionChange` | Fired when the chip selection state changes | `TsChipSelectionChange` |

## Test Helpers

Some helpers are exposed to assist with testing. These are imported from `@terminus/ui-chip/testing`;

[[source]][test-helpers-src]

| Function                                  |
|-------------------------------------------|
| `getAllChipCollectionDebugElements`       |
| `getAllChipCollectionInstances`           |
| `getChipCollectionInstance`               |
| `getChipCollectionElement`                |
| `getAllChipInstances`                     |
| `getChipInstance`                         |
| `getAllChipDebugElements`                 |
| `getChipDebugElement`                     |
| `getChipCollectionDebugElement`           |
| `getChipElement`                          |

<!-- Links -->
[test-helpers-src]:    testing/src/test-helpers.ts
[license-url]:         https://github.com/GetTerminus/terminus-oss/blob/release/LICENSE
[license-image]:       http://img.shields.io/badge/license-MIT-blue.svg
[codecov-project]:     https://codecov.io/gh/GetTerminus/terminus-oss
[codecov-badge]:       https://codecov.io/gh/GetTerminus/terminus-oss/branch/release/graph/badge.svg
[npm-version-image]:   http://img.shields.io/npm/v/@terminus/ui-chip.svg
[npm-package]:         https://www.npmjs.com/package/@terminus/ui-chip
[github-action-badge]: https://github.com/GetTerminus/terminus-oss/workflows/Release%20CI/badge.svg
[github-action-link]:  https://github.com/GetTerminus/terminus-oss/actions?query=workflow%3A%22CI+Release%22
[file-size-badge]:     http://img.badgesize.io/https://unpkg.com/@terminus/ui-chip/bundles/terminus-ui-chip.umd.min.js?compression=gzip
[raw-distribution-js]: https://unpkg.com/@terminus/ui-chip/bundles/terminus-ui-chip.umd.js

---
_Source: https://npm.io/package/@terminus/ui-chip · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
