@momo-kits/rating 0.169.4 → 0.169.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.tsx +15 -17
  2. package/package.json +1 -1
package/index.tsx CHANGED
@@ -1,8 +1,9 @@
1
- import {Colors, Icon} from '@momo-kits/foundation';
2
- import React, {FC} from 'react';
3
- import {StyleSheet, TouchableOpacity, View} from 'react-native';
4
- import {RatingProps} from './type';
1
+ import { Colors, Icon } from '@momo-kits/foundation';
2
+ import React, { FC } from 'react';
3
+ import { StyleSheet, TouchableOpacity, View } from 'react-native';
4
+ import { RatingProps } from './type';
5
5
 
6
+ /** Parses a number prop that may be a string with a comma decimal (`'4,5'`). */
6
7
  const toNumber = (value: number | string) => {
7
8
  if (typeof value === 'string') {
8
9
  return parseFloat(value.replace(',', '.'));
@@ -10,6 +11,10 @@ const toNumber = (value: number | string) => {
10
11
  return value;
11
12
  };
12
13
 
14
+ /**
15
+ * Row of `numOfStars` stars filled up to `rating`, with a half star for a
16
+ * fractional rating. Stars are pressable only when `onRatingChange` is set.
17
+ */
13
18
  const Rating: FC<RatingProps> = ({
14
19
  rating,
15
20
  numOfStars,
@@ -22,28 +27,22 @@ const Rating: FC<RatingProps> = ({
22
27
  const normalizedNumOfStars = toNumber(numOfStars);
23
28
 
24
29
  const renderStar = (index: number) => {
25
- // Full star
26
30
  if (index < Math.floor(normalizedRating)) {
27
31
  return 'reaction_star_full';
28
32
  }
29
- // Half star
30
33
  if (index === Math.floor(normalizedRating) && normalizedRating % 1 !== 0) {
31
34
  return 'reaction_star_half';
32
35
  }
33
- // Empty star
34
36
  return 'reaction_star_non';
35
37
  };
36
38
 
37
39
  const tintColor = (index: number) => {
38
- // Full star
39
40
  if (index < Math.floor(normalizedRating)) {
40
41
  return Colors.yellow_03;
41
42
  }
42
- // Half star
43
43
  if (index === Math.floor(normalizedRating) && normalizedRating % 1 !== 0) {
44
44
  return Colors.yellow_03;
45
45
  }
46
- // Empty star
47
46
  return Colors.black_07;
48
47
  };
49
48
 
@@ -56,18 +55,17 @@ const Rating: FC<RatingProps> = ({
56
55
 
57
56
  return (
58
57
  <View style={[style, styles.container]}>
59
- {Array.from({length: normalizedNumOfStars}, (_, index) => (
58
+ {Array.from({ length: normalizedNumOfStars }, (_, index) => (
60
59
  <TouchableOpacity
61
60
  key={index}
62
61
  onPress={() => onRatingChange && onRatingChange(index + 1)}
63
- disabled={!onRatingChange} // Disable interaction if no callback
64
- {...props}>
62
+ disabled={!onRatingChange}
63
+ {...props}
64
+ >
65
65
  <Icon
66
66
  source={renderStar(index)}
67
67
  size={sizes}
68
- color={
69
- tintColor(index) // Set color based on rating
70
- }
68
+ color={tintColor(index)}
71
69
  />
72
70
  </TouchableOpacity>
73
71
  ))}
@@ -81,4 +79,4 @@ const styles = StyleSheet.create({
81
79
  },
82
80
  });
83
81
 
84
- export {Rating};
82
+ export { Rating };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momo-kits/rating",
3
- "version": "0.169.4",
3
+ "version": "0.169.5",
4
4
  "private": false,
5
5
  "main": "index.tsx",
6
6
  "peerDependencies": {