Skip to content

Commit 0b403f7

Browse files
saulveethanshar
andauthored
Migrate Slider, GradientSlider and ColorSliderGroup components to TypeScript. (#1408)
* Add Props and State, Fix Imports * wip fix ts errors * Migrate demo screen, gradient slider, slider group, and slider context to TS * Migrate asSliderGroupChild HOC to TS * Migrate ColorSliderGroup to TS * Fix ts errors * Add correct exports, remove Slider.d.ts, remove comments * Add default color prop to GradientSlider, TS improvements * remove unnecessary import * Rename exported props * Small improvements * change example url * Add generated types Co-authored-by: Ethan Sharabi <[email protected]>
1 parent 971b6c0 commit 0b403f7

File tree

22 files changed

+805
-298
lines changed

22 files changed

+805
-298
lines changed

demo/src/screens/componentScreens/SliderScreen.js renamed to demo/src/screens/componentScreens/SliderScreen.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@ import React, {Component} from 'react';
22
import {StyleSheet, ScrollView} from 'react-native';
33
import {Colors, View, Text, Image, Slider, GradientSlider, ColorSliderGroup} from 'react-native-ui-lib';
44

5-
65
const INITIAL_VALUE = 0;
76
const COLOR = Colors.blue30;
87

9-
export default class SliderScreen extends Component {
10-
constructor(props) {
8+
interface SliderScreenProps {
9+
componentId: string;
10+
}
11+
12+
interface SliderScreenState {
13+
alpha: number;
14+
color: string;
15+
sliderValue: number;
16+
}
17+
18+
export default class SliderScreen extends Component<SliderScreenProps, SliderScreenState> {
19+
constructor(props: SliderScreenProps) {
1120
super(props);
1221

1322
this.state = {
@@ -17,15 +26,15 @@ export default class SliderScreen extends Component {
1726
};
1827
}
1928

20-
onSliderValueChange = (value) => {
29+
onSliderValueChange = (value: number) => {
2130
this.setState({sliderValue: value});
2231
}
2332

24-
onGradientValueChange = (value, alpha) => {
33+
onGradientValueChange = (value: string, alpha: number) => {
2534
this.setState({color: value, alpha});
2635
}
2736

28-
onGroupValueChange = (value) => {
37+
onGroupValueChange = (value: string) => {
2938
console.warn('onGroupValueChange: ', value);
3039
}
3140

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import React, { PureComponent, GetDerivedStateFromProps } from 'react';
2+
import { StyleProp, ViewStyle, TextStyle } from 'react-native';
3+
import { GradientSliderTypes } from './GradientSlider';
4+
declare type SliderOnValueChange = (value: string) => void;
5+
export declare type ColorSliderGroupProps = {
6+
/**
7+
* The gradient color
8+
*/
9+
initialColor: string;
10+
/**
11+
* Callback for onValueChange returns the new hex color
12+
*/
13+
onValueChange?: SliderOnValueChange;
14+
/**
15+
* Group container style
16+
*/
17+
containerStyle?: StyleProp<ViewStyle>;
18+
/**
19+
* Sliders style
20+
*/
21+
sliderContainerStyle?: StyleProp<ViewStyle>;
22+
/**
23+
* Show the sliders labels (defaults are: Hue, Lightness, Saturation)
24+
*/
25+
showLabels?: boolean;
26+
/**
27+
* In case you would like to change the default labels (translations etc.), you can provide
28+
* this prop with a map to the relevant labels ({hue: ..., lightness: ..., saturation: ...}).
29+
*/
30+
labels?: {
31+
[key in GradientSliderTypes]: string;
32+
};
33+
/**
34+
* The labels style
35+
*/
36+
labelsStyle?: StyleProp<TextStyle>;
37+
/**
38+
* If true the component will have accessibility features enabled
39+
*/
40+
accessible?: boolean;
41+
};
42+
interface ColorSliderGroupState {
43+
initialColor: string;
44+
}
45+
/**
46+
* @description: A Gradient Slider component
47+
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/SliderScreen.tsx
48+
* @gif: https://github.com/wix/react-native-ui-lib/blob/master/demo/showcase/ColorSliderGroup/ColorSliderGroup.gif?raw=true
49+
*/
50+
declare class ColorSliderGroup extends PureComponent<ColorSliderGroupProps, ColorSliderGroupState> {
51+
static displayName: string;
52+
static defaultProps: {
53+
labels: {
54+
hue: string;
55+
lightness: string;
56+
saturation: string;
57+
};
58+
};
59+
state: {
60+
initialColor: string;
61+
};
62+
static getDerivedStateFromProps: GetDerivedStateFromProps<ColorSliderGroupProps, ColorSliderGroupState>;
63+
onValueChange: (value: string) => void;
64+
renderSlider: (type: GradientSliderTypes) => JSX.Element;
65+
render(): JSX.Element;
66+
}
67+
declare const _default: React.ComponentClass<ColorSliderGroupProps & {
68+
useCustomTheme?: boolean | undefined;
69+
}, any> & typeof ColorSliderGroup;
70+
export default _default;
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import React, { Component } from 'react';
2+
import { StyleProp, ViewStyle } from 'react-native';
3+
import tinycolor from 'tinycolor2';
4+
import { SliderContextProps } from './context/SliderContext';
5+
declare type SliderOnValueChange = (value: string, alfa: number) => void;
6+
export declare enum GradientSliderTypes {
7+
DEFAULT = "default",
8+
HUE = "hue",
9+
LIGHTNESS = "lightness",
10+
SATURATION = "saturation"
11+
}
12+
export declare type GradientSliderProps = {
13+
/**
14+
* The gradient color
15+
*/
16+
color?: string;
17+
/**
18+
* The gradient type (default, hue, lightness, saturation)
19+
*/
20+
type?: GradientSliderTypes;
21+
/**
22+
* The gradient steps
23+
*/
24+
gradientSteps?: number;
25+
/**
26+
* Callback for onValueChange, returns the updated color
27+
*/
28+
onValueChange?: SliderOnValueChange;
29+
/**
30+
* If true the component will have accessibility features enabled
31+
*/
32+
accessible?: boolean;
33+
/**
34+
* The container style
35+
*/
36+
containerStyle?: StyleProp<ViewStyle>;
37+
/**
38+
* If true the Slider will be disabled and will appear in disabled color
39+
*/
40+
disabled?: boolean;
41+
};
42+
declare type GradientSliderComponentProps = {
43+
/**
44+
* Context of the slider group
45+
*/
46+
sliderContext: SliderContextProps;
47+
} & GradientSliderProps & typeof defaultProps;
48+
interface GradientSliderState {
49+
color: tinycolor.ColorFormats.HSLA;
50+
prevColor: string | undefined;
51+
}
52+
declare const defaultProps: {
53+
type: GradientSliderTypes;
54+
gradientSteps: number;
55+
color: string;
56+
};
57+
/**
58+
* @description: A Gradient Slider component
59+
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/SliderScreen.tsx
60+
* @gif: https://github.com/wix/react-native-ui-lib/blob/master/demo/showcase/GradientSlider/GradientSlider.gif?raw=true
61+
*/
62+
declare class GradientSlider extends Component<GradientSliderComponentProps, GradientSliderState> {
63+
static displayName: string;
64+
static defaultProps: {
65+
type: GradientSliderTypes;
66+
gradientSteps: number;
67+
color: string;
68+
};
69+
static types: typeof GradientSliderTypes;
70+
constructor(props: GradientSliderComponentProps);
71+
static getDerivedStateFromProps(nextProps: GradientSliderComponentProps, prevState: GradientSliderState): {
72+
color: tinycolor.ColorFormats.HSLA;
73+
prevColor: tinycolor.ColorFormats.HSLA;
74+
} | null;
75+
getColor(): tinycolor.ColorFormats.HSLA;
76+
getStepColor: (i: number) => string;
77+
renderDefaultGradient: () => JSX.Element;
78+
renderHueGradient: () => JSX.Element;
79+
renderLightnessGradient: () => JSX.Element;
80+
renderSaturationGradient: () => JSX.Element;
81+
onValueChange: (value: string, alpha: number) => void;
82+
updateColor(color: tinycolor.ColorFormats.HSLA): void;
83+
updateAlpha: (a: number) => void;
84+
updateHue: (h: number) => void;
85+
updateLightness: (l: number) => void;
86+
updateSaturation: (s: number) => void;
87+
render(): JSX.Element;
88+
}
89+
declare const _default: React.ComponentClass<{
90+
/**
91+
* Context of the slider group
92+
*/
93+
sliderContext: SliderContextProps;
94+
} & GradientSliderProps & {
95+
type: GradientSliderTypes;
96+
gradientSteps: number;
97+
color: string;
98+
} & {
99+
useCustomTheme?: boolean | undefined;
100+
}, any> & typeof GradientSlider;
101+
export default _default;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// <reference types="tinycolor2" />
2+
import React from 'react';
3+
export interface SliderContextProps {
4+
value?: tinycolor.ColorFormats.HSLA;
5+
setValue?: (value: tinycolor.ColorFormats.HSLA) => void;
6+
}
7+
declare const SliderContext: React.Context<SliderContextProps>;
8+
export default SliderContext;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// <reference types="tinycolor2" />
2+
import { Component } from 'react';
3+
import { StyleProp, ViewStyle } from 'react-native';
4+
interface SliderGroupProps {
5+
color: string;
6+
onValueChange: (color: string) => void;
7+
style?: StyleProp<ViewStyle>;
8+
}
9+
interface SliderGroupState {
10+
value: tinycolor.ColorFormats.HSLA;
11+
}
12+
export default class SliderGroup extends Component<SliderGroupProps, SliderGroupState> {
13+
static displayName: string;
14+
constructor(props: SliderGroupProps);
15+
getContextProviderValue(): {
16+
value: import("tinycolor2").ColorFormats.HSLA;
17+
setValue: (value: import("tinycolor2").ColorFormats.HSLA) => void;
18+
};
19+
setValue: (value: tinycolor.ColorFormats.HSLA) => void;
20+
render(): JSX.Element;
21+
}
22+
export {};
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import React from 'react';
2+
import { View } from 'react-native';
3+
declare function asSliderGroupChild(WrappedComponent: any): {
4+
new (props: {} | Readonly<{}>): {
5+
contentRef: View | undefined;
6+
render(): JSX.Element;
7+
context: any;
8+
setState<K extends never>(state: {} | ((prevState: Readonly<{}>, props: Readonly<{}>) => {} | Pick<{}, K> | null) | Pick<{}, K> | null, callback?: (() => void) | undefined): void;
9+
forceUpdate(callback?: (() => void) | undefined): void;
10+
readonly props: Readonly<{}> & Readonly<{
11+
children?: React.ReactNode;
12+
}>;
13+
state: Readonly<{}>;
14+
refs: {
15+
[key: string]: React.ReactInstance;
16+
};
17+
componentDidMount?(): void;
18+
shouldComponentUpdate?(nextProps: Readonly<{}>, nextState: Readonly<{}>, nextContext: any): boolean;
19+
componentWillUnmount?(): void;
20+
componentDidCatch?(error: Error, errorInfo: React.ErrorInfo): void;
21+
getSnapshotBeforeUpdate?(prevProps: Readonly<{}>, prevState: Readonly<{}>): any;
22+
componentDidUpdate?(prevProps: Readonly<{}>, prevState: Readonly<{}>, snapshot?: any): void;
23+
componentWillMount?(): void;
24+
UNSAFE_componentWillMount?(): void;
25+
componentWillReceiveProps?(nextProps: Readonly<{}>, nextContext: any): void;
26+
UNSAFE_componentWillReceiveProps?(nextProps: Readonly<{}>, nextContext: any): void;
27+
componentWillUpdate?(nextProps: Readonly<{}>, nextState: Readonly<{}>, nextContext: any): void;
28+
UNSAFE_componentWillUpdate?(nextProps: Readonly<{}>, nextState: Readonly<{}>, nextContext: any): void;
29+
};
30+
new (props: {}, context: any): {
31+
contentRef: View | undefined;
32+
render(): JSX.Element;
33+
context: any;
34+
setState<K extends never>(state: {} | ((prevState: Readonly<{}>, props: Readonly<{}>) => {} | Pick<{}, K> | null) | Pick<{}, K> | null, callback?: (() => void) | undefined): void;
35+
forceUpdate(callback?: (() => void) | undefined): void;
36+
readonly props: Readonly<{}> & Readonly<{
37+
children?: React.ReactNode;
38+
}>;
39+
state: Readonly<{}>;
40+
refs: {
41+
[key: string]: React.ReactInstance;
42+
};
43+
componentDidMount?(): void;
44+
shouldComponentUpdate?(nextProps: Readonly<{}>, nextState: Readonly<{}>, nextContext: any): boolean;
45+
componentWillUnmount?(): void;
46+
componentDidCatch?(error: Error, errorInfo: React.ErrorInfo): void;
47+
getSnapshotBeforeUpdate?(prevProps: Readonly<{}>, prevState: Readonly<{}>): any;
48+
componentDidUpdate?(prevProps: Readonly<{}>, prevState: Readonly<{}>, snapshot?: any): void;
49+
componentWillMount?(): void;
50+
UNSAFE_componentWillMount?(): void;
51+
componentWillReceiveProps?(nextProps: Readonly<{}>, nextContext: any): void;
52+
UNSAFE_componentWillReceiveProps?(nextProps: Readonly<{}>, nextContext: any): void;
53+
componentWillUpdate?(nextProps: Readonly<{}>, nextState: Readonly<{}>, nextContext: any): void;
54+
UNSAFE_componentWillUpdate?(nextProps: Readonly<{}>, nextState: Readonly<{}>, nextContext: any): void;
55+
};
56+
contextType?: React.Context<any> | undefined;
57+
};
58+
export default asSliderGroupChild;

0 commit comments

Comments
 (0)