2016-11-08 22 views
1

我不能让设置defaultValue的任何版本(或defaultTime甚至只是value)从状态Redux的形式初始化TimePicker工作。所有其他字段填充正确,但日期/时间选择器不响应任何内容。帮助是muuuuch赞赏!使用redux-form和materialUI为TimePicker设置defaultValue?

import React, { Component, PropTypes } from 'react'; 
import { connect } from 'react-redux'; 

import moment from 'moment'; 

import { Field, reduxForm, FieldArray, formValueSelector } from 'redux-form'; 
import MenuItem from 'material-ui/MenuItem'; 
import { 
    SelectField, 
    TextField, 
    DatePicker, 
    TimePicker 
} from 'redux-form-material-ui'; 

const selector = formValueSelector('myPantry'); 

let ServiceGroup = ({ service, index, fields, isAppointment, serviceDate }) => 
    <div className="service-type-group" key={index}> 
    <h4>Service</h4> 
    <button 
     type="button" 
     className="remove-button" 
     onClick={() => fields.remove(index)}>Remove 
    </button> 
    <div className="field-group third"> 
     <Field 
     name={`${service}.day`} 
     component={SelectField} 
     floatingLabelText="Day of week" 
     className="textfield"> 
      <MenuItem value={1} primaryText="Monday" /> 
      <MenuItem value={2} primaryText="Tuesday" /> 
     </Field> 
     <Field 
     name={`${service}.from_time`} 
     component={TimePicker} 
     value={null} 
     floatingLabelText="From" 
     className="textfield" 
     /> 
     <Field 
     name={`${service}.until_time`} 
     component={TimePicker} 
     value={null} 
     floatingLabelText="To" 
     className="textfield" 
     /> 
    </div> 
    <div className="field-group half"> 
     <Field 
     name={`${service}.service_type`} 
     component={SelectField} 
     floatingLabelText="Service type" 
     className="textfield"> 
     <MenuItem value={1} primaryText="First-come first-served" /> 
     <MenuItem value={2} primaryText="Appointment" /> 
     </Field> 
    </div> 
    {isAppointment && 
     <div className="field-group sentence-inline"> 
     <Field 
      name={`${service}.max_people`} 
      type="number" 
      component={TextField} 
      label="Number of people per timeslot" 
      className="textfield" 
     /> 
     </div> 
    } 
    </div>; 

ServiceGroup = connect(
    (state, props) => ({ 
    isAppointment: selector(state, `${props.service}.service_type`) == 2, 
    serviceDate: selector(state, `${props.service}.from_time`) 
    }) 
)(ServiceGroup); 

class MyPantry extends Component { 
    static propTypes = { 
    onSubmit: PropTypes.func.isRequired 
    } 

    constructor(props, context) { 
    super(props, context); 
    } 

    renderGroups = ({ fields, meta: { touched, error } }) => { 
    return (
     <div> 
     {fields.map((service, index) => 
      <ServiceGroup service={service} fields={fields} index={index} key={index} /> 
     )} 
     <button 
      type="button" 
      className="action-button" 
      onClick={() => fields.push({})}>Add Service 
     </button> 
     {touched && error && <span>{error}</span>} 
     </div> 
    ); 
    } 

    render() { 
    const { handleSubmit } = this.props; 
    return (
     <form onSubmit={handleSubmit}> 
     <div className="section general"> 
      <Field 
      name="name" 
      type="text" 
      component={TextField} 
      label="Pantry name" 
      floatingLabelText="Pantry name" 
      className="textfield full" 
      /> 
      <div className="field-group half"> 
      <Field 
       name="address_street_1" 
       type="text" 
       component={TextField} 
       label="Address" 
       floatingLabelText="Address" 
       className="textfield" 
      /> 
      <Field 
       name="address_street_2" 
       type="text" 
       component={TextField} 
       label="Apartment, suite, etc." 
       floatingLabelText="Apartment, suite, etc." 
       className="textfield" 
      /> 
      </div> 
     </div> 
     <h3 className="section-title">Service Setup</h3> 
     <div className="section service"> 
      <FieldArray name="service_options" component={this.renderGroups} /> 
     </div> 
     <div> 
      <button type="submit" className="action-button">Save Pantry Details</button> 
     </div> 
     </form> 
    ); 
    } 

} 

MyPantry = reduxForm({ 
    form: 'myPantry' 
})(MyPantry); 

MyPantry = connect(
    state => ({ 
    initialValues: state.pantry.data 
    }) 
)(MyPantry); 

export default MyPantry; 
+0

我下面的答案是正确的反应,查看是否需要这些帮助。 – megkadams

回答

0

现在可以通过格式= {空}以时间和日期选择器。看到这里线程/关闭的问题:https://github.com/erikras/redux-form-material-ui/issues/37 ,可以查看这里开始鸣叫:https://twitter.com/megkadams/status/804363887428665345并继续在这里:https://twitter.com/megkadams/status/804369699693793280

<Field 
     name={`${service}.from_time`} 
     component={TimePicker} 
     format={(value, name) => { 
     if (fromTime) { 
      return value === '' ? null : new Date(fromTime) 
     } else { 
      return null; 
     } 
     }} 
     defaultValue={null} 
     pedantic 
     floatingLabelText="From" 
     className="textfield" 
    /> 
0

要设置默认时间TimePicker的:

<TimePicker hintText="Pick your time" defaultTime={new Date(2007, 11, 5, 8, 23, 17)} /> 

这里是一个工作的jsfiddle:https://jsfiddle.net/mu5r94m6/2/

+0

它可以像你的例子那样在redux-form/redux-form-material-ui之外工作,但是使用redux-form的上述版本不会注册defaultTime。 在componentDidMount想通了,可以在表单状态改变(或只是重申)TimePicker值,并导致组件正确地重新绘制,但它只是感觉恶心的,不能让它为FieldArrays工作,但(我需要它),所以不是一个完全可行的解决方案。改变正常字段(未场阵列)的例子: componentDidMount(){ this.props.change( '图像',this.props.register.returnData.pantry.images); } – megkadams

0

您使用this.props.pantry...renderGroups()内。

你需要bind,其功能是能够使用this

添加constructor到类和该函数使用bind

constructor(props) { 
    super(props); 
    this.renderGroups = this.renderGroups.bind(this); 
} 
+0

不幸的是 - 我甚至无法放入硬编码的日期对象并显示出来。它不会填充任何东西。 – megkadams

+0

我不认为你可以在Redux形式的'Field'上使用defaultValue。他们都受到控制。添加默认值的唯一方法是使用'initialValues'选项或'fields.push({fromTime:moment(...)})'中的默认值初始化表单。你如何将项目添加到'FieldArray'? – jpdelatorre

相关问题