autocomplete-fix.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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).Chips(true).DeletableChips(true).Clearable(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) FieldName(v string) (r *VXAutocompleteBuilder) {
  25. b.tag.Attr(web.VFieldName(v)...)
  26. return b
  27. }
  28. func (b *VXAutocompleteBuilder) SelectedItems(v interface{}) (r *VXAutocompleteBuilder) {
  29. b.selectedItems = v
  30. return b
  31. }
  32. func (b *VXAutocompleteBuilder) HasIcon(v bool) (r *VXAutocompleteBuilder) {
  33. b.tag.Attr("has-icon", v)
  34. return b
  35. }
  36. func (b *VXAutocompleteBuilder) Sorting(v bool) (r *VXAutocompleteBuilder) {
  37. b.tag.Attr("sorting", v)
  38. return b
  39. }
  40. func (b *VXAutocompleteBuilder) Items(v interface{}) (r *VXAutocompleteBuilder) {
  41. b.items = v
  42. return b
  43. }
  44. func (b *VXAutocompleteBuilder) ChipColor(v string) (r *VXAutocompleteBuilder) {
  45. b.tag.Attr("chip-color", v)
  46. return b
  47. }
  48. func (b *VXAutocompleteBuilder) ChipTextColor(v string) (r *VXAutocompleteBuilder) {
  49. b.tag.Attr("chip-text-color", v)
  50. return b
  51. }
  52. func (b *VXAutocompleteBuilder) SetDataSource(ds *AutocompleteDataSource) (r *VXAutocompleteBuilder) {
  53. b.tag.Attr("remote-url", ds.RemoteURL)
  54. b.tag.Attr("event-name", ds.EventName)
  55. b.tag.Attr("is-paging", ds.IsPaging)
  56. b.tag.Attr("has-icon", ds.HasIcon)
  57. return b
  58. }
  59. func (b *VXAutocompleteBuilder) MarshalHTML(ctx context.Context) (r []byte, err error) {
  60. if b.items == nil {
  61. b.items = b.selectedItems
  62. }
  63. b.tag.Attr(":items", b.items)
  64. b.tag.Attr(":selected-items", b.selectedItems)
  65. return b.tag.MarshalHTML(ctx)
  66. }
  67. type AutocompleteDataSource struct {
  68. RemoteURL string `json:"remote-url"`
  69. EventName string `json:"event-name"`
  70. IsPaging bool `json:"is-paging"`
  71. HasIcon bool `json:"has-icon"`
  72. }