Skip to content

Commit 0161645

Browse files
committed
[IMP] owl_playground : ch1 rest of the tasks
1 parent e9bbaa7 commit 0161645

File tree

8 files changed

+93
-8
lines changed

8 files changed

+93
-8
lines changed
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
import { Component, useState } from "@odoo/owl";
1+
import { Component, useState, Slot } from "@odoo/owl";
22

33

44
export class Card extends Component {
55
static template = "awesome_owl.card";
66

77
static props = {
88
title: {type: String},
9-
content: {type: String}
9+
slots : { type: Object, optional: true },
10+
}
11+
setup(){
12+
this.state = useState({ show: false });
13+
}
14+
15+
toggleShow(){
16+
this.state.show = !this.state.show;
1017
}
1118
}

awesome_owl/static/src/card/card.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
<div class="card-body">
66
<h5 class="card-title">
77
<t t-out="props.title"/>
8+
<button class="btn btn-primary" t-on-click="toggleShow">Show</button>
89
</h5>
9-
<p class="card-text">
10-
<t t-out="props.content"/>
10+
<p class="card-text" t-if="state.show">
11+
<t t-slot="default"/>
1112
</p>
1213
</div>
1314
</div>

awesome_owl/static/src/playground.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Component, useState, markup } from "@odoo/owl";
22
import { Counter } from "./counter/counter";
33
import { Card } from "./card/card";
4+
import { TodoList } from "./todo/TodoList";
45

56
export class Playground extends Component {
67
static template = "awesome_owl.playground";
7-
static components = { Counter, Card };
8+
static components = { Counter, Card, TodoList };
89
no_markup_value = "<div class='text-primary'>Testing no markup</div>";
9-
markup_value = markup("<div class='text-primary'>Testing markup</div>");
10+
markup_value = markup("<Counter/>");
1011
setup(){
1112
this.state = useState({ sum: 2 });
1213
this.incrementSum = this.incrementSum.bind(this);

awesome_owl/static/src/playground.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77
<Counter callback.bind="incrementSum" />
88
</div>
99
<div>
10-
<Card title="'Task 1'" content="no_markup_value"/>
11-
<Card title="'Task 2'" content="markup_value"/>
10+
<Card title="'Task 1'"/>
11+
<Card title="'Task 2'">
12+
<Counter callback.bind="incrementSum" />
13+
</Card>
1214
</div>
1315
<p>Sum: <t t-esc="state.sum"/></p>
16+
17+
<TodoList/>
1418
</t>
1519
</templates>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Component, useRef, onMounted, useState } from "@odoo/owl";
2+
3+
4+
export class TodoItem extends Component {
5+
static template = "awesome_owl.todo_item";
6+
static props = {
7+
id: {type: Number},
8+
description: {type: String},
9+
isCompleted: {type: Boolean},
10+
onClick : { Type: Function, optional : true },
11+
}
12+
13+
toggleState(){
14+
this.props.isCompleted = !this.props.isCompleted;
15+
}
16+
17+
removeTodo() {
18+
this.props.onClick(this.props.id);
19+
}
20+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<templates xml:space="preserve">
3+
<t t-name="awesome_owl.todo_item">
4+
<p t-att-class="props.isCompleted ? 'text-muted text-decoration-line-through' : ''">
5+
<input type="checkbox"
6+
t-att-checked="props.isCompleted"
7+
t-on-change="toggleState"
8+
/> <t t-out="props.id"/>: <t t-out="props.description"/> Done: <t t-out="props.isCompleted"/> <span class="fa fa-remove" t-on-click="removeTodo"/>
9+
</p>
10+
</t>
11+
</templates>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Component, useState, useRef, onMounted } from "@odoo/owl";
2+
import { TodoItem } from "./TodoItem";
3+
4+
5+
export class TodoList extends Component {
6+
static template = "awesome_owl.todo_list";
7+
static components = { TodoItem };
8+
setup(){
9+
this.todos = useState([]);
10+
this.todoId = 0;
11+
this.inputRef = useRef("new_task");
12+
onMounted(() => {
13+
this.inputRef.el.focus();
14+
})
15+
}
16+
addTodo(e){
17+
if (e.keyCode === 13){
18+
if (this.inputRef.el.value){
19+
this.todoId += 1;
20+
this.todos.push({ id: this.todoId, description: this.inputRef.el.value, isCompleted: false });
21+
}
22+
this.inputRef.el.value = "";
23+
}
24+
}
25+
removeTodo(id){
26+
const index = this.todos.findIndex(todo => todo.id === id);
27+
if (index >= 0) {
28+
this.todos.splice(index, 1);
29+
}
30+
}
31+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<templates xml:space="preserve">
3+
<t t-name="awesome_owl.todo_list">
4+
<input t-ref="new_task" placeholder="Add new task" t-on-keyup="addTodo"/>
5+
<t t-foreach="todos" t-as="i" t-key="i.id">
6+
<TodoItem id="i.id" description="i.description" isCompleted="i.isCompleted" onClick.bind="removeTodo"/>
7+
<br/>
8+
</t>
9+
</t>
10+
</templates>

0 commit comments

Comments
 (0)