readonly-field.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package vuetifyx
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/qor5/ui/vuetify"
  6. h "github.com/theplant/htmlgo"
  7. )
  8. type VXReadonlyFieldBuilder struct {
  9. label string
  10. value interface{}
  11. children h.HTMLComponents
  12. checkbox bool
  13. }
  14. func VXReadonlyField(children ...h.HTMLComponent) *VXReadonlyFieldBuilder {
  15. b := &VXReadonlyFieldBuilder{}
  16. if len(children) > 0 {
  17. b.children = children
  18. }
  19. return b
  20. }
  21. func (b *VXReadonlyFieldBuilder) Label(v string) *VXReadonlyFieldBuilder {
  22. b.label = v
  23. return b
  24. }
  25. func (b *VXReadonlyFieldBuilder) Value(v interface{}) *VXReadonlyFieldBuilder {
  26. b.value = v
  27. return b
  28. }
  29. func (b *VXReadonlyFieldBuilder) Children(children ...h.HTMLComponent) *VXReadonlyFieldBuilder {
  30. b.children = children
  31. return b
  32. }
  33. func (b *VXReadonlyFieldBuilder) Checkbox(v bool) *VXReadonlyFieldBuilder {
  34. b.checkbox = v
  35. return b
  36. }
  37. func (b *VXReadonlyFieldBuilder) MarshalHTML(ctx context.Context) ([]byte, error) {
  38. var vComp h.HTMLComponent
  39. if b.children != nil {
  40. vComp = b.children
  41. } else {
  42. if b.checkbox {
  43. vComp = vuetify.VCheckbox().InputValue(b.value).
  44. Readonly(true).
  45. Ripple(false).
  46. HideDetails(true).
  47. Class("my-0 py-0")
  48. } else {
  49. vComp = h.Text(fmt.Sprint(b.value))
  50. }
  51. }
  52. return h.Div(
  53. h.Label(b.label).Class("v-label theme--light text-caption"),
  54. h.Div(vComp).Class("pt-1"),
  55. ).Class("mb-4").MarshalHTML(ctx)
  56. }