Skip to content

Commit b60ab17

Browse files
authored
Merge pull request #1823 from shentao/1798-fixing-required-true
fix(1798): Required should allow forms to be submitted
2 parents 3ec51ba + fb05213 commit b60ab17

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

src/Multiselect.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
autocomplete="off"
5757
:spellcheck="spellcheck"
5858
:placeholder="placeholder"
59-
:required="required"
59+
:required="isRequired"
6060
:style="inputStyle"
6161
:value="search"
6262
:disabled="disabled"
@@ -404,6 +404,13 @@ export default {
404404
? this.isOpen
405405
: true)
406406
)
407+
},
408+
isRequired () {
409+
if (this.required === false) {
410+
return false
411+
}
412+
// if we have a value, any value, then this isn't required
413+
return this.internalValue.length <= 0
407414
}
408415
}
409416
}

tests/unit/Multiselect.spec.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,4 +1764,64 @@ describe('Multiselect.vue', () => {
17641764
expect(wrapper.emitted()['update:modelValue']).toEqual([['1']])
17651765
})
17661766
})
1767+
describe('required prop', () => {
1768+
test('should not have required value if required is false', () => {
1769+
const wrapper = shallowMount(Multiselect, {
1770+
props: {
1771+
modelValue: [],
1772+
options: ['1', '2', '3', '4', '5'],
1773+
required: false
1774+
}
1775+
})
1776+
expect(wrapper.get('.multiselect__input').attributes('required')).toBeUndefined()
1777+
})
1778+
test('should have required attribute if there is no value', () => {
1779+
const wrapper = shallowMount(Multiselect, {
1780+
props: {
1781+
modelValue: [],
1782+
options: ['1', '2', '3', '4', '5'],
1783+
required: true
1784+
}
1785+
})
1786+
expect(wrapper.get('.multiselect__input').attributes('required')).toEqual('')
1787+
})
1788+
test('should not required attribute if there is a value', () => {
1789+
const wrapper = shallowMount(Multiselect, {
1790+
props: {
1791+
modelValue: ['1'],
1792+
options: ['1', '2', '3', '4', '5'],
1793+
required: true
1794+
}
1795+
})
1796+
expect(wrapper.get('.multiselect__input').attributes('required')).toBeUndefined()
1797+
})
1798+
test('should required if a value is removed', async () => {
1799+
const wrapper = shallowMount(Multiselect, {
1800+
props: {
1801+
modelValue: ['1'],
1802+
options: ['1', '2', '3', '4', '5'],
1803+
required: true,
1804+
'onUpdate:modelValue': (e) => wrapper.setProps({ modelValue: e })
1805+
}
1806+
})
1807+
expect(wrapper.get('.multiselect__input').attributes('required')).toBeUndefined()
1808+
wrapper.vm.removeElement(wrapper.vm.internalValue[0])
1809+
await wrapper.vm.$nextTick()
1810+
expect(wrapper.get('.multiselect__input').attributes('required')).toBe('')
1811+
})
1812+
test('should not required value if a value is set', async () => {
1813+
const wrapper = shallowMount(Multiselect, {
1814+
props: {
1815+
modelValue: [],
1816+
options: ['1', '2', '3', '4', '5'],
1817+
required: true,
1818+
'onUpdate:modelValue': (e) => wrapper.setProps({ modelValue: e })
1819+
}
1820+
})
1821+
expect(wrapper.get('.multiselect__input').attributes('required')).toBe('')
1822+
wrapper.vm.select(wrapper.vm.options[0])
1823+
await wrapper.vm.$nextTick()
1824+
expect(wrapper.get('.multiselect__input').attributes('required')).toBeUndefined()
1825+
})
1826+
})
17671827
})

0 commit comments

Comments
 (0)