Skip to content

Commit

Permalink
auto-focus inputs when opening add cards modals
Browse files Browse the repository at this point in the history
  • Loading branch information
ndepaola committed Mar 13, 2024
1 parent 29277cc commit f12ca77
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 8 additions & 1 deletion frontend/src/features/import/importText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* A freeform text area is exposed and the cards are processed when the user hits Submit.
*/

import React, { FormEvent, useState } from "react";
import React, { FormEvent, useRef, useState } from "react";
import { Accordion } from "react-bootstrap";
import Button from "react-bootstrap/Button";
import Dropdown from "react-bootstrap/Dropdown";
Expand Down Expand Up @@ -47,6 +47,7 @@ export function ImportText() {

const [showTextModal, setShowTextModal] = useState<boolean>(false);
const [textModalValue, setTextModalValue] = useState<string>("");
const focusRef = useRef<HTMLTextAreaElement>(null);

//# endregion

Expand Down Expand Up @@ -96,6 +97,11 @@ export function ImportText() {
<Modal
scrollable
show={showTextModal}
onEntered={() => {
if (focusRef.current) {
focusRef.current.focus();
}
}}
onHide={handleCloseTextModal}
onExited={() => setTextModalValue("")}
data-testid="import-text"
Expand Down Expand Up @@ -175,6 +181,7 @@ export function ImportText() {
<Form id="importTextForm" onSubmit={handleSubmitTextModal}>
<Form.Group className="mb-3">
<Form.Control
ref={focusRef}
as="textarea"
rows={12}
placeholder={placeholderText}
Expand Down
9 changes: 8 additions & 1 deletion frontend/src/features/import/importURL.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* to process the URL when the user hits Submit.
*/

import React, { FormEvent, useCallback, useState } from "react";
import React, { FormEvent, useCallback, useRef, useState } from "react";
import Button from "react-bootstrap/Button";
import Dropdown from "react-bootstrap/Dropdown";
import Form from "react-bootstrap/Form";
Expand Down Expand Up @@ -44,6 +44,7 @@ export function ImportURL() {
const [URLModalValue, setURLModalValue] = useState<string>("");
const [showURLModal, setShowURLModal] = useState<boolean>(false);
const [loading, setLoading] = useState<boolean>(false);
const focusRef = useRef<HTMLInputElement>(null);

//# endregion

Expand Down Expand Up @@ -130,6 +131,11 @@ export function ImportURL() {
<Modal
scrollable
show={loading || showURLModal}
onEntered={() => {
if (focusRef.current) {
focusRef.current.focus();
}
}}
onHide={handleCloseURLModal}
onExited={() => setURLModalValue("")}
>
Expand Down Expand Up @@ -164,6 +170,7 @@ export function ImportURL() {
<Form id="importURLForm" onSubmit={handleSubmitURLModal}>
<Form.Group className="mb-3">
<Form.Control
ref={focusRef}
type="url"
required={true}
placeholder="https://"
Expand Down

0 comments on commit f12ca77

Please sign in to comment.