8000 Add event listeners properly (#424) · yannickfunk/pyscript@e95b903 · GitHub
[go: up one dir, main page]

Skip to content

Commit e95b903

Browse files
authored
Add event listeners properly (pyscript#424)
1 parent 82c0d74 commit e95b903

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

pyscriptjs/src/components/pybutton.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,16 @@ export class PyButton extends BaseEvalElement {
5151

5252
this.appendChild(mainDiv);
5353
this.code = this.code.split('self').join(this.mount_name);
54-
let registrationCode = `${this.mount_name} = Element("${mainDiv.id}")`;
54+
let registrationCode = `from pyodide import create_proxy`;
55+
registrationCode += `\n${this.mount_name} = Element("${mainDiv.id}")`;
5556
if (this.code.includes('def on_focus')) {
5657
this.code = this.code.replace('def on_focus', `def on_focus_${this.mount_name}`);
57-
registrationCode += `\n${this.mount_name}< 8000 /span>.element.addEventListener('focus', on_focus_${this.mount_name})`;
58+
registrationCode += `\n${this.mount_name}.element.addEventListener('focus', create_proxy(on_focus_${this.mount_name}))`;
5859
}
5960

6061
if (this.code.includes('def on_click')) {
6162
this.code = this.code.replace('def on_click', `def on_click_${this.mount_name}`);
62-
registrationCode += `\n${this.mount_name}.element.addEventListener('click', on_click_${this.mount_name})`;
63+
registrationCode += `\n${this.mount_name}.element.addEventListener('click', create_proxy(on_click_${this.mount_name}))`;
6364
}
6465

6566
// now that we appended and the element is attached, lets connect with the event handlers

pyscriptjs/src/components/pyinputbox.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ export class PyInputBox extends BaseEvalElement {
3434
// defined for this widget
3535
this.appendChild(mainDiv);
3636
this.code = this.code.split('self').join(this.mount_name);
37-
let registrationCode = `${this.mount_name} = Element("${mainDiv.id}")`;
37+
let registrationCode = `from pyodide import create_proxy`;
38+
registrationCode += `\n${this.mount_name} = Element("${mainDiv.id}")`;
3839
if (this.code.includes('def on_keypress')) {
3940
this.code = this.code.replace('def on_keypress', `def on_keypress_${this.mount_name}`);
40-
registrationCode += `\n${this.mount_name}.element.addEventListener('keypress', on_keypress_${this.mount_name})`;
41+
registrationCode += `\n${this.mount_name}.element.addEventListener('keypress', create_proxy(on_keypress_${this.mount_name}))`;
4142
}
4243

4344
// TODO: For now we delay execution to allow pyodide to load but in the future this

0 commit comments

Comments
 (0)
0