1
|
1 |
|
import "./styles.css"; |
2
|
|
|
|
3
|
1 |
|
import React from "react"; |
4
|
1 |
|
import classNames from "classnames"; |
5
|
|
|
|
6
|
|
|
import { RaterComponent } from "./types"; |
7
|
|
|
|
8
|
1 |
|
import { buildBoxComponent } from "../../../utils"; |
9
|
1 |
|
import { ICONS } from "./icons"; |
10
|
|
|
|
11
|
1 |
|
import Button from "../../atoms/Button"; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* A vote manager component, really useful to let the user leave a review |
15
|
|
|
* |
16
|
|
|
* @since 2.0.0 |
17
|
|
|
* |
18
|
|
|
* @param {number} value actual vote |
19
|
|
|
* @param {number} max max vote (max number of icons showed) |
20
|
|
|
* @param {"star"|"circle"} type vote icons type |
21
|
|
|
* @param {(newVote:number)=>void} onChange callback triggered when user select a vote |
22
|
|
|
* @param {string} className `common modular-ui prop` - custom className (to better customize it) |
23
|
|
|
* @param {boolean} unstyled `common modular-ui prop` - Style/unstyle component (to better customize it) |
24
|
|
|
* @param {string} id `common modular-ui prop` - `data-id` parameter (for testing purpose, to easily find the component into the DOM) |
25
|
|
|
* @param {boolean} dark `common modular-ui prop` - Enable/disable dark mode |
26
|
|
|
* @param {boolean} hide `common modular-ui prop` - Hide/show component |
27
|
|
|
* @param {boolean} shadow `common modular-ui prop` - Enable/disable shadow behind component (to better customize it) |
28
|
|
|
* |
29
|
|
|
* @example <caption>Example Rater usage</caption> |
30
|
|
|
* import { render } from "react-dom"; |
31
|
|
|
* import { Rater } from '@cianciarusocataldo/modular-ui'; |
32
|
|
|
* |
33
|
|
|
* render(<Rater type="circle" value={3} />, document.getElementById("root")); |
34
|
|
|
* |
35
|
|
|
* @see https://cianciarusocataldo.github.io/modular-ui/components/molecules/Rater |
36
|
|
|
* |
37
|
|
|
* @author Cataldo Cianciaruso <https://github.com/CianciarusoCataldo> |
38
|
|
|
* |
39
|
|
|
* @copyright 2022 Cataldo Cianciaruso |
40
|
|
|
*/ |
41
|
1 |
|
const Rater: RaterComponent = ({ |
42
|
7 |
|
value, |
43
|
7 |
|
max, |
44
|
7 |
|
type, |
45
|
7 |
|
onChange, |
46
|
7 |
|
className, |
47
|
7 |
|
vertical, |
48
|
7 |
|
label, |
49
|
7 |
|
...commonProps |
50
|
|
|
}) => { |
51
|
7 |
|
const voteType = type || "star"; |
52
|
7 |
|
let startMax = max || 5; |
53
|
|
|
|
54
|
7 |
|
return buildBoxComponent<number>({ |
55
|
|
|
callBack: (actualValue, setValue) => { |
56
|
7 |
|
const [hoveredElement, setHover] = React.useState<number | null>(null); |
57
|
7 |
|
const [maxValue, setMax] = React.useState<number>(startMax); |
58
|
|
|
|
59
|
7 |
|
React.useEffect(() => { |
60
|
3 |
|
if (max) { |
61
|
3 |
|
setMax(Number.parseInt(String(max))); |
62
|
|
|
} |
63
|
|
|
}, [max]); |
64
|
|
|
|
65
|
7 |
|
let iconArray = []; |
66
|
|
|
|
67
|
41 |
|
for (let i: number = 0; i < maxValue; ++i) { |
68
|
41 |
|
let iconToShow: "FULL" | "EMPTY" = "EMPTY"; |
69
|
|
|
|
70
|
41 |
|
if (hoveredElement || hoveredElement === 0) { |
71
|
6 |
|
iconToShow = hoveredElement >= i ? "FULL" : "EMPTY"; |
72
|
|
|
} else { |
73
|
35 |
|
iconToShow = i + 1 <= actualValue ? "FULL" : "EMPTY"; |
74
|
|
|
} |
75
|
|
|
|
76
|
41 |
|
iconArray.push( |
77
|
|
|
<Button |
78
|
|
|
unstyled |
79
|
|
|
onClick={() => { |
80
|
1 |
|
let newVote: number = i + 1; |
81
|
1 |
|
setValue(newVote); |
82
|
2 |
|
onChange && onChange(i + 1); |
83
|
|
|
}} |
84
|
1 |
|
onMouseEnter={() => setHover(i)} |
85
|
1 |
|
onMouseLeave={() => setHover(null)} |
86
|
|
|
id={`vote_${i}`} |
87
|
|
|
key={`vote_${i}`} |
88
|
|
|
> |
89
|
|
|
{ICONS[voteType][iconToShow]} |
90
|
|
|
</Button> |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
7 |
|
return { |
95
|
|
|
name: "modular-ratebox", |
96
|
41 |
|
Component: iconArray.map((element) => element), |
97
|
|
|
commonProps: { |
98
|
|
|
...commonProps, |
99
|
|
|
className: classNames({ |
100
|
|
|
vertical: vertical, |
101
|
|
|
horizontal: !vertical, |
102
|
|
|
}), |
103
|
|
|
}, |
104
|
|
|
}; |
105
|
|
|
}, |
106
|
|
|
value, |
107
|
|
|
defaultValue: 0, |
108
|
|
|
label, |
109
|
|
|
}); |
110
|
|
|
}; |
111
|
|
|
|
112
|
|
|
export default Rater; |
113
|
|
|
|