diff --git a/src/components/MyAccount/Settings/AddButton.tsx b/src/components/MyAccount/Settings/AddButton.tsx index 48bf98be7..644fed06a 100644 --- a/src/components/MyAccount/Settings/AddButton.tsx +++ b/src/components/MyAccount/Settings/AddButton.tsx @@ -1,4 +1,5 @@ import { Button } from "@nypl/design-system-react-components" +import { forwardRef } from "react" type AddButtonProps = { inputType?: string @@ -6,25 +7,30 @@ type AddButtonProps = { onClick: () => void } -const AddButton = ({ inputType, label, onClick }: AddButtonProps) => { - return ( - - ) -} +const AddButton = forwardRef( + ({ inputType, label, onClick }, ref) => { + return ( + + ) + } +) + +AddButton.displayName = "AddButton" export default AddButton diff --git a/src/components/MyAccount/Settings/EditButton.tsx b/src/components/MyAccount/Settings/EditButton.tsx index 88ad45160..4bdd2b50b 100644 --- a/src/components/MyAccount/Settings/EditButton.tsx +++ b/src/components/MyAccount/Settings/EditButton.tsx @@ -1,4 +1,5 @@ import { Button, Icon } from "@nypl/design-system-react-components" +import { forwardRef } from "react" type EditButtonProps = { buttonId: string @@ -6,24 +7,32 @@ type EditButtonProps = { onClick: () => void } -const EditButton = ({ buttonId, buttonLabel, onClick }: EditButtonProps) => { - return ( - - ) -} +const EditButton = forwardRef( + ({ buttonLabel, buttonId, onClick }, ref) => { + return ( + + ) + } +) + +EditButton.displayName = "EditButton" export default EditButton diff --git a/src/components/MyAccount/Settings/PasswordForm.tsx b/src/components/MyAccount/Settings/PasswordForm.tsx index 8e6ca79c1..656e6068e 100644 --- a/src/components/MyAccount/Settings/PasswordForm.tsx +++ b/src/components/MyAccount/Settings/PasswordForm.tsx @@ -1,5 +1,6 @@ -import { useContext, useState } from "react" +import { forwardRef, useContext, useRef, useState } from "react" import { PatronDataContext } from "../../../context/PatronDataContext" +import type { TextInputRefType } from "@nypl/design-system-react-components" import { Banner, Flex, @@ -30,37 +31,37 @@ export const passwordFormMessages = { INVALID: "Invalid new pin/password.", } -const PasswordFormField = ({ - label, - handler, - name, - isInvalid, -}: PasswordFormFieldProps) => { - return ( - - - - - ) -} +const PasswordFormField = forwardRef( + ({ label, handler, name, isInvalid }: PasswordFormFieldProps, ref) => { + return ( + + + + + ) + } +) + +PasswordFormField.displayName = "PasswordFormField" const PasswordForm = ({ patronData, settingsState }: PasswordFormProps) => { const { getMostUpdatedSierraAccountData } = useContext(PatronDataContext) @@ -74,10 +75,15 @@ const PasswordForm = ({ patronData, settingsState }: PasswordFormProps) => { }) const { setStatus, setStatusMessage, editingField, setEditingField } = settingsState + const editingRef = useRef() + const inputRef = useRef() const cancelEditing = () => { setIsEditing(false) setEditingField("") + setTimeout(() => { + editingRef.current?.focus() + }, 0) } const validateForm = @@ -162,6 +168,7 @@ const PasswordForm = ({ patronData, settingsState }: PasswordFormProps) => { }} > { {editingField === "" && ( { setIsEditing(true) setEditingField("password") + setTimeout(() => { + inputRef.current?.focus() + }, 0) }} /> )} diff --git a/src/components/MyAccount/Settings/SettingsInputForm.tsx b/src/components/MyAccount/Settings/SettingsInputForm.tsx index cdf3a5f6e..6f236f536 100644 --- a/src/components/MyAccount/Settings/SettingsInputForm.tsx +++ b/src/components/MyAccount/Settings/SettingsInputForm.tsx @@ -44,6 +44,7 @@ const SettingsInputForm = ({ const [tempInputs, setTempInputs] = useState([...inputs]) const inputRefs = useRef>([]) + const editingRef = useRef() const focusLastInput = () => { const lastIndex = tempInputs.length - 1 @@ -59,6 +60,9 @@ const SettingsInputForm = ({ const formUtils = { regex: isEmail ? /^[^@]+@[^@]+\.[^@]+$/ : /^\+?[1-9]\d{1,14}$/, labelText: isEmail ? "Update email address" : "Update phone number", + primaryLabelText: isEmail + ? "Update primary email address" + : "Update primary phone number", addButtonLabel: isEmail ? "+ Add an email address" : "+ Add a phone number", errorMessage: `Please enter a valid and unique ${ isEmail ? "email address" : "phone number" @@ -126,6 +130,9 @@ const SettingsInputForm = ({ setIsEditing(false) setEditingField("") setError(false) + setTimeout(() => { + editingRef.current?.focus() + }, 0) } const submitInputs = async () => { @@ -207,7 +214,7 @@ const SettingsInputForm = ({ id={`${inputType}-text-input-${index}`} labelText={ index == 0 - ? `Update primary ${inputType}` + ? formUtils.primaryLabelText : `${formUtils.labelText} ${index}` } showLabel={false} @@ -277,6 +284,7 @@ const SettingsInputForm = ({ {editingField === "" && tempInputs.length > 0 && ( { diff --git a/src/components/MyAccount/Settings/SettingsSelectForm.tsx b/src/components/MyAccount/Settings/SettingsSelectForm.tsx index a78587d18..94bf5d648 100644 --- a/src/components/MyAccount/Settings/SettingsSelectForm.tsx +++ b/src/components/MyAccount/Settings/SettingsSelectForm.tsx @@ -1,4 +1,4 @@ -import { useContext, useState } from "react" +import { useContext, useRef, useState } from "react" import { PatronDataContext } from "../../../context/PatronDataContext" import { Flex, @@ -30,6 +30,8 @@ const SettingsSelectForm = ({ const [error, setError] = useState(false) const { setStatus, editingField, setEditingField } = settingsState + const editingRef = useRef() + const inputRef = useRef() const notificationPreferenceMap = patronData.notificationPreference === "-" @@ -89,6 +91,9 @@ const SettingsSelectForm = ({ const cancelEditing = () => { setIsEditing(false) setEditingField("") + setTimeout(() => { + editingRef.current?.focus() + }, 0) } const submitSelection = async () => { @@ -159,6 +164,7 @@ const SettingsSelectForm = ({ width="-webkit-fill-available" >