-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add rating component and ratingCard entity
- Loading branch information
Showing
20 changed files
with
271 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/entities/Article/ui/ArticleListItem/ArticleListItemSkeleton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/entities/Notification/ui/NotificationList/NotificationList.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { RatingCard } from './ui/RatingCard'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React from 'react'; | ||
import { ComponentStory, ComponentMeta } from '@storybook/react'; | ||
|
||
import { RatingCard } from './RatingCard'; | ||
|
||
export default { | ||
title: 'shared/RatingCard', | ||
component: RatingCard, | ||
argTypes: { | ||
backgroundColor: { control: 'color' }, | ||
}, | ||
} as ComponentMeta<typeof RatingCard>; | ||
|
||
const Template: ComponentStory<typeof RatingCard> = (args) => <RatingCard {...args} />; | ||
|
||
export const Normal = Template.bind({}); | ||
Normal.args = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { useTranslation } from 'react-i18next'; | ||
import { memo, useCallback, useState } from 'react'; | ||
import { BrowserView, MobileView } from 'react-device-detect'; | ||
import { classNames } from '@/shared/lib/classNames/classNames'; | ||
import { Card } from '@/shared/ui/Card/Card'; | ||
import { HStack, VStack } from '@/shared/ui/Stack'; | ||
import { Text } from '@/shared/ui/Text/Text'; | ||
import { StarRating } from '@/shared/ui/StarRating/StarRating'; | ||
import { Modal } from '@/shared/ui/Modal/Modal'; | ||
import { Input } from '@/shared/ui/Input/Input'; | ||
import { Button, ButtonSize, ButtonTheme } from '@/shared/ui/Button/Button'; | ||
import { Drawer } from '@/shared/ui/Drawer/Drawer'; | ||
|
||
interface RatingCardProps { | ||
className?: string; | ||
title?: string; | ||
feedbackTitle?: string; | ||
hasFeedback?: boolean; | ||
onCancel?: (starsCount: number) => void; | ||
onAccept?: (starsCount: number, feedback?: string) => void; | ||
} | ||
|
||
export const RatingCard = memo((props: RatingCardProps) => { | ||
const { | ||
className, | ||
onAccept, | ||
feedbackTitle, | ||
hasFeedback, | ||
onCancel, | ||
title, | ||
} = props; | ||
const { t } = useTranslation(); | ||
const [isModalOpen, setIsModalOpen] = useState(false); | ||
const [starsCount, setStarsCount] = useState(0); | ||
const [feedback, setFeedback] = useState(''); | ||
|
||
const onSelectStars = useCallback((selectedStarsCount: number) => { | ||
setStarsCount(selectedStarsCount); | ||
if (hasFeedback) { | ||
setIsModalOpen(true); | ||
} else { | ||
onAccept?.(selectedStarsCount); | ||
} | ||
}, [hasFeedback, onAccept]); | ||
|
||
const acceptHandle = useCallback(() => { | ||
setIsModalOpen(false); | ||
onAccept?.(starsCount, feedback); | ||
}, [feedback, onAccept, starsCount]); | ||
|
||
const cancelHandle = useCallback(() => { | ||
setIsModalOpen(false); | ||
onCancel?.(starsCount); | ||
}, [onCancel, starsCount]); | ||
|
||
const modalContent = ( | ||
<> | ||
<Text | ||
title={feedbackTitle} | ||
/> | ||
<Input | ||
value={feedback} | ||
onChange={setFeedback} | ||
placeholder={t('Ваш отзыв')} | ||
/> | ||
</> | ||
); | ||
|
||
return ( | ||
<Card className={classNames('', {}, [className])}> | ||
<VStack align="center" gap="8"> | ||
<Text title={title} /> | ||
<StarRating size={40} onSelect={onSelectStars} /> | ||
</VStack> | ||
<BrowserView> | ||
<Modal isOpen={isModalOpen} lazy> | ||
<VStack max gap="32"> | ||
{modalContent} | ||
<HStack max gap="16" justify="between"> | ||
<Button onClick={cancelHandle} theme={ButtonTheme.OUTLINE_RED}> | ||
{t('Закрыть')} | ||
</Button> | ||
<Button onClick={acceptHandle}> | ||
{t('Отправить')} | ||
</Button> | ||
</HStack> | ||
</VStack> | ||
</Modal> | ||
</BrowserView> | ||
<MobileView> | ||
<Drawer isOpen={isModalOpen} lazy onClose={cancelHandle}> | ||
<VStack gap="32"> | ||
{modalContent} | ||
<Button fullWidth onClick={acceptHandle} size={ButtonSize.L}> | ||
{t('Отправить')} | ||
</Button> | ||
</VStack> | ||
</Drawer> | ||
</MobileView> | ||
</Card> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,3 +85,7 @@ | |
.isDisabled { | ||
opacity: 0.5; | ||
} | ||
|
||
.fullWidth { | ||
width: 100%; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,21 @@ | ||
import { useTranslation } from 'react-i18next'; | ||
import React, { memo } from 'react'; | ||
import { classNames } from '@/shared/lib/classNames/classNames'; | ||
import cls from './Icon.module.scss'; | ||
|
||
interface IconProps { | ||
className?: string; | ||
Svg: React.VFC<React.SVGProps<SVGSVGElement>>; | ||
inverted?: boolean; | ||
interface IconProps extends React.SVGProps<SVGSVGElement> { | ||
className?: string; | ||
Svg: React.VFC<React.SVGProps<SVGSVGElement>>; | ||
inverted?: boolean; | ||
} | ||
|
||
export const Icon = memo(({ className, Svg, inverted }: IconProps) => { | ||
const { t } = useTranslation(); | ||
export const Icon = memo((props: IconProps) => { | ||
const { | ||
className, Svg, inverted, ...otherProps | ||
} = props; | ||
return ( | ||
<Svg className={classNames(inverted ? cls.inverted : cls.Icon, {}, [className])} /> | ||
<Svg | ||
className={classNames(inverted ? cls.inverted : cls.Icon, {}, [className])} | ||
{...otherProps} | ||
/> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
.StarIcon { | ||
cursor: pointer; | ||
} | ||
|
||
.normal { | ||
fill: none; | ||
} | ||
|
||
.hovered { | ||
fill: var(--primary-color); | ||
} | ||
|
||
.isSelected { | ||
cursor: auto; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from 'react'; | ||
import { ComponentStory, ComponentMeta } from '@storybook/react'; | ||
|
||
import { StarRating } from './StarRating'; | ||
|
||
export default { | ||
title: 'shared/StarRating', | ||
component: StarRating, | ||
argTypes: { | ||
backgroundColor: { control: 'color' }, | ||
}, | ||
} as ComponentMeta<typeof StarRating>; | ||
|
||
const Template: ComponentStory<typeof StarRating> = (args) => <StarRating {...args} />; | ||
|
||
export const Default = Template.bind({}); | ||
Default.args = { | ||
onSelect: (starCount: number) => { | ||
console.log(`Selected stars: ${starCount}`); | ||
}, | ||
}; | ||
|
||
export const WithSelectedStars = Template.bind({}); | ||
WithSelectedStars.args = { | ||
selectedStars: 3, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { memo, useState } from 'react'; | ||
import { classNames } from '@/shared/lib/classNames/classNames'; | ||
import cls from './StarRating.module.scss'; | ||
import { Icon } from '@/shared/ui/Icon/Icon'; | ||
import StarIcon from '@/shared/assets/icons/star.svg'; | ||
|
||
interface StarRatingProps { | ||
className?: string; | ||
onSelect?: (starCount: number) => void; | ||
size?: number; | ||
selectedStars?: number; | ||
} | ||
|
||
const stars = [1, 2, 3, 4, 5]; | ||
|
||
export const StarRating = memo((props: StarRatingProps) => { | ||
const { | ||
className, | ||
onSelect, | ||
size = 30, | ||
selectedStars = 0, | ||
} = props; | ||
|
||
const [currentStarsCount, setCurrentStarsCount] = useState(0); | ||
const [isSelected, setIsSelected] = useState(Boolean(selectedStars)); | ||
|
||
const onHover = (starsCount: number) => { | ||
if (!isSelected) { | ||
setCurrentStarsCount(starsCount); | ||
} | ||
}; | ||
|
||
const onLeave = () => { | ||
if (!isSelected) { | ||
setCurrentStarsCount(0); | ||
} | ||
}; | ||
|
||
const onClick = (starsCount: number) => { | ||
if (!isSelected) { | ||
onSelect?.(starsCount); | ||
setCurrentStarsCount(starsCount); | ||
setIsSelected(true); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className={classNames(cls.StarRating, {}, [className])}> | ||
{stars.map((starNumber) => ( | ||
<Icon | ||
className={classNames(cls.StarIcon, { [cls.selected]: isSelected }, [currentStarsCount >= starNumber ? cls.hovered : cls.normal])} | ||
Svg={StarIcon} | ||
key={starNumber} | ||
width={size} | ||
height={size} | ||
onMouseLeave={onLeave} | ||
onMouseEnter={() => onHover(starNumber)} | ||
onClick={() => onClick(starNumber)} | ||
|
||
/> | ||
))} | ||
</div> | ||
); | ||
}); |