Skip to content

Commit 861be8e

Browse files
committed
Switch component updated
1 parent e661a55 commit 861be8e

1 file changed

Lines changed: 60 additions & 25 deletions

File tree

Libraries/Components/Switch/Switch.js

Lines changed: 60 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,63 +36,98 @@ export type Props = $ReadOnly<{|
3636
...ViewProps,
3737

3838
/**
39-
* Whether the switch is disabled. Defaults to false.
39+
If true the user won't be able to toggle the switch.
40+
41+
@default false
4042
*/
4143
disabled?: ?boolean,
4244

4345
/**
44-
* Boolean value of the switch. Defaults to false.
46+
The value of the switch. If true the switch will be turned on.
47+
48+
@default false
4549
*/
4650
value?: ?boolean,
4751

4852
/**
49-
* Custom color for the switch thumb.
53+
Color of the foreground switch grip. If this is set on iOS, the switch grip will lose its drop shadow.
5054
*/
5155
thumbColor?: ?ColorValue,
5256

5357
/**
54-
* Custom colors for the switch track.
55-
*
56-
* NOTE: On iOS when the switch value is false, the track shrinks into the
57-
* border. If you want to change the color of the background exposed by the
58-
* shrunken track, use `ios_backgroundColor`.
58+
Custom colors for the switch track.
59+
60+
_iOS_: When the switch value is false, the track shrinks into the border. If you want to change the
61+
color of the background exposed by the shrunken track, use
62+
[`ios_backgroundColor`](https://reactnative.dev/docs/switch#ios_backgroundColor).
5963
*/
6064
trackColor?: ?$ReadOnly<{|
6165
false?: ?ColorValue,
6266
true?: ?ColorValue,
6367
|}>,
6468

6569
/**
66-
* On iOS, custom color for the background. This background color can be seen
67-
* either when the switch value is false or when the switch is disabled (and
68-
* the switch is translucent).
70+
On iOS, custom color for the background. This background color can be
71+
seen either when the switch value is false or when the switch is
72+
disabled (and the switch is translucent).
6973
*/
7074
ios_backgroundColor?: ?ColorValue,
7175

7276
/**
73-
* Called when the user tries to change the value of the switch.
74-
*
75-
* Receives the change event as an argument. If you want to only receive the
76-
* new value, use `onValueChange` instead.
77+
Invoked when the user tries to change the value of the switch. Receives
78+
the change event as an argument. If you want to only receive the new
79+
value, use `onValueChange` instead.
7780
*/
7881
onChange?: ?(event: SwitchChangeEvent) => Promise<void> | void,
7982

8083
/**
81-
* Called when the user tries to change the value of the switch.
82-
*
83-
* Receives the new value as an argument. If you want to instead receive an
84-
* event, use `onChange`.
84+
Invoked when the user tries to change the value of the switch. Receives
85+
the new value as an argument. If you want to instead receive an event,
86+
use `onChange`.
8587
*/
8688
onValueChange?: ?(value: boolean) => Promise<void> | void,
8789
|}>;
8890

8991
/**
90-
* A visual toggle between two mutually exclusive states.
91-
*
92-
* This is a controlled component that requires an `onValueChange` callback that
93-
* updates the `value` prop in order for the component to reflect user actions.
94-
* If the `value` prop is not updated, the component will continue to render the
95-
* supplied `value` prop instead of the expected result of any user actions.
92+
Renders a boolean input.
93+
94+
This is a controlled component that requires an `onValueChange`
95+
callback that updates the `value` prop in order for the component to
96+
reflect user actions. If the `value` prop is not updated, the
97+
component will continue to render the supplied `value` prop instead of
98+
the expected result of any user actions.
99+
100+
@example ```SnackPlayer name=Switch
101+
import React, { useState } from "react";
102+
import { View, Switch, StyleSheet } from "react-native";
103+
104+
const App = () => {
105+
const [isEnabled, setIsEnabled] = useState(false);
106+
const toggleSwitch = () => setIsEnabled(previousState => !previousState);
107+
108+
return (
109+
<View style={styles.container}>
110+
<Switch
111+
trackColor={{ false: "#767577", true: "#81b0ff" }}
112+
thumbColor={isEnabled ? "#f5dd4b" : "#f4f3f4"}
113+
ios_backgroundColor="#3e3e3e"
114+
onValueChange={toggleSwitch}
115+
value={isEnabled}
116+
/>
117+
</View>
118+
);
119+
}
120+
121+
const styles = StyleSheet.create({
122+
container: {
123+
flex: 1,
124+
alignItems: "center",
125+
justifyContent: "center"
126+
}
127+
});
128+
129+
export default App;
130+
```
96131
*/
97132
class Switch extends React.Component<Props> {
98133
_nativeSwitchRef: ?React.ElementRef<

0 commit comments

Comments
 (0)