如何设置React Native开关组件的样式?

时间:2017-08-08 08:52:06

标签: react-native tcomb-form-native

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

现在,我正在尝试设置它,所以Switch看起来像这样:enter image description here

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

1 个答案:

答案 0 :(得分: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,
        },
        ...
    },
}

我希望这可以帮到你!

祝你好运!