Skip to content

[IMP] awesome_owl: add counter component as sub component to playground #747

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
wants to merge 13 commits into
base: 18.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions awesome_owl/static/src/card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Component } from "@odoo/owl"

export class Card extends Component {
static template = "awesome_owl.card";
static props = {
title: String,
content: String,
}
}
17 changes: 17 additions & 0 deletions awesome_owl/static/src/card.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?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="border:1px solid black; width: 18rem;">
<div class="card-body">
<h5 class="card-title">
<t t-esc="props.title"/>
</h5>
<p class="card-text">
<t t-out="props.content"/>
</p>
</div>
</div>
</t>

</templates>
17 changes: 17 additions & 0 deletions awesome_owl/static/src/counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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.props.onChange();
}
}
12 changes: 12 additions & 0 deletions awesome_owl/static/src/counter.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed as this xml is parsed by either Owl or Qweb which already use UTF-8

Suggested change
<?xml version="1.0" encoding="UTF-8" ?>
<?xml version="1.0" ?>

<templates xml:space="preserve">

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed

Suggested change
<templates xml:space="preserve">
<templates>


<t t-name="awesome_owl.counter">
<div style="border:1px solid black; width: 10rem;">Hello World

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inline styles are not encouraged. You may either use Bootstrap classes directly or create your custom classes and use them.

<p>Counter: <t t-esc="state.value"/>
</p>
<button style="border:0.5px solid black;" class="btn btn-primary" t-on-click="increment">Increment</button>
</div>
</t>

</templates>
19 changes: 16 additions & 3 deletions awesome_owl/static/src/playground.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
/** @odoo-module **/

import { Component } from "@odoo/owl";
import { Component, useState, markup } from "@odoo/owl";
import { Counter } from "./counter";
import { Card } from "./card";
import { TodoList } from "./todolist";

export class Playground extends Component {
static template = "awesome_owl.playground";
static components = { Counter, Card, TodoList };

setup() {
this.state = useState({ sum: 0 });
}

card1Content = markup("<div>Some Text in div Tag</div>");
card2Content = "<div>Some Text in div Tag</div>";

incrementSum() {
this.state.sum++;
}
}
10 changes: 7 additions & 3 deletions awesome_owl/static/src/playground.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
<templates xml:space="preserve">

<t t-name="awesome_owl.playground">
<div class="p-3">
hello world
</div>
<Counter onChange.bind="incrementSum"></Counter>
<Counter onChange.bind="incrementSum"></Counter>
<p>Counters Sum: <t t-esc="state.sum"/>
</p>
<Card title="'my title'" content="card1Content"/>
<Card title="'my title'" content="card2Content"/>
<TodoList/>
</t>

</templates>
9 changes: 9 additions & 0 deletions awesome_owl/static/src/todoitem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Component } from "@odoo/owl";

export class TodoItem extends Component {
static template = "awesome_owl.todoitem";
static props = {
todo: Object,
onToggleState: Function,
}
}
9 changes: 9 additions & 0 deletions awesome_owl/static/src/todoitem.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">

<t t-name="awesome_owl.todoitem">
<input type="checkbox" t-on-change="()=> props.onToggleState(props.todo.id)"/>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<input type="checkbox" t-on-change="()=> props.onToggleState(props.todo.id)"/>
<input type="checkbox" t-on-change="() => props.onToggleState(props.todo.id)"/>

<t t-out="props.todo.id+'. '+props.todo.description"/>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<t t-out="props.todo.id+'. '+props.todo.description"/>
<t t-out="props.todo.id + '. ' + props.todo.description"/>

</t>

</templates>
31 changes: 31 additions & 0 deletions awesome_owl/static/src/todolist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Component, useState } from "@odoo/owl";
import { TodoItem } from "./todoitem";
import { useAutofocus } from "./utils";

export class TodoList extends Component {
static template = "awesome_owl.todolist";
static components = { TodoItem };

setup() {
this.state = useState({
todos: [],
idCounter: 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
idCounter: 0
idCounter: 0,

});
useAutofocus("input_todo");
};

addTodo(ev) {
if (ev.keyCode === 13) {
this.state.idCounter++
this.state.todos.push({
'id': this.state.idCounter, 'description': ev.target.value, 'isCompleted': false

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double quotes :)

});
ev.target.value = "";
}
};

toggleState(todoId) {
let todo = this.state.todos.find(todo => todo.id === todoId);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let todo = this.state.todos.find(todo => todo.id === todoId);
const todo = this.state.todos.find(todo => todo.id === todoId);

todo.isCompleted = !todo.isCompleted;
}
}
16 changes: 16 additions & 0 deletions awesome_owl/static/src/todolist.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">

<t t-name="awesome_owl.todolist">
<br/>
<input t-ref="input_todo" t-on-keyup="ev => this.addTodo(ev)" type="text" placeholder="Enter a new task"/>
<t t-foreach="state.todos" t-as="i" t-key="i.id">

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Convention: foreach -> plural, t-as -> singular.

Suggested change
<t t-foreach="state.todos" t-as="i" t-key="i.id">
<t t-foreach="state.todos" t-as="todo" t-key="todo.id">

<div style="border: 1px solid black">
<div t-att-class="{'text-muted': i.isCompleted, 'text-decoration-line-through': i.isCompleted}">
<TodoItem todo= "i" onToggleState.bind="toggleState"/>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<TodoItem todo= "i" onToggleState.bind="toggleState"/>
<TodoItem todo="todo" onToggleState.bind="toggleState"/>

</div>
</div>
</t>
</t>

</templates>
9 changes: 9 additions & 0 deletions awesome_owl/static/src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useEffect, useRef } from "@odoo/owl";

export function useAutofocus(refName) {
let ref = useRef(refName);
useEffect(
(el) => el && el.focus(),
() => [ref.el]
);
}