Skip to content

Commit 24670c2

Browse files
committed
fix(slider): android fix for wrong first value if min > 100
1 parent 2ad15f2 commit 24670c2

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/slider/slider.android.ts

+16-11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { CoercibleProperty, Color, Property, View, backgroundColorProperty, back
44
import { stepSizeProperty, thumbColorProperty, trackBackgroundColorProperty, trackFillColorProperty } from './cssproperties';
55

66
let ASlider: typeof com.google.android.material.slider.Slider;
7+
8+
const DEFAULT_MIN = 0;
9+
const DEFAULT_MAX = 0;
710
export function sliderGetEnabledColorStateList(color: Color, alpha = 255) {
811
if (!color) {
912
return null;
@@ -12,10 +15,12 @@ export function sliderGetEnabledColorStateList(color: Color, alpha = 255) {
1215
}
1316
export const valueProperty = new CoercibleProperty<Slider, number>({
1417
name: 'value',
15-
defaultValue: 0,
18+
// defaultValue: 0,
1619
coerceValue: (target, value) => {
17-
value = Math.max(value, target.minValue);
18-
value = Math.min(value, target.maxValue);
20+
if (target.minValue !== undefined && target.maxValue !== undefined) {
21+
value = Math.max(value, target.minValue);
22+
value = Math.min(value, target.maxValue);
23+
}
1924

2025
return value;
2126
},
@@ -26,7 +31,7 @@ export const valueProperty = new CoercibleProperty<Slider, number>({
2631
*/
2732
export const minValueProperty = new Property<Slider, number>({
2833
name: 'minValue',
29-
defaultValue: 0,
34+
// defaultValue: 0,
3035
valueChanged: (target, oldValue, newValue) => {
3136
maxValueProperty.coerce(target);
3237
valueProperty.coerce(target);
@@ -38,7 +43,7 @@ export const minValueProperty = new Property<Slider, number>({
3843
*/
3944
export const maxValueProperty = new CoercibleProperty<Slider, number>({
4045
name: 'maxValue',
41-
defaultValue: 100,
46+
// defaultValue: 100,
4247
coerceValue: (target, value) => {
4348
const minValue = target.minValue;
4449
if (value < minValue) {
@@ -86,9 +91,9 @@ export class Slider extends View {
8691
}
8792
}
8893
});
94+
nativeView.setValueFrom(this.minValue || DEFAULT_MIN);
95+
nativeView.setValueTo(this.maxValue || DEFAULT_MAX);
8996
nativeView.addOnChangeListener(this.listener);
90-
nativeView.setValueFrom(this.minValue);
91-
nativeView.setValueTo(this.maxValue);
9297
}
9398
disposeNativeView() {
9499
if (this.listener) {
@@ -138,23 +143,23 @@ export class Slider extends View {
138143
}
139144
}
140145
[stepSizeProperty.getDefault]() {
141-
return 0;
146+
return DEFAULT_MIN;
142147
}
143148
[stepSizeProperty.setNative](value) {
144149
this.nativeViewProtected.setStepSize(value);
145150
}
146151
[valueProperty.setNative](value) {
147152
// ensure we set min/max to prevent errors in listviews while reusing cells
148153
// will sliders with different min/max
149-
this.nativeViewProtected.setValueFrom(this.minValue);
150-
this.nativeViewProtected.setValueTo(this.maxValue);
154+
this.nativeViewProtected.setValueFrom(this.minValue || DEFAULT_MIN);
155+
this.nativeViewProtected.setValueTo(this.maxValue || DEFAULT_MAX);
151156
this.nativeViewProtected.setValue(value);
152157
}
153158
[minValueProperty.setNative](value) {
154159
this.nativeViewProtected.setValueFrom(value);
155160
}
156161
[maxValueProperty.getDefault]() {
157-
return 100;
162+
return DEFAULT_MAX;
158163
}
159164
[maxValueProperty.setNative](value) {
160165
this.nativeViewProtected.setValueTo(value);

0 commit comments

Comments
 (0)