-
Notifications
You must be signed in to change notification settings - Fork 2.4k
[IMP] Tutorial on Owl Components 🦉 #802
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
3amo-magdy
wants to merge
2
commits into
odoo:18.0
Choose a base branch
from
odoo-dev:18.0-owl-ammg
base: 18.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from . import controllers | ||
from . import controllers |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** @odoo-module **/ | ||
|
||
import { Component, useState} from "@odoo/owl"; | ||
|
||
export class Card extends Component { | ||
static template = "awesome_owl.card"; | ||
static props = { | ||
title: String, | ||
slots: Object, | ||
// content: String | ||
}; | ||
|
||
setup() { | ||
this.isOpen = useState({value:true}); | ||
} | ||
|
||
toggleCardVisibility() { | ||
this.isOpen.value = !this.isOpen.value; | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<templates xml:space="preserve"> | ||
<t t-name="awesome_owl.card"> | ||
<div class="card d-inline-block m-2" style="width: 18rem;"> | ||
<div class="card-body"> | ||
<h5 class="card-title"> | ||
<t t-esc="props.title"/> | | ||
<button t-on-click="toggleCardVisibility">Toggle</button> | ||
</h5> | ||
|
||
<t t-if="isOpen.value" t-slot="body"> | ||
<p class="card-text"> | ||
Default Content | ||
</p> | ||
</t> | ||
</div> | ||
</div> | ||
</t> | ||
</templates> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** @odoo-module **/ | ||
|
||
import { Component, useState} from "@odoo/owl"; | ||
|
||
export class Counter extends Component { | ||
static template = "awesome_owl.counter"; | ||
static props = { | ||
onChange: {type: Function, optional:true} | ||
} | ||
|
||
|
||
setup() { | ||
this.state = useState({ value: 0 }); | ||
} | ||
|
||
increment() { | ||
this.state.value = this.state.value + 1; | ||
|
||
if(this.props.onChange){ | ||
this.props.onChange() | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<templates xml:space="preserve"> | ||
<t t-name="awesome_owl.counter"> | ||
<span class="me-2">Counter: <t t-esc="state.value"/></span> | ||
<button class="btn btn-primary" t-on-click="increment">Increment</button> | ||
</t> | ||
</templates> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,22 @@ | ||
/** @odoo-module **/ | ||
|
||
import { Component } from "@odoo/owl"; | ||
import { Component, useState, markup} from "@odoo/owl"; | ||
import { Counter } from "./counter/counter" | ||
import { Card } from "./card/card"; | ||
import { TodoList } from "./todo_list/todo_list"; | ||
|
||
|
||
export class Playground extends Component { | ||
static template = "awesome_owl.playground"; | ||
static components = { Counter, Card, TodoList} | ||
html_stuff = markup("<h2>hello</h2>") | ||
|
||
setup() { | ||
this.incrementSum = this.incrementSum.bind(this) | ||
this.state = useState({ sum:0 }); | ||
} | ||
|
||
incrementSum() { | ||
this.state.sum++; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,20 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<templates xml:space="preserve"> | ||
|
||
<t t-name="awesome_owl.playground"> | ||
<div class="p-3"> | ||
hello world | ||
</div> | ||
</t> | ||
<Counter onChange="incrementSum"/> | ||
<Counter onChange="incrementSum"/> | ||
<p>Sum: <t t-esc="state.sum"/></p> | ||
<br/> | ||
|
||
<Card title="'Card 1'" > | ||
</Card> | ||
<Card title="'Card 2'"> | ||
<t t-set-slot="body"> | ||
<Counter/> | ||
</t> | ||
</Card> | ||
<br/> | ||
|
||
<TodoList/> | ||
</t> | ||
</templates> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** @odoo-module **/ | ||
|
||
import { Component, useState, markup} from "@odoo/owl"; | ||
|
||
|
||
export class TodoItem extends Component { | ||
static template = "awesome_owl.todo_item"; | ||
|
||
static props = { | ||
todo: {type: Object, shape: {id: Number, description: String, isCompleted: Boolean}}, | ||
index: Number, | ||
checkboxAction: Function, | ||
removeAction: Function | ||
} | ||
|
||
checkboxChanged() { | ||
this.props.checkboxAction(this.props.index) | ||
} | ||
|
||
removed() { | ||
this.props.removeAction(this.props.index) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<templates xml:space="preserve"> | ||
<t t-name="awesome_owl.todo_item"> | ||
<div t-att-class="{'text-muted': props.todo.isCompleted,'text-decoration-line-through': props.todo.isCompleted}"> | ||
<input type="checkbox" t-att-checked="props.todo.isCompleted" t-on-change='checkboxChanged'/> | ||
<t t-esc="props.index"/>) <t t-esc="props.todo.description"/> | ||
<span class="fa fa-remove" t-on-click="removed"/> | ||
</div> | ||
</t> | ||
</templates> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** @odoo-module **/ | ||
|
||
import { Component, useState, useRef, onMounted} from "@odoo/owl"; | ||
import { TodoItem } from "./todo_item/todo_item" | ||
import { useAutoFocus } from "./../utils" | ||
|
||
|
||
export class TodoList extends Component { | ||
static template = "awesome_owl.todo_list"; | ||
static components = { TodoItem } | ||
|
||
setup() { | ||
this.todos = useState([]); | ||
useAutoFocus('todo_input'); | ||
this.toggleTodoState = this.toggleTodoState.bind(this); | ||
this.removeTodoState = this.removeTodoState.bind(this); | ||
} | ||
|
||
addTodo(ev) { | ||
console.log("creating a Todo") | ||
if(ev.keyCode === 13 && ev.target.value){ | ||
this.todos.push({ id: this.todos.length, description: ev.target.value, isCompleted: false }); | ||
} | ||
} | ||
|
||
toggleTodoState(index) { | ||
const target = this.todos[index]; | ||
target.isCompleted = !target.isCompleted | ||
} | ||
|
||
removeTodoState(index) { | ||
if (index !== -1) { | ||
this.todos.splice(index, 1); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<templates xml:space="preserve"> | ||
<t t-name="awesome_owl.todo_list"> | ||
<input t-ref="todo_input" t-on-keyup="addTodo" placeholder="'Enter a new task'"></input> | ||
<h4>Todo List:</h4> | ||
<t t-foreach="todos" t-as="item" t-key="item_index"> | ||
<TodoItem todo="item" index="item_index" checkboxAction="toggleTodoState" removeAction="removeTodoState"/> | ||
</t> | ||
</t> | ||
</templates> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** @odoo-module **/ | ||
|
||
import { useRef, onMounted } from "@odoo/owl"; | ||
|
||
export function useAutoFocus(ref_name) { | ||
const refProxy = useRef(ref_name) | ||
onMounted(() => { | ||
if (refProxy.el) { | ||
refProxy.el.focus() | ||
} | ||
}) | ||
} | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep an eye on the EOF line