![](/img/trans.png)
[英]Create a reusable Form Input component with React & TypeScript
[英]TypeScript Reusable Form with React Bootstrap
我正在關注這篇文章來學習反應和 typescript forms。 React Bootstrap 解釋了一個可重用的表單,我喜歡實現它。 我試圖通過編寫如下的用戶表單來做到這一點。 但我明白了
TS7031:綁定元素“取消”隱式具有“任何”類型
一旦我解決了這個錯誤,我不知道如何使用它。 例如,如果我想實現一個登錄表單和一個登錄表單,我該如何參考下面的用戶表單
import React from 'react';
import Form from 'react-bootstrap/Form';
import Button from 'react-bootstrap/Button';
import styled from 'styled-components';
const UserForm =
({
cancel,
errors,
submit,
submitButtonText,
elements,
passwordErrors
}) => {
function handleSubmit(event) {
event.preventDefault();
submit();
}
function handleCancel(event) {
event.preventDefault();
cancel();
}
return (
<React.Fragment>
<ErrorsDisplay errors={errors} passwordErrors={passwordErrors} />
<Form onSubmit={handleSubmit}>
{elements()}
<Button className="mr-1" variant="primary" type="submit">{submitButtonText}</Button>
<Button className="mr-1" variant="secondary" onClick={handleCancel}>Cancel</Button>
</Form>
</React.Fragment>
);
};
function ErrorsDisplay({ errors, passwordErrors}) {
let errorDisplay = null;
if (errors.length) {
errorDisplay = (
<React.Fragment>
<ValidiationLabel>Errors:</ValidiationLabel>
<ValidiationUl>
{errors.map((error, i) => (
<li key={i}>{error}</li>
))}
</ValidiationUl>
</React.Fragment>
);
} else if (!passwordErrors) {
errorDisplay = (
<React.Fragment>
<ValidiationLabel>Errors:</ValidiationLabel>
<ValidiationUl>{<li>Passwords must match</li>}</ValidiationUl>
</React.Fragment>
);
}
return errorDisplay;
}
const ValidiationUl = styled.div`
color: red;
padding: 15px 0 40px 10px;
`;
const ValidiationLabel = styled.h2`
color: #0069c0;
font-size: 28px;
`;
export default UserForm;
首先,假設您使用的是 typescript 項目,如您所說(.tsx 文件擴展名),您需要輸入 UserForm 的參數:
// This is just an example, not necessary needs to be this exactly types for the parameters
interface UserFormProps {
cancel(): void; // cancel is a function that returns nothing
submit(): void;
errors: string[]; // errors its an array of strings
passwordErrors: boolean;
submitButtonText: string;
elements(): ReactNode; // elements its a funtion that returns react nodes
}
// Here you are destructuring the object parameter of UserForm function.
// You telling it that its an UserFormProps Type Object on ": UserFormProps"
const UserForm = ({
cancel,
submit,
elements,
errors,
submitButtonText,
passwordErrors,
}: UserFormProps) => { .....
對於錯誤顯示 function:
interface ErrorsProps {
errors: string[];
passwordErrors: boolean;
}
function ErrorsDisplay({ errors, passwordErrors }: ErrorsProps) {...
對於您的句柄函數,您需要指定事件的類型:
// You are saying the handleSubmit function receives an FormEvent from a HTML Form Element
function handleSubmit(event: React.FormEvent<HTMLFormElement>) { ....
// You are saying the handleCancel function receives an MouseEvent from a HTML Button Element
function handleCancel(event: React.MouseEvent<HTMLButtonElement>) { ....
完成此操作后,您可以在任何地方使用您的用戶窗體,例如您的登錄頁面/登錄頁面。
你只需要導入它:
import React from "react";
import Form from "react-bootstrap/Form";
// here you need to inform the path according to your project
import UserForm from "./UserForms";
const SignIn = () => {
return (
<UserForm
// I'm setting the values hardcoded just as example
cancel={() => {console.log('cancel')}}
submit={() => {console.log('submit')}}
errors={[]}
passwordErrors={false}
submitButtonText="test"
elements={() => (
<>
<Form.Group controlId="ControlId">
<Form.Control
type="email"
name="email"
value={"email@c.com.br"}
placeholder={"email"}
></Form.Control>
<Form.Control
type="password"
name="password"
value={"password"}
placeholder={"password"}
></Form.Control>
</Form.Group>
</>
)}
></UserForm>
);
};
export default SignIn;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.