Documentation
¶
Overview ¶
Package rating provides star rating components.
Available variants:
- New() creates a star rating (template: "lvt:rating:default:v1")
- NewReadonly() creates a read-only rating display (template: "lvt:rating:readonly:v1")
Required lvt-* attributes: lvt-on:click, lvt-on:mouseover, lvt-on:mouseleave
Example usage:
// In your controller/state
ProductRating: rating.New("product-rating",
rating.WithMaxStars(5),
rating.WithValue(4),
)
// In your template
{{template "lvt:rating:default:v1" .ProductRating}}
Index ¶
- func Templates() *base.TemplateSet
- type Option
- func WithAllowClear(allow bool) Option
- func WithAllowHalf(allow bool) Option
- func WithCharacter(char string) Option
- func WithColor(color string) Option
- func WithCount(count int) Option
- func WithLabel(label string) Option
- func WithMaxStars(max int) Option
- func WithReadonly(readonly bool) Option
- func WithShowCount(show bool) Option
- func WithShowValue(show bool) Option
- func WithSize(size string) Option
- func WithStyled(styled bool) Option
- func WithValue(value float64) Option
- type Rating
- func (r *Rating) Clear()
- func (r *Rating) Click(starIndex int)
- func (r *Rating) ClickHalf(starIndex int, isFirstHalf bool)
- func (r *Rating) ColorClass() string
- func (r *Rating) DisplayValue() float64
- func (r *Rating) EmptyColorClass() string
- func (r *Rating) FormatValue() string
- func (r *Rating) FormatValueWithMax() string
- func (r *Rating) Hover(starIndex int)
- func (r *Rating) HoverHalf(starIndex int, isFirstHalf bool)
- func (r *Rating) IsStarEmpty(starIndex int) bool
- func (r *Rating) IsStarFull(starIndex int) bool
- func (r *Rating) IsStarHalf(starIndex int) bool
- func (r *Rating) Leave()
- func (r *Rating) Percentage() float64
- func (r *Rating) SetValue(value float64)
- func (r *Rating) SizeClass() string
- func (r *Rating) StarState(starIndex int) string
- func (r *Rating) Stars() []int
- func (r *Rating) Styles() styles.RatingStyles
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Templates ¶
func Templates() *base.TemplateSet
Templates returns the rating component's template set for registration with the LiveTemplate framework.
Example usage in main.go:
import "github.com/livetemplate/lvt/components/rating"
tmpl, err := livetemplate.New("app",
livetemplate.WithComponentTemplates(rating.Templates()),
)
Available templates:
- "lvt:rating:default:v1" - Interactive star rating
- "lvt:rating:readonly:v1" - Read-only rating display
Types ¶
type Option ¶
type Option func(*Rating)
Option is a functional option for configuring ratings.
func WithAllowClear ¶
WithAllowClear enables clearing by clicking the current value.
func WithCharacter ¶
WithCharacter sets the rating character.
func WithMaxStars ¶
WithMaxStars sets the maximum number of stars.
func WithReadonly ¶
WithReadonly makes the rating read-only.
type Rating ¶
type Rating struct {
base.Base
// Value is the current rating (can be fractional for half stars)
Value float64
// MaxStars is the maximum number of stars (default 5)
MaxStars int
// AllowHalf enables half-star ratings
AllowHalf bool
// AllowClear allows clearing by clicking the current value
AllowClear bool
// Readonly prevents user interaction
Readonly bool
// Size is the star size ("sm", "md", "lg", "xl")
Size string
// Color is the active star color
Color string
// EmptyColor is the inactive star color
EmptyColor string
// HoverValue is the value being hovered (-1 if not hovering)
HoverValue float64
// ShowValue displays the numeric value
ShowValue bool
// ShowCount displays the rating count
ShowCount bool
// Count is the number of ratings (for display)
Count int
// Label is optional label text
Label string
// Character is the rating character (default star)
Character string
}
Rating is a star rating component. Use template "lvt:rating:default:v1" to render.
func New ¶
New creates a star rating.
Example:
r := rating.New("product",
rating.WithMaxStars(5),
rating.WithAllowHalf(true),
)
func NewReadonly ¶
NewReadonly creates a read-only rating display.
func (*Rating) ColorClass ¶
ColorClass returns CSS color classes for filled stars.
func (*Rating) DisplayValue ¶
DisplayValue returns the value to display (hover or actual).
func (*Rating) EmptyColorClass ¶
EmptyColorClass returns CSS color classes for empty stars.
func (*Rating) FormatValue ¶
FormatValue returns the formatted value string.
func (*Rating) FormatValueWithMax ¶
FormatValueWithMax returns the value formatted as "4/5".
func (*Rating) IsStarEmpty ¶
IsStarEmpty returns true if a star is empty.
func (*Rating) IsStarFull ¶
IsStarFull returns true if a star is fully filled.
func (*Rating) IsStarHalf ¶
IsStarHalf returns true if a star is half filled.
func (*Rating) Percentage ¶
Percentage returns the rating as a percentage.
func (*Rating) Styles ¶
func (r *Rating) Styles() styles.RatingStyles
Styles returns the resolved RatingStyles for this component.