revite/src/pages/login/FormField.tsx

87 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-08-05 09:47:00 -04:00
import { UseFormMethods } from "react-hook-form";
2021-07-05 06:23:23 -04:00
import { Text, Localizer } from "preact-i18n";
import InputBox from "../../components/ui/InputBox";
import Overline from "../../components/ui/Overline";
2021-06-18 15:21:54 -04:00
interface Props {
2021-07-05 06:25:20 -04:00
type: "email" | "username" | "password" | "invite" | "current_password";
showOverline?: boolean;
2021-08-05 09:47:00 -04:00
register: UseFormMethods["register"];
2021-07-05 06:25:20 -04:00
error?: string;
name?: string;
2021-06-18 15:21:54 -04:00
}
export default function FormField({
2021-07-05 06:25:20 -04:00
type,
register,
showOverline,
error,
name,
2021-06-18 15:21:54 -04:00
}: Props) {
2021-07-05 06:25:20 -04:00
return (
<>
{showOverline && (
<Overline error={error}>
<Text id={`login.${type}`} />
</Overline>
)}
<Localizer>
<InputBox
2021-08-05 09:47:00 -04:00
placeholder={
(
<Text id={`login.enter.${type}`} />
) as unknown as string
}
2021-07-05 06:25:20 -04:00
name={
type === "current_password" ? "password" : name ?? type
}
type={
type === "invite" || type === "username"
? "text"
: type === "current_password"
? "password"
: type
}
// See https://github.com/mozilla/contain-facebook/issues/783
className="fbc-has-badge"
2021-07-05 06:25:20 -04:00
ref={register(
type === "password" || type === "current_password"
? {
validate: (value: string) =>
value.length === 0
? "RequiredField"
: value.length < 8
? "TooShort"
: value.length > 1024
? "TooLong"
: undefined,
}
: type === "email"
? {
required: "RequiredField",
pattern: {
value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i,
message: "InvalidEmail",
},
}
: type === "username"
2021-08-31 00:33:47 -04:00
? {
validate: (value: string) =>
value.length === 0
? "RequiredField"
: value.length < 2
? "TooShort"
: value.length > 32
? "TooLong"
: undefined,
}
2021-07-05 06:25:20 -04:00
: { required: "RequiredField" },
)}
/>
</Localizer>
</>
);
2021-06-18 15:21:54 -04:00
}