File size: 3,886 Bytes
8e04495
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8d6fe1
8e04495
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from dataclasses import asdict
from typing import Literal

import mesop as me

ROW_GAP = 15
BOX_PADDING = 20


def load(e: me.LoadEvent):
  me.set_theme_density(-3)
  me.set_theme_mode("system")


@me.stateclass
class State:
  first_name: str
  last_name: str
  username: str
  email: str
  facebook: str
  google: str
  instagram: str
  tiktok: str
  errors: dict[str, str]


@me.page(
  security_policy=me.SecurityPolicy(
    allowed_iframe_parents=["https://mesop-dev.github.io"]
  ),
  path="/form_profile",
  on_load=load,
)
def page():
  with me.box(
    style=me.Style(
      padding=me.Padding.all(BOX_PADDING),
      max_width=1000,
      margin=me.Margin.symmetric(horizontal="auto"),
    )
  ):
    me.text(
      "Profile",
      type="headline-4",
      style=me.Style(margin=me.Margin(bottom=15)),
    )

    divider()

    with form_group(
      label="Full name",
      description="This will be displayed on your profile.",
    ):
      input_field(label="First name")
      input_field(label="Last name")

    divider()

    with form_group(label="Account", description="This info must be unique."):
      input_field(label="Username")
      input_field(label="Email", input_type="email")

    divider()

    with form_group(
      label="Social media", description="Links to your social media profiles"
    ):
      input_field(label="Facebook", input_type="url")
      input_field(key="google", label="Google+", input_type="url")
      input_field(label="Instagram", input_type="url")
      input_field(label="TikTok", input_type="url")

    divider()

    me.button(
      "Save Profile",
      type="flat",
      on_click=on_click,
      style=me.Style(padding=me.Padding.all(25), font_size=20),
    )


def divider():
  with me.box(style=me.Style(margin=me.Margin.symmetric(vertical=20))):
    me.divider()


@me.content_component
def form_group(
  label: str = "",
  description: str = "",
):
  with me.box(
    style=me.Style(display="flex", gap=25, margin=me.Margin(bottom=20))
  ):
    with me.box(style=me.Style(flex_basis="200px")):
      me.text(label, type="headline-6", style=me.Style(font_weight="bold"))
      me.text(description, style=me.Style(font_style="italic", font_size=13))
    with me.box(
      style=me.Style(
        display="flex", flex_grow=1, flex_direction="column", width="100%"
      )
    ):
      me.slot()


def input_field(
  *,
  key: str = "",
  label: str,
  value: str = "",
  width: str | int = "100%",
  input_type: Literal[
    "color",
    "date",
    "datetime-local",
    "email",
    "month",
    "number",
    "password",
    "search",
    "tel",
    "text",
    "time",
    "url",
    "week",
  ] = "text",
):
  state = me.state(State)
  key = key if key else label.lower().replace(" ", "_")
  with me.box(style=me.Style(flex_grow=1, width=width)):
    me.input(
      key=key,
      label=label,
      value=value,
      appearance="outline",
      color="warn" if key in state.errors else "primary",
      style=me.Style(width=width),
      type=input_type,
      on_blur=on_blur,
    )
    if key in state.errors:
      me.text(
        state.errors[key],
        style=me.Style(
          margin=me.Margin(top=-13, left=15, bottom=15),
          color=me.theme_var("error"),
          font_size=13,
        ),
      )


def on_change(e: me.RadioChangeEvent):
  state = me.state(State)
  setattr(state, e.key, e.value)


def on_blur(e: me.InputBlurEvent):
  state = me.state(State)
  setattr(state, e.key, e.value)


def on_click(e: me.ClickEvent):
  state = me.state(State)

  # Replace with real validation logic.
  errors = {}
  for key, value in asdict(state).items():  # type: ignore
    if key == "error":
      continue
    if not value:
      errors[key] = f"{key.replace('_', ' ').capitalize()} is required"
  state.errors = errors

  # Replace with form processing logic.
  if not state.errors:
    pass