@carissa-io/components-tag-input
v0.1.1
Published
Carissa UI: tag input (tags inside the field, delimiters, paste, limits) as a React component with SCSS variables
Readme
@carissa-io/components-tag-input
Tag input: TagInput. The tags and the text field share one input box: type, press a delimiter, get a tag. Built on the
look of @carissa-io/components-input (box, label, error line) and the
chips of @carissa-io/components-tag. A React component, SCSS-themed.
npm i @carissa-io/components-tag-inputUse
import { TagInput } from '@carissa-io/components-tag-input'
<TagInput
label={t('recipients')}
placeholder={t('add_email')}
remove_label={(tag) => t('remove_tag', { tag })}
max={10}
validate={(tag) => !tag.includes('@') && 'not_an_email'}
messages={{ duplicate: t('already_added'), max: t('too_many'), not_an_email: t('bad_email') }}
on_change={(tags) => setRecipients(tags)}
/>| Prop | |
|---|---|
| value, on_change(values) | controlled tags (array of strings); without value the component keeps its own state |
| default_value | the initial tags when uncontrolled (default []) |
| on_add(tag), on_remove(tag, index) | fire for each tag added or removed |
| delimiters | keys that commit the text (default ['Enter', ',']): KeyboardEvent.key names or any single character (;, ) |
| allow_duplicates | default false |
| max | maximum number of tags |
| validate(tag) | falsy when fine, or an error code (a string); any other truthy value means invalid |
| messages, on_error(code) | text for the codes (the bare code is shown without an entry); on_error is told about each refusal |
| error | an error the parent decides on; wins over the internal one |
| remove_label | aria-label of the remove buttons: a string, or (tag) => string (default the code remove) |
| label, placeholder, name, id, disabled, required | the label, the placeholder (only shown while there are no tags), ... |
name renders one hidden input per tag (same name), so a form submits the list. className is appended to the wrapper next
to c-input c-tag-input; unknown props go to the text <input>.
Behaviour
- A delimiter key adds the trimmed text; empty pieces are skipped. Enter on an empty field is left to the form. A refused tag
(error code
duplicate,maxorinvalid, or the code yourvalidatereturns) stays in the field so it can be fixed and the code is cleared as soon as the text changes. - A paste that contains a delimiter is split on all of them (Enter splits on line breaks) and every piece becomes a tag; a paste without one goes into the field as plain text.
- Backspace on an empty field removes the last tag. ArrowLeft on an empty field focuses the last tag; Left / Right move between tags and Right on the last one returns to the field. Backspace or Delete on a focused tag removes it (the focus goes to the next tag, or the field); clicking its button removes it too (the focus goes to the field).
- Text left in the field on blur is not added: commit it with a delimiter.
Style
@use '@carissa-io/components-tag-input/scss' with (
$c-tag-input-gap: 0.5rem,
$c-tag-primary-background: #e7f0ff,
$c-input-radius: 0
);The box is the Input's ($c-input-*) and the chips are the Tag's ($c-tag-*); the module forwards both. Classes:
c-tag-input, c-tag-input__field, c-tag-input__tag, c-tag-input__control next to the Input's (c-input__box,
c-input__label, c-input__error...) and the Tag's (c-tag...). Every variable is !default (see
scss/_variables.scss).
| Variable | Default |
|---|---|
| $c-tag-input-gap | 0.375rem (between tags and the text field, and between rows) |
| $c-tag-input-control-min-width | 6rem (the text field wraps to its own row below this width) |
