Skip to content

Commit c39b136

Browse files
author
Rishabh
committed
2 parents 37525c0 + 855d0f0 commit c39b136

File tree

5 files changed

+100
-17
lines changed

5 files changed

+100
-17
lines changed

README.md

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,41 @@ import { Rating } from "react-native-rating-element";
2626
ratingColor="#b5121b"
2727
ratingBackgroundColor="#c8c7c8"
2828
size={24}
29+
readonly
2930
icon="ios-star"
30-
/>
31+
/>;
32+
33+
If you want to record user tap on star
34+
35+
<Rating
36+
rated={3.7}
37+
totalCount={5}
38+
ratingColor="#b5121b"
39+
ratingBackgroundColor="#c8c7c8"
40+
size={24}
41+
onStarTap={position => console.log(`User pressed: ${position}`)}
42+
icon="ios-star"
43+
/>;
44+
45+
3146
```
3247

3348
## API
3449

35-
| prop | default | type | description |
36-
| ---- | ---- | ----| ---- |
37-
| `rated` | 0 | number | Represents Initial value for the rating. |
38-
| `totalCount` | 5 | number | Number of background stars to show. For ex. Rated 5 out of 10 stars. The 10 value is `totalCount` |
39-
| `ratingColor` | #b5121b | string (color) | Pass in a custom color to fill-color the rating icon. |
40-
| `ratingBackgroundColor`| #c8c7c8 | string (color) | Pass in a custom fill-color for the background of rating icon. It is sometimes referred as empty icon. |
41-
| `size` | 24 | number | Pass in a custom font size for the icon |
42-
| `icon` | 25 | number | Pass in a custom text for the icon. For ex. 'beer', 'bulb'. These icons are imported from package [react-native-vector-icons](https://oblador.github.io/react-native-vector-icons/). Please Note: For now this package only support Ionicons |
50+
| prop | default | type | description |
51+
| ------------------------- | ---------- | -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
52+
| `rated` | 0 | number | Represents Initial value for the rating. |
53+
| `totalCount` | 5 | number | Number of background stars to show. For ex. Rated 5 out of 10 stars. The 10 value is `totalCount` |
54+
| `ratingColor` | #b5121b | string (color) | Pass in a custom color to fill-color the rating icon. |
55+
| `ratingBackgroundColor` | #c8c7c8 | string (color) | Pass in a custom fill-color for the background of rating icon. It is sometimes referred as empty icon. |
56+
| `size` | 24 | number | Pass in a custom font size for the icon |
57+
| `icon` | 'ios-star' | string | Pass in a custom text for the icon. For ex. 'beer', 'bulb'. These icons are imported from package [react-native-vector-icons](https://oblador.github.io/react-native-vector-icons/). Please Note: For now this package only support Ionicons |
58+
| `marginBetweenRatingIcon` | 1 | number | Pass in custom number to manage space or margin between the rating icons. |
59+
| `onStarTap` | - | func | On press of star icon by user, this function will be invoked with `position` paramter. For ex. when user taps on 4 rating, this function will be invoked and in `position` parameter you will get value 4. |
60+
| `readonly` | false | bool | If passed true, onPress event won't be fired. |
4361

4462
## Output
63+
4564
![Output](https://i.ibb.co/R7f680V/output.png)
4665

4766
## Contributing

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-rating-element",
3-
"version": "3.0.0",
3+
"version": "4.0.1",
44
"description": "A simple rating library for react native supporting decimal point and custom icon set",
55
"main": "index.js",
66
"scripts": {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React from "react";
2+
import PropTypes from "prop-types";
3+
import Icon from "react-native-vector-icons/Ionicons";
4+
5+
import styled from "styled-components/native";
6+
7+
const StarIcon = styled(Icon)`
8+
margin: ${({ margin }) => (margin ? `0 ${margin}px` : "0 1px")};
9+
`;
10+
11+
const StarButton = ({
12+
name,
13+
size,
14+
color,
15+
margin,
16+
onStarTap,
17+
readonly,
18+
position,
19+
}) => (
20+
<StarIcon
21+
onPress={() => {
22+
if (!readonly) {
23+
onStarTap(position + 1);
24+
}
25+
}}
26+
name={name}
27+
size={size}
28+
color={color}
29+
margin={margin}
30+
/>
31+
);
32+
33+
StarButton.propTypes = {
34+
color: PropTypes.string,
35+
size: PropTypes.number,
36+
name: PropTypes.string,
37+
margin: PropTypes.number,
38+
onStarTap: PropTypes.func,
39+
readonly: PropTypes.bool,
40+
position: PropTypes.number,
41+
};
42+
43+
export default StarButton;

src/Rating/index.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react";
22
import PropTypes from "prop-types";
3+
import StarButton from "./components/StarButton";
34
import styled from "styled-components/native";
4-
import Icon from "react-native-vector-icons/Ionicons";
55

66
const StyledView = styled.View`
77
display: flex;
@@ -22,9 +22,6 @@ const ColoredStars = styled.View`
2222
position: absolute;
2323
top: 0;
2424
`;
25-
const StarIcon = styled(Icon)`
26-
margin-right: 2px;
27-
`;
2825

2926
const Rating = ({
3027
rated,
@@ -33,17 +30,36 @@ const Rating = ({
3330
ratingBackgroundColor,
3431
size,
3532
icon,
33+
marginBetweenRatingIcon,
34+
readonly,
35+
onStarTap,
3636
}) => {
3737
const percentage = (rated / totalCount) * 100;
3838
return (
3939
<StyledView>
4040
<BackgroundStars>
4141
{Array.from({ length: totalCount }, (_, i) => (
42-
<StarIcon name={icon} size={size} color={ratingBackgroundColor} />
42+
<StarButton
43+
name={icon}
44+
size={size}
45+
position={i}
46+
color={ratingBackgroundColor}
47+
margin={marginBetweenRatingIcon}
48+
onStarTap={onStarTap}
49+
readonly={readonly}
50+
/>
4351
))}
4452
<ColoredStars percentage={percentage}>
4553
{Array.from({ length: totalCount }, (_, i) => (
46-
<StarIcon name={icon} size={size} color={ratingColor} />
54+
<StarButton
55+
name={icon}
56+
size={size}
57+
color={ratingColor}
58+
position={i}
59+
margin={marginBetweenRatingIcon}
60+
onStarTap={onStarTap}
61+
readonly={readonly}
62+
/>
4763
))}
4864
</ColoredStars>
4965
</BackgroundStars>
@@ -58,6 +74,8 @@ Rating.defaultProps = {
5874
ratingBackgroundColor: "#c8c7c8",
5975
size: 12,
6076
icon: "ios-star",
77+
marginBetweenRatingIcon: 1,
78+
readonly: false,
6179
};
6280

6381
Rating.propTypes = {
@@ -67,6 +85,9 @@ Rating.propTypes = {
6785
ratingBackgroundColor: PropTypes.string,
6886
size: PropTypes.number,
6987
icon: PropTypes.string,
88+
marginBetweenRatingIcon: PropTypes.number,
89+
readonly: PropTypes.bool,
90+
onStarTap: PropTypes.func,
7091
};
7192

7293
export default Rating;

0 commit comments

Comments
 (0)