UNPKG

2.94 kBJavaScriptView Raw
1function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2
3import * as React from 'react';
4import { grey400, grey800, grey50, white, black } from '../styles/colors';
5import { NativeModules, Platform, Switch as NativeSwitch } from 'react-native';
6import setColor from 'color';
7import { withTheme } from '../core/theming';
8const version = NativeModules.PlatformConstants ? NativeModules.PlatformConstants.reactNativeVersion : undefined;
9
10/**
11 * Switch is a visual toggle between two mutually exclusive states — on and off.
12 *
13 * <div class="screenshots">
14 * <figure>
15 * <img src="screenshots/switch-enabled.android.png" />
16 * <figcaption>Android (enabled)</figcaption>
17 * </figure>
18 * <figure>
19 * <img src="screenshots/switch-disabled.android.png" />
20 * <figcaption>Android (disabled)</figcaption>
21 * </figure>
22 * <figure>
23 * <img src="screenshots/switch-enabled.ios.png" />
24 * <figcaption>iOS (enabled)</figcaption>
25 * </figure>
26 * <figure>
27 * <img src="screenshots/switch-disabled.ios.png" />
28 * <figcaption>iOS (disabled)</figcaption>
29 * </figure>
30 * </div>
31 *
32 * ## Usage
33 * ```js
34 * import * as React from 'react';
35 * import { Switch } from 'react-native-paper';
36 *
37 * const MyComponent = () => {
38 * const [isSwitchOn, setIsSwitchOn] = React.useState(false);
39 *
40 * const onToggleSwitch = () => setIsSwitchOn(!isSwitchOn);
41 *
42 * return <Switch value={isSwitchOn} onValueChange={onToggleSwitch} />;
43 * };
44 *
45 * export default MyComponent;
46 * ```
47 */
48const Switch = _ref => {
49 let {
50 value,
51 disabled,
52 onValueChange,
53 color,
54 theme,
55 ...rest
56 } = _ref;
57 const checkedColor = color || theme.colors.accent;
58 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();
59 const thumbTintColor = Platform.OS === 'ios' ? undefined : disabled ? theme.dark ? grey800 : grey400 : value ? checkedColor : theme.dark ? grey400 : grey50;
60 const props = version && version.major === 0 && version.minor <= 56 ? {
61 onTintColor,
62 thumbTintColor
63 } : Platform.OS === 'web' ? {
64 activeTrackColor: onTintColor,
65 thumbColor: thumbTintColor,
66 activeThumbColor: checkedColor
67 } : {
68 thumbColor: thumbTintColor,
69 trackColor: {
70 true: onTintColor,
71 false: ''
72 }
73 };
74 return /*#__PURE__*/React.createElement(NativeSwitch, _extends({
75 value: value,
76 disabled: disabled,
77 onValueChange: disabled ? undefined : onValueChange
78 }, props, rest));
79};
80
81export default withTheme(Switch);
82//# sourceMappingURL=Switch.js.map
\No newline at end of file