将选择菜单添加到react-native形式的字段中

时间:2015-12-22 14:41:34

标签: react-native tcomb

我想使用下拉列表将数据输入到字段日期和时间               选择菜单。请指导我如何做到这一点。我是新人               反应原生  我使用了github的tcomb-form-native模块。  请指导我完成这个。

'use strict';

var React = require('react-native');
//importing the react-native module
var t = require('tcomb-form-native');
//importing thetcomb-form-native module
var { AppRegistry, StyleSheet, Text, View, TouchableHighlight } =     React;

var Form = t.form.Form;

//  here we are: define your domain model
var timeSlot = t.struct({
day: t.String,             
time:t.String   
});

var options = {}; // optional rendering options (see documentation)

var AwesomeProject = React.createClass({

onPress: function () {
                    // call getValue() to get the values of the form
var value = this.refs.form.getValue();
if (value) {            // if validation fails, value will be null
  console.log(value);    // value here is an instance of Person
  }
},

render: function() {
return (
  <View style={styles.container}>
    {/* display */}
    <Form
      ref="form"
      type={timeSlot}
      options={options}
    />
    <TouchableHighlight style={styles.button} onPress={this.onPress}          underlayColor='#99d9f4'>
      <Text style={styles.buttonText}>continue</Text>
    </TouchableHighlight>
  </View>
  );
 }
});



var styles = StyleSheet.create({
 container: {
    justifyContent: 'center',
    marginTop: 50,
    padding: 20,
    backgroundColor: '#ffffff',
 },
 title: {
   fontSize: 30,
   alignSelf: 'center',
   marginBottom: 30
 },
 buttonText: {
   fontSize: 18,
   color: 'white',
   alignSelf: 'center'
 },
 button: {
   height: 36,
   backgroundColor: '#48BBEC',
   borderColor: '#48BBEC',
   borderWidth: 1,
   borderRadius: 8,
   marginBottom: 10,
   alignSelf: 'stretch',
   justifyContent: 'center'
 }
});


 module.exports=AwesomeProject

2 个答案:

答案 0 :(得分:1)

这是一个较旧的问题,我相信您已经找到了适合您的解决方案......但实际上有很好的文档说明如何使用tcomb-form-native实现select。 github页面上的内容只是一个起点。 Check out the extended documentation here

如果向下滚动到Select component下面的https://github.com/gcanti/tcomb-form-native

下方,github页面还有关于选择的其他文档

答案 1 :(得分:0)

我创建了一个支持select(PickerIOS)的项目,并且具有原生外观。 使用我的图书馆(可通过此链接https://github.com/MichaelCereda/react-native-form-generator访问),您可以执行此类操作。

import { Form, 
        PickerField
      } from 'react-native-form-generator';

 export class MyCoolComponent extends React.Component{
  handleFormChange(formData){
    /*
    formData will contain all the values of the form,
    in this example.

    formData = {
      gender: '',
    }
     */

   }
   render(){
      <Form
        ref='registrationForm'
        onChange={this.handleFormChange.bind(this)}
        name="Personal Information">
        
        <PickerField ref='gender' placeholder='Gender'
          options={{
            male: 'Male',
            female: 'Female'
          }}/>
        
      </Form>);
  }
}

相关问题