SingleInputForm.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import { FlexWrapper } from "@ente/shared/components/Container";
  2. import ShowHidePassword from "@ente/shared/components/Form/ShowHidePassword";
  3. import { Button, FormHelperText } from "@mui/material";
  4. import TextField from "@mui/material/TextField";
  5. import { Formik, type FormikHelpers, type FormikState } from "formik";
  6. import { t } from "i18next";
  7. import React, { useMemo, useState } from "react";
  8. import * as Yup from "yup";
  9. import SubmitButton from "./SubmitButton";
  10. interface formValues {
  11. inputValue: string;
  12. }
  13. export interface SingleInputFormProps {
  14. callback: (
  15. inputValue: string,
  16. setFieldError: (errorMessage: string) => void,
  17. resetForm: (nextState?: Partial<FormikState<formValues>>) => void,
  18. ) => Promise<void>;
  19. fieldType: "text" | "email" | "password";
  20. /** deprecated: Use realPlaceholder */
  21. placeholder?: string;
  22. /**
  23. * Placeholder
  24. *
  25. * The existing `placeholder` property uses the placeholder as a label (i.e.
  26. * it doesn't appear as the placeholder within the text input area but
  27. * rather as the label on top of it). This happens conditionally, so it is
  28. * not a matter of simple rename.
  29. *
  30. * Gradually migrate the existing UI to use this property when we really
  31. * want a placeholder, and then create a separate label property for places
  32. * that actually want to set the label.
  33. */
  34. realPlaceholder?: string;
  35. /**
  36. * Label to show on top of the text input area.
  37. *
  38. * Sibling of {@link realPlaceholder}.
  39. */
  40. realLabel?: string;
  41. buttonText: string;
  42. submitButtonProps?: any;
  43. initialValue?: string;
  44. secondaryButtonAction?: () => void;
  45. disableAutoFocus?: boolean;
  46. hiddenPreInput?: any;
  47. caption?: any;
  48. hiddenPostInput?: any;
  49. autoComplete?: string;
  50. blockButton?: boolean;
  51. hiddenLabel?: boolean;
  52. disableAutoComplete?: boolean;
  53. }
  54. export default function SingleInputForm(props: SingleInputFormProps) {
  55. const { submitButtonProps } = props;
  56. const { sx: buttonSx, ...restSubmitButtonProps } = submitButtonProps ?? {};
  57. const [loading, SetLoading] = useState(false);
  58. const [showPassword, setShowPassword] = useState(false);
  59. const submitForm = async (
  60. values: formValues,
  61. { setFieldError, resetForm }: FormikHelpers<formValues>,
  62. ) => {
  63. SetLoading(true);
  64. await props.callback(
  65. values.inputValue,
  66. (message) => setFieldError("inputValue", message),
  67. resetForm,
  68. );
  69. SetLoading(false);
  70. };
  71. const handleClickShowPassword = () => {
  72. setShowPassword(!showPassword);
  73. };
  74. const handleMouseDownPassword = (
  75. event: React.MouseEvent<HTMLButtonElement>,
  76. ) => {
  77. event.preventDefault();
  78. };
  79. const validationSchema = useMemo(() => {
  80. switch (props.fieldType) {
  81. case "text":
  82. return Yup.object().shape({
  83. inputValue: Yup.string().required(t("REQUIRED")),
  84. });
  85. case "password":
  86. return Yup.object().shape({
  87. inputValue: Yup.string().required(t("REQUIRED")),
  88. });
  89. case "email":
  90. return Yup.object().shape({
  91. inputValue: Yup.string()
  92. .email(t("EMAIL_ERROR"))
  93. .required(t("REQUIRED")),
  94. });
  95. }
  96. }, [props.fieldType]);
  97. return (
  98. <Formik<formValues>
  99. initialValues={{ inputValue: props.initialValue ?? "" }}
  100. onSubmit={submitForm}
  101. validationSchema={validationSchema}
  102. validateOnChange={false}
  103. validateOnBlur={false}
  104. >
  105. {({ values, errors, handleChange, handleSubmit }) => (
  106. <form noValidate onSubmit={handleSubmit}>
  107. {props.hiddenPreInput}
  108. <TextField
  109. hiddenLabel={props.hiddenLabel}
  110. variant="filled"
  111. fullWidth
  112. type={showPassword ? "text" : props.fieldType}
  113. id={props.fieldType}
  114. name={props.fieldType}
  115. {...(props.hiddenLabel
  116. ? { placeholder: props.placeholder }
  117. : props.realPlaceholder
  118. ? {
  119. placeholder: props.realPlaceholder,
  120. label: props.realLabel,
  121. }
  122. : { label: props.placeholder })}
  123. value={values.inputValue}
  124. onChange={handleChange("inputValue")}
  125. error={Boolean(errors.inputValue)}
  126. helperText={errors.inputValue}
  127. disabled={loading}
  128. autoFocus={!props.disableAutoFocus}
  129. autoComplete={props.autoComplete}
  130. InputProps={{
  131. autoComplete:
  132. props.disableAutoComplete ||
  133. props.fieldType === "password"
  134. ? "off"
  135. : "on",
  136. endAdornment: props.fieldType === "password" && (
  137. <ShowHidePassword
  138. showPassword={showPassword}
  139. handleClickShowPassword={
  140. handleClickShowPassword
  141. }
  142. handleMouseDownPassword={
  143. handleMouseDownPassword
  144. }
  145. />
  146. ),
  147. }}
  148. />
  149. <FormHelperText
  150. sx={{
  151. position: "relative",
  152. top: errors.inputValue ? "-22px" : "0",
  153. float: "right",
  154. padding: "0 8px",
  155. }}
  156. >
  157. {props.caption}
  158. </FormHelperText>
  159. {props.hiddenPostInput}
  160. <FlexWrapper
  161. justifyContent={"flex-end"}
  162. flexWrap={props.blockButton ? "wrap-reverse" : "nowrap"}
  163. >
  164. {props.secondaryButtonAction && (
  165. <Button
  166. onClick={props.secondaryButtonAction}
  167. size="large"
  168. color="secondary"
  169. sx={{
  170. "&&&": {
  171. mt: !props.blockButton ? 2 : 0.5,
  172. mb: !props.blockButton ? 4 : 0,
  173. mr: !props.blockButton ? 1 : 0,
  174. ...buttonSx,
  175. },
  176. }}
  177. {...restSubmitButtonProps}
  178. >
  179. {t("CANCEL")}
  180. </Button>
  181. )}
  182. <SubmitButton
  183. sx={{
  184. "&&&": {
  185. mt: 2,
  186. ...buttonSx,
  187. },
  188. }}
  189. buttonText={props.buttonText}
  190. loading={loading}
  191. {...restSubmitButtonProps}
  192. />
  193. </FlexWrapper>
  194. </form>
  195. )}
  196. </Formik>
  197. );
  198. }