Spaces:
Running
Running
File size: 5,276 Bytes
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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
from dataclasses import asdict
from typing import Literal
import mesop as me
ROW_GAP = 15
BOX_PADDING = 20
@me.stateclass
class State:
first_name: str
last_name: str
username: str
email: str
address: str
address_2: str
country: str
state: str
zip: str
payment_type: str
name_on_card: str
credit_card: str
expiration: str
cvv: str
errors: dict[str, str]
def is_mobile():
return me.viewport_size().width < 620
def calc_input_size(items: int):
return int(
(me.viewport_size().width - (ROW_GAP * items) - (BOX_PADDING * 2)) / items
)
def load(e: me.LoadEvent):
me.set_theme_density(-3)
me.set_theme_mode("system")
@me.page(
security_policy=me.SecurityPolicy(
allowed_iframe_parents=["https://google.github.io", "https://huggingface.co"]
),
path="/form_billing",
on_load=load,
)
def page():
state = me.state(State)
with me.box(
style=me.Style(
padding=me.Padding.all(BOX_PADDING),
max_width=800,
margin=me.Margin.symmetric(horizontal="auto"),
)
):
me.text(
"Billing form",
type="headline-4",
style=me.Style(margin=me.Margin(bottom=10)),
)
with form_group():
name_width = calc_input_size(2) if is_mobile() else "100%"
input_field(label="First name", width=name_width)
input_field(label="Last name", width=name_width)
with form_group():
input_field(label="Username")
with me.box(style=me.Style(display="flex", gap=ROW_GAP)):
input_field(label="Email", input_type="email")
with me.box(style=me.Style(display="flex", gap=ROW_GAP)):
input_field(label="Address")
with form_group():
input_field(label="Address 2")
with form_group():
country_state_zip_width = calc_input_size(3) if is_mobile() else "100%"
input_field(label="Country", width=country_state_zip_width)
input_field(label="State", width=country_state_zip_width)
input_field(label="Zip", width=country_state_zip_width)
divider()
me.text(
"Payment",
type="headline-4",
style=me.Style(margin=me.Margin(bottom=10)),
)
with form_group(flex_direction="column"):
me.radio(
key="payment_type",
on_change=on_change,
options=[
me.RadioOption(label="Credit card", value="credit_card"),
me.RadioOption(label="Debit card", value="debit_card"),
me.RadioOption(label="Paypal", value="paypal"),
],
style=me.Style(
display="flex", flex_direction="column", margin=me.Margin(bottom=20)
),
)
if "payment_type" in state.errors:
me.text(
state.errors["payment_type"],
style=me.Style(
margin=me.Margin(top=-30, left=5, bottom=15),
color=me.theme_var("error"),
font_size=13,
),
)
with form_group():
payments_width = calc_input_size(2) if is_mobile() else "100%"
input_field(label="Name on card", width=payments_width)
input_field(label="Credit card", width=payments_width)
with form_group():
input_field(label="Expiration", width=payments_width)
input_field(label="CVV", width=payments_width, input_type="number")
divider()
me.button(
"Continue to checkout",
type="flat",
style=me.Style(width="100%", padding=me.Padding.all(25), font_size=20),
on_click=on_click,
)
def divider():
with me.box(style=me.Style(margin=me.Margin.symmetric(vertical=20))):
me.divider()
@me.content_component
def form_group(flex_direction: Literal["row", "column"] = "row"):
with me.box(
style=me.Style(
display="flex", flex_direction=flex_direction, gap=ROW_GAP, 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
|