2015-03-09 23:18:15 +00:00
|
|
|
/**
|
2017-05-06 03:50:47 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
2016-07-12 12:51:57 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
2015-03-23 22:07:33 +00:00
|
|
|
* @flow
|
2017-02-25 11:05:32 +00:00
|
|
|
* @providesModule SwitchExample
|
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');
|
2015-03-09 23:18:15 +00:00
|
|
|
var {
|
2016-04-09 03:36:40 +00:00
|
|
|
Platform,
|
2016-01-11 20:45:17 +00:00
|
|
|
Switch,
|
2015-03-09 23:18:15 +00:00
|
|
|
Text,
|
|
|
|
View
|
2016-04-09 03:36:40 +00:00
|
|
|
} = ReactNative;
|
2015-03-09 23:18:15 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
class BasicSwitchExample extends React.Component {
|
|
|
|
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
|
2015-03-09 23:18:15 +00:00
|
|
|
onValueChange={(value) => this.setState({falseSwitchIsOn: value})}
|
|
|
|
style={{marginBottom: 10}}
|
|
|
|
value={this.state.falseSwitchIsOn} />
|
2016-01-11 20:45:17 +00:00
|
|
|
<Switch
|
2015-03-09 23:18:15 +00:00
|
|
|
onValueChange={(value) => this.setState({trueSwitchIsOn: value})}
|
|
|
|
value={this.state.trueSwitchIsOn} />
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2015-03-09 23:18:15 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
class DisabledSwitchExample extends React.Component {
|
2015-03-09 23:18:15 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<View>
|
2016-01-11 20:45:17 +00:00
|
|
|
<Switch
|
2015-03-09 23:18:15 +00:00
|
|
|
disabled={true}
|
|
|
|
style={{marginBottom: 10}}
|
|
|
|
value={true} />
|
2016-01-11 20:45:17 +00:00
|
|
|
<Switch
|
2015-03-09 23:18:15 +00:00
|
|
|
disabled={true}
|
|
|
|
value={false} />
|
|
|
|
</View>
|
|
|
|
);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ColorSwitchExample extends React.Component {
|
|
|
|
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
|
2015-03-09 23:18:15 +00:00
|
|
|
onValueChange={(value) => this.setState({colorFalseSwitchIsOn: value})}
|
|
|
|
onTintColor="#00ff00"
|
|
|
|
style={{marginBottom: 10}}
|
|
|
|
thumbTintColor="#0000ff"
|
|
|
|
tintColor="#ff0000"
|
|
|
|
value={this.state.colorFalseSwitchIsOn} />
|
2016-01-11 20:45:17 +00:00
|
|
|
<Switch
|
2015-03-09 23:18:15 +00:00
|
|
|
onValueChange={(value) => this.setState({colorTrueSwitchIsOn: value})}
|
|
|
|
onTintColor="#00ff00"
|
|
|
|
thumbTintColor="#0000ff"
|
|
|
|
tintColor="#ff0000"
|
|
|
|
value={this.state.colorTrueSwitchIsOn} />
|
|
|
|
</View>
|
|
|
|
);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class EventSwitchExample extends React.Component {
|
|
|
|
state = {
|
|
|
|
eventSwitchIsOn: false,
|
|
|
|
eventSwitchRegressionIsOn: true,
|
|
|
|
};
|
2015-03-09 23:18:15 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<View style={{ flexDirection: 'row', justifyContent: 'space-around' }}>
|
|
|
|
<View>
|
2016-01-11 20:45:17 +00:00
|
|
|
<Switch
|
2015-03-09 23:18:15 +00:00
|
|
|
onValueChange={(value) => this.setState({eventSwitchIsOn: value})}
|
|
|
|
style={{marginBottom: 10}}
|
|
|
|
value={this.state.eventSwitchIsOn} />
|
2016-01-11 20:45:17 +00:00
|
|
|
<Switch
|
2015-03-09 23:18:15 +00:00
|
|
|
onValueChange={(value) => this.setState({eventSwitchIsOn: value})}
|
|
|
|
style={{marginBottom: 10}}
|
|
|
|
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
|
2015-03-09 23:18:15 +00:00
|
|
|
onValueChange={(value) => this.setState({eventSwitchRegressionIsOn: value})}
|
|
|
|
style={{marginBottom: 10}}
|
|
|
|
value={this.state.eventSwitchRegressionIsOn} />
|
2016-01-11 20:45:17 +00:00
|
|
|
<Switch
|
2015-03-09 23:18:15 +00:00
|
|
|
onValueChange={(value) => this.setState({eventSwitchRegressionIsOn: value})}
|
|
|
|
style={{marginBottom: 10}}
|
|
|
|
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',
|
2016-10-16 11:11:59 +00:00
|
|
|
render(): React.Element<any> { return <BasicSwitchExample />; }
|
2015-03-09 23:18:15 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Switches can be disabled',
|
2016-10-16 11:11:59 +00:00
|
|
|
render(): React.Element<any> { return <DisabledSwitchExample />; }
|
2015-03-09 23:18:15 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Change events can be detected',
|
2016-10-16 11:11:59 +00:00
|
|
|
render(): React.Element<any> { return <EventSwitchExample />; }
|
2015-03-09 23:18:15 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Switches are controlled components',
|
2016-10-16 11:11:59 +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',
|
2016-10-16 11:11:59 +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;
|