Skip to content

Commit 71045a6

Browse files
Fix: Added support for ordering rating summary and fixed minor issues (#17)
* Added support for ordering rating summary, fixed console error related to svg, updated readMe * formatted code
1 parent 3e949fe commit 71045a6

File tree

8 files changed

+71
-47
lines changed

8 files changed

+71
-47
lines changed

README.md

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ Props that can be passed to the component are listed below:
176176
</tr>
177177
<tr>
178178
<td><code><b>ratingAverageIconProps?:</b> object</code></td>
179-
<td>An object defining the fill color and background color for customizing the appearance of star icon in the average rating section.</td>
179+
<td>An object defining the fill color ( fillColor?: string ) and background color ( bgColor?: string ) for customizing the appearance of star icon in the average rating section.</td>
180180
<td><code>undefined</code></td>
181181
</tr>
182182
<tr>
@@ -189,6 +189,11 @@ Props that can be passed to the component are listed below:
189189
<td>A string used to customize the text accompanying the star rating average which provides additional information about the total number of reviews.</td>
190190
<td><code>'reviews'</code></td>
191191
</tr>
192+
<tr>
193+
<td><code><b>order?:</b> 'ORIGINAL' | 'REVERSE'</code></td>
194+
<td>The order prop dictates the summary section's display order. Possible values are: 'ORIGINAL' or 'REVERSE'. For numeric ratingIds, it sorts in ascending (ORIGINAL) or descending (REVERSE) order. For string based ratingIds, it reflects the original/reversed order of keys in the ratings prop.</td>
195+
<td><code>'REVERSE'</code></td>
196+
</tr>
192197
</tbody>
193198
</table>
194199

@@ -247,22 +252,22 @@ import RatingSummary from '@keyvaluesystems/react-star-rating-summary';
247252
function App() {
248253

249254
const countColors = {
250-
1: 'red',
251-
2: 'yellow',
252-
3: 'blue',
253-
4: 'orange',
254-
5: 'white'
255+
1: 'red',
256+
2: 'yellow',
257+
3: 'blue',
258+
4: 'orange',
259+
5: 'white'
255260
};
256261

257262
return (
258263
<RatingSummary
259264
ratings={{
260-
1: 100,
261-
2: 200,
262-
3: 300,
263-
4: 400,
264-
5: 500
265-
}}
265+
1: 100,
266+
2: 200,
267+
3: 300,
268+
4: 400,
269+
5: 500
270+
}}
266271
styles={{
267272
Average: { color: 'purple' },
268273
AverageStarIcon: {

src/assets/star-grey.svg

Lines changed: 0 additions & 1 deletion
Loading

src/constants.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ export enum Elements {
1919
Label = 'Label',
2020
LabelStarIcon = 'LabelStarIcon'
2121
}
22+
23+
export enum ORDER {
24+
ORIGINAL = 'ORIGINAL',
25+
REVERSE = 'REVERSE'
26+
}
27+
2228
export const INTERNATIONAL_NUMBER_SYSTEM_REGEX = /\B(?=(\d{3})+(?!\d))/g;
2329

2430
export const DEFAULT_BAR_COLORS = {

src/rating-average/star/Star.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,7 @@ const Star: FC<StarProp> = (props) => {
66
const { fillColor, bgColor, colorFilledFraction, id } = props;
77

88
return (
9-
<svg
10-
width="100%"
11-
height="auto"
12-
viewBox="0 0 32 32"
13-
xmlns="http://www.w3.org/2000/svg"
14-
>
9+
<svg width="100%" viewBox="0 0 32 32" xmlns="http://www.w3.org/2000/svg">
1510
<defs>
1611
<linearGradient id={id} shapeRendering="crispEdges">
1712
<stop offset={colorFilledFraction} stopColor={fillColor} />

src/rating-summary/RatingSummary.tsx

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React, { FC } from 'react';
33
import { ISummaryProp, RatingRanks } from './types';
44
import RatingLabel from '../rating-label';
55
import RatingDistributionItem from '../rating-distribution-item';
6-
import { Elements, GenericElements } from '../constants';
6+
import { Elements, GenericElements, ORDER } from '../constants';
77
import { getStyles, getTotalRatingCount } from '../utils';
88
import RatingAverage from '../rating-average';
99
import classes from './styles.module.scss';
@@ -23,7 +23,8 @@ const RatingSummary: FC<ISummaryProp> = (props) => {
2323
averageRatingPrecision = 1,
2424
ratingAverageIconProps = {},
2525
thousandsSeparator,
26-
ratingAverageSubText = 'reviews'
26+
ratingAverageSubText = 'reviews',
27+
order = ORDER.REVERSE
2728
} = props;
2829

2930
const getRatingRanks = (): RatingRanks => {
@@ -39,6 +40,11 @@ const RatingSummary: FC<ISummaryProp> = (props) => {
3940

4041
const ranks: RatingRanks = getRatingRanks();
4142

43+
const ratingKeys =
44+
order === ORDER.REVERSE
45+
? Object.keys(ratings).reverse()
46+
: Object.keys(ratings);
47+
4248
return (
4349
<div className={classes.container} style={styles[GenericElements.Root]}>
4450
{showAverageRating && (
@@ -58,30 +64,28 @@ const RatingSummary: FC<ISummaryProp> = (props) => {
5864
style={styles[GenericElements.SummaryContainer]}
5965
id="ratings-container"
6066
>
61-
{Object.keys(ratings)
62-
.reverse()
63-
.map((ratingId) => (
64-
<div
65-
key={ratingId}
66-
className={classes.ratingWrapper}
67-
style={getStyles(styles, Elements.SummaryItemContainer, ratingId)}
68-
>
69-
{(renderLabel && <>{renderLabel(ratingId)}</>) || (
70-
<RatingLabel ratingId={ratingId} styles={styles} />
71-
)}
72-
<RatingDistributionItem
73-
currentRatingId={ratingId}
74-
currentRatingValue={ratings[ratingId]}
75-
totalRatingCount={getTotalRatingCount(ratings)}
76-
showCount={showCount}
77-
showAnimation={showAnimation}
78-
styles={styles}
79-
barColors={barColors}
80-
onBarClick={onBarClick}
81-
thousandsSeparator={thousandsSeparator}
82-
/>
83-
</div>
84-
))}
67+
{ratingKeys.map((ratingId) => (
68+
<div
69+
key={ratingId}
70+
className={classes.ratingWrapper}
71+
style={getStyles(styles, Elements.SummaryItemContainer, ratingId)}
72+
>
73+
{(renderLabel && <>{renderLabel(ratingId)}</>) || (
74+
<RatingLabel ratingId={ratingId} styles={styles} />
75+
)}
76+
<RatingDistributionItem
77+
currentRatingId={ratingId}
78+
currentRatingValue={ratings[ratingId]}
79+
totalRatingCount={getTotalRatingCount(ratings)}
80+
showCount={showCount}
81+
showAnimation={showAnimation}
82+
styles={styles}
83+
barColors={barColors}
84+
onBarClick={onBarClick}
85+
thousandsSeparator={thousandsSeparator}
86+
/>
87+
</div>
88+
))}
8589
</div>
8690
</div>
8791
);

src/rating-summary/types.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ReactElement } from 'react';
22

3-
import { Elements, GenericElements } from '../constants';
3+
import { Elements, GenericElements, ORDER } from '../constants';
44

55
export type StyleObjectType = React.CSSProperties;
66

@@ -48,4 +48,5 @@ export type ISummaryProp = {
4848
ratingAverageIconProps?: RatingAverageIconProps;
4949
thousandsSeparator?: string;
5050
ratingAverageSubText?: string;
51+
order?: ORDER;
5152
};

src/stories/RatingSummary.stories.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ export default {
4848
fillColor: '#919191',
4949
bgColor: '#F2F2F2'
5050
}
51+
},
52+
order: {
53+
control: { type: 'radio' },
54+
options: ['ORIGINAL', 'REVERSE']
5155
}
5256
}
5357
} as ComponentMeta<typeof Component>;
@@ -205,3 +209,9 @@ VariantWithCustomRanks.args = {
205209
},
206210
ratingAverageSubText: 'total'
207211
};
212+
213+
export const VariantWithOriginalOrder = Template.bind({});
214+
VariantWithOriginalOrder.args = {
215+
...VariantWithStringBasedRatings.args,
216+
order: 'ORIGINAL'
217+
};

src/tests/ratingSummary.test.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import React from 'react';
22
import { render, queryByAttribute } from '@testing-library/react';
33
import '@testing-library/jest-dom/extend-expect';
4+
45
import RatingSummary from '../rating-summary';
56
import { IRatings } from '../rating-summary/types';
7+
import { ORDER } from '../constants';
68

79
const getById = queryByAttribute.bind(null, 'id');
810
describe('RatingSummary', () => {
@@ -15,7 +17,9 @@ describe('RatingSummary', () => {
1517
};
1618
const total = 575;
1719
it('should render the RatingSummary component', () => {
18-
const { container } = render(<RatingSummary ratings={ratings} />);
20+
const { container } = render(
21+
<RatingSummary ratings={ratings} order={ORDER.ORIGINAL} />
22+
);
1923
const ratingSummaryComponent = getById(container, 'ratings-container');
2024
if (!ratingSummaryComponent) throw Error('No Component present');
2125
});

0 commit comments

Comments
 (0)