From 3bd1ae9d63dbcb53dc518b380105759519a2ec1f Mon Sep 17 00:00:00 2001 From: Dima Date: Thu, 30 May 2024 16:11:26 +0200 Subject: [PATCH] added `)"; + std::string input_type = Exists(f.type) ? Value(f.type) : "text"; + if (input_type == "select" && Exists(f.options) && !Value(f.options).empty()) { + std::string const default_option = Exists(f.default_option) ? Value(f.default_option) : Value(f.options).front(); oss << R"( - disabled=disabled)"; - } - oss << R"( - autocomplete=false - id=")" - << f.id << '"'; - if (Exists(f.placeholder)) { + +
)"; + } else { + oss << R"( + +
)"; } - oss << R"( - size="100" - /> -
)"; } oss << R"(
diff --git a/src/lib_c5t_htmlform.h b/src/lib_c5t_htmlform.h index f158223..00f561d 100644 --- a/src/lib_c5t_htmlform.h +++ b/src/lib_c5t_htmlform.h @@ -25,7 +25,9 @@ CURRENT_STRUCT(Field) { CURRENT_FIELD(text, Optional); CURRENT_FIELD(placeholder, Optional); CURRENT_FIELD(value, Optional); - CURRENT_FIELD(type, Optional); + CURRENT_FIELD(type, Optional); // can be unset, "readonly", "password", or "select" + CURRENT_FIELD(options, Optional>); + CURRENT_FIELD(default_option, Optional); CURRENT_CONSTRUCTOR(Field)(std::string id = "id") : id(std::move(id)) {} @@ -49,6 +51,18 @@ CURRENT_STRUCT(Field) { type = "password"; return *this; } + Field& Select(std::vector arg_options) { + type = "select"; + options = std::move(arg_options); + default_option = nullptr; + return *this; + } + Field& Select(std::vector arg_options, std::string arg_default_option) { + type = "select"; + options = std::move(arg_options); + default_option = std::move(arg_default_option); + return *this; + } }; CURRENT_STRUCT(Form) { diff --git a/src/wss.cc b/src/wss.cc index 63a1721..f0a415c 100644 --- a/src/wss.cc +++ b/src/wss.cc @@ -14,8 +14,9 @@ CURRENT_STRUCT(StopResponseSchema) { CURRENT_FIELD(msg, std::string); }; CURRENT_STRUCT(SumResponseSchema) { - CURRENT_FIELD(sum, int64_t); + CURRENT_FIELD(result, int64_t); CURRENT_FIELD(pw, Optional); + CURRENT_FIELD(op, Optional); }; inline std::string BasePathOf(std::string const& s) { @@ -124,6 +125,7 @@ int main(int argc, char** argv) { using namespace current::htmlform; if (r.method == "GET") { auto const f = Form() + .Add(Field("e").Text("Operation").Select({"mul", "add"}, "add")) .Add(Field("a").Text("First summand").Placeholder("3 for example").Value("3")) .Add(Field("b").Text("Second summand").Placeholder("4 for example")) .Add(Field("c").Text("Read-only caption").Value("Read-only text").Readonly()) @@ -136,7 +138,7 @@ int main(int argc, char** argv) { if (isNaN(a)) return { error: "A is not a number." }; const b = parseInt(input.b); if (isNaN(b)) return { error: "B is not a number." }; - return { sum: a + b, pw: String(input.d) }; + return { result: (input.e === "mul" ? a * b : a + b), pw: String(input.d), op: input.e }; })"); r(FormAsHTML(f), HTTPResponseCode.OK, current::net::constants::kDefaultHTMLContentType); } else { @@ -146,7 +148,10 @@ int main(int argc, char** argv) { if (Exists(body.pw)) { std::cout << "PW: " << Value(body.pw) << std::endl; } - res.fwd = "/is/" + current::ToString(body.sum); + if (Exists(body.op)) { + std::cout << "OP: " << Value(body.op) << std::endl; + } + res.fwd = "/is/" + current::ToString(body.result); } catch (current::Exception& e) { res.msg = "error!"; } @@ -155,7 +160,7 @@ int main(int argc, char** argv) { }); routes += http.Register( - "/sum/is", URLPathArgs::CountMask::One, [](Request r) { r("the sum is " + r.url_path_args[0] + '\n'); }); + "/sum/is", URLPathArgs::CountMask::One, [](Request r) { r("the result is " + r.url_path_args[0] + '\n'); }); routes += http.Register("/dlib", [](Request r) { std::ostringstream oss;