[go: nahoru, domu]

Skip to content

Commit

Permalink
Creating the Stars
Browse files Browse the repository at this point in the history
  • Loading branch information
AmirrMahmoudi committed Jan 1, 2024
1 parent f5c4189 commit dbee663
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
54 changes: 50 additions & 4 deletions src/components/StarRating.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, { useState } from "react";

const containerStyle = {
display: "flex",
Expand All @@ -8,7 +8,6 @@ const containerStyle = {

const starContainerStyle = {
display: "flex",
gap: "4px",
};

const textStyle = {
Expand All @@ -17,16 +16,63 @@ const textStyle = {
};

const StarRating = ({ maxRating = 5 }) => {
const [raiting, setRating] = useState(1);

function handleRating(rating) {
setRating(rating);
}

return (
<div style={containerStyle}>
<div style={starContainerStyle}>
{Array.from({ length: maxRating }, (_, i) => (
<span>S{i + 1}</span>
<Star
key={i}
onRate={() => handleRating(i + 1)}
full={raiting >= i + 1}
/>
))}
</div>
<p style={textStyle}>10</p>
<p style={textStyle}>{raiting || ""}</p>
</div>
);
};

export default StarRating;

const StarStyle = {
width: "48px",
height: "48px",
display: "block",
cursor: "pointer",
};
function Star({ onRate, full }) {
return (
<span role="button" style={StarStyle} onClick={onRate}>
{full ? (
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="#000"
stroke="#000"
>
<path d="M9.049 2.927c.3-.921 1.603-.921 1.902 0l1.07 3.292a1 1 0 00.95.69h3.462c.969 0 1.371 1.24.588 1.81l-2.8 2.034a1 1 0 00-.364 1.118l1.07 3.292c.3.921-.755 1.688-1.54 1.118l-2.8-2.034a1 1 0 00-1.175 0l-2.8 2.034c-.784.57-1.838-.197-1.539-1.118l1.07-3.292a1 1 0 00-.364-1.118L2.98 8.72c-.783-.57-.38-1.81.588-1.81h3.461a1 1 0 00.951-.69l1.07-3.292z" />
</svg>
) : (
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke="#000"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="{2}"
d="M11.049 2.927c.3-.921 1.603-.921 1.902 0l1.519 4.674a1 1 0 00.95.69h4.915c.969 0 1.371 1.24.588 1.81l-3.976 2.888a1 1 0 00-.363 1.118l1.518 4.674c.3.922-.755 1.688-1.538 1.118l-3.976-2.888a1 1 0 00-1.176 0l-3.976 2.888c-.783.57-1.838-.197-1.538-1.118l1.518-4.674a1 1 0 00-.363-1.118l-3.976-2.888c-.784-.57-.38-1.81.588-1.81h4.914a1 1 0 00.951-.69l1.519-4.674z"
/>
</svg>
)}
</span>
);
}
11 changes: 1 addition & 10 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -352,16 +352,7 @@ FONT SIZE SYSTEM (px)
/*
FULL STAR
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="#000"
stroke="#000"
>
<path
d="M9.049 2.927c.3-.921 1.603-.921 1.902 0l1.07 3.292a1 1 0 00.95.69h3.462c.969 0 1.371 1.24.588 1.81l-2.8 2.034a1 1 0 00-.364 1.118l1.07 3.292c.3.921-.755 1.688-1.54 1.118l-2.8-2.034a1 1 0 00-1.175 0l-2.8 2.034c-.784.57-1.838-.197-1.539-1.118l1.07-3.292a1 1 0 00-.364-1.118L2.98 8.72c-.783-.57-.38-1.81.588-1.81h3.461a1 1 0 00.951-.69l1.07-3.292z"
/>
</svg>
EMPTY STAR
Expand Down
2 changes: 0 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@ root.render(
<React.StrictMode>
{/* <App /> */}
<StarRating maxRating={5} />
<StarRating maxRating={10} />
<StarRating />
</React.StrictMode>
);

0 comments on commit dbee663

Please sign in to comment.