from starhtml import *
from starui import *
step = Signal("step", 0)
questions = [
{
"signal_name": "survey_q1",
"label": "What is your primary programming language?",
"options": [
{"value": "javascript", "label": "JavaScript/TypeScript"},
{"value": "python", "label": "Python"},
{"value": "java", "label": "Java"},
{"value": "csharp", "label": "C#"},
{"value": "go", "label": "Go"},
{"value": "rust", "label": "Rust"},
{"value": "other", "label": "Other"}
]
},
{
"signal_name": "survey_q2",
"label": "How many years of development experience do you have?",
"options": [
{"value": "0-1", "label": "Less than 1 year"},
{"value": "1-3", "label": "1-3 years"},
{"value": "3-5", "label": "3-5 years"},
{"value": "5-10", "label": "5-10 years"},
{"value": "10+", "label": "More than 10 years"}
]
},
{
"signal_name": "survey_q3",
"label": "Which frontend framework do you prefer?",
"options": [
{"value": "react", "label": "React"},
{"value": "vue", "label": "Vue.js"},
{"value": "angular", "label": "Angular"},
{"value": "svelte", "label": "Svelte"},
{"value": "datastar", "label": "Datastar"},
{"value": "vanilla", "label": "Vanilla JS"},
{"value": "none", "label": "I'm a backend developer"}
]
}
]
signals = {q["signal_name"]: Signal(q["signal_name"], "") for q in questions}
answered_count = Signal("answered_count", sum(signals[q["signal_name"]] != "" for q in questions))
total_questions = len(questions)
at_end = step == total_questions - 1
Card(
CardHeader(
CardTitle(data_text="Question " + (step + 1) + " of " + total_questions),
CardDescription("Web Development Survey")
),
CardContent(
Div(
step,
*signals.values(),
answered_count,
Div(
P(
data_text="Question " + (step + 1) + " of " + total_questions,
cls="text-xs text-muted-foreground mb-2"
),
Div(
Div(
data_style_width=(answered_count / total_questions * 100) + "%",
cls="h-2 bg-primary rounded-full transition-all duration-300"
),
cls="w-full bg-secondary rounded-full h-2 mb-6"
),
cls="mb-4"
),
Div(
*[Div(
RadioGroupWithLabel(
label=q["label"],
options=q["options"],
signal=signals[q["signal_name"]],
required=True
),
data_show=step.eq(i)
) for i, q in enumerate(questions)],
),
Div(
Button(
"Previous",
data_on_click=(step > 0).then(step.sub(1)),
data_show=step > 0,
variant="outline",
),
Button(
data_text=at_end.if_("Complete", "Next"),
data_on_click=[
at_end.then(seq(
step.set(0),
*[signals[q["signal_name"]].set("") for q in questions]
)),
(~at_end).then(step.add(1)),
],
variant="default"
),
cls="flex justify-between mt-6"
)
)
),
cls="w-full max-w-lg"
)