livebook/assets/js/focus_on_update/index.js
Jonatan Kłosko 8dea99e600
Support inputs (#328)
* Render basic input

* Make each cell type a separate struct

* Add operation to set input value

* Prototype communication

* Make input name editable

* Refactoring

* Unify cell attribute setters

* IO tests

* Document input communication protocol in Runtime

* Add more tests

* Add persistence

* Fix status update on cell attributes change

* Rework persistence

* Integrate input cell with insert mode

* Add missing users keybinding

* Mimic autofocus for dynamically inserted elements

* Support input type selection

* Support URL input and set up validation

* Convert input error into a more meaningful io error

* Add missing client checks

* Consume every input only once per evaluation

* Fixes
2021-06-08 12:33:50 +02:00

27 lines
506 B
JavaScript

import { isEditableElement } from "../lib/utils";
/**
* A hook used to focus an element whenever it receives LV update.
*/
const FocusOnUpdate = {
mounted() {
this.__focus();
},
updated() {
if (this.el !== document.activeElement) {
this.__focus();
}
},
__focus() {
if (isEditableElement(document.activeElement)) {
return;
}
this.el.focus();
this.el.selectionStart = this.el.selectionEnd = this.el.value.length;
},
};
export default FocusOnUpdate;