Rating
Pico Library This library has less than 2 KBytesThe LemonadeJS star rating plugin is a micro JavaScript component that implements the five star rating.
Documentation
Installation
npm install @lemonadejs/rating
Attributes
Attribute | Description |
---|---|
name?: String | The name of the component |
number?: Number | The number of stars. Default 5. |
value?: Number | The initial value. Default null. |
Events
Attribute | Description |
---|---|
onchange?: Function | When the value of the component changesself.onchange(value: Number) => void |
Examples
Changing theself.value
will trigger the view updates. If you set the self.value
with the same
current value, that will reset the value.Basic example
Source code
Browser
NPM
<html> <script src="https://cdn.jsdelivr.net/npm/lemonadejs/dist/lemonade.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/@lemonadejs/rating/dist/index.min.js"></script> <div id='root'></div> <script> var Component = function() { const self = this; self.change = function(newValue) { console.log('New value', newValue); } self.number = 5; self.value = 3; return `<> <Rating value="{{self.value}}" number="{{self.number}}" onchange="{{self.change}}" /> <input type="button" value="Set value" onclick="self.value = 3" /> <input type="button" value="Add star" onclick="self.number++" /> <input type="button" value="Remove star" onclick="self.number--" /> </>`; } </script> </html>
import lemonade from "lemonadejs"; import Rating from "@lemonadejs/rating"; // Register the component to be used across the application lemonade.setComponents({ Rating }); export default function Component() { const self = this; self.number = 5; self.value = 3; self.change = function(newValue) { console.log('New value', newValue); } return `<> <Rating value="{{self.value}}" number="{{self.number}}" onchange="{{self.change}}" /> <input type="button" value="Set value" onclick="self.value = 3" /> <input type="button" value="Add star" onclick="self.number++" /> <input type="button" value="Remove star" onclick="self.number--" /> </>`; }