Skip to content

Commit

Permalink
Handle native input event
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwillchen committed Dec 16, 2023
1 parent c979f82 commit bf361f0
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 7 deletions.
2 changes: 1 addition & 1 deletion generator/fixtures/component_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
insert_composite_component,
register_event_mapper,
)
from mesop.events import MesopEvent, ClickEvent
from mesop.events import MesopEvent, ClickEvent, InputEvent

# INSERT_EVENTS

Expand Down
11 changes: 11 additions & 0 deletions generator/generate_ng_ts.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ def generate_ts_event_method(prop: pb.OutputProp) -> str:

def generate_ts_native_event_method(native_event: str) -> str:
native_event_name = upper_camel_case(native_event)
if native_event == "input":
return f"""
on{native_event_name}(event: Event): void {{
const userEvent = new UserEvent();
userEvent.setHandlerId(this.config().getOn{native_event_name}HandlerId())
userEvent.setString((event.target as HTMLInputElement).value);
userEvent.setKey(this.key);
this.channel.dispatch(userEvent);
}}
"""

return f"""
on{native_event_name}(event: Event): void {{
const userEvent = new UserEvent();
Expand Down
4 changes: 2 additions & 2 deletions generator/output_data/input.binarypb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

�
input"MatInput*input:matInputRerrorStateMatcher2)
�
input"MatInput*input:matInputJinputRerrorStateMatcher2)
@angular/material/inputMatInputModule22
@angular/material/form-fieldMatFormFieldModuleX
disabledboolean
Expand Down
2 changes: 1 addition & 1 deletion generator/output_data/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"elementName": "input",
"tsFilename": "",
"directiveNamesList": ["matInput"],
"nativeEventsList": [],
"nativeEventsList": ["input"],
"skipPropertyNamesList": ["errorStateMatcher"],
"ngModulesList": [
{
Expand Down
1 change: 1 addition & 0 deletions generator/spec_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const inputSpecInput = (() => {
i.setName('input');
i.setElementName('input');
i.addDirectiveNames('matInput');
i.addNativeEvents('input');
i.setIsFormField(true);
i.addSkipPropertyNames('errorStateMatcher');
return i;
Expand Down
3 changes: 3 additions & 0 deletions mesop/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
from mesop.events import (
ClickEvent as ClickEvent,
)
from mesop.events import (
InputEvent as InputEvent,
)
from mesop.features import page as page
from mesop.key import Key as Key

Expand Down
25 changes: 24 additions & 1 deletion mesop/components/input/e2e/input_app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
import mesop as me


@me.stateclass
class State:
input: str = ""
count: int = 0
checked: bool = False


@me.on(me.InputEvent)
def on_input(e: me.InputEvent):
state = me.state(State)
state.input = e.value


@me.on(me.CheckboxChangeEvent)
def on_change(e: me.CheckboxChangeEvent):
state = me.state(State)
state.checked = e.checked


@me.page(path="/components/input/e2e/input_app")
def app():
me.text(text="Hello, world!")
me.input(label="Basic input")
with me.checkbox(on_change=on_change):
me.text(text="check")
s = me.state(State)
me.input(label="Basic input", on_input=on_input, key=str(s.checked))
me.text(text=s.input)
1 change: 1 addition & 0 deletions mesop/components/input/input.ng.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
[aria-describedby]="config().getUserAriaDescribedBy()"
[value]="config().getValue()"
[readonly]="config().getReadonly()"
(input)="onInput($event)"
/>
</mat-form-field>
}
3 changes: 2 additions & 1 deletion mesop/components/input/input.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ message InputType {
string subscript_sizing = 14;
string hint_label = 15;
string label = 16;
int32 variant_index = 17;
string on_input_handler_id = 17;
int32 variant_index = 18;
}
6 changes: 5 additions & 1 deletion mesop/components/input/input.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from typing import Literal
from typing import Any, Callable, Literal

from pydantic import validate_arguments

import mesop.components.input.input_pb2 as input_pb
from mesop.component_helpers import (
handler_type,
insert_component,
)
from mesop.events import InputEvent


@validate_arguments
Expand All @@ -28,6 +30,7 @@ def input(
subscript_sizing: Literal["fixed", "dynamic"] = "fixed",
hint_label: str = "",
label: str = "",
on_input: Callable[[InputEvent], Any] | None = None,
variant: Literal["matInput"] = "matInput",
):
"""
Expand All @@ -53,6 +56,7 @@ def input(
subscript_sizing=subscript_sizing,
hint_label=hint_label,
label=label,
on_input_handler_id=handler_type(on_input) if on_input else "",
variant_index=_get_variant_index(variant),
),
)
Expand Down
8 changes: 8 additions & 0 deletions mesop/components/input/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ export class InputComponent {
getSubscriptSizing(): 'fixed' | 'dynamic' {
return this.config().getSubscriptSizing() as 'fixed' | 'dynamic';
}

onInput(event: Event): void {
const userEvent = new UserEvent();
userEvent.setHandlerId(this.config().getOnInputHandlerId());
userEvent.setString((event.target as HTMLInputElement).value);
userEvent.setKey(this.key);
this.channel.dispatch(userEvent);
}
}
8 changes: 8 additions & 0 deletions mesop/event_handler/event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,11 @@ def wrapper(action: E):
key=key,
),
)

runtime().register_event_mapper(
events.InputEvent,
lambda userEvent, key: events.InputEvent(
value=userEvent.string,
key=key,
),
)
3 changes: 3 additions & 0 deletions mesop/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
from .events import (
ClickEvent as ClickEvent,
)
from .events import (
InputEvent as InputEvent,
)
from .events import (
MesopEvent as MesopEvent,
)
5 changes: 5 additions & 0 deletions mesop/events/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class ClickEvent(MesopEvent):
pass


@dataclass
class InputEvent(MesopEvent):
value: str


@dataclass
class ChangeEvent(MesopEvent):
value: str

0 comments on commit bf361f0

Please sign in to comment.