Skip to content

Commit 6f07f8f

Browse files
author
Rishabh
committed
adding direction aware rating icons
1 parent 6eb95a5 commit 6f07f8f

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

src/Rating/index.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,27 @@ import styled from "styled-components/native";
55

66
const StyledView = styled.View`
77
display: flex;
8-
flex-direction: row;
98
align-items: center;
109
justify-content: center;
10+
flex-direction: ${({ dir }) => `${dir}`};
1111
`;
1212
const BackgroundStars = styled.View`
1313
display: flex;
14-
flex-direction: row;
1514
position: relative;
15+
flex-direction: ${({ dir }) => `${dir}`};
1616
`;
1717
const ColoredStars = styled.View`
18-
width: ${({ percentage }) => `${percentage}%`};
1918
display: flex;
20-
flex-direction: row;
2119
overflow: hidden;
2220
position: absolute;
23-
top: 0;
21+
flex-direction: ${({ dir }) => `${dir}`};
22+
width: ${({ percentage, dir }) =>
23+
dir === "column" || dir === "column-reverse" ? `100%` : `${percentage}%`};
24+
height: ${({ percentage, dir }) =>
25+
dir === "row" || dir === "row-reverse" ? `100%` : `${percentage}%`};
26+
27+
top: ${({ dir }) => (dir === "column" ? 0 : "auto")};
28+
bottom: ${({ dir }) => (dir === "column-reverse" ? 0 : "auto")};
2429
`;
2530

2631
const Rating = ({
@@ -33,11 +38,12 @@ const Rating = ({
3338
marginBetweenRatingIcon,
3439
readonly,
3540
onStarTap,
41+
direction,
3642
}) => {
3743
const percentage = (rated / totalCount) * 100;
3844
return (
39-
<StyledView>
40-
<BackgroundStars>
45+
<StyledView dir={direction}>
46+
<BackgroundStars dir={direction}>
4147
{Array.from({ length: totalCount }, (_, i) => (
4248
<StarButton
4349
name={icon}
@@ -50,7 +56,7 @@ const Rating = ({
5056
readonly={readonly}
5157
/>
5258
))}
53-
<ColoredStars percentage={percentage}>
59+
<ColoredStars percentage={percentage} dir={direction}>
5460
{Array.from({ length: totalCount }, (_, i) => (
5561
<StarButton
5662
name={icon}
@@ -78,6 +84,7 @@ Rating.defaultProps = {
7884
icon: "ios-star",
7985
marginBetweenRatingIcon: 1,
8086
readonly: false,
87+
direction: "row",
8188
};
8289

8390
Rating.propTypes = {
@@ -90,6 +97,12 @@ Rating.propTypes = {
9097
marginBetweenRatingIcon: PropTypes.number,
9198
readonly: PropTypes.bool,
9299
onStarTap: PropTypes.func,
100+
direction: PropTypes.oneOf([
101+
"row",
102+
"row-reverse",
103+
"column",
104+
"column-reverse",
105+
]),
93106
};
94107

95108
export default Rating;

0 commit comments

Comments
 (0)