Skip to content

Commit

Permalink
fix: When setting a placeholder option a select field set the value t…
Browse files Browse the repository at this point in the history
…o an empty string '' to allow for required validation to occur
  • Loading branch information
lucasnetau committed Oct 25, 2023
1 parent cf04f32 commit 924ca5c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/js/control/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ export default class controlSelect extends control {
if (placeholder && isSelect) {
options.push(
this.markup('option', placeholder, {
disabled: null,
selected: null,
disabled: true,
selected: true,
value: '',
}),
)
}
Expand Down
81 changes: 81 additions & 0 deletions tests/control/select.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import controlSelect from '../../src/js/control/select.js'

describe('Test Text Control', () => {
test('test building select element', async () => {
const controlInstance = new controlSelect({
'type': 'select',
'required': false,
'label': 'Select',
'className': 'form-control',
'name': 'test-select',
'multiple': false,
'values': [
{
'label': 'Option 1',
'value': 'option-1',
'selected': true
},
{
'label': 'Option 2',
'value': 'option-2',
'selected': false
},
{
'label': 'Option 3',
'value': 'option-3',
'selected': false
}
]
}, false)
expect(typeof controlInstance).toBe('object')
expect(controlInstance.constructor.name).toBe('controlSelect')

const element = controlInstance.build()
expect(element.constructor.name).toBe('HTMLSelectElement')
expect(element.name).toBe('test-select')
expect(element.validity.valid).toBeTruthy()
})

test('required select with placeholder to be :invalid until an option is selected', async () => {
const controlInstance = new controlSelect({
'type': 'select',
'required': true,
'label': 'Select',
'className': 'form-control',
'placeholder': 'Select an option...',
'name': 'test-select',
'multiple': false,
'values': [
{
'label': 'Option 1',
'value': 'option-1',
'selected': true
},
{
'label': 'Option 2',
'value': 'option-2',
'selected': false
},
{
'label': 'Option 3',
'value': 'option-3',
'selected': false
}
]
}, false)

const element = controlInstance.build()

controlInstance.on('render')(element)

expect(element.constructor.name).toBe('HTMLSelectElement')
expect(element.name).toBe('test-select')
expect(element.value).toBe('')
expect(element.validity.valid).toBeFalsy()
expect(element.options[0].disabled).toBeTruthy()
expect(element.options[0].selected).toBeTruthy()

element.value = 'option-1'
expect(element.validity.valid).toBeTruthy()
})
})

0 comments on commit 924ca5c

Please sign in to comment.