Skip to content

Commit 3bb2508

Browse files
Add way for user to specify a min and max length of a tag
1 parent 1fc68d4 commit 3bb2508

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

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('mati@tucci.me')
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('mati@tucci.me')
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('mati@tucci.me')
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)