简体   繁体   English

ReactJs 组件结构 - 模态内的表单

[英]ReactJs component structure - Form inside modal

I am using the react-bootstrap Modal, Form and Button.我正在使用 react-bootstrap 模态、表单和按钮。

Desiring the functionality of clicking the button should open the modal with a form inside it.想要单击按钮的功能应该打开带有表单的模式。 After filling out the form, one clicks a button (on the modal ) and it validates the form data and posts it through a REST API.填写完表单后,单击一个按钮(在modal 上),它会验证表单数据并通过 REST API 发布它。

I got far enough to figure out that my component split should be as follows:我已经足够弄清楚我的组件拆分应该如下:

A button component, a modal component and a form component.一个按钮组件、一个模态组件和一个表单组件。

What would be the correct way to structure these components in terms of props/state and placing the functions for validating the data?根据道具/状态构建这些组件并放置用于验证数据的功能的正确方法是什么? I am having trouble in understanding the child/parent relationship and when it's applicable我在理解孩子/父母关系以及何时适用时遇到困难

Components:成分:

  1. App Component: This is going to be the top level component应用组件:这将是顶级组件

  2. Button Component (If its just a button can also be just a button): If this is just a button you can keep this has a just a button in App component , if you are willing to reuse this with some custom element place it in a component.按钮组件(如果它只是一个按钮也可以只是一个按钮):如果这只是一个按钮,你可以保持它在App component只有一个按钮,如果你愿意用一些自定义元素重用它,把它放在一个成分。

  3. Modal component: This is going to hold your modal like header , body , footer模态组件:这将保存您的模态,如页眉正文页脚
  4. Form component: This is a component which will hold the form and its validations.表单组件:这是一个保存表单及其验证的组件。

Component Tree:组件树:

App Component will contain a state like showModal , we need to have a handler to set this value and this handler gets triggered when the button is clicked. App 组件将包含类似showModal状态,我们需要有一个处理程序来设置此值,并且在单击按钮时会触发此处理程序。

import FormModal from './FormModal';   

class App extends React.Component {
   state = {
    showModal : false
  }

  showModalHandler = (event) =>{
    this.setState({showModal:true});
  }

  hideModalHandler = (event) =>{
    this.setState({showModal:false});
  }

  render() {
    return (
      <div className="shopping-list">
        <button type="button" onClick={this.showModalHandler}>Click Me! 
        </button>
      </div>
      <FormModal showModal={this.sate.showModal} hideModalHandler={this.hideModalHandler}></FormModal>
    );
  }
}

Form Modal:形式模态:

import FormContent from './FormContent';

class FormModal extends React.Component {
  render() {
    const formContent = <FormContent></FormContent>;
    const modal = this.props.showModal ? <div>{formContent}</div> : null;
    return (
      <div>
        {modal}
      </div>
    );
  }
}

export default FormModal;

Hope that helped!希望有所帮助!

For basic pseudo code对于基本伪代码

Main Component:主要组件:

import Modal from './Modal'
class Super extends React.Component {
    constructor(){
        this.state={
            modalShowToggle: false
        }
    }
    ModalPopUpHandler=()=>{
        this.setState({
            modalShowToggle: !modalShowToggle
        })
    }
    render(){
        return(){
            <div>
                <Button title='ModalOpen' onClick='this.ModalPopUpHandler'> </Button>
                <ModalComponent show={this.state.modalShowToggle}>
            </div>
        }
    }
}

ModalPopUp component: ModalPopUp 组件:

import FormComponent from 'FormComponent'
class ModalComponent extends React.Component {
    constructor(props){
        super(props)
        this.state={
            modalToggle: props.show
        }
    }
    render(){
        if(this.state.modalToggle){
            return(
                <div> 
                    <div className='ModalContainer'>
                        <FormComponent />
                    </div>
                </div>
            )
        } else {
            </div>
        }
    }
}

Form Component:表单组件:

import Button from './Button'
class FormComponent extends React.Component {
    constructor(){
        this.state={
            submitButtonToggle: true,
            username: ''
        }
    }

    inputHandler=(e)=>{
        if(e){
            this.setState({
                username: e.target.value
            })
        }
    }

    render(){
        return(
            <div>
                <input type='text' value='this.state.username' id='username' onChange='inputHandler' />
                <Button title='Submit' disabled={this.state.username.length > 0}> </Button>
            </div>
        )
    }
}

Above are the basic superComponent which we have rendered in app/main entry file.以上是我们在 app/main 入口文件中渲染的基本超级组件 And form ||形成 || Modal Component.模态组件。 are the child component.是子组件。

So in modal component I have called the same Form-component.所以在模态组件中我调用了相同的表单组件。

Here in Form-component input type handler, submit button is disabled from state.. with input string length we are handling its validation.表单组件输入类型处理程序中,提交按钮从状态中被禁用..输入字符串长度我们正在处理其验证。

I hope it works for you.我希望这个对你有用。

Would the same still apply with mulitple pop up forms or say a SPA with a get started form pop and cta onclick reaction and a contact us form built into the main App component template. 同样适用于多种弹出表单,或者说SPA具有入门表单弹出和cta onclick反应以及内置在主App组件模板中的“与我们联系”表单。 What would you suggest for creating api's for react sites also is .NET Core good, Laravel, PHP, suggestions for a novice getting into react. 您还建议使用.NET Core good,Laravel,PHP来为React网站创建api,这是对新手入门的建议。 Most api's i work with are php and i know generators and mapping tools exist suggestions on tools to help automate the api process. 我使用的大多数api都是php,而且我知道生成器和映射工具对帮助自​​动化api过程的工具提出了建议。 Like a code-first approach in .NET MVC 就像.NET MVC中的代码优先方法一样

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

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