1.7.1 • Published 3 months ago

bootstrap5-tags v1.7.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

Tags for Bootstrap 4/5

NPM Downloads

How to use

An ES6 native replacement for select using standards Bootstrap 5 (and 4) styles.

(almost) No additional CSS needed! Supports creation of new tags.

import Tags from "./tags.js";
Tags.init();
// Tags.init(selector, opts);
// You can pass global settings in opts that will apply
// to all Tags instances

By default, only provided options are available. Validation error will be displayed in case of invalid tag.

<label for="tags-input" class="form-label">Tags</label>
<select class="form-select" id="tags-input" name="tags[]" multiple>
  <option disabled hidden value="">Choose a tag...</option>
  <option value="1" selected="selected">Apple</option>
  <option value="2">Banana</option>
  <option value="3">Orange</option>
</select>
<div class="invalid-feedback">Please select a valid tag.</div>

Creation of new tags

Use attribute data-allow-new to allow creation of new tags. Their default value will be equal to the text. Since you can enter arbitrary text, no validation will occur.

<select class="form-select" id="tags-input" name="tags[]" multiple data-allow-new="true"></select>

You can force these new tags to respect a given regex.

NOTE: don't forget the [] if you need multiple values!

Server side support

You can also use options provided by the server. This script expects a json response that is an array or an object with the data key containing an array.

Simply set data-server where your endpoint is located. It should provide an array of value/label objects. The suggestions will be populated upon init except if data-live-server is set, in which case, it will be populated on type. A ?query= parameter is passed along with the current value of the searchInput.

You can preselect values either by using data-selected or by marking the suggestion as selected in the json result.

<label for="validationTagsJson" class="form-label">Tags (server side)</label>
<select
  class="form-select"
  id="validationTagsJson"
  name="tags_json[]"
  multiple
  data-allow-new="true"
  data-server="demo.json"
  data-live-server="1"
>
  <option disabled hidden value="">Choose a tag...</option>
</select>

You can pass additionnal parameters with data-server-params and choose the method with data-server-method (GET or POST).

Setting data programmatically

You can use the items config key to pass options. These will be added to the select object. Items can be an array of Suggestions, or a key:value object.

You can also call setData manually.

Options

Options can be either passed to the constructor (eg: optionName) or in data-option-name format. You can also use the data-config attribute with a json encoded string.

NameTypeDescription
itemsArray.<(Suggestion|SuggestionGroup)>Source items
allowNewBooleanAllows creation of new tags
showAllSuggestionsBooleanShow all suggestions even if they don't match. Disables validation.
badgeStyleStringColor of the badge (color can be configured per option as well)
allowClearBooleanShow a clear icon
clearEndBooleanPlace clear icon at the end
selectedArrayA list of initially selected values
regexStringRegex for new tags
separatorArray | StringA list (pipe separated) of characters that should act as separator (default is using enter key)
maxNumberLimit to a maximum of tags (0 = no limit)
placeholderStringProvides a placeholder if none are provided as the first empty option
clearLabelStringText as clear tooltip
searchLabelStringDefault placeholder
showDropIconBooleanShow dropdown icon
keepOpenBooleanKeep suggestions open after selection, clear on focus out
allowSameBooleanAllow same tags used multiple times
baseClassStringCustomize the class applied to badges
addOnBlurBooleanAdd new tags on blur (only if allowNew is enabled)
showDisabledBooleanShow disabled tags
hideNativeValidationBooleanHide native validation tooltips
suggestionsThresholdNumberNumber of chars required to show suggestions
maximumItemsNumberMaximum number of items to display
autoselectFirstBooleanAlways select the first item
updateOnSelectBooleanUpdate input value on selection (doesn't play nice with autoselectFirst)
highlightTypedBooleanHighlight matched part of the suggestion
highlightClassStringClass applied to the mark element
fullWidthBooleanMatch the width on the input field
fixedBooleanUse fixed positioning (solve overflow issues)
fuzzyBooleanFuzzy search
startsWithBooleanMust start with the string. Defaults to false (it matches any position).
singleBadgeBooleanShow badge for single elements
activeClassesArrayBy default: "bg-primary", "text-white"
labelFieldStringKey for the label
valueFieldStringKey for the value
searchFieldsArrayKey for the search
queryParamStringName of the param passed to endpoint (query by default)
serverStringEndpoint for data provider
serverMethodStringHTTP request method for data provider, default is GET
serverParamsString | ObjectParameters to pass along to the server. You can specify a "related" key with the id of a related field.
serverDataKeyStringBy default: data
fetchOptionsObjectAny other fetch options (https://developer.mozilla.org/en-US/docs/Web/API/fetch#syntax)
liveServerBooleanShould the endpoint be called each time on input
noCacheBooleanPrevent caching by appending a timestamp
allowHtmlBooleanAllow html in input (can lead to script injection)
inputFilterfunctionFunction to filter input
sanitizerfunctionAlternative function to sanitize content
debounceTimeNumberDebounce time for live server
notFoundMessageStringDisplay a no suggestions found message. Leave empty to disable
onRenderItemRenderCallbackCallback function that returns the suggestion
onSelectItemItemCallbackCallback function to call on selection
onClearItemValueCallbackCallback function to call on clear
onCreateItemCreateCallbackCallback function when an item is created
onBlurEventCallbackCallback function on blur
onFocusEventCallbackCallback function on focus
onCanAddAddCallbackCallback function to validate item. Return false to show validation message.
onServerResponseServerCallbackCallback function to process server response. Must return a Promise
confirmClearModalItemCallbackAllow modal confirmation of clear. Must return a Promise
confirmAddModalItemCallbackAllow modal confirmation of add. Must return a Promise

Any of these config option can be changed later with setConfig.

To know more about these features, check the demo!

Callbacks

EventCallback ⇒ void

ParamType
eventEvent
instTags

ServerCallback ⇒ Promise

ParamType
responseResponse

ModalItemCallback ⇒ Promise

ParamType
labelString
instTags

RenderCallback ⇒ String

ParamType
itemSuggestion
labelString
instTags

ItemCallback ⇒ void

ParamType
itemSuggestion
instTags

ValueCallback ⇒ void

ParamType
valueString
instTags

AddCallback ⇒ void | Boolean

ParamType
valueString
dataObject
instTags

CreateCallback ⇒ void

ParamType
optionHTMLOptionElement
instTags

Tips

  • You can also use it on single selects! :-)
  • Use arrow down to show dropdown
  • If you have a really long list of options, a scrollbar will be used
  • Access Tags instance on a given element with Tags.getInstance(mySelect)

Style

While styling is not mandatory, some pseudo styles may help align your styles with a regular bootstrap form-control We basically replicate input state as pseudo classes on the form-control container

  • Support focus styles by implementing a pseudo class form-control-focus
  • Support improved floating labels by implementing a pseudo class form-placeholder-shown
  • Support disabled styles by implementing a pseudo class form-control-disabled

These styles can be found in _tags.scss

You can also use the tags-pure.scss file which provide you a css vars only version of the required styles (works well with bootstrap 5.3)

Without Bootstrap 5

Bootstrap 4 support

Even if it was not the initial idea to support Bootstrap 4, this component is now compatible with Bootstrap 4 because it only requires minimal changes.

Check out demo-bs4.html

Standalone usage

Obviously, this package works great with the full bootstrap library, but you can also use it without Bootstrap or with a trimmed down version of it

Actually, this library doesn't even use the js library to position the dropdown menu, so its only dependencies is on css classes. You can check out the .scss file to see how to reduce bootstrap 5 css to a smaller size.

Check out demo-standalone.html

Demo

https://codepen.io/lekoalabe/pen/ExWYEqx

How does it look ?

screenshot

Do you need to init this automagically ?

You can now use this as a custom element as part of my Formidable Elements collection.

Or you can use Modular Behaviour (see demo)

Browser supports

Modern browsers (edge, chrome, firefox, safari... not IE11). Add a warning if necessary.

Also check out

How to contribute

If you want to make a PR, please make your changes in tags.js and do not commit any build files They will be updated upon release of a new version.

If you want to test your changes, simply run npm start and test in demo.html (feel free to add new test cases).

For scss updates, apply changes to scss files. They need to be compiled manually since they are not meant to be used by themselves.

1.7.1

3 months ago

1.7.0

3 months ago

1.6.16

3 months ago

1.6.9

8 months ago

1.6.8

8 months ago

1.6.7

10 months ago

1.6.6

10 months ago

1.6.5

10 months ago

1.6.11

7 months ago

1.6.10

7 months ago

1.6.13

5 months ago

1.6.12

5 months ago

1.6.15

5 months ago

1.6.14

5 months ago

1.6.4

10 months ago

1.6.3

10 months ago

1.6.2

11 months ago

1.6.1

11 months ago

1.6.0

11 months ago

1.5.24

11 months ago

1.5.23

11 months ago

1.5.16

1 year ago

1.5.18

1 year ago

1.5.17

1 year ago

1.5.19

1 year ago

1.5.21

1 year ago

1.5.20

1 year ago

1.5.22

12 months ago

1.5.14

1 year ago

1.5.15

1 year ago

1.5.9

1 year ago

1.5.8

1 year ago

1.5.7

1 year ago

1.5.6

1 year ago

1.5.5

1 year ago

1.5.10

1 year ago

1.5.12

1 year ago

1.5.11

1 year ago

1.5.13

1 year ago

1.5.4

1 year ago

1.5.3

1 year ago

1.5.2

1 year ago

1.5.1

1 year ago

1.4.39

2 years ago

1.5.0

1 year ago

1.4.40

2 years ago

1.4.42

1 year ago

1.4.41

2 years ago

1.4.35

2 years ago

1.4.37

2 years ago

1.4.38

2 years ago

1.4.31

2 years ago

1.4.30

2 years ago

1.4.33

2 years ago

1.4.32

2 years ago

1.4.34

2 years ago

1.4.26

2 years ago

1.4.28

2 years ago

1.4.27

2 years ago

1.4.29

2 years ago

1.4.25

2 years ago

1.4.6

2 years ago

1.4.5

2 years ago

1.4.4

2 years ago

1.4.3

2 years ago

1.4.2

2 years ago

1.2.4

2 years ago

1.4.1

2 years ago

1.2.3

2 years ago

1.4.0

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.4.20

2 years ago

1.4.22

2 years ago

1.4.21

2 years ago

1.4.24

2 years ago

1.4.23

2 years ago

1.3.8

2 years ago

1.3.7

2 years ago

1.3.6

2 years ago

1.3.5

2 years ago

1.3.4

2 years ago

1.3.3

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.4.9

2 years ago

1.4.11

2 years ago

1.4.8

2 years ago

1.4.10

2 years ago

1.4.7

2 years ago

1.4.13

2 years ago

1.4.12

2 years ago

1.4.15

2 years ago

1.4.14

2 years ago

1.4.17

2 years ago

1.4.16

2 years ago

1.4.19

2 years ago

1.4.18

2 years ago

1.2.0

2 years ago

1.1.6

2 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.1.2

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago