Skip to content

Commit 7be12ba

Browse files
committed
fix: badge and dot ordering are index based to match items
1 parent c8a6549 commit 7be12ba

File tree

4 files changed

+19
-19
lines changed

4 files changed

+19
-19
lines changed

index.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,10 @@ declare module 'react-native-dropdown-picker' {
9292
badgeDotStyle: StyleProp<ViewStyle>;
9393
badgeStyle: StyleProp<ViewStyle>;
9494
badgeTextStyle: StyleProp<TextStyle>;
95-
getBadgeColor: (value: string) => string;
96-
getBadgeDotColor: (value: string) => string;
95+
getBadgeColor: (value: string, index: number) => string;
96+
getBadgeDotColor: (value: string, index: number) => string;
9797
IconComponent: () => JSX.Element;
98+
index: number;
9899
label: string;
99100
onPress: (value: T) => void;
100101
props: TouchableOpacityProps;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-dropdown-picker",
3-
"version": "5.5.0",
3+
"version": "5.5.1",
44
"description": "A single / multiple, categorizable, customizable, localizable and searchable item picker (drop-down) component for react native which supports both Android & iOS.",
55
"keywords": [
66
"picker",

src/components/Picker.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,29 +1037,25 @@ function Picker({
10371037
/**
10381038
* Get badge color.
10391039
* @param {string} str
1040+
* @param {number} index
10401041
* @returns {string}
10411042
*/
10421043
const getBadgeColor = useCallback(
1043-
(str) => {
1044-
str = `${str}`;
1045-
1046-
const index = Math.abs(ASCII_CODE(str)) % _badgeColors.length;
1047-
return _badgeColors[index];
1044+
(str, index) => {
1045+
return _badgeColors[index % _badgeColors.length];
10481046
},
10491047
[_badgeColors],
10501048
);
10511049

10521050
/**
10531051
* Get badge dot color.
10541052
* @param {string} str
1053+
* @param {number} index
10551054
* @returns {string}
10561055
*/
10571056
const getBadgeDotColor = useCallback(
1058-
(str) => {
1059-
str = `${str}`;
1060-
1061-
const index = Math.abs(ASCII_CODE(str)) % _badgeDotColors.length;
1062-
return _badgeDotColors[index];
1057+
(str, index) => {
1058+
return _badgeDotColors[index % _badgeDotColors.length];
10631059
},
10641060
[_badgeDotColors],
10651061
);
@@ -1078,7 +1074,7 @@ function Picker({
10781074
* @returns {JSX.Element}
10791075
*/
10801076
const __renderBadge = useCallback(
1081-
({ item }) => (
1077+
({ item, index }) => (
10821078
<RenderBadgeComponent
10831079
props={badgeProps}
10841080
rtl={rtl}
@@ -1095,6 +1091,7 @@ function Picker({
10951091
onPress={onPressBadge}
10961092
theme={theme}
10971093
THEME={THEME}
1094+
index={index}
10981095
/>
10991096
),
11001097
[
@@ -1218,7 +1215,7 @@ function Picker({
12181215
<View style={extendableBadgeContainerStyle}>
12191216
{selectedItems.map((item, index) => (
12201217
<View key={index} style={extendableBadgeItemContainerStyle}>
1221-
<__renderBadge item={item} />
1218+
<__renderBadge item={item} index={index} />
12221219
</View>
12231220
))}
12241221
</View>

src/components/RenderBadgeItem.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ function RenderBadge({
1818
showBadgeDot,
1919
onPress,
2020
THEME,
21+
index,
2122
}) {
2223
/**
2324
* onPress.
@@ -33,10 +34,10 @@ function RenderBadge({
3334
RTL_DIRECTION(rtl, THEME.badgeStyle),
3435
...[badgeStyle].flat(),
3536
{
36-
backgroundColor: getBadgeColor(value),
37+
backgroundColor: getBadgeColor(value, index),
3738
},
3839
],
39-
[THEME, rtl, badgeStyle, getBadgeColor],
40+
[THEME, rtl, badgeStyle, getBadgeColor, index],
4041
);
4142

4243
/**
@@ -48,10 +49,10 @@ function RenderBadge({
4849
RTL_STYLE(rtl, THEME.badgeDotStyle),
4950
...[badgeDotStyle].flat(),
5051
{
51-
backgroundColor: getBadgeDotColor(value),
52+
backgroundColor: getBadgeDotColor(value, index),
5253
},
5354
],
54-
[THEME, rtl, badgeDotStyle, getBadgeDotColor],
55+
[THEME, rtl, badgeDotStyle, getBadgeDotColor, index],
5556
);
5657

5758
/**
@@ -77,6 +78,7 @@ const areEqual = (nextProps, prevProps) => {
7778
if (nextProps.showBadgeDot !== prevProps.showBadgeDot) return false;
7879
if (nextProps.rtl !== prevProps.rtl) return false;
7980
if (nextProps.theme !== prevProps.theme) return false;
81+
if (nextProps.index !== prevProps.index) return false;
8082

8183
return true;
8284
};

0 commit comments

Comments
 (0)