Skip to content

Commit 805a7fe

Browse files
authored
fix(form-state): explicitly handle when state is set to empty string. fixes #2166 (#2167)
* fix(form-state): explicitly handle when state is set to empty string state was inadvertently being flagged as valid (true) when state was set to an empty string. fixes #2166 * test for state="" * Update form-state.js * Update form-state.js * Update form-input.spec.js
1 parent 2709902 commit 805a7fe

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,17 @@ describe('form-input', async () => {
124124
expect(input.classes()).not.toContain('is-invalid')
125125
})
126126

127+
it('does not have is-valid or is-invalid classes when state=""', async () => {
128+
const wrapper = mount(Input, {
129+
propsData: {
130+
state: ''
131+
}
132+
})
133+
const input = wrapper.find('input')
134+
expect(input.classes()).not.toContain('is-valid')
135+
expect(input.classes()).not.toContain('is-invalid')
136+
})
137+
127138
it('has class is-valid when state=true', async () => {
128139
const wrapper = mount(Input, {
129140
propsData: {
@@ -135,6 +146,17 @@ describe('form-input', async () => {
135146
expect(input.classes()).not.toContain('is-invalid')
136147
})
137148

149+
it('has class is-valid when state="valid"', async () => {
150+
const wrapper = mount(Input, {
151+
propsData: {
152+
state: 'valid'
153+
}
154+
})
155+
const input = wrapper.find('input')
156+
expect(input.classes()).toContain('is-valid')
157+
expect(input.classes()).not.toContain('is-invalid')
158+
})
159+
138160
it('has class is-invalid when state=false', async () => {
139161
const wrapper = mount(Input, {
140162
propsData: {
@@ -146,6 +168,17 @@ describe('form-input', async () => {
146168
expect(input.classes()).not.toContain('is-valid')
147169
})
148170

171+
it('has class is-invalid when state="invalid"', async () => {
172+
const wrapper = mount(Input, {
173+
propsData: {
174+
state: 'invalid'
175+
}
176+
})
177+
const input = wrapper.find('input')
178+
expect(input.classes()).toContain('is-invalid')
179+
expect(input.classes()).not.toContain('is-valid')
180+
})
181+
149182
it('does not have aria-invalid attribute by default', async () => {
150183
const wrapper = mount(Input)
151184
expect(wrapper.contains('[aria-invalid]')).toBe(false)

src/mixins/form-state.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ export default {
1111
props: {
1212
state: {
1313
// true/'valid', false/'invalid', '',null
14-
type: [Boolean, String],
14+
// The order must be String first, then Boolean!
15+
type: [String, Boolean],
1516
default: null
1617
}
1718
},
1819
computed: {
1920
computedState () {
2021
const state = this.state
21-
if (state === true || state === 'valid') {
22+
if (state === '') {
23+
return null
24+
} else if (state === true || state === 'valid') {
2225
return true
2326
} else if (state === false || state === 'invalid') {
2427
return false

0 commit comments

Comments
 (0)