2015-03-09 23:18:15 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2016-07-12 12:51:57 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-07-12 12:51:57 +00:00
|
|
|
*
|
2018-05-11 20:32:37 +00:00
|
|
|
* @format
|
2015-03-23 22:07:33 +00:00
|
|
|
* @flow
|
2015-03-09 23:18:15 +00:00
|
|
|
*/
|
2018-05-11 20:32:37 +00:00
|
|
|
|
2015-03-09 23:18:15 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-04-09 03:36:40 +00:00
|
|
|
var React = require('react');
|
|
|
|
var ReactNative = require('react-native');
|
2018-05-11 20:32:37 +00:00
|
|
|
var {Platform, Switch, Text, View} = ReactNative;
|
2015-03-09 23:18:15 +00:00
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
class BasicSwitchExample extends React.Component<{}, $FlowFixMeState> {
|
2016-07-26 08:00:02 +00:00
|
|
|
state = {
|
|
|
|
trueSwitchIsOn: true,
|
|
|
|
falseSwitchIsOn: false,
|
|
|
|
};
|
|
|
|
|
2015-03-09 23:18:15 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<View>
|
2016-01-11 20:45:17 +00:00
|
|
|
<Switch
|
2018-05-11 20:32:37 +00:00
|
|
|
onValueChange={value => this.setState({falseSwitchIsOn: value})}
|
2015-03-09 23:18:15 +00:00
|
|
|
style={{marginBottom: 10}}
|
2018-05-11 20:32:37 +00:00
|
|
|
value={this.state.falseSwitchIsOn}
|
|
|
|
/>
|
2016-01-11 20:45:17 +00:00
|
|
|
<Switch
|
2018-05-11 20:32:37 +00:00
|
|
|
onValueChange={value => this.setState({trueSwitchIsOn: value})}
|
|
|
|
value={this.state.trueSwitchIsOn}
|
|
|
|
/>
|
2015-03-09 23:18:15 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2015-03-09 23:18:15 +00:00
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
class DisabledSwitchExample extends React.Component<{}> {
|
2015-03-09 23:18:15 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<View>
|
2018-05-11 20:32:37 +00:00
|
|
|
<Switch disabled={true} style={{marginBottom: 10}} value={true} />
|
|
|
|
<Switch disabled={true} value={false} />
|
2015-03-09 23:18:15 +00:00
|
|
|
</View>
|
|
|
|
);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
class ColorSwitchExample extends React.Component<{}, $FlowFixMeState> {
|
2016-07-26 08:00:02 +00:00
|
|
|
state = {
|
|
|
|
colorTrueSwitchIsOn: true,
|
|
|
|
colorFalseSwitchIsOn: false,
|
|
|
|
};
|
2015-03-09 23:18:15 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<View>
|
2016-01-11 20:45:17 +00:00
|
|
|
<Switch
|
2018-05-11 20:32:37 +00:00
|
|
|
onValueChange={value => this.setState({colorFalseSwitchIsOn: value})}
|
2015-03-09 23:18:15 +00:00
|
|
|
style={{marginBottom: 10}}
|
2018-08-01 03:54:09 +00:00
|
|
|
thumbColor="#0000ff"
|
|
|
|
trackColor={{
|
|
|
|
false: '#ff0000',
|
|
|
|
true: '#00ff00',
|
|
|
|
}}
|
2018-05-11 20:32:37 +00:00
|
|
|
value={this.state.colorFalseSwitchIsOn}
|
|
|
|
/>
|
2016-01-11 20:45:17 +00:00
|
|
|
<Switch
|
2018-05-11 20:32:37 +00:00
|
|
|
onValueChange={value => this.setState({colorTrueSwitchIsOn: value})}
|
2018-08-01 03:54:09 +00:00
|
|
|
thumbColor="#0000ff"
|
|
|
|
trackColor={{
|
|
|
|
false: '#ff0000',
|
|
|
|
true: '#00ff00',
|
|
|
|
}}
|
2018-05-11 20:32:37 +00:00
|
|
|
value={this.state.colorTrueSwitchIsOn}
|
|
|
|
/>
|
2015-03-09 23:18:15 +00:00
|
|
|
</View>
|
|
|
|
);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
class EventSwitchExample extends React.Component<{}, $FlowFixMeState> {
|
2016-07-26 08:00:02 +00:00
|
|
|
state = {
|
|
|
|
eventSwitchIsOn: false,
|
|
|
|
eventSwitchRegressionIsOn: true,
|
|
|
|
};
|
2015-03-09 23:18:15 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2018-05-11 20:32:37 +00:00
|
|
|
<View style={{flexDirection: 'row', justifyContent: 'space-around'}}>
|
2015-03-09 23:18:15 +00:00
|
|
|
<View>
|
2016-01-11 20:45:17 +00:00
|
|
|
<Switch
|
2018-05-11 20:32:37 +00:00
|
|
|
onValueChange={value => this.setState({eventSwitchIsOn: value})}
|
2015-03-09 23:18:15 +00:00
|
|
|
style={{marginBottom: 10}}
|
2018-05-11 20:32:37 +00:00
|
|
|
value={this.state.eventSwitchIsOn}
|
|
|
|
/>
|
2016-01-11 20:45:17 +00:00
|
|
|
<Switch
|
2018-05-11 20:32:37 +00:00
|
|
|
onValueChange={value => this.setState({eventSwitchIsOn: value})}
|
2015-03-09 23:18:15 +00:00
|
|
|
style={{marginBottom: 10}}
|
2018-05-11 20:32:37 +00:00
|
|
|
value={this.state.eventSwitchIsOn}
|
|
|
|
/>
|
2016-05-21 07:09:46 +00:00
|
|
|
<Text>{this.state.eventSwitchIsOn ? 'On' : 'Off'}</Text>
|
2015-03-09 23:18:15 +00:00
|
|
|
</View>
|
|
|
|
<View>
|
2016-01-11 20:45:17 +00:00
|
|
|
<Switch
|
2018-05-11 20:32:37 +00:00
|
|
|
onValueChange={value =>
|
|
|
|
this.setState({eventSwitchRegressionIsOn: value})
|
|
|
|
}
|
2015-03-09 23:18:15 +00:00
|
|
|
style={{marginBottom: 10}}
|
2018-05-11 20:32:37 +00:00
|
|
|
value={this.state.eventSwitchRegressionIsOn}
|
|
|
|
/>
|
2016-01-11 20:45:17 +00:00
|
|
|
<Switch
|
2018-05-11 20:32:37 +00:00
|
|
|
onValueChange={value =>
|
|
|
|
this.setState({eventSwitchRegressionIsOn: value})
|
|
|
|
}
|
2015-03-09 23:18:15 +00:00
|
|
|
style={{marginBottom: 10}}
|
2018-05-11 20:32:37 +00:00
|
|
|
value={this.state.eventSwitchRegressionIsOn}
|
|
|
|
/>
|
2015-10-16 11:02:18 +00:00
|
|
|
<Text>{this.state.eventSwitchRegressionIsOn ? 'On' : 'Off'}</Text>
|
2015-03-09 23:18:15 +00:00
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2015-03-09 23:18:15 +00:00
|
|
|
|
2016-01-11 20:45:17 +00:00
|
|
|
var examples = [
|
2015-03-09 23:18:15 +00:00
|
|
|
{
|
|
|
|
title: 'Switches can be set to true or false',
|
2018-05-11 20:32:37 +00:00
|
|
|
render(): React.Element<any> {
|
|
|
|
return <BasicSwitchExample />;
|
|
|
|
},
|
2015-03-09 23:18:15 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Switches can be disabled',
|
2018-05-11 20:32:37 +00:00
|
|
|
render(): React.Element<any> {
|
|
|
|
return <DisabledSwitchExample />;
|
|
|
|
},
|
2015-03-09 23:18:15 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Change events can be detected',
|
2018-05-11 20:32:37 +00:00
|
|
|
render(): React.Element<any> {
|
|
|
|
return <EventSwitchExample />;
|
|
|
|
},
|
2015-03-09 23:18:15 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Switches are controlled components',
|
2018-05-11 20:32:37 +00:00
|
|
|
render(): React.Element<any> {
|
|
|
|
return <Switch />;
|
|
|
|
},
|
2017-01-31 21:33:38 +00:00
|
|
|
},
|
|
|
|
{
|
2016-01-11 20:45:17 +00:00
|
|
|
title: 'Custom colors can be provided',
|
2018-05-11 20:32:37 +00:00
|
|
|
render(): React.Element<any> {
|
|
|
|
return <ColorSwitchExample />;
|
|
|
|
},
|
|
|
|
},
|
2017-01-31 21:33:38 +00:00
|
|
|
];
|
2016-01-11 20:45:17 +00:00
|
|
|
|
|
|
|
exports.title = '<Switch>';
|
|
|
|
exports.displayName = 'SwitchExample';
|
|
|
|
exports.description = 'Native boolean input';
|
|
|
|
exports.examples = examples;
|