无法使用redux-form键入文本字段

时间:2016-10-22 20:01:34

标签: reactjs redux redux-form

我在使用redux-form的模态中有一个表单。我有几个文本字段,但您无法输入它们。我怀疑文本字段没有从redux-form获得onChange事件,但我无法找到任何关于我做得好的线索。

我的代码是:

import React from 'react'
import { Button, Modal, Form, Message } from 'semantic-ui-react'
import { Field, reduxForm } from 'redux-form'

const renderField = ({ input, label, type, meta: { touched, error, warning } }) => {
  console.log(input)
  return (
  <Form.Field>
    <label>{label}</label>
    <input {...input} placeholder={label} type={type} />
    {touched && (error && <Message error>{error}</Message>)}
  </Form.Field>
)}

let AddNewModal = (props) => {
  const { handleSubmit, pristine, submitting, closeNewSite, isAddNewOpen, submit } = props

  return (
    <Modal dimmer='blurring' open={isAddNewOpen} onClose={closeNewSite}>
      <Modal.Header>Add a new site</Modal.Header>
      <Modal.Content>
        <Form onSubmit={handleSubmit}>
          <Form.Group widths='equal'>
            <Field name='domain' type='text' component={renderField} label='Domain' />
            <Field name='sitemap' type='text' component={renderField} label='Sitemap URL' />
          </Form.Group>
          /**
           * Other fields 
           * /
          <Button type='submit' disabled={pristine || submitting}>Save</Button>
        </Form>
      </Modal.Content>
      <Modal.Actions>
        <Button color='black' onClick={closeNewSite} content='Close' />
        <Button positive icon='save' labelPosition='right' onClick={submit} content='Save' disabled={pristine || submitting} />
      </Modal.Actions>
    </Modal>
  )
}

export default reduxForm({
  form: 'newsite'
})(AddNewModal)

4 个答案:

答案 0 :(得分:42)

我添加了减速机并仍然遇到了同样的问题。最后,我发现它必须添加attr&#39;形式&#39;。

const reducers = {
  routing,
  form: formReducer
};

答案 1 :(得分:10)

我发现了问题。我忘了注入redux-form的减速器。

答案 2 :(得分:1)

我实际上有类似的问题。我将使用redux-form的V6发布我正在进行表单验证的代码。它现在可以正常工作,但您想要查看的内容是componentDidMount,handleInitialize和handleFormSubmit。在那里我想出了link

/**
* Created by marcusjwhelan on 10/22/16.
*/

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { reduxForm, Field } from 'redux-form';  // V6 !!!!!!!!
import { createPost } from '../actions/index';

const renderInput = ({ input, label, type, meta: {touched, invalid, error }}) => (
  <div className={`form-group ${touched && invalid ? 'has-danger' : ''}`}>
    <label>{label}</label>
    <input className="form-control" {...input} type={type}/>
    <div className="text-help" style={{color: 'red'}}>
      { touched ? error : '' }
    </div>
  </div>
);

const renderTextarea = ({ input, label, type, meta: {touched, invalid, error }}) => (
  <div className={`form-group ${touched && invalid ? 'has-danger' : ''}`}>
    <label>{label}</label>
    <textarea className="form-control" {...input}/>
    <div className="text-help" style={{color: 'red'}}>
      { touched ? error : '' }
    </div>
  </div>
);

class PostsNew extends Component{
  componentDidMount(){
  this.handleInitialize();
}
handleInitialize(){
  const initData = {
    "title": '',
    "categories": '',
    "content": ''
  };
  this.props.initialize(initData);
}

handleFormSubmit(formProps){
  this.props.createPost(formProps)
}

render(){
  const { handleSubmit } = this.props;
  return (
    <form onSubmit={handleSubmit(this.handleFormSubmit.bind(this))}>
      <h3>Create A New Post</h3>

      <Field
        label="Title"
        name="title"
        type="text"
        component={renderInput} />

      <Field
        label="Categories"
        name="categories"
        type="text"
        component={renderInput}
      />

      <Field
        label="Content"
        name="content"
        component={renderTextarea}
      />

      <button type="submit" className="btn btn-primary" >Submit</button>
    </form>
  );
}
}

function validate(formProps){
  const errors = {};

  if(!formProps.title){
    errors.title = 'Enter a username';
  }
  if(!formProps.categories){
    errors.categories = 'Enter categories';
  }
  if(!formProps.content){
    errors.content = 'Enter content';
  }
  return errors;
}

const form = reduxForm({
  form: 'PostsNewForm', 
  validate
});

export default connect(null, { createPost })(form(PostsNew));

答案 3 :(得分:1)

您需要将Form Reducer连接到您的Combine Reducer

表格:formReducer

import { combineReducers } from 'redux';
import { reducer as formReducer } from 'redux-form';

import authReducer from './authReducer';
import productsReducer from './productsReducer';

export default combineReducers({
    auth: authReducer,
    form: formReducer,
    products: productsReducer,
});