Skip to content

Commit 5554e43

Browse files
authored
migrate Switch to TS (#991)
* migrate Switch to TS * TS fixes * fix style type
1 parent 544d75e commit 5554e43

File tree

5 files changed

+131
-77
lines changed

5 files changed

+131
-77
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@ class SwitchScreen extends Component {
1515
return (
1616
<View flex bottom padding-20>
1717
<View flex center>
18-
<Switch testID="switch" value={this.state.value1} onValueChange={value1 => this.setState({value1})} style={{marginBottom: 20}}/>
18+
<Switch testID="switch" value={this.state.value1} onValueChange={(value1: boolean) => this.setState({value1})} style={{marginBottom: 20}}/>
1919
<Switch
2020
onColor={Colors.purple30}
2121
offColor={Colors.purple60}
2222
value={this.state.value2}
23-
onValueChange={value2 => this.setState({value2})}
23+
onValueChange={(value2: boolean) => this.setState({value2})}
2424
style={{marginBottom: 20}}
2525
/>
2626
<Switch
2727
width={80}
2828
height={38}
2929
thumbSize={34}
3030
value={this.state.value3}
31-
onValueChange={value3 => this.setState({value3})}
31+
onValueChange={(value3: boolean) => this.setState({value3})}
3232
style={{marginBottom: 20}}
3333
/>
3434
<Switch
@@ -39,21 +39,21 @@ class SwitchScreen extends Component {
3939
offColor={Colors.dark60}
4040
thumbColor={Colors.dark10}
4141
value={this.state.value4}
42-
onValueChange={value4 => this.setState({value4})}
42+
onValueChange={(value4: boolean) => this.setState({value4})}
4343
style={{marginBottom: 20}}
4444
/>
4545
<View row marginB-20>
4646
<Text text70 centerV>Disabled: </Text>
4747
<Switch
4848
disabled
4949
value={this.state.value5}
50-
onValueChange={value5 => this.setState({value5})}
50+
onValueChange={(value5: boolean) => this.setState({value5})}
5151
style={{marginRight: 10}}
5252
/>
5353
<Switch
5454
disabled
5555
value={!this.state.value5}
56-
onValueChange={value5 => this.setState({value5})}
56+
onValueChange={(value5: boolean) => this.setState({value5})}
5757
/>
5858
</View>
5959
</View>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import React from 'react';
2+
import { StyleProp, ViewStyle } from 'react-native';
3+
export declare type SwitchProps = {
4+
/**
5+
* The value of the switch. If true the switch will be turned on. Default value is false
6+
*/
7+
value?: boolean;
8+
/**
9+
* Invoked with the new value when the value changes
10+
*/
11+
onValueChange?: (value: boolean) => void;
12+
/**
13+
* Whether the switch should be disabled
14+
*/
15+
disabled?: boolean;
16+
/**
17+
* The Switch width
18+
*/
19+
width?: number;
20+
/**
21+
* The Switch height
22+
*/
23+
height?: number;
24+
/**
25+
* The Switch background color when it's turned on
26+
*/
27+
onColor?: string;
28+
/**
29+
* The Switch background color when it's turned off
30+
*/
31+
offColor?: string;
32+
/**
33+
* The Switch background color when it's disabled
34+
*/
35+
disabledColor?: string;
36+
/**
37+
* The Switch's thumb color
38+
*/
39+
thumbColor?: string;
40+
/**
41+
* The Switch's thumb size (width & height)
42+
*/
43+
thumbSize?: number;
44+
/**
45+
* The Switch's thumb style
46+
*/
47+
thumbStyle?: object | number | [];
48+
style?: StyleProp<ViewStyle>;
49+
testID?: string;
50+
};
51+
declare const _default: React.ComponentClass<SwitchProps & {
52+
useCustomTheme?: boolean | undefined;
53+
}, any>;
54+
export default _default;

generatedTypes/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export {default as Image, ImageProps} from './components/image';
2121
export {default as Overlay, OverlayTypes} from './components/overlay';
2222
export {default as RadioButton, RadioButtonPropTypes} from './components/radioButton/RadioButton';
2323
export {default as RadioGroup, RadioGroupPropTypes} from './components/radioButton/RadioGroup';
24+
export {default as Switch, SwitchProps} from './components/switch';
2425
export {default as TabBar} from './components/TabBar';
2526
export {default as Fader, FaderProps, FaderPosition} from './components/fader';
2627
export {default as ExpandableSection, ExpandableSectionProps } from './components/ExpandableSection';
@@ -98,7 +99,6 @@ export const SafeAreaInsetsManager;
9899
export const SafeAreaSpacerView;
99100
export const ScrollBar;
100101
export const SelectableComponent;
101-
export const Switch;
102102
export const TabController;
103103
export const TextField;
104104
export const Wizard;
Lines changed: 69 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,86 @@
11
import _ from 'lodash';
2-
import PropTypes from 'prop-types';
3-
import React from 'react';
4-
import {StyleSheet, Animated, Easing} from 'react-native';
2+
import React, {Component} from 'react';
3+
import {StyleSheet, Animated, Easing, StyleProp, ViewStyle} from 'react-native';
54
import {Constants} from '../../helpers';
65
import {Colors, BorderRadiuses} from '../../style';
7-
import {BaseComponent} from '../../commons';
6+
import {asBaseComponent} from '../../commons/new';
87
import TouchableOpacity from '../touchableOpacity';
98

109
const INNER_PADDING = 2;
1110
const DEFAULT_WIDTH = 42;
1211
const DEFAULT_HEIGHT = 24;
1312
const DEFAULT_THUMB_SIZE = 20;
1413

14+
export type SwitchProps = {
15+
/**
16+
* The value of the switch. If true the switch will be turned on. Default value is false
17+
*/
18+
value?: boolean;
19+
/**
20+
* Invoked with the new value when the value changes
21+
*/
22+
onValueChange?: (value: boolean) => void;
23+
/**
24+
* Whether the switch should be disabled
25+
*/
26+
disabled?: boolean;
27+
/**
28+
* The Switch width
29+
*/
30+
width?: number;
31+
/**
32+
* The Switch height
33+
*/
34+
height?: number;
35+
/**
36+
* The Switch background color when it's turned on
37+
*/
38+
onColor?: string;
39+
/**
40+
* The Switch background color when it's turned off
41+
*/
42+
offColor?: string;
43+
/**
44+
* The Switch background color when it's disabled
45+
*/
46+
disabledColor?: string;
47+
/**
48+
* The Switch's thumb color
49+
*/
50+
thumbColor?: string;
51+
/**
52+
* The Switch's thumb size (width & height)
53+
*/
54+
thumbSize?: number;
55+
/**
56+
* The Switch's thumb style
57+
*/
58+
thumbStyle?: object | number | [];
59+
style?: StyleProp<ViewStyle>;
60+
testID?: string;
61+
}
62+
1563
/**
1664
* Switch component for toggling boolean value related to some context
1765
*/
18-
class Switch extends BaseComponent {
66+
class Switch extends Component<SwitchProps> {
1967
static displayName = 'Switch';
20-
21-
static propTypes = {
22-
/**
23-
* The value of the switch. If true the switch will be turned on. Default value is false
24-
*/
25-
value: PropTypes.bool,
26-
/**
27-
* Invoked with the new value when the value changes
28-
*/
29-
onValueChange: PropTypes.func,
30-
/**
31-
* Whether the switch should be disabled
32-
*/
33-
disabled: PropTypes.bool,
34-
/**
35-
* The Switch width
36-
*/
37-
width: PropTypes.number,
38-
/**
39-
* The Switch height
40-
*/
41-
height: PropTypes.number,
42-
/**
43-
* The Switch background color when it's turned on
44-
*/
45-
onColor: PropTypes.string,
46-
/**
47-
* The Switch background color when it's turned off
48-
*/
49-
offColor: PropTypes.string,
50-
/**
51-
* The Switch background color when it's disabled
52-
*/
53-
disabledColor: PropTypes.string,
54-
/**
55-
* The Switch's thumb color
56-
*/
57-
thumbColor: PropTypes.string,
58-
/**
59-
* The Switch's thumb size (width & height)
60-
*/
61-
thumbSize: PropTypes.number,
62-
/**
63-
* The Switch's thumb style
64-
*/
65-
thumbStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.number, PropTypes.array])
66-
};
67-
68+
6869
state = {
6970
thumbPosition: new Animated.Value(this.props.value ? 1 : 0)
7071
};
7172

72-
generateStyles() {
73-
this.styles = createStyles(this.getThemeProps());
74-
}
75-
76-
componentDidUpdate(prevProps) {
77-
const {value} = this.getThemeProps();
73+
styles = createStyles(this.props);
74+
75+
componentDidUpdate(prevProps: SwitchProps) {
76+
const {value} = this.props;
7877
if (prevProps.value !== value) {
7978
this.toggle(value);
8079
}
8180
}
8281

8382
getAccessibilityProps() {
84-
const {disabled, value} = this.getThemeProps();
83+
const {disabled, value} = this.props;
8584

8685

8786
return {
@@ -92,7 +91,7 @@ class Switch extends BaseComponent {
9291
};
9392
}
9493

95-
toggle(value) {
94+
toggle(value?: boolean) {
9695
const {thumbPosition} = this.state;
9796

9897
Animated.timing(thumbPosition, {
@@ -104,15 +103,15 @@ class Switch extends BaseComponent {
104103
}
105104

106105
onPress = () => {
107-
const {disabled} = this.getThemeProps();
106+
const {disabled} = this.props;
108107

109108
if (!disabled) {
110-
_.invoke(this.getThemeProps(), 'onValueChange', !this.props.value);
109+
_.invoke(this.props, 'onValueChange', !this.props.value);
111110
}
112111
};
113112

114113
calcThumbOnPosition() {
115-
const props = this.getThemeProps();
114+
const props = this.props;
116115
const width = props.width || DEFAULT_WIDTH;
117116
const thumbSize = props.thumbSize || DEFAULT_THUMB_SIZE;
118117
let position = width - (2 * INNER_PADDING + thumbSize);
@@ -121,8 +120,8 @@ class Switch extends BaseComponent {
121120
}
122121

123122
getSwitchStyle() {
124-
const {value, onColor, offColor, style: propsStyle, disabled, disabledColor} = this.getThemeProps();
125-
const style = [this.styles.switch];
123+
const {value, onColor, offColor, style: propsStyle, disabled, disabledColor} = this.props;
124+
const style: SwitchProps['style'] = [this.styles.switch];
126125

127126
if (disabled) {
128127
style.push(disabledColor ? {backgroundColor: disabledColor} : this.styles.switchDisabled);
@@ -137,7 +136,7 @@ class Switch extends BaseComponent {
137136
}
138137

139138
renderThumb() {
140-
const {thumbStyle} = this.getThemeProps();
139+
const {thumbStyle} = this.props;
141140
const {thumbPosition} = this.state;
142141

143142
const interpolatedTranslateX = thumbPosition.interpolate({
@@ -153,9 +152,9 @@ class Switch extends BaseComponent {
153152
}
154153

155154
render() {
156-
const {...others} = this.getThemeProps();
157-
155+
const {...others} = this.props;
158156
return (
157+
// @ts-ignore
159158
<TouchableOpacity
160159
{...this.getAccessibilityProps()}
161160
activeOpacity={1}
@@ -204,4 +203,4 @@ function createStyles({
204203
});
205204
}
206205

207-
export default Switch;
206+
export default asBaseComponent<SwitchProps>(Switch);

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export {default as Image, ImageProps} from './components/image';
2323
export {default as Overlay, OverlayTypes} from './components/overlay';
2424
export {default as RadioButton, RadioButtonPropTypes} from './components/radioButton/RadioButton';
2525
export {default as RadioGroup, RadioGroupPropTypes} from './components/radioButton/RadioGroup';
26+
export {default as Switch, SwitchProps} from './components/switch';
2627
export {default as TabBar} from './components/TabBar';
2728
export {default as Fader, FaderProps, FaderPosition} from './components/fader';
2829
export {default as ExpandableSection, ExpandableSectionProps} from './components/expandableSection';

0 commit comments

Comments
 (0)