Skip to content

Commit fee433b

Browse files
authored
Merge pull request #31 from makeupsomething/feature/tag-length-validation
Add way for user to specify a min and max length of a tag
2 parents 1fc68d4 + f47e65d commit fee433b

File tree

4 files changed

+80
-4
lines changed

4 files changed

+80
-4
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ import BetterInputTag from 'better-vue-input-tag'
3535
| on-paste-delimiter | String | "" | During pasting, this delimiter is used to create multiple tags |
3636
| read-only | Boolean | false | Set input to readonly |
3737
| on-change | Function | undefined | Callback to get the tags when there is a change |
38-
| validate | String | "" | Apply certain validator for user input. Choose from `email`, `url`, `text`, `digits` or `isodate`
38+
| validate | String | "" | Apply certain validator for user input. Choose from `email`, `url`, `text`, `digits` or `isodate` |
39+
| length | Object | undefined | Set a minimum and/or maximum length for tags `{min: 1, max: 10}` |
3940

4041
**This project was built with [generator-vue-component](https://github.com/ianaya89/generator-vue-component) ❤️**
4142

docs/App.vue

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
onPasteSeparator: '',
1414
tags: ['Jerry', 'Kramer', 'Elaine', 'George'],
1515
htmlCode: '',
16-
validate: ''
16+
validate: '',
17+
length: {min: null, max: null}
1718
}
1819
},
1920
@@ -29,6 +30,7 @@
2930
html += this.tags ? ' :tags="tags"' : ''
3031
html += this.readOnly ? ' :read-only="true"' : ''
3132
html += this.validate ? ` validate="${this.validate}"` : ''
33+
html += this.length.min || this.length.max ? ` length="{min: ${this.length.min}, max: ${this.length.max}}"` : ''
3234
return `${html}></input-tag>`
3335
}
3436
}
@@ -73,7 +75,8 @@
7375
:placeholder='placeholder',
7476
:on-paste-separator='onPasteSeparator',
7577
:read-only='readOnly',
76-
:validate='validate'
78+
:validate='validate',
79+
:length='length'
7780
)
7881

7982
h3
@@ -105,6 +108,13 @@
105108
option(value='digits') Digits
106109
option(value='isodate') ISO Date
107110

111+
.form-group
112+
p.label min tag length:
113+
input(v-model='length.min' type='number')
114+
br
115+
p.label max tag length:
116+
input(v-model='length.max' type='number')
117+
108118
.form-group
109119
p.label tags:
110120
code {{ tags }}

src/BetterInputTag.vue

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@
6262
onPasteSeparator: {
6363
type: String,
6464
default: null
65+
},
66+
length: {
67+
type: Object,
68+
default: null
6569
}
6670
},
6771
@@ -86,7 +90,7 @@
8690
return
8791
}
8892
89-
if (tag && this.tags.indexOf(tag) === -1 && this.validateIfNeeded(tag)) {
93+
if (tag && this.tags.indexOf(tag) === -1 && this.validateIfNeeded(tag) && this.validateLengthIfNeeded(tag)) {
9094
this.tags.push(tag)
9195
this.tagChange()
9296
}
@@ -102,6 +106,19 @@
102106
return true
103107
},
104108
109+
validateLengthIfNeeded (tagValue) {
110+
if (this.length === null || this.length === undefined) {
111+
return true
112+
} else if (this.length.min && this.length.max) {
113+
return tagValue.length >= this.length.min && tagValue.length <= this.length.max
114+
} else if (this.length.min) {
115+
return tagValue.length >= this.length.min
116+
} else if (this.length.max) {
117+
return tagValue.length <= this.length.max
118+
}
119+
return true
120+
},
121+
105122
remove (index) {
106123
this.tags.splice(index, 1)
107124
this.tagChange()

test/BetterInputTag.spec.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,52 @@ describe('BetterInputTag.vue', () => {
207207
expect(BetterInputTagISODateOnly.tags.length).toEqual(1)
208208
})
209209
})
210+
211+
describe('validate length of tag', () => {
212+
const BetterInputTagMinMaxOnly = new ClonedComponent({
213+
propsData: { length: {min: 1, max: 5} }
214+
}).$mount()
215+
216+
it('should only tags with length >= 1 and <= 5 characters', () => {
217+
BetterInputTagMinMaxOnly.addNew('foo')
218+
BetterInputTagMinMaxOnly.addNew('123')
219+
BetterInputTagMinMaxOnly.addNew('[email protected]')
220+
BetterInputTagMinMaxOnly.addNew('https://tucci.me')
221+
BetterInputTagMinMaxOnly.addNew('2002-04-03')
222+
223+
expect(BetterInputTagMinMaxOnly.tags.length).toEqual(2)
224+
})
225+
})
226+
227+
describe('validate length of tag', () => {
228+
const BetterInputTagMinOnly = new ClonedComponent({
229+
propsData: { length: {min: 1} }
230+
}).$mount()
231+
232+
it('should only tags with length >= to 1 characters', () => {
233+
BetterInputTagMinOnly.addNew('foo')
234+
BetterInputTagMinOnly.addNew('123')
235+
BetterInputTagMinOnly.addNew('[email protected]')
236+
BetterInputTagMinOnly.addNew('https://tucci.me')
237+
BetterInputTagMinOnly.addNew('2002-04-03')
238+
239+
expect(BetterInputTagMinOnly.tags.length).toEqual(5)
240+
})
241+
})
242+
243+
describe('validate length of tag', () => {
244+
const BetterInputTagMaxOnly = new ClonedComponent({
245+
propsData: { length: {max: 7} }
246+
}).$mount()
247+
248+
it('should only tags with length <= 5 characters', () => {
249+
BetterInputTagMaxOnly.addNew('foo')
250+
BetterInputTagMaxOnly.addNew('123')
251+
BetterInputTagMaxOnly.addNew('[email protected]')
252+
BetterInputTagMaxOnly.addNew('https://tucci.me')
253+
BetterInputTagMaxOnly.addNew('2002-04-03')
254+
255+
expect(BetterInputTagMaxOnly.tags.length).toEqual(2)
256+
})
257+
})
210258
})

0 commit comments

Comments
 (0)