Skip to content

Commit 0339ad8

Browse files
Soviuttmorehouse
authored andcommitted
feat(form-input): Added support for datalists to text form-inputs (bootstrap-vue#2781)
1 parent ee52844 commit 0339ad8

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

src/components/form-input/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,48 @@ On some browsers, scrolling the mousewheel while a numeric-like input is focused
416416
decrement the input's value. To disable this browser feature, just set the `no-wheel` prop to
417417
`true`.
418418

419+
## Datalist support
420+
421+
Datalists are a native HTML tag `<datalist>` that contains a list of `<option>` tags. By assigning
422+
an ID to the datalist tag, the list can be references from a text input by adding a `list`
423+
attribute.
424+
425+
This gives the input the behavior of a combo box or auto-complete, allowing existing values to be
426+
chosen, or new values to be entered.
427+
428+
```html
429+
<template>
430+
<b-form-input list="my-list-id" />
431+
432+
<datalist id="my-list-id">
433+
<option>Manual Option</option>
434+
<option v-for="size in sizes">{{ size }}</option>
435+
</datalist>
436+
</template>
437+
438+
<script>
439+
export default {
440+
data() {
441+
return {
442+
sizes: ['Small', 'Medium', 'Large', 'Extra Large']
443+
}
444+
}
445+
}
446+
</script>
447+
448+
<!-- b-form-input-datalist.vue -->
449+
```
450+
451+
**Notes:**
452+
453+
- Datalists work in conjunction with the browser's built in auto-complete, displaying datalist
454+
options first, followed by auto-complete options. To only display datalist options, set
455+
`autocomplete="off"`.
456+
- Datalists cannot be applied to input fields with `type="password"`.
457+
- Not all browsers fully support `<datalist>` and implementations can be buggy. It is
458+
recommended that datalists be treated as an enhancement and not be relied upon at this time.
459+
Check [Can I Use](https://caniuse.com/#feat=datalist) for full support details on all browsers.
460+
419461
## `v-model` modifiers
420462

421463
Vue does not officially support `.lazy`, `.trim`, and `.number` modifiers on the `v-model` of custom

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ export default {
6363
step: {
6464
type: [String, Number],
6565
default: null
66+
},
67+
list: {
68+
type: String,
69+
default: null
6670
}
6771
},
6872
computed: {
@@ -143,6 +147,7 @@ export default {
143147
min: self.min,
144148
max: self.max,
145149
step: self.step,
150+
list: self.localType !== 'password' ? self.list : null,
146151
'aria-required': self.required ? 'true' : null,
147152
'aria-invalid': self.computedAriaInvalid
148153
},

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,33 @@ describe('form-input', async () => {
124124
expect(input.attributes('form')).toBe('foobar')
125125
})
126126

127+
it('does not have list attribute when list prop not set', async () => {
128+
const wrapper = mount(Input)
129+
const input = wrapper.find('input')
130+
expect(input.attributes('list')).not.toBeDefined()
131+
})
132+
133+
it('has list attribute when list prop set', async () => {
134+
const wrapper = mount(Input, {
135+
propsData: {
136+
list: 'foobar'
137+
}
138+
})
139+
const input = wrapper.find('input')
140+
expect(input.attributes('list')).toBe('foobar')
141+
})
142+
143+
it('does not have list attribute when list prop set and type=password', async () => {
144+
const wrapper = mount(Input, {
145+
propsData: {
146+
list: 'foobar',
147+
type: 'password'
148+
}
149+
})
150+
const input = wrapper.find('input')
151+
expect(input.attributes('list')).not.toBeDefined()
152+
})
153+
127154
it('renders text input by default', async () => {
128155
const wrapper = mount(Input)
129156
const input = wrapper.find('input')

0 commit comments

Comments
 (0)