from textual.app import App, ComposeResult, on
from textual_wtf import Form, StringField, IntegerField, BooleanField
from textual_wtf import EmailValidator

class ContactForm(Form):
    title = "Contact Us"

    name    = StringField("Name", required=True, min_length=2)
    email   = StringField("Email", required=True, validators=[EmailValidator()])
    age     = IntegerField("Age", minimum=0, maximum=120)
    updates = BooleanField("Subscribe to updates")


class ContactApp(App):
    def compose(self) -> ComposeResult:
        self.form = ContactForm()
        yield from self.form.layout()

    @on(ContactForm.Submitted)
    def on_submitted(self, event: ContactForm.Submitted) -> None:
        data = event.form.get_data()
        self.notify(f"Submitted: {data}")

    @on(ContactForm.Cancelled)
    def on_cancelled(self, event: ContactForm.Cancelled) -> None:
        self.app.exit()


if __name__ == "__main__":
    ContactApp().run()