2021-06-08 18:33:50 +08:00
|
|
|
import { isEditableElement } from "../lib/utils";
|
|
|
|
|
2021-02-21 23:54:44 +08:00
|
|
|
/**
|
|
|
|
* A hook used to focus an element whenever it receives LV update.
|
|
|
|
*/
|
|
|
|
const FocusOnUpdate = {
|
|
|
|
mounted() {
|
2022-03-16 18:33:53 +08:00
|
|
|
this.focus();
|
2021-02-21 23:54:44 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
updated() {
|
2021-05-21 07:59:00 +08:00
|
|
|
if (this.el !== document.activeElement) {
|
2022-03-16 18:33:53 +08:00
|
|
|
this.focus();
|
2021-05-21 07:59:00 +08:00
|
|
|
}
|
2021-02-21 23:54:44 +08:00
|
|
|
},
|
|
|
|
|
2022-03-16 18:33:53 +08:00
|
|
|
focus() {
|
2021-06-08 18:33:50 +08:00
|
|
|
if (isEditableElement(document.activeElement)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-21 23:54:44 +08:00
|
|
|
this.el.focus();
|
|
|
|
this.el.selectionStart = this.el.selectionEnd = this.el.value.length;
|
2021-12-04 23:29:14 +08:00
|
|
|
this.el.scrollLeft = this.el.scrollWidth;
|
2021-02-21 23:54:44 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export default FocusOnUpdate;
|