Keel v0.5.1 中文 GitHub

Rating

Live example: a real Keel application compiled to WebAssembly. Interact with it here. The initial download is about 10 MB.Open full screen
Example sourceGo · Copyable

This is the complete source for the gallery example above.examples/components/rating.go

Reuse the component construction and Render methods in a scaffolded project. The example’s registerSection and shared helpers are provided by the component gallery.

package main

import (
	"github.com/dyike/keel/ui/core"
	"github.com/dyike/keel/ui/el"
	"github.com/dyike/keel/ui/kit"
	"github.com/dyike/keel/ui/theme"
)

func init() {
	registerSection("rating", "inputs", func() core.Widget {
		mine := kit.Rating(demoText("Your rating · Click a filled star again to reduce the rating", "你的评分 · 再点已填星可减分"), 5).Size(32)
		avg := kit.Rating(demoText("Average 3.7/5 · Small", "平均 3.7/5 · 小尺寸"), 5).Size(16).ReadOnly()
		avg.SetScore(3.7)
		half := kit.Rating(demoText("Average 4.5/5", "平均 4.5/5"), 5).ReadOnly()
		half.SetScore(4.5)
		return el.Embed(el.ViewFunc(func(cx *el.Context) el.Element {
			return el.Div().P(24).Gap(14).Items(el.Start).Child(mine.Render(cx), avg.Render(cx), half.Color(theme.PrimaryText).Render(cx))
		}))
	})
}

Star rating.

score := kit.Rating("Rating", 5).OnChange(func(n int) { … })
avg := kit.Rating("Average", 5).ReadOnly()
avg.SetScore(3.7)
  • Click on the unfilled i-th star to set it to i point; click on the filled star to set it to i-1 point (for example, at 4 minutes, the second star becomes 1 point, and the first star is cleared to zero); after getting the focus, adjust ← →, and Home / End jumps to 0 or full points. Preview the score that will be set on hover.
  • Score() / SetScore(float64) Keep decimal ratings, display half stars or any decimals with a star's horizontal fill ratio. In ReadOnly(), you cannot click or use keys to change; in edit mode, you still press the whole star to select. NaN is returned to zero, the infinite value is clamped to 0 or full score, and the program setting does not trigger a callback.
  • Value() returns the integer part and is compatible with existing integer scores; use Score() to read the average score.
  • Value() / SetValue (limited to 0–max), ReadOnly(), SetDisabled.

Agent: role slider, value is "score/full score", such as 3/5.

Verify: go run ./examples/components -section rating, add -theme dark to check the dark theme.

Size(dp) sets the size of each star, range 8–128dp, default 22; illegal values are ignored and the rating is not changed. Color(color.NRGBA{...}) changes the fill color, defaults to Warning with the theme, and the outline remains Muted. Color is also used for hover previews and decimal fills. The score after the hover preview is clicked. The actual value and callback are only changed during operation; when the size is larger or the number of stars is large, the parent container provides sufficient width.