Skip to content

Commit b899fac

Browse files
feat(b-form-tags): new option to specify input type (closes #4644) (#4645)
Co-authored-by: Jacob Müller <jacob.mueller.elz@gmail.com>
1 parent 950f6b9 commit b899fac

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

src/components/form-tags/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ The default slot scope properties are as follows:
320320
| ------------------ | ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
321321
| `tags` | Array | Array of current tag strings |
322322
| `inputAttrs` | Object | Object of attributes to apply to the new tag input element via `v-bind="inputAttrs"`. See below for details |
323+
| `inputType` | String | <span class="badge badge-secondary">v2.3.0+</span> Type of input to render (normalized version of prop `input-type`) |
323324
| `inputHandlers` | Object | Object of event handlers to apply to the new tag input element via `v-on="inputHandlers"`. See below for details |
324325
| `removeTag` | Function | Method to remove a tag. Accepts one argument which is the tag value to remove |
325326
| `addTag` | Function | Method to add a new tag. Assumes the tag is the value of the input, but optionally accepts one argument which is the tag value to be added |

src/components/form-tags/form-tags.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ import { BFormInvalidFeedback } from '../form/form-invalid-feedback'
1616
import { BFormText } from '../form/form-text'
1717
import { BButton } from '../button/button'
1818

19+
// --- Constants ---
20+
1921
const NAME = 'BFormTags'
2022

21-
// --- Pre-compiled regular expressions for performance reasons ---
23+
// Supported input types (for built in input)
24+
const TYPES = ['text', 'email', 'tel', 'url', 'number']
2225

26+
// Pre-compiled regular expressions for performance reasons
2327
const RX_SPACES = /[\s\uFEFF\xA0]+/g
2428

2529
// --- Utility methods ---
@@ -90,6 +94,11 @@ export const BFormTags = /*#__PURE__*/ Vue.extend({
9094
type: String,
9195
default: null
9296
},
97+
inputType: {
98+
type: String,
99+
default: 'text',
100+
validator: type => arrayIncludes(TYPES, type)
101+
},
93102
inputClass: {
94103
type: [String, Array, Object],
95104
default: null
@@ -181,6 +190,10 @@ export const BFormTags = /*#__PURE__*/ Vue.extend({
181190
computedInputId() {
182191
return this.inputId || this.safeId('__input__')
183192
},
193+
computedInputType() {
194+
// We only allow certain types
195+
return arrayIncludes(TYPES, this.inputType) ? this.inputType : 'text'
196+
},
184197
computedInputAttrs() {
185198
return {
186199
// Merge in user supplied attributes
@@ -469,6 +482,7 @@ export const BFormTags = /*#__PURE__*/ Vue.extend({
469482
tags,
470483
addTag,
471484
removeTag,
485+
inputType,
472486
inputAttrs,
473487
inputHandlers,
474488
inputClass,
@@ -541,7 +555,7 @@ export const BFormTags = /*#__PURE__*/ Vue.extend({
541555
attrs: {
542556
...inputAttrs,
543557
'aria-describedby': ariaDescribedby || null,
544-
type: 'text',
558+
type: inputType,
545559
placeholder: placeholder || null
546560
},
547561
domProps: { value: inputAttrs.value },
@@ -660,6 +674,8 @@ export const BFormTags = /*#__PURE__*/ Vue.extend({
660674
// Methods
661675
removeTag: this.removeTag,
662676
addTag: this.addTag,
677+
// We don't include this in the attrs, as users may want to override this
678+
inputType: this.computedInputType,
663679
// <input> v-bind:inputAttrs
664680
inputAttrs: this.computedInputAttrs,
665681
// <input> v-on:inputHandlers

src/components/form-tags/form-tags.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ describe('form-tags', () => {
132132

133133
expect($input.exists()).toBe(true)
134134
expect($input.element.value).toBe('')
135+
expect($input.element.type).toBe('text')
135136

136137
$input.element.value = 'pear'
137138
$input.trigger('input')

src/components/form-tags/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
"prop": "inputAttrs",
3535
"description": "Additional attributes to apply to the new tag input element"
3636
},
37+
{
38+
"prop": "inputType",
39+
"version": "2.3.0",
40+
"description": "Specifies the type of input to use: 'text', 'email', 'tel', 'url', or 'number'. Default is 'text'"
41+
},
3742
{
3843
"prop": "addButtonText",
3944
"description": "Text for the built in 'Add' button. Slot `add-button-text' takes precedence"
@@ -115,6 +120,11 @@
115120
"type": "Object",
116121
"description": "Object of event handlers to apply to native input elements via 'v-on=\"inputHandlers\"'"
117122
},
123+
{
124+
"prop": "inputType",
125+
"version": "2.3.0",
126+
"description": "The type of input to use: 'type', 'email', 'tel', 'url', or 'number'. Default is 'text'. Normalized value of the 'input-type' prop"
127+
},
118128
{
119129
"prop": "inputId",
120130
"type": "String",

0 commit comments

Comments
 (0)