forked from callstack/react-native-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwitch.tsx
158 lines (149 loc) · 3.45 KB
/
Switch.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import * as React from 'react';
import { grey400, grey800, grey50, white, black } from '../styles/colors';
import {
NativeModules,
Platform,
StyleProp,
Switch as NativeSwitch,
ViewStyle,
} from 'react-native';
import setColor from 'color';
import { withTheme } from '../core/theming';
import { Theme } from '../types';
const version = NativeModules.PlatformConstants
? NativeModules.PlatformConstants.reactNativeVersion
: undefined;
type Props = React.ComponentProps<typeof NativeSwitch> & {
/**
* Disable toggling the switch.
*/
disabled?: boolean;
/**
* Value of the switch, true means 'on', false means 'off'.
*/
value?: boolean;
/**
* Custom color for switch.
*/
color?: string;
/**
* Callback called with the new value when it changes.
*/
onValueChange?: Function;
style?: StyleProp<ViewStyle>;
/**
* @optional
*/
theme: Theme;
};
/**
* Switch is a visual toggle between two mutually exclusive states — on and off.
*
* <div class="screenshots">
* <figure>
* <img src="screenshots/switch-enabled.android.png" />
* <figcaption>Android (enabled)</figcaption>
* </figure>
* <figure>
* <img src="screenshots/switch-disabled.android.png" />
* <figcaption>Android (disabled)</figcaption>
* </figure>
* <figure>
* <img src="screenshots/switch-enabled.ios.png" />
* <figcaption>iOS (enabled)</figcaption>
* </figure>
* <figure>
* <img src="screenshots/switch-disabled.ios.png" />
* <figcaption>iOS (disabled)</figcaption>
* </figure>
* </div>
*
* ## Usage
* ```js
* import * as React from 'react';
* import { Switch } from 'react-native-paper';
*
* export default class MyComponent extends React.Component {
* state = {
* isSwitchOn: false,
* };
*
* render() {
* const { isSwitchOn } = this.state;
* return (
* <Switch
* value={isSwitchOn}
* onValueChange={() =>
* { this.setState({ isSwitchOn: !isSwitchOn }); }
* }
* />
* );
* }
* }
* ```
*/
class Switch extends React.Component<Props> {
render() {
const {
value,
disabled,
onValueChange,
color,
theme,
...rest
} = this.props;
const checkedColor = color || theme.colors.accent;
const onTintColor =
Platform.OS === 'ios'
? checkedColor
: disabled
? theme.dark
? setColor(white)
.alpha(0.1)
.rgb()
.string()
: setColor(black)
.alpha(0.12)
.rgb()
.string()
: setColor(checkedColor)
.alpha(0.5)
.rgb()
.string();
const thumbTintColor =
Platform.OS === 'ios'
? undefined
: disabled
? theme.dark
? grey800
: grey400
: value
? checkedColor
: theme.dark
? grey400
: grey50;
const props =
version && version.major === 0 && version.minor <= 56
? {
onTintColor,
thumbTintColor,
}
: {
thumbColor: thumbTintColor,
trackColor: {
true: onTintColor,
false: '',
},
};
return (
<NativeSwitch
value={value}
disabled={disabled}
onValueChange={disabled ? undefined : onValueChange}
{...props}
{...rest}
/>
);
}
}
export default withTheme(Switch);