Skip to content

Commit 3d8bd44

Browse files
Merge pull request #69 from ReDI-School/enhancement/rating-group-#68
Enhancement/rating group #68
2 parents fe1397e + 8d38505 commit 3d8bd44

File tree

3 files changed

+29
-31
lines changed

3 files changed

+29
-31
lines changed

frontend/src/components/RatingGroup/RatingGroup.stories.tsx

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,9 @@ export default meta;
1212

1313
const createIconButtons = (size: 'small' | 'medium' | 'large') => {
1414
return [
15-
<IconButton
16-
key={0}
17-
icon="thumbs-down"
18-
size={size}
19-
variant="outlined"
20-
theme="primary"
21-
selected
22-
/>,
23-
<IconButton
24-
key={1}
25-
icon="thumbs-up"
26-
size={size}
27-
variant="outlined"
28-
theme="primary"
29-
selected={false}
30-
/>,
31-
<IconButton
32-
key={2}
33-
icon="double-thumbs-up"
34-
size={size}
35-
variant="outlined"
36-
theme="primary"
37-
selected={false}
38-
/>,
15+
<IconButton icon="thumbs-down" size={size} variant="outlined" theme="primary" />,
16+
<IconButton icon="thumbs-up" size={size} variant="outlined" theme="primary" selected />,
17+
<IconButton icon="double-thumbs-up" size={size} variant="outlined" theme="primary" />,
3918
];
4019
};
4120

frontend/src/components/RatingGroup/RatingGroup.tsx

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import type { RatingGroupProps } from './RatingGroup.types';
2-
import styles from './RatingGroup.module.css';
31
import { cva } from 'class-variance-authority';
2+
import { cloneElement, useState } from 'react';
3+
4+
import type { RatingGroupProps } from './index';
5+
import styles from './RatingGroup.module.css';
46

57
const styledRatingGroup = cva(styles.ratingGroup, {
68
variants: {
@@ -13,7 +15,24 @@ const styledRatingGroup = cva(styles.ratingGroup, {
1315
});
1416

1517
const RatingGroup = ({ children, size = 'medium', className }: RatingGroupProps) => {
16-
return <div className={styledRatingGroup({ size, className })}>{children}</div>;
18+
const [selectedIndex, setSelectedIndex] = useState<number | null>(
19+
children.findIndex((c) => c.props.selected)
20+
);
21+
22+
const handleItemClick = (index: number) => {
23+
setSelectedIndex(index);
24+
};
25+
26+
return (
27+
<div className={styledRatingGroup({ size, className })}>
28+
{children.map((iconButton, index) => {
29+
return cloneElement(iconButton, {
30+
onClick: () => handleItemClick(index),
31+
selected: selectedIndex === index,
32+
});
33+
})}
34+
</div>
35+
);
1736
};
1837

1938
export default RatingGroup;

frontend/src/components/RatingGroup/RatingGroup.types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import type React from 'react';
2-
import type { IconButton } from '../IconButton';
1+
import type { IconButtonProps } from '../IconButton';
2+
import type { ReactElement } from 'react';
33

44
interface RatingGroupProps {
55
/**
66
* The array of IconButton Component to display
77
*/
8-
children: React.ReactElement<typeof IconButton>[];
8+
children: ReactElement<IconButtonProps>[];
99

1010
/**
11-
* The array of IconButton Component to display
11+
* The size of IconButton Components to display
1212
*/
1313
size?: 'small' | 'medium' | 'large';
1414

0 commit comments

Comments
 (0)