4.0.0 • Published 2 years ago

@terminus/ui-chip v4.0.0

Weekly downloads
22
License
MIT
Repository
github
Last release
2 years ago

CI/CD Status Codecov MIT License
NPM version Library size

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.

Table of Contents

Installation

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

ng add @terminus/ui-chip

CSS imports

In your top-level stylesheet, add these imports:

@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:

<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:

myChips = ['one', 'two', 'three'];
<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.

<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.

<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)

<ts-chip-collection>
  <ts-chip
    *ngFor="let chip of myChips"
    (remove)="removeChip($event)"
  >{{ chip }}</ts-chip>
</ts-chip-collection>
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:

<!-- 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:

<!-- 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:

<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:

: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

EventDescriptionPayload
collectionChangeFired when any chips are added or removedTsChipCollectionChange
removedFired when a chip is removedTsChipEvent
tabUpdateFocusFired when the user tabs out of the collectionvoid

Chip events

EventDescriptionPayload
clickedFired when the chip is clickedTsChipClickEvent
destroyedFired when the chip is destroyedTsChipEvent
blurredFired when focus leaves the chipvoid
removeFired when the chip should be removedTsChipEvent
selectionChangeFired when the chip selection state changesTsChipSelectionChange

Test Helpers

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

[source]

Function
getAllChipCollectionDebugElements
getAllChipCollectionInstances
getChipCollectionInstance
getChipCollectionElement
getAllChipInstances
getChipInstance
getAllChipDebugElements
getChipDebugElement
getChipCollectionDebugElement
getChipElement
4.0.1

2 years ago

4.0.0

3 years ago

3.0.7

3 years ago

3.0.6

3 years ago

3.0.4

3 years ago

3.0.5

3 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.9

4 years ago

2.0.8

4 years ago

2.0.7

4 years ago

2.0.6

4 years ago

2.0.5

4 years ago

2.0.4

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.0.1

4 years ago