import React, { ComponentProps } from 'react'; import { View } from 'react-native'; import { Typo } from '../../foundation'; import { isNil } from '../../internal/utils/functions'; import { STAR_COUNT } from './constants'; import { ActionRating } from './ActionRating'; import { Star } from './Star'; import { formatNumber } from '../../internal/utils/numberUtils'; export type RatingStar = 'full' | 'half' | 'empty'; type BaseProps = { score: number }; type RatingWithScoreType = BaseProps & { count?: number; type?: 'full_rate' | 'single_rate'; size?: 'small' | 'medium' | 'large'; showCount?: boolean; scoreFont?: ComponentProps['type']; countColor?: ComponentProps['color']; countFont?: ComponentProps['type']; }; export type RatingWithActionType = BaseProps & { type: 'action_rate'; onPressStar: (index: number) => void; }; type Props = RatingWithScoreType | RatingWithActionType; /** * # Rating * - 별점 또는 점수와 개수를 표시하는 컴포넌트 * * ## Props * * ### `score` * - 별점 점수. 0 ~ 5 사이의 값 ex) 3.5 * - action_rate 타입일 경우, 초기 별 상태를 결정하는 값 * * * ### `count` * - 개수 ex) 리뷰 개수 * - count값을 전달하지 않았을 경우, showCount가 false가 아니라면 0으로 표시됨 * * * ### `type` * - 별점 표시 유형 (기본값: `full_rate`) * - `full_rate`: 별 다섯개 표시 * - `single_rate`: 별 하나만 표시 * - `action_rate`: 별을 눌러서 점수를 선택할 수 있는 상호작용 가능 * * * ### `size` * - 별 크기와 폰트 크기가 변경됨 (기본값: `small`) * - `small` | `medium` | `large` * - action_rate 타입일 경우, 36 고정 사이즈 * * * ### `showCount` * - 리뷰 개수 표시 여부 (기본값: `true`) * * * ### `scoreFont` * - 점수 폰트 타입 (기본값: `body_14_semibold`) * - size에 따라 default 값이 변경됨 * - `small`: `body_14_semibold` * - `medium`: `body_18_semibold` * - `large`: `headline_24_bold` * * * ### `countFont` * - 개수 폰트 타입 (기본값: `body_12_regular`) * - size에 따라 default 값이 변경됨 * - `small`: `body_12_regular` * - `medium`: `body_16_regular` * - `large`: `body_16_regular` * * * ### `countColor` * - 개수 색상 (기본값: `text_primary`) * * * ### `onPressStar` * - action_rate 타입에서 사용, 별을 눌렀을 때 호출되는 콜백 * - 선택한 별의 점수가 인자로 전달됨 * * * ## 예시 * * @example * ```tsx * // action_rate 타입 * { * setScore(value); * }} * /> * * // full_rate 타입 * * * * // single_rate 타입 * * * ``` */ export function Rating(props: Props): React.JSX.Element { const type = props.type ?? 'full_rate'; // 초기 별 상태: score가 있으면 계산된 값, 없으면 모두 empty const initialStars = (() => { if (isNil(props.score)) return createEmptyStars(); const fullStars = Math.floor(props.score); const decimal = props.score - fullStars; const stars: RatingStar[] = createEmptyStars(); if (decimal <= 0.3) { // 그대로 full 개수만큼만 채움 for (let i = 0; i < fullStars; i++) { stars[i] = 'full'; } } else if (decimal <= 0.8) { // full + half for (let i = 0; i < fullStars; i++) { stars[i] = 'full'; } stars[fullStars] = 'half'; } else { // decimal > 0.8 → 반올림해서 full 하나 더 for (let i = 0; i < fullStars + 1; i++) { stars[i] = 'full'; } } return stars; })(); if (type === 'action_rate') { const actionProps = props as RatingWithActionType; return ; } const { score, count, countColor = 'text_primary', showCount = true, size = 'small', scoreFont, countFont, } = props as RatingWithScoreType; const { starSize, scoreFontType, countFontType } = getRatingSizeProps(size); const scoreTypoType = scoreFont ?? scoreFontType; const countTypoType = countFont ?? countFontType; return ( {type === 'single_rate' ? ( // Typo와 별 높이를 맞추기 위해 paddingVertical을 조절 ) : ( {initialStars.map((starType, index) => ( ))} )} {Number(score).toFixed(1)} {showCount && ( ({formatNumber(count ?? 0)}) )} ); } function createEmptyStars() { return Array(STAR_COUNT).fill('empty'); } function getRatingSizeProps(size: 'small' | 'medium' | 'large') { switch (size) { case 'small': return { starSize: 16 as const, scoreFontType: 'body_14_semibold' as const, countFontType: 'body_12_regular' as const, }; case 'medium': return { starSize: 20 as const, scoreFontType: 'body_18_semibold' as const, countFontType: 'body_16_regular' as const, }; case 'large': default: return { starSize: 32 as const, scoreFontType: 'headline_24_bold' as const, countFontType: 'body_16_regular' as const, }; } }