简体   繁体   中英

Customize tcomb-form-native's data filds

I'm trying to customize the Data field of a tcomb-form-native module.

I wish the date fields were a classic input field but I still tried different methods, I didn't succeed.

I tried to override the datepicker field style but put the style when opening the picker to insert the date and not around the message.

在此处输入图像描述

Instead of 'Tap here to select a date' I would like to insert a phrase at will. How can I do?

Also, how can I customize the date format? I tried following this issue of github but it didn't solve the problem. This is the part of code for formatting the data:

config: {
  format: date => {
    let toBeFormatted = new Date(date);
    return String('Valida dal' + toBeFormatted.format('DD/MM/YYYY'));
  },
  dateFormat: date => {
    let toBeFormatted = new Date(date);
    return String('Valida dal' + toBeFormatted.format('DD/MM/YYYY'));
  },
  timeFormat: date => {
    let toBeFormatted = new Date(date);
    return String('Valida dal' + toBeFormatted.format('DD/MM/YYYY'));
  },
}

Okay. I can give you my code. I had a little trouble finding it, but finally, everything is in the tcomb documentation.

The two important points to answer your question are: " defaultValueText " and " format: (date) =>... "

import React, { Component } from "react";
import Expo from "expo";
import t from "tcomb-form-native";
import moment from 'moment';
import { StyleSheet, Text, Date} from "react-native";
import { Button } from "react-native-elements";

const Form = t.form.Form;

Form.stylesheet.dateValue.normal.borderColor = '#d0d2d3';
Form.stylesheet.dateValue.normal.backgroundColor = '#ffffff';
Form.stylesheet.dateValue.normal.borderRadius= 5,
Form.stylesheet.dateValue.normal.color = 'grey';
Form.stylesheet.dateValue.normal.borderWidth = 1;

const User = t.struct({
  pseudo: t.String,
  birthday: t.Date,
});

const options = {
    order: ['pseudo','birthday'],
    fields: {
    pseudo: {
        placeholder: 'Enter Name',
        error: 'Name is empty?',
      },
      birthday: {
         mode: 'date',
         label: 'birthday',
         config: {
            defaultValueText: 'Enter birthday', // Allows you to format the PlaceHolders !!
            format: (date) => {
               return moment(date).format('DD-MM-YYYY'); // Allows you to format the date !!
            },
          }
      },
    },
};

... ...

export default class SignUp extends Component {
state = {...

  render() {
      return (
        <View style={styles.container}>
          <Form
            type={User}
            ref={c => (this._form = c)} // assign a ref
            options={options} //set form options
          />

          <Button
            title="Sign Up!"
            buttonStyle={styles.button}
            onPress={this.handleSubmit}
          />
        </View>
      );
    }
  }
 } ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM