Skip to content

Commit f7f4d95

Browse files
committed
migration to OWL 2
1 parent 562fc06 commit f7f4d95

File tree

4 files changed

+43
-37
lines changed

4 files changed

+43
-37
lines changed

src/Root.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ const ROOT_TEMPLATE = xml/*xml*/ `
1111
<div class="w-full flex items-center justify-between">
1212
<a class="flex items-center text-reddish-brown no-underline hover:no-underline font-bold text-2xl lg:text-4xl" href="#">
1313
<img class="h-8 mx-auto lg:mr-0" src="owl.svg"/>
14-
OWL Tailwind App Starter Project
14+
OWL TypeScript, Vite, Tailwind Starter Project
1515
</a>
16-
<div class="flex w-1/2 justify-end content-center">
16+
<div class="flex w-1/3 justify-end content-center">
1717
<a class="inline-block text-center h-10 p-2 md:h-auto md:p-4" href="https://github.com/Coding-Dodo/OWL-JavaScript-Tailwind-Project-Starter">
1818
<img class="w-2/6 mx-auto lg:mr-0" src="github-logo.svg"/>
1919
</a>
@@ -31,7 +31,13 @@ const ROOT_TEMPLATE = xml/*xml*/ `
3131
<h2 class="mb-4 text-base text-gray-700 font-bold leading-tight text-center md:text-left slide-in-bottom-h1">
3232
Dropdown Component
3333
</h2>
34-
<DropDown buttonText="'OWL Dropdown'" menuItems="state.menuItems" dropdownLeft="true" selectedMenu="state.selectedMenu">
34+
<DropDown
35+
buttonText="'OWL Dropdown'"
36+
menuItems="state.menuItems"
37+
dropdownLeft="true"
38+
value="state.selectedMenu"
39+
onSelectMenuItem.bind="selectMenuItem"
40+
>
3541
</DropDown>
3642
</div>
3743
<div class="block py-6 fit-content">
@@ -41,7 +47,7 @@ const ROOT_TEMPLATE = xml/*xml*/ `
4147
<AutoCompleteInput
4248
data="state.searchData"
4349
value="state.selectedTechnology"
44-
t-on-chosen="onOptionChosen"
50+
onOptionChosen.bind="onOptionChosen"
4551
inputClass="'border border-gray-300 py-2 px-3 rounded-md focus:outline-none focus:shadow-outline w-full'"
4652
placeHolder="'Type something...'"
4753
/>
@@ -70,7 +76,6 @@ state.selectedTechnology: <t t-esc="toJsonString(state.selectedTechnology)"/>
7076
export type MenuItem = {
7177
id: string;
7278
name: string;
73-
callBack?: (menuItem: MenuItem) => void;
7479
};
7580

7681
export type RootState = {
@@ -87,17 +92,14 @@ export class Root extends Component {
8792
{
8893
id: "item-1",
8994
name: "OWL",
90-
callBack: (menuItem: MenuItem) => this.clickMenu(menuItem),
9195
},
9296
{
9397
id: "item-2",
9498
name: "Rollup.js",
95-
callBack: (menuItem: MenuItem) => this.clickMenu(menuItem),
9699
},
97100
{
98101
id: "item-3",
99102
name: "TailwindCSS",
100-
callBack: (menuItem: MenuItem) => this.clickMenu(menuItem),
101103
},
102104
],
103105
selectedMenu: undefined,
@@ -115,7 +117,7 @@ export class Root extends Component {
115117
selectedTechnology: undefined,
116118
});
117119

118-
clickMenu(menuItem: MenuItem) {
120+
selectMenuItem(menuItem: MenuItem) {
119121
this.state.selectedMenu = menuItem;
120122
}
121123

src/components/AutoCompleteInput.ts

+19-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { Component, useState, xml, useExternalListener, useRef } from "@odoo/owl";
1+
import {
2+
Component,
3+
useState,
4+
xml,
5+
useExternalListener,
6+
useRef,
7+
} from "@odoo/owl";
28

39
const AUTO_COMPLETE_INPUT_TEMPLATE = xml/*xml*/ `
410
<div class="relative" v-click-outside="clickedOutside">
@@ -15,7 +21,7 @@ const AUTO_COMPLETE_INPUT_TEMPLATE = xml/*xml*/ `
1521
/>
1622
<span
1723
t-if="props.value"
18-
t-on-click.prevent="reset()"
24+
t-on-click.prevent="reset"
1925
class="absolute inset-y-0 right-0 pr-3 flex items-center cursor-pointer"
2026
>
2127
x
@@ -51,10 +57,10 @@ export class AutoCompleteInput extends Component {
5157
type: Array,
5258
},
5359
value: { name: "", optional: true },
60+
onOptionChosen: { type: Function, optional: true },
5461
};
5562
state = useState({
5663
showOptions: false,
57-
chosenOption: "",
5864
searchTerm: "",
5965
});
6066
autoCompleteInputRef = useRef("autoCompleteInputRef");
@@ -64,18 +70,15 @@ export class AutoCompleteInput extends Component {
6470
}
6571

6672
hideOptions(event: Event) {
67-
console.log("hide Options", this.autoCompleteInputRef.el);
6873
if (!this.autoCompleteInputRef.el?.contains(event.target as Node)) {
69-
Object.assign(this.state, { showOptions: false });
74+
this.state.showOptions = false;
7075
}
7176
}
7277

7378
reset() {
74-
// this.trigger("input", "");
75-
// this.trigger("chosen", { selectedTechnology: null });
76-
this.state.chosenOption = "";
7779
this.state.searchTerm = "";
7880
this.state.showOptions = false;
81+
this.props.onOptionChosen(null);
7982
}
8083

8184
handleShowOptions(evt: Event) {
@@ -90,18 +93,16 @@ export class AutoCompleteInput extends Component {
9093
if (!this.state.searchTerm) {
9194
return this.props.data;
9295
}
93-
return this.props.data.filter((item: { name: string; optional: boolean }) => {
94-
return item.name.toLowerCase().includes(this.state.searchTerm.toLowerCase());
95-
});
96+
return this.props.data.filter(
97+
(item: { name: string; optional: boolean }) => {
98+
return item.name
99+
.toLowerCase()
100+
.includes(this.state.searchTerm.toLowerCase());
101+
}
102+
);
96103
}
97104

98105
handleClick(item: { name: string; optional: boolean }) {
99-
// this.trigger("input", item.name);
100-
// this.trigger("chosen", { selectedTechnology: item });
101-
Object.assign(this.state, {
102-
chosenOption: item.name,
103-
showOptions: false,
104-
searchTerm: item.name,
105-
});
106+
this.props.onOptionChosen(item);
106107
}
107108
}

src/components/DropDown.ts

+13-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ const DROPDOWN_TEMPLATE = xml/*xml*/ `
1010
<div t-ref="DropDown" class="relative inline-block text-left">
1111
<div>
1212
<button type="button" class="inline-flex justify-center w-full rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-sm font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-100 focus:ring-indigo-500" id="menu-button" aria-expanded="true" aria-haspopup="true" t-on-click.prevent="toggleDropDown">
13-
<t t-esc="props.buttonText"/>
13+
<t t-if="props.value">
14+
<t t-esc="props.value.name"/>
15+
</t>
16+
<t t-else="">
17+
<t t-esc="props.buttonText"/>
18+
</t>
1419
<svg class="-mr-1 ml-2 h-5 w-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
1520
<path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd" />
1621
</svg>
@@ -21,9 +26,9 @@ const DROPDOWN_TEMPLATE = xml/*xml*/ `
2126
<t t-foreach="props.menuItems" t-as="menuItem" t-key="menuItem.id">
2227
<t t-slot="menu-item">
2328
<a
24-
t-on-click="() => onClickMenuItem.bind(this)(menuItem)"
29+
t-on-click="() => this.onClickMenuItem(menuItem)"
2530
t-attf-href="{{menuItem.target ? menuItem.target : '#'}}"
26-
t-attf-class="hover:bg-gray-100 hover:text-gray-900 text-gray-700 block px-4 py-2 text-sm {{ props.selectedMenu and props.selectedMenu.id == menuItem.id ? 'bg-gray-100 text-gray-900': ''}}"
31+
t-attf-class="hover:bg-gray-100 hover:text-gray-900 text-gray-700 block px-4 py-2 text-sm {{ props.value and props.value.id == menuItem.id ? 'bg-gray-100 text-gray-900': ''}}"
2732
role="menuitem"
2833
tabindex="-1"
2934
t-att-id="menuItem.id"
@@ -40,7 +45,6 @@ const DROPDOWN_TEMPLATE = xml/*xml*/ `
4045
export type MenuItem = {
4146
id: string;
4247
name: string;
43-
callBack: (menuItem: MenuItem) => void;
4448
};
4549

4650
export class DropDown extends Component {
@@ -54,10 +58,10 @@ export class DropDown extends Component {
5458
id: String,
5559
target: { type: String, optional: true },
5660
name: String,
57-
callBack: { type: Function, optional: true },
5861
},
5962
},
60-
selectedMenu: {
63+
onSelectMenuItem: { type: Function, optional: true },
64+
value: {
6165
type: Object,
6266
optional: true,
6367
},
@@ -73,16 +77,16 @@ export class DropDown extends Component {
7377

7478
closeDropDown(event: Event) {
7579
if (!this.dropdownContainerRef.el?.contains(event.target as Node)) {
76-
Object.assign(this.state, { open: false });
80+
this.state.open = false;
7781
}
7882
}
7983

8084
toggleDropDown() {
81-
Object.assign(this.state, { open: !this.state.open });
85+
this.state.open = !this.state.open;
8286
}
8387

8488
onClickMenuItem(menuItem: MenuItem) {
85-
menuItem.callBack(menuItem);
89+
this.props.onSelectMenuItem(menuItem);
8690
this.state.open = false;
8791
}
8892
}

src/components/MyComponent.ts

-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,5 @@ export class MyComponent extends Component {
1212

1313
updateState() {
1414
this.state.text = this.state.text === "Owl" ? "World" : "Owl";
15-
this.env.router.navigate("/about");
1615
}
1716
}

0 commit comments

Comments
 (0)