@momo-kits/rating 0.102.3-beta.1
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.
- package/index.tsx +71 -0
- package/package.json +17 -0
- package/publish.sh +27 -0
- package/type.ts +25 -0
package/index.tsx
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import React, {FC} from 'react';
|
|
2
|
+
import {StyleSheet, TouchableOpacity, View} from 'react-native';
|
|
3
|
+
import {Colors} from '../Consts';
|
|
4
|
+
import {Icon} from '../Icon';
|
|
5
|
+
import {RatingProps} from './type';
|
|
6
|
+
|
|
7
|
+
const Rating: FC<RatingProps> = (
|
|
8
|
+
{rating, numOfStars, onRatingChange, size, style},
|
|
9
|
+
props
|
|
10
|
+
) => {
|
|
11
|
+
const normalizedRating =
|
|
12
|
+
typeof rating === 'string' ? parseFloat(rating.replace(',', '.')) : rating;
|
|
13
|
+
const normalizedNumOfStars =
|
|
14
|
+
typeof numOfStars === 'string' ? parseFloat(numOfStars) : numOfStars;
|
|
15
|
+
|
|
16
|
+
const renderStar = (index: number) => {
|
|
17
|
+
// Full star
|
|
18
|
+
if (index < Math.floor(normalizedRating)) {
|
|
19
|
+
return 'reaction_star_full';
|
|
20
|
+
}
|
|
21
|
+
// Half star
|
|
22
|
+
if (index === Math.floor(normalizedRating) && normalizedRating % 1 !== 0) {
|
|
23
|
+
return 'reaction_star_half';
|
|
24
|
+
}
|
|
25
|
+
// Empty star
|
|
26
|
+
return 'reaction_star_non';
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const tintColor = (index: number) => {
|
|
30
|
+
// Full star
|
|
31
|
+
if (index < Math.floor(normalizedRating)) {
|
|
32
|
+
return Colors.yellow_03;
|
|
33
|
+
}
|
|
34
|
+
// Half star
|
|
35
|
+
if (index === Math.floor(normalizedRating) && normalizedRating % 1 !== 0) {
|
|
36
|
+
return Colors.yellow_03;
|
|
37
|
+
}
|
|
38
|
+
// Empty star
|
|
39
|
+
return Colors.black_07;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const sizes = size === 'small' ? 16 : size === 'medium' ? 20 : 24;
|
|
43
|
+
|
|
44
|
+
return (
|
|
45
|
+
<View style={[style, styles.container]}>
|
|
46
|
+
{Array.from({length: normalizedNumOfStars}, (_, index) => (
|
|
47
|
+
<TouchableOpacity
|
|
48
|
+
key={index}
|
|
49
|
+
onPress={() => onRatingChange && onRatingChange(index + 1)}
|
|
50
|
+
disabled={!onRatingChange} // Disable interaction if no callback
|
|
51
|
+
{...props}>
|
|
52
|
+
<Icon
|
|
53
|
+
source={renderStar(index)}
|
|
54
|
+
size={sizes}
|
|
55
|
+
color={
|
|
56
|
+
tintColor(index) // Set color based on rating
|
|
57
|
+
}
|
|
58
|
+
/>
|
|
59
|
+
</TouchableOpacity>
|
|
60
|
+
))}
|
|
61
|
+
</View>
|
|
62
|
+
);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const styles = StyleSheet.create({
|
|
66
|
+
container: {
|
|
67
|
+
flexDirection: 'row',
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
export {Rating};
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@momo-kits/rating",
|
|
3
|
+
"version": "0.102.3-beta.1",
|
|
4
|
+
"private": false,
|
|
5
|
+
"main": "index.tsx",
|
|
6
|
+
"peerDependencies": {
|
|
7
|
+
"@momo-kits/foundation": "latest",
|
|
8
|
+
"react": "16.9.0",
|
|
9
|
+
"react-native": ">=0.55",
|
|
10
|
+
"prop-types": "^15.7.2"
|
|
11
|
+
},
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"@momo-platform/versions": "4.1.11"
|
|
14
|
+
},
|
|
15
|
+
"license": "MoMo",
|
|
16
|
+
"dependencies": {}
|
|
17
|
+
}
|
package/publish.sh
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
|
|
3
|
+
# Prepare dist files
|
|
4
|
+
rm -rf dist
|
|
5
|
+
mkdir dist
|
|
6
|
+
rsync -r --exclude=/dist ./* dist
|
|
7
|
+
cd dist
|
|
8
|
+
|
|
9
|
+
if [ "$1" == "stable" ]; then
|
|
10
|
+
npm version $(npm view @momo-kits/avatar@stable version)
|
|
11
|
+
npm version patch
|
|
12
|
+
npm publish --tag stable --access=public
|
|
13
|
+
elif [ "$1" == "latest" ]; then
|
|
14
|
+
npm version $(npm view @momo-kits/foundation@latest version)
|
|
15
|
+
npm publish --tag latest --access=public
|
|
16
|
+
else
|
|
17
|
+
npm version $(npm view @momo-kits/avatar@beta version)
|
|
18
|
+
npm version prerelease --preid=beta
|
|
19
|
+
npm publish --tag beta --access=public
|
|
20
|
+
fi
|
|
21
|
+
|
|
22
|
+
PACKAGE_NAME=$(npm pkg get name)
|
|
23
|
+
NEW_PACKAGE_VERSION=$(npm pkg get version)
|
|
24
|
+
|
|
25
|
+
# Clean up
|
|
26
|
+
cd ..
|
|
27
|
+
rm -rf dist
|
package/type.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import {TouchableOpacityProps} from 'react-native';
|
|
2
|
+
|
|
3
|
+
export interface RatingProps extends TouchableOpacityProps {
|
|
4
|
+
/**
|
|
5
|
+
* `numOfStars`: Represents the number of stars in the Rating component.
|
|
6
|
+
*/
|
|
7
|
+
numOfStars: number | string;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* `rating`: Represents the rating value of the Rating component.
|
|
11
|
+
*/
|
|
12
|
+
rating: number | string;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* `onRatingChange`: A callback function that is called when the rating value of the Rating component changes.
|
|
16
|
+
* It is triggered when the user interacts with the Rating component.
|
|
17
|
+
*/
|
|
18
|
+
onRatingChange: (rating: number) => void;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* `size`: Optional. Represents the size of the Rating component.
|
|
22
|
+
* Defaults to `large` is 24 if not provided.
|
|
23
|
+
*/
|
|
24
|
+
size?: 'small' | 'medium' | 'large';
|
|
25
|
+
}
|