8000 add handlers to input box as well · robinboot/pyscript@ac64b2a · GitHub
[go: up one dir, main page]

Skip to content

Commit ac64b2a

Browse files
committed
add handlers to input box as well
1 parent 44afda7 commit ac64b2a

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

pyscriptjs/examples/todo-pylist.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
<body>
2121
<py-title>To Do List</py-title>
2222
<py-box widths="2/3;1/3">
23-
<py-inputbox id="new-task-content"></py-inputbox>
23+
<py-inputbox id="new-task-content">
24+
def on_keypress(e):
25+
if (e.code == "Enter"):
26+
add_task()
27+
</py-inputbox>
2428
<py-button id="new-task-btn" label="Add Task!">
2529
def on_click(evt):
2630
task = { "content": new_task_content.value, "done": False, "created_at": dt.now() }
@@ -30,7 +34,6 @@
3034
</py-box>
3135

3236
<py-list id="myList"></py-list>
33-
3437
<py-repl id="my-repl" auto-generate="true"> </py-repl>
3538
</body>
3639
</html>

pyscriptjs/src/components/pyinputbox.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class PyInputBox extends BaseEvalElement {
2323

2424

2525
connectedCallback() {
26-
this.label = htmlDecode(this.innerHTML);
26+
this.code = htmlDecode(this.innerHTML);
2727
this.mount_name = this.id.split("-").join("_");
2828
this.innerHTML = '';
2929

@@ -37,14 +37,23 @@ export class PyInputBox extends BaseEvalElement {
3737

3838
// now that we appended and the element is attached, lets connect with the event handlers
3939
// defined for this widget
40-
this.code = `${this.mount_name} = Element("${ mainDiv.id }")`;
40+
this.appendChild(mainDiv);
41+
this.code = this.code.split("self").join(this.mount_name);
42+
let registrationCode = `${this.mount_name} = Element("${ mainDiv.id }")`;
43+
if (this.code.includes("def on_keypress")){
44+
this.code = this.code.replace("def on_keypress", `def on_keypress_${this.mount_name}`);
45+
registrationCode += `\n${this.mount_name}.element.onkeypress = on_keypress_${this.mount_name}`
46+
}
47+
4148
setTimeout(() => {
4249
this.eval(this.code).then(() => {
43-
console.log('registered handlers');
50+
this.eval(registrationCode).then(() => {
51+
console.log('registered handlers');
52+
});
4453
});
4554
}, 4000);
46-
47-
console.log('py-title connected');
55+
56+
console.log('py-inputbox connected');
4857
}
4958
}
5059

pyscriptjs/src/interpreter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class PyWidgetTheme:
140140
141141
142142
class PyListTemplate:
143-
theme = PyWidgetTheme("flex flex-col-reverse mt-4")
143+
theme = PyWidgetTheme("flex flex-col-reverse mt-8 mx-4")
144144
145145
146146
def __init__(self, parent):

0 commit comments

Comments
 (0)
0