Skip to content

Commit

Permalink
[IMP] awesome_owl: Add input auto-focus using t-ref and useRef
Browse files Browse the repository at this point in the history
• Implemented t-ref and useAutofocus to reference the input field.
• Extracted logic into a useAutofocus hook for better reusability.
  • Loading branch information
maap-odoo committed Feb 21, 2025
1 parent 8f896f5 commit 64134fc
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions awesome_owl/static/src/todo_list/todo_list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Component, useState } from "@odoo/owl";
import { TodoItem } from "./todo_item";
import { useAutofocus } from "../utils";

export class TodoList extends Component {
static template = "awesome_owl.TodoList";
Expand All @@ -8,6 +9,7 @@ export class TodoList extends Component {
setup() {
this.nextId = 1;
this.todos = useState([]);
useAutofocus("input")
}

addTodo(ev) {
Expand Down
2 changes: 1 addition & 1 deletion awesome_owl/static/src/todo_list/todo_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<div class="card-body">
<h5 class="card-title text-primary">Todo List</h5>
<div class="list-group">
<input class="form-control mb-3" type="text" placeholder="Add a todo" t-on-keyup="addTodo"/>
<input class="form-control mb-3" type="text" placeholder="Add a todo" t-on-keyup="addTodo" t-ref="input"/>
<t t-foreach="todos" t-as="todo" t-key="todo.id">
<div class="list-group-item border-0">
<TodoItem todo="todo"/>
Expand Down
8 changes: 8 additions & 0 deletions awesome_owl/static/src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { useRef, onMounted } from "@odoo/owl";

export function useAutofocus(refName) {
const ref = useRef(refName);
onMounted(() => {
ref.el.focus();
});
}

0 comments on commit 64134fc

Please sign in to comment.