/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @flow * @format */ 'use strict'; import type {RNTesterModuleExample} from '../../types/RNTesterTypes'; import RNTesterText from '../../components/RNTesterText'; import React from 'react'; import {Platform, Switch, View} from 'react-native'; type OnOffIndicatorProps = $ReadOnly<{on: boolean, testID: string}>; function OnOffIndicator({on, testID}: OnOffIndicatorProps) { return {on ? 'On' : 'Off'}; } type ExampleRowProps = $ReadOnly<{children: React.Node}>; function ExampleRow({children}: ExampleRowProps) { return ( {children} ); } type SimpleSwitchExampleState = $ReadOnly<{ trueSwitchIsOn: boolean, falseSwitchIsOn: boolean, }>; class BasicSwitchExample extends React.Component<{}, SimpleSwitchExampleState> { state: SimpleSwitchExampleState = { trueSwitchIsOn: true, falseSwitchIsOn: false, }; render(): React.Node { return ( this.setState({falseSwitchIsOn: value})} trackColor={{ true: 'yellow', false: 'purple', }} value={this.state.falseSwitchIsOn} /> this.setState({trueSwitchIsOn: value})} value={this.state.trueSwitchIsOn} /> ); } } class DisabledSwitchExample extends React.Component< {}, SimpleSwitchExampleState, > { state: SimpleSwitchExampleState = { trueSwitchIsOn: true, falseSwitchIsOn: false, }; render(): React.Node { return ( this.setState({falseSwitchIsOn: value})} value={this.state.falseSwitchIsOn} /> this.setState({trueSwitchIsOn: value})} value={this.state.trueSwitchIsOn} /> ); } } class ColorSwitchExample extends React.Component<{...}, $FlowFixMe> { state: any | {colorFalseSwitchIsOn: boolean, colorTrueSwitchIsOn: boolean} = { colorTrueSwitchIsOn: true, colorFalseSwitchIsOn: false, }; render(): React.Node { return ( this.setState({colorFalseSwitchIsOn: value})} style={{marginBottom: 10}} thumbColor="#0000ff" trackColor={{ false: '#ff0000', true: '#00ff00', }} value={this.state.colorFalseSwitchIsOn} /> this.setState({colorTrueSwitchIsOn: value})} thumbColor="#0000ff" trackColor={{ false: '#ff0000', true: '#00ff00', }} value={this.state.colorTrueSwitchIsOn} /> ); } } class EventSwitchExample extends React.Component<{...}, $FlowFixMe> { state: any | {eventSwitchIsOn: boolean, eventSwitchRegressionIsOn: boolean} = { eventSwitchIsOn: false, eventSwitchRegressionIsOn: true, }; render(): React.Node { return ( this.setState({eventSwitchIsOn: value})} style={{marginBottom: 10}} value={this.state.eventSwitchIsOn} /> this.setState({eventSwitchIsOn: value})} style={{marginBottom: 10}} value={this.state.eventSwitchIsOn} /> {this.state.eventSwitchIsOn ? 'On' : 'Off'} this.setState({eventSwitchRegressionIsOn: value}) } style={{marginBottom: 10}} value={this.state.eventSwitchRegressionIsOn} /> this.setState({eventSwitchRegressionIsOn: value}) } style={{marginBottom: 10}} value={this.state.eventSwitchRegressionIsOn} /> {this.state.eventSwitchRegressionIsOn ? 'On' : 'Off'} ); } } class IOSBackgroundColEx extends React.Component<{...}, $FlowFixMe> { state: any | {iosBackgroundColor: string} = { iosBackgroundColor: '#ffa500', }; render(): React.Node { return ( The background color can be seen either when the switch value is false or when the switch is disabled (and the switch is translucent).{' '} ); } } class OnChangeExample extends React.Component<{...}, $FlowFixMe> { render(): React.Node { return ( { // eslint-disable-next-line no-alert alert('OnChange Called'); }} /> ); } } class ContainerBackgroundColorStyleExample extends React.Component< {...}, $FlowFixMe, > { render(): React.Node { return ( ); } } exports.title = 'Switch'; exports.documentationURL = 'https://reactnative.dev/docs/switch'; exports.category = 'UI'; exports.displayName = 'SwitchExample'; exports.description = 'Native boolean input'; exports.examples = [ { title: 'Switches can be set to true or false', name: 'basic', render(): React.MixedElement { return ; }, }, { title: 'Switches can be disabled', name: 'disabled', render(): React.MixedElement { return ; }, }, { title: 'Change events can be detected', name: 'events', render(): React.MixedElement { return ; }, }, { title: 'Switches are controlled components', name: 'controlled', render(): React.MixedElement { return ; }, }, { title: 'Custom colors can be provided', name: 'custom-colors', render(): React.MixedElement { return ; }, }, { title: 'OnChange receives the change event as an argument', render(): React.MixedElement { return ; }, }, { title: "The container's background color can be set", render(): React.MixedElement { return ; }, }, ] as Array; if (Platform.OS === 'ios') { exports.examples.push({ title: '[iOS Only] Custom background colors can be set', render(): React.MixedElement { return ; }, }); }