Skip to content

feat(b-form-tags): new option to specify input type (closes #4644) #4645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/components/form-tags/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ The default slot scope properties are as follows:
| ------------------ | ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `tags` | Array | Array of current tag strings |
| `inputAttrs` | Object | Object of attributes to apply to the new tag input element via `v-bind="inputAttrs"`. See below for details |
| `inputType` | String | <span class="badge badge-secondary">v2.3.0+</span> Type of input to render (normalized version of prop `input-type`) |
| `inputHandlers` | Object | Object of event handlers to apply to the new tag input element via `v-on="inputHandlers"`. See below for details |
| `removeTag` | Function | Method to remove a tag. Accepts one argument which is the tag value to remove |
| `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 |
Expand Down
20 changes: 18 additions & 2 deletions src/components/form-tags/form-tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ import { BFormInvalidFeedback } from '../form/form-invalid-feedback'
import { BFormText } from '../form/form-text'
import { BButton } from '../button/button'

// --- Constants ---

const NAME = 'BFormTags'

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

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

// --- Utility methods ---
Expand Down Expand Up @@ -90,6 +94,11 @@ export const BFormTags = /*#__PURE__*/ Vue.extend({
type: String,
default: null
},
inputType: {
type: String,
default: 'text',
validator: type => arrayIncludes(TYPES, type)
},
inputClass: {
type: [String, Array, Object],
default: null
Expand Down Expand Up @@ -181,6 +190,10 @@ export const BFormTags = /*#__PURE__*/ Vue.extend({
computedInputId() {
return this.inputId || this.safeId('__input__')
},
computedInputType() {
// We only allow certain types
return arrayIncludes(TYPES, this.inputType) ? this.inputType : 'text'
},
computedInputAttrs() {
return {
// Merge in user supplied attributes
Expand Down Expand Up @@ -469,6 +482,7 @@ export const BFormTags = /*#__PURE__*/ Vue.extend({
tags,
addTag,
removeTag,
inputType,
inputAttrs,
inputHandlers,
inputClass,
Expand Down Expand Up @@ -541,7 +555,7 @@ export const BFormTags = /*#__PURE__*/ Vue.extend({
attrs: {
...inputAttrs,
'aria-describedby': ariaDescribedby || null,
type: 'text',
type: inputType,
placeholder: placeholder || null
},
domProps: { value: inputAttrs.value },
Expand Down Expand Up @@ -660,6 +674,8 @@ export const BFormTags = /*#__PURE__*/ Vue.extend({
// Methods
removeTag: this.removeTag,
addTag: this.addTag,
// We don't include this in the attrs, as users may want to override this
inputType: this.computedInputType,
// <input> v-bind:inputAttrs
inputAttrs: this.computedInputAttrs,
// <input> v-on:inputHandlers
Expand Down
1 change: 1 addition & 0 deletions src/components/form-tags/form-tags.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ describe('form-tags', () => {

expect($input.exists()).toBe(true)
expect($input.element.value).toBe('')
expect($input.element.type).toBe('text')

$input.element.value = 'pear'
$input.trigger('input')
Expand Down
10 changes: 10 additions & 0 deletions src/components/form-tags/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
"prop": "inputAttrs",
"description": "Additional attributes to apply to the new tag input element"
},
{
"prop": "inputType",
"version": "2.3.0",
"description": "Specifies the type of input to use: 'text', 'email', 'tel', 'url', or 'number'. Default is 'text'"
},
{
"prop": "addButtonText",
"description": "Text for the built in 'Add' button. Slot `add-button-text' takes precedence"
Expand Down Expand Up @@ -115,6 +120,11 @@
"type": "Object",
"description": "Object of event handlers to apply to native input elements via 'v-on=\"inputHandlers\"'"
},
{
"prop": "inputType",
"version": "2.3.0",
"description": "The type of input to use: 'type', 'email', 'tel', 'url', or 'number'. Default is 'text'. Normalized value of the 'input-type' prop"
},
{
"prop": "inputId",
"type": "String",
Expand Down