以redux-form v5

时间:2016-12-20 08:31:49

标签: reactjs datepicker redux redux-form react-dates

我试图在redux-form中使用SingleDatePicker。这是我表单中的render方法

render() {
    const {fields: {date}, handleSubmit} = this.props;

    return (
        <form onSubmit={handleSubmit}>
            <div>
                <SingleDatePicker
                    {...date}
                    id="date_input"
                    selected={date.value ? moment(date.value, 'DD/MM/YYYY') : moment()}
                    onChange={param => date.onChange(param)} />
            </div>

            <button type="submit"><Link to="/Step1">Submit</Link></button>
        </form>
    );
}

并在此表单组件的末尾

export default reduxForm({
form: 'thisForm',
fields: ['date']})(thisForm);

我在页面上得到的是像这样的静态输入

enter image description here

当我点击它时,什么也没发生。没有突出显示,没有像this这样的下拉日历。

有什么想法吗?有什么建议在redux-form中处理这样的datepicker吗?

2 个答案:

答案 0 :(得分:1)

尝试为redux-form创建一个隐藏字段,然后包装日期选择器,以便设置隐藏表单字段的值:

// Wrap the Airbnb component so that it conforms to the property API expected by redux-form
// See: https://github.com/erikras/redux-form/issues/1860
// Also, see: https://github.com/airbnb/react-dates/blob/master/examples/SingleDatePickerWrapper.jsx
class DateField extends Component {
  // The props are supplied via redux-form's <Field /> component
  static propTypes = {
    autoFocus: PropTypes.bool.isRequired,
    input: PropTypes.shape({
      value: PropTypes.string,
      onChange: PropTypes.func.isRequired,
      onFocus: PropTypes.func.isRequired,
      onBlur: PropTypes.func.isRequired
    }).isRequired
  }

  constructor (props) {
    super(props)
    const { value } = this.props.input
    this.state = {
      date: (!value || value === '') ? null : moment(value),
      focused: this.props.autoFocus
    }
  }

  // Use empty value instead of null to ensure it's treated as a controlled component
  getValueAsString = date => (date ? date.toISOString() : '')

  handleDateChange = (date, foo) => {
    this.setState({ date }, () => {
      const dateStr = this.getValueAsString(this.state.date)
      this.props.input.onChange(dateStr)
    })
  }

  handleFocusChange = (e) => {
    this.setState({ focused: e.focused }, () => {
      const date = this.state.date
      const dateStr = this.getValueAsString(date)
      if (e.focused) {
        this.props.input.onFocus(dateStr)
      } else {
        this.props.input.onBlur(dateStr)
      }
    })
  }

  renderHiddenField = field => (
    <input {...field.input} type={'hidden'} />
  )

  render () {
    // eslint-disable-next-line
    const { name, input, meta, ...rest } = this.props
    const dateStr = this.getValueAsString(this.state.date)
    return (
      <div>
        <Field
          {...this.props}
          name={`_hidden_${input.name}`}
          value={dateStr}
          component={this.renderHiddenField}
        />
        <SingleDatePicker
          id={`_wrapped_${input.name}`}
          date={this.state.date}
          focused={this.state.focused}
          onDateChange={this.handleDateChange}
          onFocusChange={this.handleFocusChange}
          {...rest}
        />
      </div>
    )
  }
}

答案 1 :(得分:-1)

try-> this.state.focused.focused

<SingleDatePicker
  date={this.state.createdAt}
  focused={this.state.calanderFocused.focused}
  onDateChange={this.onDateChange}
  onFocusChange={this.onFocusChange}
/>