autocomplete-fix.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package vuetifyx
  2. import (
  3. "context"
  4. "github.com/qor5/ui/vuetify"
  5. "github.com/qor5/web"
  6. h "github.com/theplant/htmlgo"
  7. )
  8. type VXAutocompleteBuilder struct {
  9. tag *h.HTMLTagBuilder
  10. selectedItems interface{}
  11. items interface{}
  12. }
  13. func VXAutocomplete(children ...h.HTMLComponent) (r *VXAutocompleteBuilder) {
  14. r = &VXAutocompleteBuilder{
  15. tag: h.Tag("vx-autocomplete").Children(children...),
  16. }
  17. r.Multiple(true)
  18. return
  19. }
  20. func (b *VXAutocompleteBuilder) ErrorMessages(v ...string) (r *VXAutocompleteBuilder) {
  21. vuetify.SetErrorMessages(b.tag, v)
  22. return b
  23. }
  24. func (b *VXAutocompleteBuilder) Items(v interface{}) (r *VXAutocompleteBuilder) {
  25. b.items = v
  26. return b
  27. }
  28. func (b *VXAutocompleteBuilder) FieldName(v string) (r *VXAutocompleteBuilder) {
  29. b.tag.Attr(web.VFieldName(v)...)
  30. return b
  31. }
  32. func (b *VXAutocompleteBuilder) SelectedItems(v interface{}) (r *VXAutocompleteBuilder) {
  33. b.selectedItems = v
  34. return b
  35. }
  36. func (b *VXAutocompleteBuilder) SetDataSource(ds *AutocompleteDataSource) (r *VXAutocompleteBuilder) {
  37. b.tag.Attr("remote-url", ds.RemoteURL)
  38. b.tag.Attr("event-name", ds.EventName)
  39. b.tag.Attr("is-paging", ds.IsPaging)
  40. b.tag.Attr("has-icon", ds.HasIcon)
  41. return b
  42. }
  43. func (b *VXAutocompleteBuilder) MarshalHTML(ctx context.Context) (r []byte, err error) {
  44. if b.items == nil {
  45. b.items = b.selectedItems
  46. }
  47. b.tag.Attr(":items", b.items)
  48. b.tag.Attr(":selected-items", b.selectedItems)
  49. return b.tag.MarshalHTML(ctx)
  50. }
  51. type AutocompleteDataSource struct {
  52. RemoteURL string `json:"remote-url"`
  53. EventName string `json:"event-name"`
  54. IsPaging bool `json:"is-paging"`
  55. HasIcon bool `json:"has-icon"`
  56. }