mirror of
https://github.com/tenrok/vue-select.git
synced 2026-06-19 09:50:33 +03:00
feat(1735): add mapKeyPress prop
This commit is contained in:
+48
-30
@@ -4,8 +4,8 @@
|
|||||||
## selectOnKeyCodes <Badge text="v3.3.0+" />
|
## selectOnKeyCodes <Badge text="v3.3.0+" />
|
||||||
|
|
||||||
`selectOnKeyCodes {Array}` is an array of keyCodes that will trigger a typeAheadSelect. Any keyCodes
|
`selectOnKeyCodes {Array}` is an array of keyCodes that will trigger a typeAheadSelect. Any keyCodes
|
||||||
in this array will prevent the default event action and trigger a typeahead select. By default,
|
in this array will prevent the default event action and trigger a typeahead select. By default,
|
||||||
it's just `[13]` for return. For example, maybe you want to tag on a comma keystroke:
|
it's just `[13]` for return. For example, maybe you want to tag on a comma keystroke:
|
||||||
|
|
||||||
<TagOnComma />
|
<TagOnComma />
|
||||||
|
|
||||||
@@ -13,7 +13,8 @@
|
|||||||
|
|
||||||
## mapKeyDown <Badge text="v3.3.0+" />
|
## mapKeyDown <Badge text="v3.3.0+" />
|
||||||
|
|
||||||
Vue Select provides the `map-keydown` Function prop to allow for customizing the components response to
|
Vue Select provides the `mapKeyDown` Function prop to allow for customizing the components response
|
||||||
|
to
|
||||||
keydown events while the search input has focus.
|
keydown events while the search input has focus.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
@@ -29,37 +30,37 @@ By default, the prop is a no–op returning the same object `map` object it rece
|
|||||||
maps keyCodes to handlers: `{ <keyCode>: <callback> }`. Modifying this object can override default
|
maps keyCodes to handlers: `{ <keyCode>: <callback> }`. Modifying this object can override default
|
||||||
functionality, or add handlers for different keys that the component doesn't normally listen for.
|
functionality, or add handlers for different keys that the component doesn't normally listen for.
|
||||||
|
|
||||||
Note that any keyCodes you've added to `selectOnKeyCodes` will be passed to `map-keydown` as well,
|
Note that any keyCodes you've added to `selectOnKeyCodes` will be passed to `mapKeyDown` as well,
|
||||||
so `map-keydown` will always take precedence.
|
so `mapKeyDown` will always take precedence.
|
||||||
|
|
||||||
**Default Handlers**
|
**Default Handlers**
|
||||||
|
|
||||||
```js
|
```js
|
||||||
// delete
|
const defaults = {
|
||||||
8: e => this.maybeDeleteValue()
|
// backspace
|
||||||
|
8: (e) => this.maybeDeleteValue(),
|
||||||
// tab
|
// tab
|
||||||
9: e => this.onTab()
|
9: (e) => this.onTab(),
|
||||||
|
// esc
|
||||||
// enter
|
27: (e) => this.onEscape(),
|
||||||
13: e => {
|
// up.prevent
|
||||||
e.preventDefault();
|
38: (e) => {
|
||||||
return this.typeAheadSelect();
|
e.preventDefault()
|
||||||
}
|
if (!this.open) {
|
||||||
|
this.open = true
|
||||||
// esc
|
return
|
||||||
27: e => this.onEscape()
|
}
|
||||||
|
return this.typeAheadUp()
|
||||||
// up
|
},
|
||||||
38: e => {
|
// down.prevent
|
||||||
e.preventDefault();
|
40: (e) => {
|
||||||
return this.typeAheadUp();
|
e.preventDefault()
|
||||||
}
|
if (!this.open) {
|
||||||
|
this.open = true
|
||||||
// down
|
return
|
||||||
40: e => {
|
}
|
||||||
e.preventDefault();
|
return this.typeAheadDown()
|
||||||
return this.typeAheadDown();
|
},
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -71,3 +72,20 @@ This is example listens for the `@` key, and autocompletes an email address with
|
|||||||
|
|
||||||
<<< @/.vuepress/components/CustomHandlers.vue
|
<<< @/.vuepress/components/CustomHandlers.vue
|
||||||
|
|
||||||
|
## mapKeyPress <Badge text="v3.21+" />
|
||||||
|
|
||||||
|
`mapKeyPress` prop is identical in functionality to `mapKeyDown`, but it's called on `keypress`
|
||||||
|
events instead of `keydown`. The keypress event is used to detect space bar presses when the input
|
||||||
|
has focus, and the menu is closed.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const defaults = {
|
||||||
|
// space
|
||||||
|
32: (e) => {
|
||||||
|
if (!this.open) {
|
||||||
|
e.preventDefault()
|
||||||
|
this.open = true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
+33
-11
@@ -596,7 +596,6 @@ export default {
|
|||||||
* for the search input. Can be used to implement
|
* for the search input. Can be used to implement
|
||||||
* custom behaviour for key presses.
|
* custom behaviour for key presses.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
mapKeydown: {
|
mapKeydown: {
|
||||||
type: Function,
|
type: Function,
|
||||||
/**
|
/**
|
||||||
@@ -607,6 +606,21 @@ export default {
|
|||||||
default: (map, vm) => map,
|
default: (map, vm) => map,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to modify the default keypress events map
|
||||||
|
* for the search input. Can be used to implement
|
||||||
|
* custom behaviour for key presses.
|
||||||
|
*/
|
||||||
|
mapKeyPress: {
|
||||||
|
type: Function,
|
||||||
|
/**
|
||||||
|
* @param map {Object}
|
||||||
|
* @param vm {VueSelect}
|
||||||
|
* @return {Object}
|
||||||
|
*/
|
||||||
|
default: (map, vm) => map,
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Append the dropdown element to the end of the body
|
* Append the dropdown element to the end of the body
|
||||||
* and size/position it dynamically. Use it if you have
|
* and size/position it dynamically. Use it if you have
|
||||||
@@ -1331,19 +1345,17 @@ export default {
|
|||||||
38: (e) => {
|
38: (e) => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
if (!this.open) {
|
if (!this.open) {
|
||||||
this.open = true
|
return (this.open = true)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
return this.typeAheadUp()
|
this.typeAheadUp()
|
||||||
},
|
},
|
||||||
// down.prevent
|
// down.prevent
|
||||||
40: (e) => {
|
40: (e) => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
if (!this.open) {
|
if (!this.open) {
|
||||||
this.open = true
|
return (this.open = true)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
return this.typeAheadDown()
|
this.typeAheadDown()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1359,13 +1371,23 @@ export default {
|
|||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @todo: Probably want to add a mapKeyPress method just like we have for keydown.
|
|
||||||
* @param {KeyboardEvent} e
|
* @param {KeyboardEvent} e
|
||||||
*/
|
*/
|
||||||
onSearchKeyPress(e) {
|
onSearchKeyPress(e) {
|
||||||
if (!this.open && e.keyCode === 32) {
|
const defaults = {
|
||||||
e.preventDefault()
|
// space
|
||||||
this.open = true
|
32: (e) => {
|
||||||
|
if (!this.open) {
|
||||||
|
e.preventDefault()
|
||||||
|
this.open = true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
const handlers = this.mapKeyPress(defaults, this)
|
||||||
|
|
||||||
|
if (typeof handlers[e.keyCode] === 'function') {
|
||||||
|
return handlers[e.keyCode](e)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user