logo dile-components dile-components

dile-input-tags

Tags editor: the assigned tags are shown as dile-chip above a text field with suggestions. Typing adds a new tag, choosing a suggestion or writing it by hand.

It is a controlled component: it never changes its tags property by itself. It emits dile-input-tags-add and dile-input-tags-remove, and you update tags (for instance, after saving the change on the server).

It extends dile-input-suggestions, so it accepts the same suggestion properties. There is an Ajax variant, dile-ajax-input-tags, in @dile/crud.

Installation

npm i @dile/ui

Usage

Import the component.

import '@dile/ui/components/input-tags/input-tags.js';

Use the component and update tags when it asks to add or remove one.

<dile-input-tags
  name="tags"
  label="Tags"
  .tags=${this.tags}
  .suggestions=${this.knownTags}
  @dile-input-tags-add=${(e) => this.tags = [...this.tags, e.detail.item ?? { name: e.detail.text }]}
  @dile-input-tags-remove=${(e) => this.tags = this.tags.filter((tag) => tag !== e.detail.item)}
></dile-input-tags>
<script type="module">
  import '@dile/ui/components/input-tags/input-tags.js';
  const field = document.getElementById('tagsDemo');
  field.suggestions = [{ name: 'Madrid' }, { name: 'Málaga' }, { name: 'VIP', color: '#fde68a' }, { name: 'Wholesale' }];
  field.tags = [{ name: 'VIP', color: '#fde68a' }];
  field.addEventListener('dile-input-tags-add', (e) => {
    field.tags = [...field.tags, e.detail.item ?? { name: e.detail.text }];
  });
  field.addEventListener('dile-input-tags-remove', (e) => {
    field.tags = field.tags.filter((tag) => tag !== e.detail.item);
  });
</script>
<dile-input-tags id="tagsDemo" name="tags" label="Tags" colorProperty="color" delay="100" placeholder="Type a tag and press Enter"></dile-input-tags>

Adding and removing

Properties

To color the chips with other rules, extend the component and override tagChipStyle(tag), which returns the inline style of each chip.

Events

It does not emit dile-input-suggestions-selected.

Styling

Custom property Description Default
--dile-input-tags-gap Space between tags 0.4rem
--dile-input-tags-margin-bottom Space between the tags and the field 0.5rem
--dile-input-tags-add-color Color of the + icon var(--dile-input-color, #303030)
--dile-input-tags-add-size Size of the + icon 20px
--dile-input-tags-colored-text-color Text and icon color of the chips with their own color #1f2937

The chips use the dile-chip custom properties (--dile-chip-*), and each one is exposed as the tag part.