@lemonadejs/rating 5.2.0 → 5.8.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 CHANGED
@@ -1,169 +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)
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 CHANGED
@@ -15,7 +15,7 @@ declare namespace Rating {
15
15
  /** Current value */
16
16
  value?: number;
17
17
  /** Tooltips displayed on hover */
18
- tooltip?: [] | string;
18
+ tooltip?: string | string[];
19
19
  /** Size */
20
20
  size?: 'default' | 'small';
21
21
  /** Onchange Event */
@@ -30,7 +30,7 @@ declare namespace Rating {
30
30
  /** Current value */
31
31
  value: number;
32
32
  /** Tooltips displayed on hover */
33
- tooltip?: [] | string;
33
+ tooltip?: string | string[];
34
34
  /** Size */
35
35
  size?: 'default' | 'small';
36
36
  /** Gets the current value */
package/dist/index.js CHANGED
@@ -84,17 +84,13 @@ if (!lemonade && typeof (require) === 'function') {
84
84
  if (typeof(index) === 'string') {
85
85
  index = Number(index);
86
86
  }
87
- // Toggle value
88
- if (index === current) {
89
- index = 0;
90
- }
91
87
  // Apply value to the selected property in each star
92
88
  for (let i = 0; i < self.number; i++) {
93
89
  self.stars[i].selected = i <= index - 1 ? 1 : 0;
94
90
  }
95
91
  // Keep current value
96
92
  current = index;
97
- // Dispatch methdo
93
+ // Dispatch method
98
94
  if (events !== false) {
99
95
  Dispatch.call(self, change, 'change', {
100
96
  instance: self,
@@ -103,18 +99,39 @@ if (!lemonade && typeof (require) === 'function') {
103
99
  }
104
100
  }
105
101
 
102
+ const getElementPosition = function(child) {
103
+ if (child.tagName === 'I') {
104
+ let root = self.el;
105
+ for (let i = 0; i < root.children.length; i++) {
106
+ let c = root.children[i];
107
+ if (c === child) {
108
+ return i;
109
+ }
110
+ }
111
+ }
112
+ return -1;
113
+ }
114
+
106
115
  const click = function(e, s) {
107
- let index = self.stars.indexOf(s) + 1;
108
- val(index);
116
+ let ret = getElementPosition(e.target);
117
+ if (ret !== -1) {
118
+ let index = ret + 1;
119
+ if (index === current) {
120
+ index = 0;
121
+ }
122
+ self.value = index;
123
+ }
109
124
  }
110
125
 
111
126
  const mouseover = function(e, s) {
112
- let index = self.stars.indexOf(s);
113
- for (let i = 0; i < self.number; i++) {
114
- if (i <= index) {
115
- self.stars[i].hover = 1;
116
- } else {
117
- self.stars[i].hover = 0;
127
+ let index = getElementPosition(e.target);
128
+ if (index !== -1) {
129
+ for (let i = 0; i < self.number; i++) {
130
+ if (i <= index) {
131
+ self.stars[i].hover = 1;
132
+ } else {
133
+ self.stars[i].hover = 0;
134
+ }
118
135
  }
119
136
  }
120
137
  }
@@ -156,6 +173,10 @@ if (!lemonade && typeof (require) === 'function') {
156
173
  len();
157
174
  // Ignore events
158
175
  val(self.value, false);
176
+
177
+ self.el.addEventListener('click', click);
178
+ self.el.addEventListener('mouseout', mouseout);
179
+ self.el.addEventListener('mouseover', mouseover);
159
180
  });
160
181
 
161
182
  self.getValue = function () {
@@ -166,8 +187,8 @@ if (!lemonade && typeof (require) === 'function') {
166
187
  self.value = index;
167
188
  }
168
189
 
169
- return render => render`<div class="lm-rating" value="{{self.value}}" number="{{self.number}}" name="{{self.name}}" data-size="{{self.size}}" :loop="self.stars" :ref="self.component" onmouseout="${mouseout}">
170
- <i class="material-symbols-outlined material-icons" data-selected="{{self.selected}}" data-hover="{{self.hover}}" title="{{self.title}}" onclick="${click}" onmouseover="${mouseover}">star</i>
190
+ return `<div class="lm-rating" value="{{self.value}}" number="{{self.number}}" name="{{self.name}}" data-size="{{self.size}}" :loop="self.stars">
191
+ <i class="material-symbols-outlined material-icons" data-selected="{{self.selected}}" data-hover="{{self.hover}}" title="{{self.title}}">star</i>
171
192
  </div>`;
172
193
  }
173
194
 
package/dist/style.css CHANGED
@@ -5,8 +5,14 @@
5
5
  -webkit-user-drag: none;
6
6
  }
7
7
 
8
+ .lm-rating[data-size="small"] {
9
+ height: 16px;
10
+ line-height: 16px;
11
+ font-size: 16px;
12
+ }
13
+
8
14
  .lm-rating[data-size="small"] i {
9
- font-size: 18px;
15
+ font-size: 16px;
10
16
  }
11
17
 
12
18
  .lm-rating i[data-hover="1"] {
package/package.json CHANGED
@@ -1,23 +1,23 @@
1
- {
2
- "name": "@lemonadejs/rating",
3
- "title": "LemonadeJS star rating javascript plugin",
4
- "description": "LemonadeJS star rating plugin",
5
- "author": {
6
- "name": "Contact <contact@lemonadejs.net>",
7
- "url": "https://lemonadejs.net"
8
- },
9
- "keywords": [
10
- "javascript star rating",
11
- "javascript rating",
12
- "lemonadejs plugins",
13
- "js",
14
- "library",
15
- "javascript plugins"
16
- ],
17
- "dependencies": {
18
- "lemonadejs": "^5.2.0"
19
- },
20
- "main": "dist/index.js",
21
- "types": "dist/index.d.ts",
22
- "version": "5.2.0"
23
- }
1
+ {
2
+ "name": "@lemonadejs/rating",
3
+ "title": "LemonadeJS star rating javascript plugin",
4
+ "description": "LemonadeJS star rating plugin",
5
+ "author": {
6
+ "name": "Contact <contact@lemonadejs.net>",
7
+ "url": "https://lemonadejs.net"
8
+ },
9
+ "keywords": [
10
+ "javascript star rating",
11
+ "javascript rating",
12
+ "lemonadejs plugins",
13
+ "js",
14
+ "library",
15
+ "javascript plugins"
16
+ ],
17
+ "dependencies": {
18
+ "lemonadejs": "^5.8.0"
19
+ },
20
+ "main": "dist/index.js",
21
+ "types": "dist/index.d.ts",
22
+ "version": "5.8.0"
23
+ }