nb-input-chip

This is an input chip component for Vue.js 3+. It allows users to add and remove chips (tags) by typing and pressing Enter.

Loading component...

Installation

NbInputChip masks (input-mask) use the same peer dependency as NbInput: vue-the-mask@0.11.1. Tokens and behaviour are documented under Input mask (input-mask). Custom tokens (input-mask-tokens) are the same as on NbInput — see Custom mask tokens (input-mask-tokens).

Yarn
yarn add @vlalg-nimbus/nb-inputs vue-the-mask@0.11.1
NPM
npm install @vlalg-nimbus/nb-inputs vue-the-mask@0.11.1

Usage

Vue 3
import { createApp } from 'vue'
import App from './App.vue'

import NbInputComponents from '@vlalg-nimbus/nb-inputs'
import "@vlalg-nimbus/nb-inputs/dist/style.css";

const app = createApp(App)
app.use(NbInputComponents)
app.mount('#app')
Nuxt 3
import NbInputComponents from '@vlalg-nimbus/nb-inputs'
import "@vlalg-nimbus/nb-inputs/dist/style.css";

export default defineNuxtPlugin(context => {
  context.vueApp.use(NbInputComponents)
})

To use, simply call the component, in this case it will be NbInputChip or nb-input-chip.

Mode 1
<template>
  <NbInputChip nb-id="chip1" input-name="tags" />
</template>
Mode 2
<template>
  <nb-input-chip nb-id="chip1" input-name="tags" />
</template>
Mode 3
<template>
  <nb-input-chip nb-id="chip1" input-name="tags"></nb-input-chip>
</template>

Preview & Playground

Select the component you want to edit/test

Loading Sandbox...

Props

Items with an (*) mean they are required

To set initial value, use the currentList prop.

nameValue typeDefaultDescription
currentListArrayDefines the initial list of chips. Accepts Array of strings.

Other props

nameValue typeDefaultDescription
nbId (*)StringSets the id attribute to differentiate from other components
displayString'b'Defines the display type. Accepts ib and b.
tabIndexNumber0Defines the tab index. Set this property to make the input focusable by the keyboard.
hasTabIndexEnterBooleantrueEnables the input to be activated with the Enter key when focused.
ariaLabelString'Input Chip'Defines the aria-label attribute for screen readers.
ariaAttrsObject{}Allows passing custom aria attributes as an object. Keys will automatically receive the aria- prefix. Example: { 'describedby': 'help-id', 'invalid': 'false' }
titleString''Tooltip text shown when hovering over the component.
caretColorString''Defines the caret (cursor) color. Accepts Color name and Hex. If empty, uses default browser color.
selectionBgColorString''Defines the background color of selected text. Accepts Color name and Hex. If empty, uses default browser color.
selectionTextColorString''Defines the text color of selected text. Accepts Color name and Hex. If empty, uses default browser color.
widthNumber185Defines the component width in pixels. Minimum value is 185.
textColorString'#ffffff'Defines the text color. Accepts Color name and Hex
paddingXNumber0.2Defines the horizontal padding in rem units.
paddingYNumber0.2Defines the vertical padding in rem units.
hasBorderRadiusBooleanfalseDefines if the component should have border-radius.
borderRadiusNumber0.375Defines border-radius in rem units.
disabledBooleanfalseDefines if the component is disabled
requiredBooleanfalseDefines if the input is required
inputReadonlyBooleanfalseDefines if the input is readonly
blockPasteBooleanfalseWhen set to true, prevents pasting content into the input field.
inputNameString''Sets the name attribute for the input element
inputPlaceholderString'Type and press Enter'Defines the input placeholder text
inputUppercaseBooleanfalseDefines if the input text should be uppercase.
inputAutocompleteString'on'Defines the autocomplete attribute. Accepts on and off.
inputMaskString | ArraynullOptional vue-the-mask@0.11.1 pattern for the chip text field (always type="text"). String or array of strings — same API as NbInput input-mask.
inputMaskEmitString'masked'Only when input-mask is set. Same modes as NbInput: applies to input-changed, added, and removed (chip / list). added-complete / removed-complete always use { masked, clean }.
inputMaskTokensObjectnullOnly when input-mask is set. Same merge rules and semantics as NbInput input-mask-tokens. Template: input-mask-tokens.
currentListArrayDefines the initial list of chips.
allowDuplicatesBooleanfalseDefines if duplicate chips are allowed.
minChipsNumber0Defines the minimum number of chips allowed.
maxChipsNumber10Defines the maximum number of chips allowed.
inputPositionString'bottom'Defines the input position. Accepts top and bottom.
textAlignString'left'Defines the text alignment. Accepts left, center, right.
activeTextStyleString'normal'Defines the text style when input is active/focused. Accepts normal, italic, oblique.
themeString'light'Defines the theme. Accepts light and dark.
inputStyleString'background'Defines the input style. Accepts background, line, border.
fontFamilyString"'Lato', sans-serif"Defines the font-family
fontSizeStringnull (default: 1.4em)Defines the font-size for the input. If null, uses 1.4em as default size. Pass a string with the font-size and the unit (e.g., "1.2em").
fontWeightNumber400Defines the font-weight
lightBgColorString'#f8f8f2'Defines the background color for light theme
lightBgColorFocusString'#eaeaea'Defines the background color when focused for light theme
lightBorderColorString'#353734'Defines the border color for light theme
lightBorderColorFocusString'#272936'Defines the border color when focused for light theme
lightDisabledBgColorString'#dfdfd9'Defines the disabled background color for light theme
lightTextColorString'#000000'Defines the text color for light theme
lightChipBgColorString'#e0e0e0'Defines the chip background color for light theme
lightChipTextColorString'#000000'Defines the chip text color for light theme
lightChipRemoveColorString'#f44336'Defines the chip remove button color for light theme
lightDisabledBorderColorString'rgba(53, 55, 52, 0.3)'Defines the disabled border color for light theme
darkBgColorString'#353734'Defines the background color for dark theme
darkBgColorFocusString'#272936'Defines the background color when focused for dark theme
darkBorderColorString'#44475a'Defines the border color for dark theme
darkBorderColorFocusString'rgba(68, 71, 90, 0.4)'Defines the border color when focused for dark theme
darkDisabledBgColorString'rgba(40, 42, 54, 1)'Defines the disabled background color for dark theme
darkTextColorString'#ffffff'Defines the text color for dark theme
darkChipBgColorString'#44475a'Defines the chip background color for dark theme
darkChipTextColorString'#ffffff'Defines the chip text color for dark theme
darkChipRemoveColorString'#f44336'Defines the chip remove button color for dark theme
darkDisabledBorderColorString'rgba(68, 71, 90, 0.3)'Defines the disabled border color for dark theme
showLabelBooleanfalseDefines if the label should be displayed. When true, shows a floating label that moves to the top when the input is active, has content, or has chips.
labelString'Label text'Defines the label text displayed above the input.
labelBackgroundString'transparent'Defines the background color of the label when active. Accepts Color name and Hex.
labelPaddingString'1px 5px'Defines the padding of the label when active. Must be space-separated values (e.g., "1px 5px").
labelBorderRadiusNumber0Defines the border-radius of the label when active in rem units.
labelLeftNumber5Defines the left position of the label when inactive in pixels.
inputLabelMarginActiveNumber15Defines the top margin of the component when the label is active in pixels. This creates space for the label above the component.
labelActiveTopNumber-13Defines the top position of the label when active in pixels. Negative values move the label above the component.
labelActiveLeftNumber5Defines the left position of the label when active in pixels.
labelBreakOnActiveBooleantrueDefines if the label text should break or use ellipsis when active
fontFamilyChipString"'Lato', sans-serif"Defines the font-family for the chips
fontSizeChipString'1.2em'Defines the font-size for the chips
fontWeightChipNumber400Defines the font-weight for the chips
fontFamilyLabelString"'Lato', sans-serif"Defines the font-family for the label
fontSizeLabelString'1em'Defines the font-size for the label when inactive
fontSizeLabelActiveString'0.8em'Defines the font-size for the label when active
fontWeightLabelNumber400Defines the font-weight for the label
lightTextColorLabelString'#333333'Defines the label text color for light theme when inactive
lightTextColorLabelActiveString'#333333'Defines the label text color for light theme when active
darkTextColorLabelString'#ffffff'Defines the label text color for dark theme when inactive
darkTextColorLabelActiveString'#ffffff'Defines the label text color for dark theme when active

Events

nameReturn typeDescription
clickedMouseEvent | KeyboardEventFired when the component container is clicked. Receives the native DOM event as first argument.
changed{ list }Fired when the chip list changes, returns an object with the current chip list (list).
added{ chip, index, list }Fired when a chip is added. With input-mask, shape follows input-mask-emit: masked — string with mask; clean — token-only string; both{ masked, clean } per chip / list item. Without mask, plain strings.
added-complete{ chip, index, list }Same timing as added; chip and each list item are always { masked, clean }. Use list.map(p => p.masked) to sync current-list for display.
removed{ chip, index, list }Fired when a chip is removed. Same shape rules as added (including input-mask-emit). index is the chip position before removal (stable with allow-duplicates: removal is by index, not by value).
removed-complete{ chip, index, list }Same timing as removed; payload shape matches added-complete.
input-changed{ value }Fired when the input value changes. Shape depends on input-mask and input-mask-emit (same options as NbInput).
focusednothingFired when the input receives focus.
blurrednothingFired when the input loses focus.
pastestringFired when content is pasted into the input, returns the pasted value as a string. Always fired, even when blockPaste is true (in which case the paste action is blocked but the event is still emitted).
mask-error{ reason, masked, clean, requiredTokenCount, filledTokenCount, activePattern }Only with input-mask. Fired when the user presses Enter with a non-empty value that does not fill every mask token slot (chip is not added). reason is 'incomplete'. masked is the trimmed field value; clean comes from vue-the-mask (masker with masked: false). requiredTokenCount / filledTokenCount count token placeholders (#, X, …). activePattern is the pattern string used for that value (for an array mask, the one chosen by the same rules as vue-the-mask dynamic masks).

Slots

The component has slots for custom content:

chip

Custom chip slot for displaying custom chip content. The slot receives the following props:

  • chip: The chip value (string)
  • index: Position of this chip in the list (number) — use with removeChip so duplicates (allow-duplicates) remove only the clicked item
  • chips: The complete chip list (array)
  • removeChip: (index: number) => void — removes the chip at that index
<template>
  <NbInputChip
    nb-id="chip1"
    input-name="tags"
  >
    <template #chip="{ chip, index, removeChip }">
      <span class="chip-text">{{ chip }}</span>
      <span class="chip-remove" @click="removeChip(index)">×</span>
    </template>
  </NbInputChip>
</template>

Input mask (input-mask)

The chip text field is always type="text". Use input-mask (kebab-case) for the same vue-the-mask@0.11.1 patterns as NbInput: string for a fixed pattern, or an array when length varies. Install the peer dependency (see Installation). Full rules, tokens, and more examples: Input mask (input-mask).

Chips in the UI store the masked string when the user confirms with Enter. added / removed use the same input-mask-emit rules as NbInput for chip and list (masked, clean, or both). added-complete / removed-complete always use { masked, clean } per chip — sync current-list from *-complete.list mapped to .masked when the parent drives the list.

input-mask-tokens (prop inputMaskTokens) is supported on NbInputChip as well: use it when you need custom vue-the-mask tokens (merged with the library defaults). Full explanation, merge rules, and the signed-integer example pattern are in NbInput — Custom mask tokens (input-mask-tokens).

Example — CPF per chip

<template>
  <NbInputChip
    nb-id="chip-cpf"
    input-name="cpf-tags"
    input-mask="###.###.###-##"
    input-placeholder="CPF, then Enter"
    :current-list="initialCpfs"
    @added="onCpfAdded"
    @added-complete="onCpfAddedComplete"
  />
</template>

<script setup>
import { ref } from 'vue'

const initialCpfs = ref([])

function onCpfAdded({ chip, list }) {
  console.log('API (no mask):', chip, list)
}

function onCpfAddedComplete({ list }) {
  initialCpfs.value = list.map(p => p.masked)
}
</script>

Example — multiple patterns (landline / mobile)

<NbInputChip
  nb-id="chip-phone"
  input-name="phones"
  :input-mask="['(##) ####-####', '(##) #####-####']"
  input-placeholder="Phone, then Enter"
/>

Example — signed integer per chip (custom token)

Same token definition as NbInput: mask N### with N = minus or digit, then three digits.

<script setup>
const signedIntMaskTokens = {
  N: { pattern: /[-0-9]/ },
}
</script>

<template>
  <NbInputChip
    nb-id="chip-signed-int"
    input-name="signed-ints"
    input-mask="N###"
    :input-mask-tokens="signedIntMaskTokens"
    input-placeholder="-42 then Enter"
  />
</template>