-
Notifications
You must be signed in to change notification settings - Fork 2.7k
[IMP] awesome_owl: added counter, card and todo_list components - MOALN #1018
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
base: 18.0
Are you sure you want to change the base?
Changes from all commits
66dedaa
ba6f6cd
55a272d
61f568d
1ae0835
6d6a862
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
| from . import controllers | ||
| from . import controllers |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
| from . import controllers | ||
| from . import controllers |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import {Component, useState} from "@odoo/owl" | ||
|
|
||
|
|
||
| export class Card extends Component { | ||
| static template = "awesome_owl.card"; | ||
|
|
||
| static props = { | ||
| title: String, | ||
| slots: {type: Object, optional: true}, | ||
| }; | ||
|
|
||
| setup() { | ||
| const {title} = this.props; | ||
| this.title = title; | ||
| this.state = useState({isOpened: true}) | ||
| } | ||
|
|
||
| get isOpened() { | ||
| return this.state.isOpened; | ||
| } | ||
|
|
||
| toggle() { | ||
| this.state.isOpened = !this.isOpened; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <?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 p-2" style="width: 18rem;"> | ||
| <div class="d-inline-block"> | ||
| <h3 class="card-title"><t t-esc="title"/></h3> | ||
| <button class="btn btn-primary" t-on-click="toggle">Toggle</button> | ||
| </div> | ||
| <div t-if="isOpened" class="card-body"> | ||
| <t t-slot="default"/> | ||
| </div> | ||
| </div> | ||
| </t> | ||
| </templates> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { Component, useState } from "@odoo/owl"; | ||
|
|
||
| export class Counter extends Component { | ||
| static template = "awesome_owl.counter"; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. You should use PascalCase for the name after the dot. |
||
|
|
||
| static props = { | ||
| onChange: { type: Function, optional: true } | ||
| }; | ||
|
|
||
| setup() { | ||
| this.state = useState({ value: 0 }); | ||
| } | ||
|
|
||
| increment() { | ||
| this.state.value++; | ||
| this.props.onChange(); | ||
| } | ||
| } | ||
| 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.counter"> | ||
| <div class="d-inline-block p-3"> | ||
| Counter: <t t-esc="this.state.value"/> | ||
| <button class="btn btn-primary" t-on-click="increment">Increment</button> | ||
| </div> | ||
| </t> | ||
| </templates> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ import { whenReady } from "@odoo/owl"; | |
| import { mountComponent } from "@web/env"; | ||
| import { Playground } from "./playground"; | ||
|
|
||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That diff is not necessary 😃. We try to do as little change as possible as It can generate a big PR and it can make it complicate to read and understand. |
||
| const config = { | ||
| dev: true, | ||
| name: "Owl Tutorial", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,20 @@ | ||
| /** @odoo-module **/ | ||
| import { Card } from "./card/card" | ||
| import { Component, useState } from "@odoo/owl"; | ||
| import { Counter } from "./counter/counter"; | ||
| import { TodoList } from "./todo_list/todo_list"; | ||
|
|
||
| import { Component } from "@odoo/owl"; | ||
|
|
||
| export class Playground extends Component { | ||
| static template = "awesome_owl.playground"; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They should have used PascalCase 😢. No need to change that. |
||
|
|
||
| static components = { Counter, Card, TodoList }; | ||
|
|
||
| setup() { | ||
| this.state = useState({ sum: 0 }); | ||
| this.incrementSum = this.incrementSum.bind(this); | ||
| } | ||
|
|
||
| incrementSum() { | ||
| this.state.sum++; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,17 @@ | ||
| <?xml version="1.0" encoding="UTF-8" ?> | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <templates xml:space="preserve"> | ||
|
|
||
| <t t-name="awesome_owl.playground"> | ||
| <div class="p-3"> | ||
| hello world | ||
| <Card title="'card1'"> | ||
| <Counter onChange="incrementSum"/> | ||
| </Card> | ||
| <Card title="'card2'"> | ||
| <Counter onChange="incrementSum"/> | ||
| </Card> | ||
| <p>The sum is: <t t-esc="this.state.sum"/></p> | ||
| <br/> | ||
| <br/> | ||
| <TodoList/> | ||
| </div> | ||
| </t> | ||
|
|
||
| </templates> |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,26 @@ | ||||||
| import { Component } from "@odoo/owl" | ||||||
|
|
||||||
|
|
||||||
| export class TodoItem extends Component { | ||||||
| static template = "awesome_owl.todo.item"; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| static props = { | ||||||
| id: Number, | ||||||
| description: String, | ||||||
| isCompleted: Boolean, | ||||||
| toggleState: Function, | ||||||
| deleteTodo: Function, | ||||||
| } | ||||||
|
|
||||||
| get id() { | ||||||
| return this.props.id; | ||||||
| } | ||||||
|
|
||||||
| get description() { | ||||||
| return this.props.description; | ||||||
| } | ||||||
|
|
||||||
| get isCompleted() { | ||||||
| return this.props.isCompleted; | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <templates xml:space="preserve"> | ||
| <t t-name="awesome_owl.todo.item"> | ||
| <p t-att-class="{'text-muted': isCompleted, 'text-decoration-line-through': isCompleted}"> | ||
| <input type="checkbox" t-att-checked="isCompleted" t-on-change="() => props.toggleState(id)"/> | ||
| <t t-esc="id"/>. | ||
| <t t-esc="description"/> | ||
| <span class="fa fa-remove" t-on-click="() => props.deleteTodo(id)"/> | ||
| </p> | ||
| </t> | ||
| </templates> |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,50 @@ | ||||||
| import { Component, useState } from "@odoo/owl" | ||||||
| import { TodoItem } from "./todo_item"; | ||||||
| import { useAutofocus } from "../utils"; | ||||||
|
|
||||||
|
|
||||||
| export class TodoList extends Component { | ||||||
| static template = "awesome_owl.todo.list"; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| static components = { TodoItem }; | ||||||
|
|
||||||
| static idGen = 0; | ||||||
|
|
||||||
| static get id() { | ||||||
| return this.idGen++; | ||||||
| } | ||||||
|
|
||||||
| setup() { | ||||||
| this.todos = useState([]); | ||||||
| useAutofocus('todo_input'); | ||||||
| this.toggleTodo = this.toggleTodo.bind(this); | ||||||
| this.deleteTodo = this.deleteTodo.bind(this); | ||||||
| } | ||||||
|
|
||||||
| addTodo(event) { | ||||||
| if (event.keyCode === 13 && event.target.value) { | ||||||
| this.todos.push( | ||||||
| { | ||||||
| id: this.constructor.id, | ||||||
| description: event.target.value, | ||||||
| isCompleted: false | ||||||
| } | ||||||
| ); | ||||||
| event.target.value = ''; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| toggleTodo(id) { | ||||||
| const idx = this.todos.findIndex((elem) => elem.id === id); | ||||||
| if (idx !== -1) { | ||||||
| this.todos[idx].isCompleted = !this.todos[idx].isCompleted; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| deleteTodo(id) { | ||||||
| const idx = this.todos.findIndex((elem) => elem.id === id); | ||||||
| if (idx !== -1) { | ||||||
| this.todos.splice(idx, 1); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <templates xml:space="preserve"> | ||
| <t t-name="awesome_owl.todo.list"> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
| <div> | ||
| <input placeholder="Enter a new task" t-on-keyup="addTodo" t-ref="todo_input"/> | ||
| </div> | ||
| <div> | ||
| <t t-foreach="todos" t-as="todo" t-key="todo_index"> | ||
| <TodoItem id="todo.id" description="todo.description" isCompleted="todo.isCompleted" | ||
| toggleState="toggleTodo" deleteTodo="deleteTodo"/> | ||
| </t> | ||
| </div> | ||
| </t> | ||
| </templates> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { onMounted, useRef } from "@odoo/owl"; | ||
|
|
||
|
|
||
| export function useAutofocus(name) { | ||
| const ref = useRef(name); | ||
| onMounted(() => { | ||
| ref.el.focus(); | ||
| }); | ||
| } |
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.