处理输入表单,其中输入字段是一个组件

jk9hmnmh  于 2021-09-13  发布在  Java
关注(0)|答案(2)|浏览(474)

这里有一个动态表单组件。
main.js

import RegistrationItemfrom "./RegistrationItem"
.......
         <Form onSubmit={(e) => {e.preventDefault();}}>
              <RegistrationItem
                inputtype="email"
                inputplaceholder="Email Address*"
                icon={faEnvelope}
              />
              <RegistrationItem
                inputtype="password"
                inputplaceholder="Password*"
                icon={faLock}
              />
             <button onClick={handleLogin()} > Login </Login>
        </Form>

用于添加样式和图标的输入字段的文件。
registrationitem.js。

const RegistrationItem = () ={
return(
    <InputGroup className=" mb-4">
      <Input
        type={inputtype}
        name={inputname}
        id={inputid}
        placeholder={inputplaceholder}
        className="border-right-0"
      />
      <InputGroupAddon addonType="append">
        <InputGroupText className="bg-transparent text-primary border-left-0">
          <FontAwesomeIcon className="text-primary" icon={icon} />
        </InputGroupText>
      </InputGroupAddon>
</InputGroup>
)
}

如何通过登录按钮实现表单的提交 <button onClick={handleLogin()} > Login </Login>

knpiaxh1

knpiaxh11#

根据给定的代码,我们需要两个值。一个是电子邮件,另一个是密码。
因此,需要在表单元素email和password中添加两种状态。这些值需要根据输入中的更改进行更改。
因此,在提交表单时,可以使用这些值执行所需的任何操作
如果我们只使用react,而不使用其他表单库(如react-hook-form),那么代码可能是这样的
main.js
从“/registrationitem”导入注册项。。。。。。。

const [email,setEmail] = useState('');
    const [password,setPasword] = useState('')

     <Form onSubmit={(e) => {e.preventDefault();handleSubmit(email,password,...otherArgs);}}>
          <RegistrationItem
            inputtype="email"
            inputplaceholder="Email Address*"
            icon={faEnvelope}
            value={email}
            setValue={setEmail}
          />
          <RegistrationItem
            inputtype="password"
            inputplaceholder="Password*"
            icon={faLock}
            value={password}
            setValue={setPassword}
          />
         <button type="submit" > Login </button>
    </Form>

registrationitem.js

const RegistrationItem = ({inputtype,inputname,inputid,inputplaceholder,value,setValue,icon}) ={
return(
    <InputGroup className=" mb-4">
      <Input
        type={inputtype}
        name={inputname}
        id={inputid}
        placeholder={inputplaceholder}
        value={value}
        onChange={e=>setValue(e.target.value)}
        className="border-right-0"
      />
      <InputGroupAddon addonType="append">
        <InputGroupText className="bg-transparent text-primary border-left-0">
          <FontAwesomeIcon className="text-primary" icon={icon} />
        </InputGroupText>
      </InputGroupAddon>
</InputGroup>
)
}
3pvhb19x

3pvhb19x2#

you need to declare state and pass that to your child component and can update the state from child element and when you click on login you can use the state.

below i have shown you how you can declare the state and how you can pass it to your child element and in child element you can use it.
note : i have just passed it in your child component and over ther in argument you can see it so wherever you want to use them you can use it.

  const [registrationTypeValue,setRegistrationTypeValue]=useState("")
             <Form onSubmit={(e) => {e.preventDefault();}}>
                  <RegistrationItem
                    inputtype="email"
                    inputplaceholder="Email Address*"
                    icon={faEnvelope}
                    registrationTypeValue={registrationTypeValue}
                    setRegistrationTypeValue={setRegistrationTypeValue}/>
                  <RegistrationItem
                    inputtype="password"
                    inputplaceholder="Password*"
                    icon={faLock}
                  />
                 <button onClick={handleLogin()} > Login </Login></Form>

    const RegistrationItem = ({registrationTypeValue,setRegistrationTypeValue}) ={
    return(
        <InputGroup className=" mb-4">
          <Input
            type={inputtype}
            name={inputname}
            id={inputid}
            placeholder={inputplaceholder}
            className="border-right-0"
          />
          <InputGroupAddon addonType="append">
            <InputGroupText className="bg-transparent text-primary border-left-0">
              <FontAwesomeIcon className="text-primary" icon={icon} />
            </InputGroupText>
          </InputGroupAddon>
    </InputGroup>
    )
    }

相关问题