JavaScript Rating
Pico Library
This library has less than 2 KBytes
The LemonadeJS Star Rating plugin is a micro JavaScript component that provides a customizable five-star rating system. It enables expressive and flexible ratings for products or services, going beyond basic numeric feedback.
Documentation
Installation
npm install @lemonadejs/rating
Material icons
It requires Google Material Icons.
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
Attributes
Attribute | Description |
---|---|
name?: String |
The name of the component. |
number?: Number |
The number of stars. The default is 5 . |
value?: Number |
The initial value. The default is null . |
Events
Event | Description |
---|---|
onchange: Function |
Triggered when the component's value changes. |
Examples
Updating this.value
will automatically update the view. If you set this.value
to its current value, the component will reset to its initial state.
Basic example
starstarstarstar_outlinestar_outline
<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>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<div id='root'></div>
<script>
let { onchange } = lemonade;
function Component() {
const plus = () => {
this.number++;
}
const minus = () => {
this.number--;
}
const reset = () => {
this.value = 1;
}
const change = (newValue) => {
console.log('New value', newValue);
}
this.number = 5;
this.value = 3;
return render => render`<>
<Rating value="${this.value}" number="${this.number}" onchange="${change}" />
<br>
<input type="button" value="Value=1" onclick="${reset}" />
<input type="button" value="Add star" onclick="${plus}" />
<input type="button" value="Remove star" onclick="${minus}" />
</>`;
}
// Render component
lemonade.render(Component, document.getElementById('root'));
</script>
</html>