@lemonadejs/rating 5.0.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
@@ -14,15 +14,29 @@ declare namespace Rating {
14
14
  name?: string;
15
15
  /** Current value */
16
16
  value?: number;
17
+ /** Tooltips displayed on hover */
18
+ tooltip?: string | string[];
19
+ /** Size */
20
+ size?: 'default' | 'small';
17
21
  /** Onchange Event */
18
- onchange?: (newValue: number) => void;
22
+ onchange?: (instance: Instance, newValue: number) => void;
19
23
  }
20
24
 
21
25
  interface Instance {
22
26
  /** Number of stars */
23
27
  number: number;
28
+ /** Form element name */
29
+ name?: string;
24
30
  /** Current value */
25
31
  value: number;
32
+ /** Tooltips displayed on hover */
33
+ tooltip?: string | string[];
34
+ /** Size */
35
+ size?: 'default' | 'small';
36
+ /** Gets the current value */
37
+ getValue: () => number;
38
+ /** Sets the value */
39
+ setValue: (index: number) => void;
26
40
  }
27
41
  }
28
42
 
package/dist/index.js CHANGED
@@ -8,16 +8,48 @@ if (!lemonade && typeof (require) === 'function') {
8
8
  global.Rating = factory();
9
9
  }(this, (function () {
10
10
 
11
- const Rating = function() {
11
+ class CustomEvents extends Event {
12
+ constructor(type, props, options) {
13
+ super(type, {
14
+ bubbles: true,
15
+ composed: true,
16
+ ...options,
17
+ });
18
+
19
+ if (props) {
20
+ for (const key in props) {
21
+ // Avoid assigning if property already exists anywhere on `this`
22
+ if (! (key in this)) {
23
+ this[key] = props[key];
24
+ }
25
+ }
26
+ }
27
+ }
28
+ }
29
+
30
+ // Dispatcher
31
+ const Dispatch = function(method, type, options) {
32
+ // Try calling the method directly if provided
33
+ if (typeof method === 'function') {
34
+ let a = Object.values(options);
35
+ return method(...a);
36
+ } else if (this.tagName) {
37
+ this.dispatchEvent(new CustomEvents(type, options));
38
+ }
39
+ }
40
+
41
+ const Rating = function(children, { onchange, onload }) {
12
42
  let self = this;
13
43
 
44
+ // Event
45
+ let change = self.onchange;
46
+ self.onchange = null;
47
+
14
48
  if (! self.number) {
15
49
  self.number = 5;
16
50
  }
17
- self.stars = [];
18
51
 
19
- // Event
20
- let change = self.onchange;
52
+ self.stars = [];
21
53
 
22
54
  // Current self star
23
55
  let current = null;
@@ -25,7 +57,7 @@ if (!lemonade && typeof (require) === 'function') {
25
57
  /**
26
58
  * Update the number of stars
27
59
  */
28
- const len = function() {
60
+ const len = function () {
29
61
  // Remove stars
30
62
  if (self.number < self.stars.length) {
31
63
  self.stars.splice(self.number, self.stars.length);
@@ -37,66 +69,126 @@ if (!lemonade && typeof (require) === 'function') {
37
69
  for (let i = 0; i < self.number; i++) {
38
70
  if (! self.stars[i]) {
39
71
  self.stars[i] = {
40
- icon: 'star_outline',
72
+ icon: 'star',
41
73
  };
74
+ if (self.tooltip[i]) {
75
+ self.stars[i].title = self.tooltip[i];
76
+ }
42
77
  }
43
78
  }
44
79
  // Refresh
45
80
  self.refresh('stars');
46
81
  }
47
82
 
48
- const val = function() {
49
- // Update value
50
- if (self.value > 0) {
51
- let t = self.stars[self.value-1];
52
- if (t) {
53
- self.click(null, t);
83
+ const val = function (index, events) {
84
+ if (typeof(index) === 'string') {
85
+ index = Number(index);
86
+ }
87
+ // Apply value to the selected property in each star
88
+ for (let i = 0; i < self.number; i++) {
89
+ self.stars[i].selected = i <= index - 1 ? 1 : 0;
90
+ }
91
+ // Keep current value
92
+ current = index;
93
+ // Dispatch method
94
+ if (events !== false) {
95
+ Dispatch.call(self, change, 'change', {
96
+ instance: self,
97
+ value: index,
98
+ });
99
+ }
100
+ }
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
+
115
+ const click = function(e, s) {
116
+ let ret = getElementPosition(e.target);
117
+ if (ret !== -1) {
118
+ let index = ret + 1;
119
+ if (index === current) {
120
+ index = 0;
54
121
  }
122
+ self.value = index;
123
+ }
124
+ }
125
+
126
+ const mouseover = function(e, s) {
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
+ }
135
+ }
136
+ }
137
+ }
138
+
139
+ const mouseout = function(e, s) {
140
+ for (let i = 0; i < self.number; i++) {
141
+ self.stars[i].hover = 0;
55
142
  }
56
143
  }
57
144
 
58
- self.onchange = function(prop) {
145
+ onchange((prop) => {
59
146
  if (prop === 'number') {
60
147
  len();
61
148
  } else if (prop === 'value') {
62
- val();
149
+ val(self.value);
150
+ } else if (prop === 'tooltip') {
151
+ if (typeof(self.tooltip) === 'string') {
152
+ self.tooltip = self.tooltip.split(',')
153
+ }
154
+ len();
63
155
  }
64
- }
156
+ })
65
157
 
66
- self.onload = function() {
158
+ onload(() => {
67
159
  // Bind global method to be compatible with LemonadeJS forms
68
- self.el.val = function(v) {
69
- if (typeof(v) === 'undefined') {
160
+ self.el.val = function (v) {
161
+ if (typeof (v) === 'undefined') {
70
162
  return self.value;
71
163
  } else {
72
164
  self.value = v;
73
165
  }
74
166
  }
75
167
 
168
+ if (self.tooltip && typeof(self.tooltip) === 'string') {
169
+ self.tooltip = self.tooltip.split(',')
170
+ } else {
171
+ self.tooltip = '';
172
+ }
76
173
  len();
77
- val();
78
- }
174
+ // Ignore events
175
+ val(self.value, false);
79
176
 
80
- self.click = function(e, s) {
81
- if (! s.selected) {
82
- current = null;
83
- }
84
- let index = self.stars.indexOf(s);
85
- for (let i = 0; i < self.number; i++) {
86
- let selected = i <= index && s !== current ? 1 : 0;
87
- self.stars[i].selected = selected;
88
- self.stars[i].el.style.color = selected ? 'red' : '';
89
- self.stars[i].icon = selected ? 'star' : 'star_outline';
90
- }
91
- current = s;
177
+ self.el.addEventListener('click', click);
178
+ self.el.addEventListener('mouseout', mouseout);
179
+ self.el.addEventListener('mouseover', mouseover);
180
+ });
92
181
 
93
- if (typeof(change) === 'function') {
94
- change(index+1, s);
95
- }
182
+ self.getValue = function () {
183
+ return Number(self.value);
184
+ }
185
+
186
+ self.setValue = function (index) {
187
+ self.value = index;
96
188
  }
97
189
 
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>
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>
100
192
  </div>`;
101
193
  }
102
194
 
package/dist/style.css ADDED
@@ -0,0 +1,24 @@
1
+ .lm-rating i {
2
+ color: lightgray;
3
+ cursor: pointer;
4
+ user-select: none;
5
+ -webkit-user-drag: none;
6
+ }
7
+
8
+ .lm-rating[data-size="small"] {
9
+ height: 16px;
10
+ line-height: 16px;
11
+ font-size: 16px;
12
+ }
13
+
14
+ .lm-rating[data-size="small"] i {
15
+ font-size: 16px;
16
+ }
17
+
18
+ .lm-rating i[data-hover="1"] {
19
+ color: gray;
20
+ }
21
+
22
+ .lm-rating i[data-selected="1"] {
23
+ color: red;
24
+ }
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.0.3"
19
- },
20
- "main": "dist/index.js",
21
- "types": "dist/index.d.ts",
22
- "version": "5.0.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
+ }