Skip to content

Commit 0613882

Browse files
committed
feat(slider): stepSize property
1 parent 3df91bf commit 0613882

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

src/slider/cssproperties.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ export const trackBackgroundColorProperty = new CssProperty<Style, Color>({
44
name: 'trackBackgroundColor',
55
cssName: 'track-background-color',
66
equalityComparer: Color.equals,
7-
valueConverter: v => new Color(v)
7+
valueConverter: (v) => new Color(v)
88
});
99
trackBackgroundColorProperty.register(Style);
1010
export const thumbColorProperty = new CssProperty<Style, Color>({
1111
cssName: 'thumb-color',
1212
name: 'thumbColor',
1313
equalityComparer: Color.equals,
14-
valueConverter: v => new Color(v)
14+
valueConverter: (v) => new Color(v)
1515
});
1616
thumbColorProperty.register(Style);
1717

1818
export const trackFillColorProperty = new CssProperty<Style, Color>({
1919
cssName: 'track-fill-color',
2020
name: 'trackFillColor',
2121
equalityComparer: Color.equals,
22-
valueConverter: v => new Color(v)
22+
valueConverter: (v) => new Color(v)
2323
});
2424
trackFillColorProperty.register(Style);
2525
export const thumbHollowAtStartProperty = new CssProperty<Style, boolean>({
@@ -28,3 +28,11 @@ export const thumbHollowAtStartProperty = new CssProperty<Style, boolean>({
2828
valueConverter: booleanConverter
2929
});
3030
thumbHollowAtStartProperty.register(Style);
31+
32+
export const stepSizeProperty = new CssProperty<Style, number>({
33+
name: 'stepSize',
34+
cssName: 'step-size',
35+
defaultValue: 0,
36+
valueConverter: (v) => parseFloat(v)
37+
});
38+
stepSizeProperty.register(Style);

src/slider/slider-common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ export abstract class SliderBase extends NSSlider {
88
@cssProperty trackFillColor: Color;
99
@cssProperty thumbColor: Color;
1010
@cssProperty elevation: number;
11+
@cssProperty stepSize: number;
1112
}

src/slider/slider.android.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { cssProperty, rippleColorProperty, themer } from '@nativescript-community/ui-material-core';
22
import { getEnabledColorStateList, state } from '@nativescript-community/ui-material-core/android/utils';
33
import { CoercibleProperty, Color, Property, View, backgroundColorProperty, backgroundInternalProperty, colorProperty } from '@nativescript/core';
4-
import { thumbColorProperty, trackBackgroundColorProperty, trackFillColorProperty } from './cssproperties';
4+
import { stepSizeProperty, thumbColorProperty, trackBackgroundColorProperty, trackFillColorProperty } from './cssproperties';
55

66
let ASlider: typeof com.google.android.material.slider.Slider;
77
export function sliderGetEnabledColorStateList(color: Color, alpha = 255) {
@@ -56,6 +56,7 @@ export class Slider extends View {
5656
@cssProperty trackBackgroundColor: Color;
5757
@cssProperty trackFillColor: Color;
5858
@cssProperty thumbColor: Color;
59+
@cssProperty stepSize: number;
5960
@cssProperty elevation: number;
6061
_supressNativeValue: boolean;
6162
public value: number;
@@ -136,8 +137,17 @@ export class Slider extends View {
136137
this[thumbColorProperty.setNative](this.thumbColor);
137138
}
138139
}
140+
[stepSizeProperty.getDefault]() {
141+
return 0;
142+
}
143+
[stepSizeProperty.setNative](value) {
144+
this.nativeViewProtected.setStepSize(value);
145+
}
139146
[valueProperty.setNative](value) {
140-
// this.nativeViewProtected.setValueTo(this.maxValue);
147+
// ensure we set min/max to prevent errors in listviews while reusing cells
148+
// will sliders with different min/max
149+
this.nativeViewProtected.setValueFrom(this.minValue);
150+
this.nativeViewProtected.setValueTo(this.maxValue);
141151
this.nativeViewProtected.setValue(value);
142152
}
143153
[minValueProperty.setNative](value) {

src/slider/slider.ios.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { elevationProperty, rippleColorProperty, themer } from '@nativescript-community/ui-material-core';
22
import { Color, backgroundColorProperty, colorProperty } from '@nativescript/core';
3-
import { thumbColorProperty, thumbHollowAtStartProperty, trackBackgroundColorProperty, trackFillColorProperty } from './cssproperties';
3+
import { maxValueProperty } from '@nativescript/core/ui/progress';
4+
import { minValueProperty } from '@nativescript/core/ui/slider/slider-common';
5+
import { stepSizeProperty, thumbColorProperty, thumbHollowAtStartProperty, trackBackgroundColorProperty, trackFillColorProperty } from './cssproperties';
46
import { SliderBase } from './slider-common';
57

68
export class Slider extends SliderBase {
@@ -60,4 +62,28 @@ export class Slider extends SliderBase {
6062
[elevationProperty.setNative](value: number) {
6163
this.nativeViewProtected.thumbElevation = value;
6264
}
65+
[minValueProperty.setNative](value) {
66+
this.nativeViewProtected.minimumValue = value;
67+
if (this.stepSize !== 0) {
68+
this.nativeViewProtected.numberOfDiscreteValues = (this.maxValue - this.minValue) / value;
69+
}
70+
}
71+
[maxValueProperty.setNative](value) {
72+
this.nativeViewProtected.maximumValue = value;
73+
if (this.stepSize !== 0) {
74+
this.nativeViewProtected.numberOfDiscreteValues = (this.maxValue - this.minValue) / value;
75+
}
76+
}
77+
[stepSizeProperty.getDefault]() {
78+
return 0;
79+
}
80+
[stepSizeProperty.setNative](value) {
81+
if (value === 0) {
82+
this.nativeViewProtected.discrete = false;
83+
} else {
84+
this.nativeViewProtected.discrete = true;
85+
this.nativeViewProtected.numberOfDiscreteValues = (this.maxValue - this.minValue) / value;
86+
this.nativeViewProtected.shouldDisplayDiscreteValueLabel = false;
87+
}
88+
}
6389
}

0 commit comments

Comments
 (0)