@lemonadejs/rating 2.0.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +169 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +11 -5
- package/dist/react.d.ts +18 -0
- package/dist/react.js +36 -0
- package/dist/vue.d.ts +15 -0
- package/dist/vue.js +47 -0
- package/package.json +3 -2
package/README.md
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
# LemonadeJS Rating
|
|
2
|
+
|
|
3
|
+
[Official website and documentation is here](https://lemonadejs.net/components/rating)
|
|
4
|
+
|
|
5
|
+
Compatible with Vanilla JavaScript, LemonadeJS, React, Vue or Angular.
|
|
6
|
+
|
|
7
|
+
The LemonadeJS star rating plugin is a micro JavaScript component that implements the five star rating.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- Lightweight: The JavaScript rating is only about 2 KBytes;
|
|
12
|
+
- Integration: It can be used as a standalone library or integrated with any modern framework;
|
|
13
|
+
|
|
14
|
+
## Getting Started
|
|
15
|
+
|
|
16
|
+
You can install using NPM or using directly from a CDN.
|
|
17
|
+
|
|
18
|
+
### npm Installation
|
|
19
|
+
|
|
20
|
+
To install it in your project using npm, run the following command:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
$ npm install @lemonadejs/rating
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### CDN
|
|
27
|
+
|
|
28
|
+
To use rating via a CDN, include the following script tags in your HTML file:
|
|
29
|
+
|
|
30
|
+
```html
|
|
31
|
+
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lemonadejs/dist/lemonade.min.js"></script>
|
|
32
|
+
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@lemonadejs/rating/dist/index.min.js"></script>
|
|
33
|
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@lemonadejs/rating/dist/style.min.css" />
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Usage
|
|
37
|
+
|
|
38
|
+
Quick example with Lemonade
|
|
39
|
+
|
|
40
|
+
```javascript
|
|
41
|
+
// For installation: % npm install @lemonadejs/rating
|
|
42
|
+
// Add to your HTML: <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
|
|
43
|
+
import lemonade from "lemonadejs";
|
|
44
|
+
import Rating from "@lemonadejs/rating";
|
|
45
|
+
|
|
46
|
+
export default function Component() {
|
|
47
|
+
const self = this;
|
|
48
|
+
|
|
49
|
+
self.plus = () => {
|
|
50
|
+
self.number++;
|
|
51
|
+
}
|
|
52
|
+
self.minus = () => {
|
|
53
|
+
self.number--;
|
|
54
|
+
}
|
|
55
|
+
self.reset = () => {
|
|
56
|
+
self.value = 1;
|
|
57
|
+
}
|
|
58
|
+
self.change = (newValue) => {
|
|
59
|
+
console.log('New value', newValue);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
self.number = 5;
|
|
63
|
+
self.value = 3;
|
|
64
|
+
|
|
65
|
+
return `<div>
|
|
66
|
+
<p><Rating :value="self.value" :number="self.number" onchange="self.change" /></p>
|
|
67
|
+
<input type="button" value="Value=1" onclick="self.reset" />
|
|
68
|
+
<input type="button" value="Add star" onclick="self.plus" />
|
|
69
|
+
<input type="button" value="Remove star" onclick="self.minus" />
|
|
70
|
+
</div>`;
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Quick example with React
|
|
75
|
+
|
|
76
|
+
```jsx
|
|
77
|
+
import React, { useRef, useState } from "react";
|
|
78
|
+
import Rating from "@lemonadejs/rating/dist/react";
|
|
79
|
+
|
|
80
|
+
export default function App() {
|
|
81
|
+
const ratingRef = useRef();
|
|
82
|
+
const [number, setNumber] = useState(5);
|
|
83
|
+
const [value, setValue] = useState(3);
|
|
84
|
+
|
|
85
|
+
const plus = () => {
|
|
86
|
+
setNumber(number+1)
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const minus = () => {
|
|
90
|
+
setNumber(number-1)
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const reset = () => {
|
|
94
|
+
setValue(1);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const change = (newValue) => {
|
|
98
|
+
console.log('New value', newValue);
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
return (
|
|
102
|
+
<>
|
|
103
|
+
<Rating value={value} number={number} onchange={change} ref={ratingRef} />
|
|
104
|
+
<input type="button" value="Value=1" onClick={reset} />
|
|
105
|
+
<input type="button" value="Add star" onClick={plus} />
|
|
106
|
+
<input type="button" value="Remove star" onClick={minus} />
|
|
107
|
+
</>
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Quick example with React
|
|
113
|
+
|
|
114
|
+
```vue
|
|
115
|
+
<template>
|
|
116
|
+
<Rating :value="1" :number="5" :onchange="handleChange" ref="component" />
|
|
117
|
+
<button @click="reset">Value=1</button>
|
|
118
|
+
<button @click="plus">Add Star</button>
|
|
119
|
+
<button @click="minus">Remove Star</button>
|
|
120
|
+
</template>
|
|
121
|
+
|
|
122
|
+
<script>
|
|
123
|
+
import Rating from '@lemonadejs/rating/dist/vue';
|
|
124
|
+
|
|
125
|
+
export default {
|
|
126
|
+
name: 'App',
|
|
127
|
+
components: {
|
|
128
|
+
Rating,
|
|
129
|
+
},
|
|
130
|
+
methods: {
|
|
131
|
+
reset: function () {
|
|
132
|
+
this.$refs.component.current.value = 1;
|
|
133
|
+
},
|
|
134
|
+
plus: function () {
|
|
135
|
+
this.$refs.component.current.number++;
|
|
136
|
+
},
|
|
137
|
+
minus: function () {
|
|
138
|
+
this.$refs.component.current.number--;
|
|
139
|
+
},
|
|
140
|
+
handleChange: function (v) {
|
|
141
|
+
console.log('New value: ', v)
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
</script>
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Attributes
|
|
149
|
+
|
|
150
|
+
| Attribute | Type | Description |
|
|
151
|
+
| --- | --- | --- |
|
|
152
|
+
| name? | String | The name of the component |
|
|
153
|
+
| number? | Number | The number of stars. Default 5. |
|
|
154
|
+
| value? | Number | The initial value. Default null. |
|
|
155
|
+
|
|
156
|
+
### Events
|
|
157
|
+
|
|
158
|
+
| Attribute | Description |
|
|
159
|
+
| --- | --- |
|
|
160
|
+
| onchange? | When the value of the component changes. |
|
|
161
|
+
|
|
162
|
+
## License
|
|
163
|
+
|
|
164
|
+
The [LemonadeJS](https://lemonadejs.net) LemonadeJS Rating is released under the MIT.
|
|
165
|
+
|
|
166
|
+
## Other Tools
|
|
167
|
+
|
|
168
|
+
- [jSuites Plugins](https://jsuites.net/docs)
|
|
169
|
+
- [Jspreadsheet Data Grid](https://jspreadsheet.com)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Official Type definitions for LemonadeJS plugins
|
|
3
|
+
* https://lemonadejs.net
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
declare function Rating(el: HTMLElement, options?: Rating.Options): Rating.Instance;
|
|
7
|
+
|
|
8
|
+
declare namespace Rating {
|
|
9
|
+
|
|
10
|
+
interface Options {
|
|
11
|
+
/** Number of stars */
|
|
12
|
+
number?: number;
|
|
13
|
+
/** Form element name */
|
|
14
|
+
name?: string;
|
|
15
|
+
/** Current value */
|
|
16
|
+
value?: number;
|
|
17
|
+
/** Onchange Event */
|
|
18
|
+
onchange?: (newValue: number) => void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface Instance {
|
|
22
|
+
/** Number of stars */
|
|
23
|
+
number: number;
|
|
24
|
+
/** Current value */
|
|
25
|
+
value: number;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export default Rating;
|
package/dist/index.js
CHANGED
|
@@ -36,7 +36,9 @@ if (!lemonade && typeof (require) === 'function') {
|
|
|
36
36
|
// Add missing stars
|
|
37
37
|
for (let i = 0; i < self.number; i++) {
|
|
38
38
|
if (! self.stars[i]) {
|
|
39
|
-
self.stars[i] = {
|
|
39
|
+
self.stars[i] = {
|
|
40
|
+
icon: 'star_outline',
|
|
41
|
+
};
|
|
40
42
|
}
|
|
41
43
|
}
|
|
42
44
|
// Refresh
|
|
@@ -48,7 +50,7 @@ if (!lemonade && typeof (require) === 'function') {
|
|
|
48
50
|
if (self.value > 0) {
|
|
49
51
|
let t = self.stars[self.value-1];
|
|
50
52
|
if (t) {
|
|
51
|
-
self.click(t);
|
|
53
|
+
self.click(null, t);
|
|
52
54
|
}
|
|
53
55
|
}
|
|
54
56
|
}
|
|
@@ -75,7 +77,7 @@ if (!lemonade && typeof (require) === 'function') {
|
|
|
75
77
|
val();
|
|
76
78
|
}
|
|
77
79
|
|
|
78
|
-
self.click = function(s) {
|
|
80
|
+
self.click = function(e, s) {
|
|
79
81
|
if (! s.selected) {
|
|
80
82
|
current = null;
|
|
81
83
|
}
|
|
@@ -84,6 +86,7 @@ if (!lemonade && typeof (require) === 'function') {
|
|
|
84
86
|
let selected = i <= index && s !== current ? 1 : 0;
|
|
85
87
|
self.stars[i].selected = selected;
|
|
86
88
|
self.stars[i].el.style.color = selected ? 'red' : '';
|
|
89
|
+
self.stars[i].icon = selected ? 'star' : 'star_outline';
|
|
87
90
|
}
|
|
88
91
|
current = s;
|
|
89
92
|
|
|
@@ -92,12 +95,15 @@ if (!lemonade && typeof (require) === 'function') {
|
|
|
92
95
|
}
|
|
93
96
|
}
|
|
94
97
|
|
|
95
|
-
return `<div value="{{self.value}}" number="{{self.number}}" name="{{self.name}}"
|
|
96
|
-
<i class="material-icons" onclick="self.parent.click
|
|
98
|
+
return `<div value="{{self.value}}" number="{{self.number}}" name="{{self.name}}" :loop="self.stars" :ref="self.component" style="cursor: pointer">
|
|
99
|
+
<i class="material-icons" onclick="self.parent.click">{{self.icon}}</i>
|
|
97
100
|
</div>`;
|
|
98
101
|
}
|
|
99
102
|
|
|
103
|
+
// Register the LemonadeJS Component
|
|
100
104
|
lemonade.setComponents({ Rating: Rating });
|
|
105
|
+
// Register the web component
|
|
106
|
+
lemonade.createWebComponent('rating', Rating);
|
|
101
107
|
|
|
102
108
|
return function (root, options) {
|
|
103
109
|
if (typeof (root) === 'object') {
|
package/dist/react.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Official Type definitions for the LemonadeJS plugins
|
|
3
|
+
* https://lemonadejs.net
|
|
4
|
+
* Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
|
5
|
+
*/
|
|
6
|
+
import Component from './index';
|
|
7
|
+
|
|
8
|
+
interface Rating {
|
|
9
|
+
ref?: MutableRefObject<undefined>;
|
|
10
|
+
(): any
|
|
11
|
+
[key: string]: any
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
type Props = IntrinsicAttributes & Component.Options & Rating;
|
|
15
|
+
|
|
16
|
+
declare function Rating<Rating>(props: Props): any;
|
|
17
|
+
|
|
18
|
+
export default Rating;
|
package/dist/react.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import React, { useRef, useEffect } from "react";
|
|
2
|
+
import Component from './index';
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
export default React.forwardRef((props, mainReference) => {
|
|
6
|
+
// Dom element
|
|
7
|
+
const Ref = useRef(null);
|
|
8
|
+
|
|
9
|
+
// Get the properties for the spreadsheet
|
|
10
|
+
let options = { ...props };
|
|
11
|
+
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
if (!Ref.current.innerHTML) {
|
|
14
|
+
mainReference.current = Component(Ref.current, options);
|
|
15
|
+
}
|
|
16
|
+
}, []);
|
|
17
|
+
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
for (let key in props) {
|
|
20
|
+
if (key === 'onchange' || key === 'onload') {
|
|
21
|
+
continue
|
|
22
|
+
} else if (props.hasOwnProperty(key) && mainReference.current.hasOwnProperty(key)) {
|
|
23
|
+
if (props[key] !== mainReference.current[key]) {
|
|
24
|
+
mainReference.current[key] = props[key];
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}, [props])
|
|
29
|
+
|
|
30
|
+
let prop = {
|
|
31
|
+
ref: Ref,
|
|
32
|
+
style: { height: '100%', width: '100%' }
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
return React.createElement("div", prop);
|
|
36
|
+
})
|
package/dist/vue.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Official Type definitions for the LemonadeJS plugins
|
|
3
|
+
* https://lemonadejs.net
|
|
4
|
+
* Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
|
5
|
+
*/
|
|
6
|
+
import Component from './index';
|
|
7
|
+
|
|
8
|
+
interface Rating {
|
|
9
|
+
(): any
|
|
10
|
+
[key: string]: any
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
declare function Rating<Rating>(props: Component.Options): any;
|
|
14
|
+
|
|
15
|
+
export default Rating;
|
package/dist/vue.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { h } from 'vue';
|
|
2
|
+
import component from "./index";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
export const Rating = {
|
|
6
|
+
inheritAttrs: false,
|
|
7
|
+
mounted() {
|
|
8
|
+
let options = {
|
|
9
|
+
...this.$attrs
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
this.el = this.$refs.container;
|
|
13
|
+
|
|
14
|
+
this.current = component(this.$refs.container, options);
|
|
15
|
+
},
|
|
16
|
+
setup() {
|
|
17
|
+
let containerProps = {
|
|
18
|
+
ref: 'container',
|
|
19
|
+
style: {
|
|
20
|
+
width: '100%',
|
|
21
|
+
height: '100%',
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
return () => h('div', containerProps);
|
|
25
|
+
},
|
|
26
|
+
watch: {
|
|
27
|
+
$attrs: {
|
|
28
|
+
deep: true,
|
|
29
|
+
handler() {
|
|
30
|
+
this.updateState();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
methods: {
|
|
35
|
+
updateState() {
|
|
36
|
+
for (let key in this.$attrs) {
|
|
37
|
+
if (this.$attrs.hasOwnProperty(key) && this.current.hasOwnProperty(key)) {
|
|
38
|
+
if (this.$attrs[key] !== this.current[key]) {
|
|
39
|
+
this.current[key] = this.$attrs[key];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export default Rating;
|