简体   繁体   English

使用react-jsonschema-form将开始日期和结束日期转换为Unix时间

[英]Convert startdate and enddate to unix time with react-jsonschema-form

I use this library to build my forms: https://github.com/mozilla-services/react-jsonschema-form and I have a uischema for date inputs, but when I send the form I want to convert startdate and enddate in unix time. 我使用此库来构建表单: https : //github.com/mozilla-services/react-jsonschema-form,并且我有用于输入日期的uischema,但是当我发送表单时,我想在UNIX中转换startdate和enddate时间。

Here is the code: 这是代码:

 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> import React, { Component, FormGroup, FormControl,ControlLabel } from 'react'; import NavBar from '../Header/NavBar'; import * as dropdown from "../../helpers/dropdown"; import Form from "react-jsonschema-form"; import * as formSubmit from "../../helpers/sumary"; import * as moment from 'moment'; class Sumary extends Component { constructor(props) { super(props); this.state = { activityschema: {}, machineschema: {}, activitiesTypes: [], startDate: moment(), endDate: moment(), activityType: 0, step: 0 }; } componentWillMount = async () => { const activitiesTypes = await dropdown.getActivitiesType(); const machines = await dropdown.getMachines(); this.setState({ activityschema: { type: "object", properties: { typeNameId: { type: 'number', title: 'Type:', enum: activitiesTypes.map((item) => { return item.id; }), enumNames: activitiesTypes.map((item) => { return item.name; }) }, startDate: {type: "string", title: 'Start date:'}, endDate: {type: "string", title: 'End date:'}, } }, machineschema: { type: "object", properties: { machinesId: { type: 'number', title: 'Machine Name:', enum: machines.map((item) => { return item.id; }), enumNames: machines.map((item) => { return item.name; }) }, //startDate: {type: "string", title: 'Start date:'}, //endDate: {type: "string", title: 'End date:'}, } }, uiSchema: { startDate: { "ui:widget": "date" }, endDate: { "ui:widget": "date" } } }); }; onSubmit = async ({formData}) => { if (formData.typeNameId === 10) { this.setState({ step: 1, formData: { ...this.state.formData, ...formData }, }); await formSubmit.getSumary(formData); console.log(formData); } else { await formSubmit.getSumary(formData); console.log(formData); } }; render () { let schema= null; schema = this.state.activityschema; let uiSchema = this.state.uiSchema; switch(this.state.step){ case 1: schema = this.state.machineschema; break; default: break; } return ( <Form schema={schema} uiSchema={uiSchema} onSubmit={this.onSubmit} formData={this.state.formData} /> ); } } export default Sumary; 

The code above works fine, but when I submit data, my formData in startDate and endDate are on this type of format: 上面的代码工作正常,但是当我提交数据时,startDate和endDate中的formData处于这种格式:

Object {typeNameId: 3, startDate: "2017-12-22", endDate: "2017-12-27"}

But I want them in unix time. 但是我希望它们在Unix时间里。

I tryed something like this: 我尝试过这样的事情:

onSubmit = async ({formData}) => {
      this.state.formData.startDate.unix();
      this.state.formData.endDate.unix();
      if (formData.typeNameId === 10) {
        this.setState({
          step: 1,
          formData: {
            ...this.state.formData, 
            ...formData
          },
        });
        await formSubmit.getSumary(formData);
        console.log(formData);
      } else {
        await formSubmit.getSumary(formData);
        console.log(formData);
      }
    };

But this will get: 但这将得到:

Unhandled Rejection (TypeError): Cannot read property 'startDate' of undefined 未处理的拒绝(TypeError):无法读取未定义的属性“ startDate”

So I see following problems with your code: 因此,我发现您的代码存在以下问题:

  1. You don't have formData in your React initial state. 您在React初始状态下没有formData
  2. If you want to use formData with your state you need to track onChange event to get the latest data 如果要在state使用formData ,则需要跟踪onChange事件以获取最新数据
  3. In your onSubmit if you use formData that you get in onSubmit call, it should be enough to remove Rejection error onSubmit如果您使用在onSubmit调用中获得的formData ,则它应该足以消除拒绝错误
  4. Date has a format of string , so it's not exactly moment object, you need to construct it from the string and convert to unix format 日期的格式为string ,因此它不是矩对象,您需要从字符串构造它并转换为unix格式
  5. If you want to input with type date you need to specify format on your schema object, otherwise it's just a text input field 如果要输入date类型,则需要在架构对象上指定格式,否则只是文本输入字段

You are right in your approach, there seems to be no straight forward way to do this, without adding additional widgets or fields. 您的方法是正确的,如果没有添加其他小部件或字段,似乎没有任何直接方法可以做到这一点。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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