在Redux存储中存储多页表单

时间:2019-03-18 22:00:30

标签: javascript reactjs redux

基本上我有一个3页的表单,单击一页上的“提交”按钮会将您带到下一页,等等。

在第三页上,我想将3页中的所有数据放入redux存储中。

我正在使用redux-form和以下示例将其实现到我的项目https://codesandbox.io/s/0Qzz3843中。

在该示例中,页面上有一个部分,在键入内容时,值将显示在组件中,我不需要它。

我希望在提交最终表格时提醒所有数据。

对不起,我不能将其放在诸如代码笔之类的东西上,因为这是一个很大的项目。我也不能更改表单,因为im所使用的react-bootstrap表单必须具有以下确切布局:

最后一页:

 import React, { Component } from 'react';
    import { Col, Row, Button, Form, FormGroup, Label, Input, FormText } from 'reactstrap';
    import { withRouter } from 'react-router-dom';
    import validate from '../validate';
    import { reduxForm } from 'redux-form';
    import './NewForm.css';
    import renderField from '../renderField';
    import data from '../data.json';

    const NewFormPOI = props => {
            const { handleSubmit } = props;
            let placeholders = data.en.placeholders;

            return (
                <Form onSubmit={handleSubmit} className="form">
                    {/* General Information */}
                    <FormGroup row>
                        <Col sm={6}>
                            <Input component={renderField} type="text" name="poiName" id="poiName" placeholder={placeholders.poiName} />
                        </Col>
                        <Col sm={6}>
                            <Input component={renderField} id="poiType" className="form-control" type="select" name="poiType">
                            <option className="selectDefault" disabled selected>{placeholders.poiType}</option>
                            <option value="Hu">Hi</option>
                            </Input>
                        </Col>
                    </FormGroup>
                    <FormGroup row>
                        <Col sm={12}>
                            <Input component={renderField} type="textarea" name="poiDesc" id="poiDesc" placeholder={placeholders.POIdesc} />
                        </Col>
                    </FormGroup>
                    <FormGroup row>
                        <Col sm={6}>
                            <Input component={renderField} type="email" name="poiDuration" id="poiDuration" placeholder={placeholders.poiDuration} />
                        </Col> 
                        <Col sm={6}>
                            <Input component={renderField} type="text" name="poiRanking" id="poiRanking" placeholder={placeholders.poiRanking} />
                        </Col>
                    </FormGroup>
                    <FormGroup row>
                        <Col sm={6}>
                            <Input component={renderField} type="text" name="poiAccess" id="poiAccess" placeholder={placeholders.access} />
                        </Col>
                        <Col sm={6}>
                            <Input component={renderField} type="textarea" name="poiTime" id="poiTime" placeholder={placeholders.times} />
                        </Col>
                    </FormGroup>
                    <hr/>
                    <FormGroup row>
                        <Col sm={12}>
                            <Input component={renderField} type="textarea" name="txtHeader" id="txtHeader" placeholder={placeholders.textHeader} />
                        </Col>
                    </FormGroup>
                    <FormGroup row>
                        <Col sm={12}>
                            <Input component={renderField} type="textarea" name="txtContent" id="txtContent" placeholder={placeholders.textContent}/>
                        </Col>
                    </FormGroup>
                    <FormGroup check row>
                        <Col sm={{ size: 6 }}>
                            <Button className="btn-info" type="submit" className="next">Next </Button>
                        </Col>
                    </FormGroup> 
                </Form>
            )
        }
        export default withRouter(reduxForm({
            form: 'wizard', //                 <------ same form name
            destroyOnUnmount: false, //        <------ preserve form data
            forceUnregisterOnUnmount: true // <------ unregister fields on unmount
          })(NewFormPOI));

WIZARDFORM.JS

import React, { Component } from 'react'
import PropTypes from 'prop-types'
import NewFormDetails from './components/NewFormDetails';
import NewFormTours from './components/NewFormTours';
import NewFormPOI from './components/NewFormPOI';

class WizardForm extends Component {
  constructor(props) {
    super(props)
    this.nextPage = this.nextPage.bind(this)
    this.previousPage = this.previousPage.bind(this)
    this.state = {
      page: 1
    }
  }
  nextPage() {
    this.setState({ page: this.state.page + 1 })
  }

  previousPage() {
    this.setState({ page: this.state.page - 1 })
  }

  render() {
    const { onSubmit } = this.props
    const { page } = this.state
    return (
      <div>
        {page === 1 && <NewFormDetails onSubmit={this.nextPage} />}
        {page === 2 && (
          <NewFormTours
            previousPage={this.previousPage}
            onSubmit={this.nextPage}
          />
        )}
        {page === 3 && (
          <NewFormPOI
            previousPage={this.previousPage}
            onSubmit={onSubmit}
          />
        )}
      </div>
    )
  }
}

WizardForm.propTypes = {
  onSubmit: PropTypes.func.isRequired
}

export default WizardForm

STORE.JS

  import { createStore, combineReducers } from 'redux';
    import { reducer as reduxFormReducer } from 'redux-form';

    const reducer = combineReducers({
      form: reduxFormReducer, // mounted under "form"
    });
    const store = (window.devToolsExtension
      ? window.devToolsExtension()(createStore)
      : createStore)(reducer);

    export default store;

INDEX.JS

import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router } from "react-router-dom";
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import { createBrowserHistory } from "history";
import { Provider } from "react-redux";
import store from './store';


const history =  createBrowserHistory();

ReactDOM.render(
  <Provider store={store}>
    <Router history={history}>
      <App />
    </Router>
  </Provider>,
  document.getElementById("root")
);

serviceWorker.unregister();

APP.JS

 render() {

        const mouseEnter = e => {
          this.setState({sideNav: "sideNav sidenav---sidenav---_2tBP sidenav---expanded---1KdUL"});
        }    

        const mouseLeave = e => {
          this.setState({sideNav: "sidenav---sidenav---_2tBP sidenav---collapsed---LQDEv"});
        }


        return (
          <div className="App container">
            <div>
              <SideNav 
                onMouseEnter={mouseEnter} 
                onMouseLeave={mouseLeave}
                className={this.state.sideNav}
                onSelect={(selected) => {
                    // Add your code here
                }}
              >
                <SideNav.Nav  defaultSelected="home">
                    <NavItem eventKey="home">
                        <NavIcon>
                            <Link to="/"><img src={Dash}/></Link>
                        </NavIcon>
                        <NavText>
                            <Link to="/">Dashboard</Link>
                        </NavText>
                    </NavItem>
                    <NavItem eventKey="sites">
                        <NavIcon>
                          <Link to="/sites"><img src={Site} /></Link>
                        </NavIcon>
                        <NavText>
                            <Link to="/sites">Sites</Link>
                        </NavText>
                    </NavItem>
                    <NavItem eventKey="tours">
                      <NavIcon>
                        <Link to="/tours"><img src={Tour}/></Link>
                      </NavIcon>
                      <NavText>
                          <Link to="/tours">Tours</Link>
                      </NavText>
                    </NavItem>
                    <NavItem eventKey="media">
                        <NavIcon>
                          <Link to="/media"><img src={Media}/> </Link>
                        </NavIcon>
                        <NavText>
                            <Link to="/media">Media</Link>
                        </NavText>
                    </NavItem>
                    <NavItem eventKey="newSite">
                        <NavIcon>
                            <Link to="/newSite/details"><img src={NewSite} /></Link>
                        </NavIcon>
                        <NavText>
                            <Link to="/newSite/details">Add new Site</Link>
                        </NavText>
                    </NavItem>
                    <NavItem eventKey="language">
                        <NavIcon>
                            <Link to="/language"><img src={Lang} /></Link>
                        </NavIcon>
                        <NavText>
                            <Link to="/language">Language</Link>
                        </NavText>
                    </NavItem>
                    <NavItem eventKey="profile">
                        <NavIcon>
                            <Link to="/profile"><img src={Profile} /></Link>
                        </NavIcon>
                        <NavText>
                            <Link to="/profile">Profile</Link>
                        </NavText>
                    </NavItem>

                </SideNav.Nav>
              </SideNav>
            </div>
            <WizardForm onSubmit={showResults} />
            {/*<Routes childProps={this.state} /> */ }
          </div>
        );
      }
    }

    export default App;

0 个答案:

没有答案