Skip to content

Commit 64de740

Browse files
Merge pull request #27 from SimformSolutionsPvtLtd/fix/UNT-T8848
Fix issue and add feature
2 parents a7245dc + 9c5aeba commit 64de740

File tree

12 files changed

+110
-6908
lines changed

12 files changed

+110
-6908
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
# react-native-radial-slider
44

5-
[![npm version](https://img.shields.io/badge/npm%20package-0.0.3-orange)](https://www.npmjs.org/package/react-native-radial-slider) [![Android](https://img.shields.io/badge/Platform-Android-green?logo=android)](https://www.android.com) [![iOS](https://img.shields.io/badge/Platform-iOS-green?logo=apple)](https://developer.apple.com/ios) [![MIT](https://img.shields.io/badge/License-MIT-green)](https://opensource.org/licenses/MIT)
5+
[![npm version](https://img.shields.io/badge/npm%20package-0.0.4-orange)](https://www.npmjs.org/package/react-native-radial-slider) [![Android](https://img.shields.io/badge/Platform-Android-green?logo=android)](https://www.android.com) [![iOS](https://img.shields.io/badge/Platform-iOS-green?logo=apple)](https://developer.apple.com/ios) [![MIT](https://img.shields.io/badge/License-MIT-green)](https://opensource.org/licenses/MIT)
66

77
---
88

9-
This is a pure javascript and react-native-svg based library that provides many variants of `Radial Slider` and `Speedo Meter` including `default`, `radial-circle-slider`, `speedometer` and `speedometer-marker`
10-
9+
This is a pure javascript and react-native-svg based library that provides many variants of `Radial Slider` and `Speedo Meter` including `default`, `radial-circle-slider`, `speedometer` and `speedometer-marker`
1110

1211
Radial Slider allows you to select any specific value from a range of values. It comes with two variants, one is default and which allows selection on a 180-degree arc and the second one is 360-degree which allows selection of values on a complete circle. It can be used to select/set goals, vision, range, etc.
1312

@@ -133,6 +132,7 @@ export default RadialVariant;
133132
```
134133

135134
# SpeedoMeter
135+
136136
> The speedometer will not take user inputs, when we need to update dynamic values at that time we can use it
137137
138138
- SpeedoMeter has two different variants, speedometer and speedometer-marker

example/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ local.properties
3535
node_modules/
3636
npm-debug.log
3737
yarn-error.log
38+
yarn.lock
3839

3940
# BUCK
4041
buck-out/

example/yarn.lock

Lines changed: 0 additions & 6879 deletions
This file was deleted.

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-radial-slider",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"description": "React Native component to select or highlight a specific value from a range of values",
55
"homepage": "https://github.com/SimformSolutionsPvtLtd/react-native-radial-slider#readme",
66
"main": "lib/index.js",

src/components/RadialSlider/ButtonContent.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const ButtonContent = (props: ButtonProps) => {
1111
activeOpacity={0.7}
1212
disabled={disabled}
1313
onPress={onPress}
14+
onLongPress={props?.onLongPress}
15+
onPressOut={props?.onPressOut}
1416
style={style}>
1517
<Svg height="30" width="45">
1618
<G>

src/components/RadialSlider/LineContent.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ const LineContent = (props: LineContentProps) => {
3131
isMarkerVariant,
3232
marks,
3333
isRadialCircleVariant,
34+
isRadialSliderVariant,
35+
isSpeedoMeterVariant,
3436
} = useRadialSlider(props);
3537

3638
const markerInnerValue = Math.round((max / markerValueInterval) as number);
@@ -73,9 +75,10 @@ const LineContent = (props: LineContentProps) => {
7375

7476
const isSpeedoMarker = !isMarkerVariant ? 0 : isMarkerLine ? -10 : 0;
7577

76-
const isSpeedoMeterMarkerLine = isRadialCircleVariant
77-
? false
78-
: isMarkerLine;
78+
const isSpeedoMeterMarkerLine =
79+
isRadialCircleVariant || isRadialSliderVariant || isSpeedoMeterVariant
80+
? false
81+
: isMarkerLine;
7982

8083
const radialCircleLineRotation = isRadialCircleVariant ? 86 : 90;
8184

src/components/RadialSlider/MarkerValueContent.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const MarkerValueContent = (props: MarkerValueContentProps) => {
2121

2222
return (
2323
<>
24-
{marks.map((mark, index) => {
24+
{marks.map((mark: { value: number }, index: number) => {
2525
const markIndex = Math.floor(
2626
(((((!fixedMarker ? (markerValue as number) : (value as number)) -
2727
min) *

src/components/RadialSlider/RadialSlider.tsx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useEffect, useState } from 'react';
22
import Svg, {
33
Path,
44
Defs,
@@ -20,6 +20,9 @@ import TailText from './TailText';
2020
import LineContent from './LineContent';
2121

2222
const RadialSlider = (props: RadialSliderProps & typeof defaultProps) => {
23+
const [isStart, setIsStart] = useState<boolean>(false);
24+
const [iconPosition, setIconPosition] = useState<string>('');
25+
2326
const {
2427
step,
2528
radius,
@@ -61,6 +64,22 @@ const RadialSlider = (props: RadialSliderProps & typeof defaultProps) => {
6164
centerValue,
6265
} = useRadialSlider(props);
6366

67+
useEffect(() => {
68+
//check max value length
69+
const maxLength = max?.toString()?.length;
70+
71+
const timerId = setTimeout(handleValue, maxLength > 2 ? 10 : 100);
72+
return () => clearTimeout(timerId);
73+
});
74+
75+
const handleValue = () => {
76+
if (iconPosition === 'up' && max > value) {
77+
isStart && setValue((prevState: number) => prevState + step);
78+
} else if (iconPosition === 'down' && min < value) {
79+
isStart && setValue((prevState: number) => prevState - step);
80+
}
81+
};
82+
6483
const leftButtonStyle = StyleSheet.flatten([
6584
leftIconStyle,
6685
(disabled || min === value) && {
@@ -169,6 +188,11 @@ const RadialSlider = (props: RadialSliderProps & typeof defaultProps) => {
169188
<View style={styles.center}>
170189
<ButtonContent
171190
onPress={() => onPressButtons('down')}
191+
onLongPress={() => {
192+
setIsStart(true);
193+
setIconPosition('down');
194+
}}
195+
onPressOut={() => setIsStart(false)}
172196
buttonType="left-btn"
173197
style={leftButtonStyle}
174198
disabled={disabled || min === value}
@@ -177,6 +201,11 @@ const RadialSlider = (props: RadialSliderProps & typeof defaultProps) => {
177201
<ButtonContent
178202
disabled={disabled || max === value}
179203
onPress={() => onPressButtons('up')}
204+
onLongPress={() => {
205+
setIsStart(true);
206+
setIconPosition('up');
207+
}}
208+
onPressOut={() => setIsStart(false)}
180209
style={rightButtonStyle}
181210
buttonType="right-btn"
182211
stroke={stroke ?? Colors.blue}

src/components/RadialSlider/hooks/useRadialSlider.ts

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useMemo } from 'react';
1+
import React, { useEffect, useMemo } from 'react';
22
import {
33
createRange,
44
getExtraSize,
@@ -22,11 +22,31 @@ const useRadialSlider = (props: RadialSliderHookProps) => {
2222

2323
const centerValue = Math.round((max - min) / 2) as number;
2424

25+
//For default variant in radial slider
26+
const isRadialSliderVariant = variant === Constants.radialSlider;
27+
28+
//For radial-circle-slider variant
2529
const isRadialCircleVariant = variant === Constants.radialCircleSlider;
2630

31+
//For speedometer-marker variant
32+
const isMarkerVariant = variant === Constants.speedoMeterMarker;
33+
34+
//For speedometer variant
35+
const isSpeedoMeterVariant = variant === Constants.speedometer;
36+
2737
const radianValue = isRadialCircleVariant ? 0.057 : openingRadian;
2838

29-
const isMarkerVariant = variant === Constants.speedoMeterMarker;
39+
useEffect(() => {
40+
if (isMarkerVariant)
41+
if (min < 0) {
42+
throw 'Negative number is not allowed';
43+
} else if (max < 0) {
44+
throw 'Negative number is not allowed';
45+
}
46+
if (max < min) {
47+
throw 'max value should be greater than min';
48+
}
49+
}, [isMarkerVariant, max, min]);
3050

3151
const angle = (radianValue * 180.0) / Math.PI;
3252

@@ -64,17 +84,33 @@ const useRadialSlider = (props: RadialSliderHookProps) => {
6484
);
6585

6686
const marks = useMemo(() => {
67-
const stepsLength = Math.round((max - min) / step);
68-
69-
return [...Array(stepsLength + 1)].map((_val, index) => {
70-
const isEven = index % 2 === 0;
71-
72-
return {
73-
isEven,
74-
value: Math.round(index * step),
75-
};
76-
});
77-
}, [max, min, step]);
87+
if (isMarkerVariant) {
88+
const stepsLength = Math.round((max - min) / step);
89+
90+
return [...Array(stepsLength + 1)].map((_val, index) => {
91+
const isEven = index % 2 === 0;
92+
93+
return {
94+
isEven,
95+
value: Math.round(index * step),
96+
};
97+
});
98+
} else {
99+
const array: any = [];
100+
for (let i = 0; i <= max; i++) {
101+
array.push(i);
102+
}
103+
104+
return array.map((index: number) => {
105+
const isEven = index % 2 === 0;
106+
107+
return {
108+
isEven,
109+
value: Math.round(index * step),
110+
};
111+
});
112+
}
113+
}, [isMarkerVariant, max, min, step]);
78114

79115
return {
80116
angle,
@@ -91,6 +127,8 @@ const useRadialSlider = (props: RadialSliderHookProps) => {
91127
marks,
92128
isRadialCircleVariant,
93129
centerValue,
130+
isRadialSliderVariant,
131+
isSpeedoMeterVariant,
94132
};
95133
};
96134

src/components/RadialSlider/hooks/useSilderAnimation.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,17 @@ const useSilderAnimation = (props: RadialSliderAnimationHookProps) => {
3939
const prevValue = useRef(props.value > min ? props.value : min);
4040

4141
const [value, setValue] = useState(
42-
props.value >= min ? props.value || min : min
42+
props?.value < min ? min : props?.value > max ? max : props?.value
4343
);
4444

4545
useEffect(() => {
46-
if (props?.value < min) {
47-
setValue(min);
48-
} else if (props?.value > max) {
46+
if (max < props?.value) {
4947
setValue(max);
50-
} else {
51-
setValue(props.value);
48+
} else if (min > props?.value) {
49+
setValue(min);
5250
}
53-
}, [max, min, props.value]);
51+
// eslint-disable-next-line react-hooks/exhaustive-deps
52+
}, [max, min]);
5453

5554
const handlePanResponderGrant = () => {
5655
moveStartValue = prevValue.current;

0 commit comments

Comments
 (0)