page.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package e22_vuetify_variant_sub_form
  2. // @snippet_begin(VuetifyVariantSubForm)
  3. import (
  4. "github.com/qor5/docs/docsrc/utils"
  5. . "github.com/qor5/ui/vuetify"
  6. "github.com/qor5/web"
  7. h "github.com/theplant/htmlgo"
  8. )
  9. type myFormValue struct {
  10. Type string
  11. Form1 struct {
  12. Gender string
  13. }
  14. Form2 struct {
  15. Feature1 bool
  16. Slider1 int
  17. }
  18. }
  19. func VuetifyVariantSubForm(ctx *web.EventContext) (pr web.PageResponse, err error) {
  20. var fv myFormValue
  21. ctx.MustUnmarshalForm(&fv)
  22. if fv.Type == "" {
  23. fv.Type = "Type1"
  24. }
  25. var verr web.ValidationErrors
  26. pr.Body = VContainer(
  27. utils.PrettyFormAsJSON(ctx),
  28. VSelect().
  29. Items([]string{
  30. "Type1",
  31. "Type2",
  32. }).
  33. Value(fv.Type).
  34. Attr("@change", web.POST().
  35. FieldValue("Type", web.Var("$event")).
  36. EventFunc("switchForm").
  37. Go()),
  38. web.Portal(
  39. h.If(fv.Type == "Type1",
  40. form1(ctx, &fv),
  41. ).Else(
  42. form2(ctx, &fv, &verr),
  43. ),
  44. ).Name("subform"),
  45. VBtn("Submit").OnClick("submit"),
  46. )
  47. return
  48. }
  49. func form1(ctx *web.EventContext, fv *myFormValue) h.HTMLComponent {
  50. return VContainer(
  51. h.H1("Form1"),
  52. VRadioGroup(
  53. VRadio().Value("F").Label("Female"),
  54. VRadio().Value("M").Label("Male"),
  55. ).FieldName("Form1.Gender").
  56. Value(fv.Form1.Gender).
  57. Label("Gender"),
  58. )
  59. }
  60. func form2(ctx *web.EventContext, fv *myFormValue, verr *web.ValidationErrors) h.HTMLComponent {
  61. return VContainer(
  62. h.H1("Form2"),
  63. VSwitch().
  64. FieldName("Form2.Feature1").
  65. InputValue(fv.Form2.Feature1).
  66. Label("Feature1"),
  67. VSlider().FieldName("Form2.Slider1").
  68. ErrorMessages(verr.GetFieldErrors("Slider1")...).
  69. Value(fv.Form2.Slider1).
  70. Label("Slider1"),
  71. )
  72. }
  73. func submit(ctx *web.EventContext) (r web.EventResponse, err error) {
  74. r.Reload = true
  75. return
  76. }
  77. func switchForm(ctx *web.EventContext) (r web.EventResponse, err error) {
  78. var verr web.ValidationErrors
  79. var fv myFormValue
  80. ctx.MustUnmarshalForm(&fv)
  81. form := form1(ctx, &fv)
  82. if fv.Type == "Type2" {
  83. form = form2(ctx, &fv, &verr)
  84. }
  85. r.UpdatePortals = append(r.UpdatePortals, &web.PortalUpdate{
  86. Name: "subform",
  87. Body: form,
  88. })
  89. return
  90. }
  91. var VuetifyVariantSubFormPB = web.Page(VuetifyVariantSubForm).
  92. EventFunc("switchForm", switchForm).
  93. EventFunc("submit", submit)
  94. const VuetifyVariantSubFormPath = "/samples/vuetify-variant-sub-form"
  95. // @snippet_end