picker.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package vuetifyx
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/qor5/ui/vuetify"
  6. "github.com/qor5/web"
  7. h "github.com/theplant/htmlgo"
  8. )
  9. type PickerBuilder struct {
  10. value interface{}
  11. label string
  12. fieldName string
  13. comp h.MutableAttrHTMLComponent
  14. }
  15. func Picker(c h.MutableAttrHTMLComponent) (r *PickerBuilder) {
  16. r = &PickerBuilder{comp: c}
  17. return
  18. }
  19. func (b *PickerBuilder) Value(v interface{}) (r *PickerBuilder) {
  20. b.value = v
  21. return b
  22. }
  23. func (b *PickerBuilder) Label(v string) (r *PickerBuilder) {
  24. b.label = v
  25. return b
  26. }
  27. func (b *PickerBuilder) FieldName(v string) (r *PickerBuilder) {
  28. b.fieldName = v
  29. return b
  30. }
  31. func (b *PickerBuilder) MarshalHTML(ctx context.Context) ([]byte, error) {
  32. menuLocal := fmt.Sprintf("picker_%s_menu", b.fieldName)
  33. valueLocal := fmt.Sprintf("picker_%s_value", b.fieldName)
  34. b.comp.SetAttr("@change", fmt.Sprintf(`locals.%s = false; locals.%s = $event; $plaid().form(plaidForm).fieldValue(%s, $event)`, menuLocal, valueLocal, h.JSONString(b.fieldName)))
  35. return web.Scope(
  36. vuetify.VMenu(
  37. web.Slot(
  38. vuetify.VTextField().
  39. Label(b.label).
  40. Attr(web.VFieldName(b.fieldName)...).
  41. Value(b.value).
  42. Readonly(true).
  43. PrependIcon("edit_calendar").
  44. Attr("v-model", fmt.Sprintf("locals.%s", valueLocal)).
  45. Attr("v-bind", "attrs").
  46. Attr("v-on", "on"),
  47. ).Name("activator").Scope("{ on, attrs }"),
  48. b.comp,
  49. ).Attr("v-model", fmt.Sprintf("locals.%s", menuLocal)).
  50. CloseOnContentClick(false).
  51. MaxWidth(290),
  52. ).Init(fmt.Sprintf(`{%s: %s, %s: false}`, valueLocal, h.JSONString(b.value), menuLocal)).
  53. VSlot("{ locals }").
  54. MarshalHTML(ctx)
  55. }