【问题标题】:How to style React Native switch component?如何设置 React Native 开关组件的样式?
【发布时间】:2017-08-08 08:52:06
【问题描述】:

我正在使用 tcomb-form-native (https://github.com/gcanti/tcomb-form-native) 模块在 React Native 中制作表单。该模块提供布尔类型的输入,它呈现一个 Switch 组件 (https://facebook.github.io/react-native/docs/switch.html)。

现在,我正在尝试设置它的样式,所以 Switch 看起来像这样:

我不确定实现这一目标的最佳方法是什么?使用开关组件?使用触发真实开关的假组件(将被隐藏)?

【问题讨论】:

    标签: react-native tcomb-form-native


    【解决方案1】:

    我认为一个好方法是定制工厂 我做过一次定制开关,我会在这里发布。我没有修改 Switch 的外观,但我相信您可以创建一个看起来像您的图像的组件,然后在您的工厂中使用它。就我而言,我将一些文本与带有可点击链接的开关对齐,等等。我没有尝试过,但我想你可以用你自己的替换 Switch 组件。

    import React from 'react';
    import { View, Text, Switch, TouchableOpacity } from 'react-native';
    import t from 'tcomb-form-native';
    import Strings from '../config/strings.js';
    
    var Component = t.form.Component;
    
    class TermsSwitch extends Component {
    
      constructor (props) {
        super(props);
        var locals = super.getLocals();
      }
    
      getLocals () {
        var locals = super.getLocals();
        return locals;
      }
    
      getTemplate () {
        return function (locals) {
          var stylesheet = locals.stylesheet;
          var formGroupStyle = stylesheet.formGroup.normal;
          var controlLabelStyle = stylesheet.controlLabel.normal;
          var checkboxStyle = stylesheet.checkbox.normal;
          var helpBlockStyle = stylesheet.helpBlock.normal;
          var errorBlockStyle = stylesheet.errorBlock;
    
          if (locals.hasError) {
            formGroupStyle = stylesheet.formGroup.error;
            controlLabelStyle = stylesheet.controlLabel.error;
            checkboxStyle = stylesheet.checkbox.error;
            helpBlockStyle = stylesheet.helpBlock.error;
          }
    
          var label = locals.label ? <Text style={controlLabelStyle}>{locals.label}</Text> : null;
          var help = locals.help ? <Text style={helpBlockStyle}>{locals.help}</Text> : null;
          var error = locals.hasError && locals.error ? <Text accessibilityLiveRegion="polite" style={[errorBlockStyle, {marginTop: 2}]}>{locals.error}</Text> : null;
    
          return (
            <View style={formGroupStyle}>
              {label}
              <View style={{flexDirection: 'row', alignItems: 'center'}}>
                <View style={{flexDirection: 'row', flexWrap: 'wrap', alignItems: 'center'}}>
                  <Text style={{fontSize: 14}}>Jag har läst och accepterar </Text>
                  <TouchableOpacity onPress={() => locals.config.onPressTerms()}>
                    <Text style={{fontStyle: 'italic', fontSize: 14, textDecorationLine: 'underline'}}>villkoren</Text>
                  </TouchableOpacity>
                </View>
                <View style={{flex: 1, flexDirection: 'column', justifyContent: 'center', paddingTop: 6}}>
                  <Switch
                    accessibilityLabel={locals.label}
                    ref="input"
                    disabled={locals.disabled}
                    onTintColor={locals.onTintColor}
                    thumbTintColor={locals.thumbTintColor}
                    tintColor={locals.tintColor}
                    style={checkboxStyle}
                    onValueChange={(value) => locals.onChange(value)}
                    value={locals.value} />
                </View>
              </View>
              {help}
              {error}
            </View>
          );
        }
      }
    }
    
    export default TermsSwitch
    

    然后像这样使用你的工厂:

    const options = {
        fields: {
            your_switch: {
              config: {
                onPressTerms: () => {
                  ...
                },
              },
              factory: TermsSwitch,
              stylesheet: stylesheet,
            },
            ...
        },
    }
    

    希望对你有帮助!

    祝你好运!

    【讨论】:

    • 目前,我得到了完整的自定义内容。但我可能需要使用你的答案,因为它肯定更可靠。我不知道factory: TermsSwitch
    猜你喜欢
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 2018-01-22
    • 2018-08-26
    • 2022-10-16
    • 1970-01-01
    • 2021-08-22
    相关资源
    最近更新 更多