Skip to content

Commit 2c81f44

Browse files
authored
Slider - wrap in asBaseComponent and fix typings (#2940)
1 parent e0d5423 commit 2c81f44

File tree

10 files changed

+216
-308
lines changed

10 files changed

+216
-308
lines changed

demo/src/screens/componentScreens/SliderScreen.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ export default class SliderScreen extends Component<SliderScreenProps, SliderScr
112112
step={1}
113113
containerStyle={styles.sliderContainer}
114114
disableRTL={forceLTR}
115-
// @ts-expect-error
116115
ref={this.slider}
117116
onReset={this.onSliderReset}
118117
/>
@@ -208,7 +207,6 @@ export default class SliderScreen extends Component<SliderScreenProps, SliderScr
208207
maximumValue={100}
209208
step={1}
210209
disableRTL={forceLTR}
211-
// @ts-expect-error
212210
ref={this.rangeSlider}
213211
onReset={this.onRangeSliderReset}
214212
/>

src/components/slider/ColorSlider.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React from 'react';
2-
32
import Text from '../text';
4-
import GradientSlider, {GradientSliderTypes} from './GradientSlider';
5-
import {ColorSliderGroupProps} from './ColorSliderGroup';
3+
import {GradientSliderTypes, ColorSliderGroupProps} from './types';
4+
import GradientSlider from './GradientSlider';
65

76
type ColorSliderProps = Pick<
87
ColorSliderGroupProps,
@@ -40,4 +39,5 @@ const ColorSlider = (props: ColorSliderProps) => {
4039
);
4140
};
4241

42+
ColorSlider.displayName = 'IGNORE';
4343
export default ColorSlider;

src/components/slider/ColorSliderGroup.tsx

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,9 @@
11
import React, {useState, useEffect, useCallback} from 'react';
2-
import {StyleProp, ViewStyle, TextStyle} from 'react-native';
32
import {asBaseComponent} from '../../commons/new';
4-
import GradientSlider, {GradientSliderTypes} from './GradientSlider';
3+
import GradientSlider from './GradientSlider';
54
import SliderGroup from './context/SliderGroup';
65
import ColorSlider from './ColorSlider';
7-
8-
type SliderOnValueChange = (value: string) => void;
9-
10-
export type ColorSliderGroupProps = {
11-
/**
12-
* The gradient color
13-
*/
14-
initialColor: string;
15-
/**
16-
* Callback for onValueChange returns the new hex color
17-
*/
18-
onValueChange?: SliderOnValueChange;
19-
/**
20-
* Group container style
21-
*/
22-
containerStyle?: StyleProp<ViewStyle>;
23-
/**
24-
* Sliders style
25-
*/
26-
sliderContainerStyle?: StyleProp<ViewStyle>;
27-
/**
28-
* Show the sliders labels (defaults are: Hue, Lightness, Saturation)
29-
*/
30-
showLabels?: boolean;
31-
/**
32-
* In case you would like to change the default labels (translations etc.), you can provide
33-
* this prop with a map to the relevant labels ({hue: ..., lightness: ..., saturation: ...}).
34-
*/
35-
labels?: {[key in GradientSliderTypes]: string};
36-
/**
37-
* The labels style
38-
*/
39-
labelsStyle?: StyleProp<TextStyle>;
40-
/**
41-
* If true the component will have accessibility features enabled
42-
*/
43-
accessible?: boolean;
44-
/**
45-
* Whether to use the new Slider implementation using Reanimated
46-
*/
47-
migrate?: boolean;
48-
};
6+
import {ColorSliderGroupProps} from './types';
497

508
/**
519
* @description: A Gradient Slider component

src/components/slider/GradientSlider.tsx

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,17 @@
11
import _ from 'lodash';
22
import tinycolor from 'tinycolor2';
33
import React, {useCallback, useEffect, useState, useRef, useMemo} from 'react';
4-
import {StyleProp, ViewStyle} from 'react-native';
5-
import {Colors} from '../../style';
64
import {asBaseComponent, forwardRef, ForwardRefInjectedProps} from '../../commons/new';
75
import {ComponentStatics} from '../../typings/common';
8-
import Slider, {SliderProps} from './index';
6+
import {Colors} from '../../style';
97
import {Slider as NewSlider} from '../../incubator';
8+
import Gradient from '../gradient';
9+
import {GradientSliderProps, GradientSliderTypes} from './types';
10+
import Slider from './index';
1011
import {SliderContextProps} from './context/SliderContext';
1112
import asSliderGroupChild from './context/asSliderGroupChild';
12-
import Gradient from '../gradient';
13-
14-
type SliderOnValueChange = (value: string, alfa: number) => void;
15-
16-
export enum GradientSliderTypes {
17-
DEFAULT = 'default',
18-
HUE = 'hue',
19-
LIGHTNESS = 'lightness',
20-
SATURATION = 'saturation'
21-
}
22-
23-
export type GradientSliderProps = Omit<
24-
SliderProps,
25-
'onValueChange' | 'value' | 'minimumValue' | 'maximumValue' | 'step' | 'thumbHitSlop' | 'useGap'
26-
> & {
27-
/**
28-
* The gradient color
29-
*/
30-
color?: string;
31-
/**
32-
* The gradient type (default, hue, lightness, saturation)
33-
*/
34-
type?: GradientSliderTypes | `${GradientSliderTypes}`;
35-
/**
36-
* The gradient steps
37-
*/
38-
gradientSteps?: number;
39-
/**
40-
* Callback for onValueChange, returns the updated color
41-
*/
42-
onValueChange?: SliderOnValueChange;
43-
/**
44-
* If true the component will have accessibility features enabled
45-
*/
46-
accessible?: boolean;
47-
/**
48-
* The container style
49-
*/
50-
containerStyle?: StyleProp<ViewStyle>;
51-
/**
52-
* If true the Slider will be disabled and will appear in disabled color
53-
*/
54-
disabled?: boolean;
55-
} & Partial<Pick<SliderProps, 'value' | 'minimumValue' | 'maximumValue' | 'step' | 'thumbHitSlop' | 'useGap'>>; // Fixes typing errors with the old slider.
5613

5714
type GradientSliderComponentProps = {
58-
/**
59-
* Context of the slider group
60-
*/
6115
sliderContext: SliderContextProps;
6216
} & GradientSliderProps;
6317

@@ -215,7 +169,5 @@ const GradientSlider = (props: Props) => {
215169

216170
GradientSlider.displayName = 'GradientSlider';
217171
GradientSlider.types = GradientSliderTypes;
218-
219-
// eslint-disable-next-line max-len
220172
// @ts-expect-error
221173
export default asBaseComponent<GradientSliderProps, ComponentStatics<typeof GradientSlider>>(forwardRef(asSliderGroupChild(forwardRef(GradientSlider))));

src/components/slider/context/SliderGroup.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import tinycolor from 'tinycolor2';
12
import React, {useCallback, useMemo, useState} from 'react';
23
import {StyleProp, ViewStyle} from 'react-native';
34
import {Colors} from '../../../style';
4-
import SliderContext from './SliderContext';
55
import View from '../../view';
6-
import tinycolor from 'tinycolor2';
6+
import SliderContext from './SliderContext';
77

88
interface SliderGroupProps {
99
color: string;

src/components/slider/index.tsx

Lines changed: 12 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import _ from 'lodash';
2-
import React, {PureComponent, ReactElement} from 'react';
2+
import React, {PureComponent} from 'react';
33
import {
44
StyleSheet,
55
PanResponder,
@@ -14,12 +14,13 @@ import {
1414
AccessibilityRole,
1515
View as RNView
1616
} from 'react-native';
17-
import {Constants} from '../../commons/new';
17+
import {Constants, asBaseComponent} from '../../commons/new';
18+
import {extractAccessibilityProps} from '../../commons/modifiers';
1819
import {Colors} from '../../style';
19-
import IncubatorSlider from '../../incubator/Slider';
2020
import View from '../view';
21-
import Thumb, {ThumbProps} from './Thumb';
22-
import {extractAccessibilityProps} from '../../commons/modifiers';
21+
import IncubatorSlider from '../../incubator/Slider';
22+
import {SliderProps} from './types';
23+
import Thumb from './Thumb';
2324

2425
const TRACK_SIZE = 6;
2526
const THUMB_SIZE = 24;
@@ -29,103 +30,7 @@ const ACTIVE_COLOR = Colors.$backgroundPrimaryHeavy;
2930
const INACTIVE_COLOR = Colors.$backgroundNeutralMedium;
3031
const MIN_RANGE_GAP = 4;
3132

32-
export type SliderOnValueChange = (value: number) => void;
33-
export type SliderOnRangeChange = (values: {min: number; max: number}) => void;
34-
35-
export type SliderProps = Omit<ThumbProps, 'ref'> & {
36-
/**
37-
* Initial value
38-
*/
39-
value?: number;
40-
/**
41-
* Track minimum value
42-
*/
43-
minimumValue?: number;
44-
/**
45-
* Track maximum value
46-
*/
47-
maximumValue?: number;
48-
/**
49-
* Initial minimum value (when useRange is true)
50-
*/
51-
initialMinimumValue?: number;
52-
/**
53-
* Initial maximum value (when useRange is true)
54-
*/
55-
initialMaximumValue?: number;
56-
/**
57-
* Step value of the slider. The value should be between 0 and (maximumValue - minimumValue)
58-
*/
59-
step?: number;
60-
/**
61-
* The color used for the track from minimum value to current value
62-
*/
63-
minimumTrackTintColor?: string;
64-
/**
65-
* The track color
66-
*/
67-
maximumTrackTintColor?: string;
68-
/**
69-
* Custom render instead of rendering the track
70-
*/
71-
renderTrack?: () => ReactElement | ReactElement[];
72-
/**
73-
* Callback for onValueChange
74-
*/
75-
onValueChange?: SliderOnValueChange;
76-
/**
77-
* Callback that notifies about slider seeking is started
78-
*/
79-
onSeekStart?: () => void;
80-
/**
81-
* Callback that notifies about slider seeking is finished
82-
*/
83-
onSeekEnd?: () => void;
84-
/**
85-
* Callback that notifies when the reset function was invoked
86-
*/
87-
onReset?: () => void;
88-
/**
89-
* The container style
90-
*/
91-
containerStyle?: StyleProp<ViewStyle>;
92-
/**
93-
* The track style
94-
*/
95-
trackStyle?: StyleProp<ViewStyle>;
96-
/**
97-
* If true the Slider will be disabled and will appear in disabled color
98-
*/
99-
disabled?: boolean;
100-
/**
101-
* If true the Slider will display a second thumb for the min value
102-
*/
103-
useRange?: boolean;
104-
/**
105-
* If true the min and max thumbs will not overlap
106-
*/
107-
useGap?: boolean;
108-
/**
109-
* Callback for onRangeChange. Returns values object with the min and max values
110-
*/
111-
onRangeChange?: SliderOnRangeChange;
112-
/**
113-
* If true the Slider will stay in LTR mode even if the app is on RTL mode
114-
*/
115-
disableRTL?: boolean;
116-
/**
117-
* If true the component will have accessibility features enabled
118-
*/
119-
accessible?: boolean;
120-
/**
121-
* The slider's test identifier
122-
*/
123-
testID?: string;
124-
/**
125-
* Whether to use the new Slider implementation using Reanimated
126-
*/
127-
migrate?: boolean;
128-
} & typeof defaultProps;
33+
type InternalSliderProps = SliderProps & typeof defaultProps;
12934

13035
interface State {
13136
containerSize: Measurements;
@@ -160,7 +65,7 @@ const defaultProps = {
16065
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/SliderScreen.tsx
16166
* @gif: https://github.com/wix/react-native-ui-lib/blob/master/demo/showcase/Slider/Slider.gif?raw=true
16267
*/
163-
export default class Slider extends PureComponent<SliderProps, State> {
68+
class Slider extends PureComponent<InternalSliderProps, State> {
16469
static displayName = 'Slider';
16570

16671
static defaultProps = defaultProps;
@@ -193,7 +98,7 @@ export default class Slider extends PureComponent<SliderProps, State> {
19398

19499
private didMount: boolean;
195100

196-
constructor(props: SliderProps) {
101+
constructor(props: InternalSliderProps) {
197102
super(props);
198103

199104
this.activeThumbRef = this.thumb;
@@ -244,7 +149,7 @@ export default class Slider extends PureComponent<SliderProps, State> {
244149
return useRange ? initialMaximumValue || maximumValue : value;
245150
}
246151

247-
checkProps(props: SliderProps) {
152+
checkProps(props: InternalSliderProps) {
248153
const {useRange, minimumValue, maximumValue, value} = props;
249154
if (minimumValue >= maximumValue) {
250155
console.warn('Slider minimumValue must be lower than maximumValue');
@@ -766,6 +671,8 @@ export default class Slider extends PureComponent<SliderProps, State> {
766671
}
767672
}
768673

674+
export default asBaseComponent<SliderProps>(Slider);
675+
769676
const styles = StyleSheet.create({
770677
container: {
771678
height: THUMB_SIZE + SHADOW_RADIUS,

src/components/slider/slider.driver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {SliderProps} from './index';
1+
import {SliderProps} from './types';
22
import {ComponentDriver} from '../../testkit/Component.driver';
33

44
export class SliderDriver extends ComponentDriver<SliderProps> {

0 commit comments

Comments
 (0)