rating

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 2, 2026 License: MIT Imports: 5 Imported by: 0

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-click, lvt-mouseover, lvt-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

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

func WithAllowClear(allow bool) Option

WithAllowClear enables clearing by clicking the current value.

func WithAllowHalf

func WithAllowHalf(allow bool) Option

WithAllowHalf enables half-star ratings.

func WithCharacter

func WithCharacter(char string) Option

WithCharacter sets the rating character.

func WithColor

func WithColor(color string) Option

WithColor sets the active star color.

func WithCount

func WithCount(count int) Option

WithCount sets the number of ratings.

func WithLabel

func WithLabel(label string) Option

WithLabel sets the label text.

func WithMaxStars

func WithMaxStars(max int) Option

WithMaxStars sets the maximum number of stars.

func WithReadonly

func WithReadonly(readonly bool) Option

WithReadonly makes the rating read-only.

func WithShowCount

func WithShowCount(show bool) Option

WithShowCount displays the rating count.

func WithShowValue

func WithShowValue(show bool) Option

WithShowValue displays the numeric value.

func WithSize

func WithSize(size string) Option

WithSize sets the star size.

func WithStyled

func WithStyled(styled bool) Option

WithStyled enables Tailwind CSS styling.

func WithValue

func WithValue(value float64) Option

WithValue sets the initial rating value.

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

func New(id string, opts ...Option) *Rating

New creates a star rating.

Example:

r := rating.New("product",
    rating.WithMaxStars(5),
    rating.WithAllowHalf(true),
)

func NewReadonly

func NewReadonly(id string, value float64, opts ...Option) *Rating

NewReadonly creates a read-only rating display.

func (*Rating) Clear

func (r *Rating) Clear()

Clear clears the rating.

func (*Rating) Click

func (r *Rating) Click(starIndex int)

Click handles clicking on a star.

func (*Rating) ClickHalf

func (r *Rating) ClickHalf(starIndex int, isFirstHalf bool)

ClickHalf handles clicking on a half star.

func (*Rating) ColorClass

func (r *Rating) ColorClass() string

ColorClass returns CSS color classes for filled stars.

func (*Rating) DisplayValue

func (r *Rating) DisplayValue() float64

DisplayValue returns the value to display (hover or actual).

func (*Rating) EmptyColorClass

func (r *Rating) EmptyColorClass() string

EmptyColorClass returns CSS color classes for empty stars.

func (*Rating) FormatValue

func (r *Rating) FormatValue() string

FormatValue returns the formatted value string.

func (*Rating) FormatValueWithMax

func (r *Rating) FormatValueWithMax() string

FormatValueWithMax returns the value formatted as "4/5".

func (*Rating) Hover

func (r *Rating) Hover(starIndex int)

Hover sets the hover value.

func (*Rating) HoverHalf

func (r *Rating) HoverHalf(starIndex int, isFirstHalf bool)

HoverHalf sets the hover value for half stars.

func (*Rating) IsStarEmpty

func (r *Rating) IsStarEmpty(starIndex int) bool

IsStarEmpty returns true if a star is empty.

func (*Rating) IsStarFull

func (r *Rating) IsStarFull(starIndex int) bool

IsStarFull returns true if a star is fully filled.

func (*Rating) IsStarHalf

func (r *Rating) IsStarHalf(starIndex int) bool

IsStarHalf returns true if a star is half filled.

func (*Rating) Leave

func (r *Rating) Leave()

Leave clears the hover value.

func (*Rating) Percentage

func (r *Rating) Percentage() float64

Percentage returns the rating as a percentage.

func (*Rating) SetValue

func (r *Rating) SetValue(value float64)

SetValue sets the rating value.

func (*Rating) SizeClass

func (r *Rating) SizeClass() string

SizeClass returns CSS size classes.

func (*Rating) StarState

func (r *Rating) StarState(starIndex int) string

StarState returns "full", "half", or "empty" for a star.

func (*Rating) Stars

func (r *Rating) Stars() []int

Stars returns the star indices (1 to MaxStars).

func (*Rating) Styles

func (r *Rating) Styles() styles.RatingStyles

Styles returns the resolved RatingStyles for this component.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL