* fix: add type="button" to modal close buttons to fix Enter key form submission Previously, pressing Enter in modal form inputs would dismiss the modal instead of submitting the form because close buttons defaulted to type="submit". This adds explicit type="button" to all modal close buttons to ensure Enter key properly submits forms. Fixes: - New board creation via Enter key - New workspace creation via Enter key - New card creation via Enter key - New list creation via Enter key - Member invitation via Enter key - Label creation/editing via Enter key - Board URL updates via Enter key - Import boards forms via Enter key * fix: submit forms on enter key submission --------- Co-authored-by: Henry <henry_ball@hotmail.co.uk>
83 lines
2.4 KiB
TypeScript
83 lines
2.4 KiB
TypeScript
import { useEffect } from "react";
|
|
import { useForm } from "react-hook-form";
|
|
import { HiXMark } from "react-icons/hi2";
|
|
|
|
import type { NewBoardInput } from "@kan/api/types";
|
|
|
|
import Button from "~/components/Button";
|
|
import Input from "~/components/Input";
|
|
import { useModal } from "~/providers/modal";
|
|
import { useWorkspace } from "~/providers/workspace";
|
|
import { api } from "~/utils/api";
|
|
|
|
export function NewBoardForm() {
|
|
const utils = api.useUtils();
|
|
const { closeModal } = useModal();
|
|
const { workspace } = useWorkspace();
|
|
|
|
const { register, handleSubmit } = useForm<NewBoardInput>({
|
|
defaultValues: {
|
|
name: "",
|
|
workspacePublicId: workspace.publicId || "",
|
|
},
|
|
});
|
|
|
|
const refetchBoards = () => utils.board.all.refetch();
|
|
|
|
const createBoard = api.board.create.useMutation({
|
|
onSuccess: async () => {
|
|
closeModal();
|
|
await refetchBoards();
|
|
},
|
|
});
|
|
|
|
const onSubmit = (data: NewBoardInput) => {
|
|
createBoard.mutate(data);
|
|
};
|
|
|
|
useEffect(() => {
|
|
const titleElement: HTMLElement | null =
|
|
document.querySelector<HTMLElement>("#name");
|
|
if (titleElement) titleElement.focus();
|
|
}, []);
|
|
|
|
return (
|
|
<form onSubmit={handleSubmit(onSubmit)}>
|
|
<div className="px-5 pt-5">
|
|
<div className="text-neutral-9000 flex w-full items-center justify-between pb-4 dark:text-dark-1000">
|
|
<h2 className="text-sm font-bold">New board</h2>
|
|
<button
|
|
type="button"
|
|
className="hover:bg-li ght-300 rounded p-1 focus:outline-none dark:hover:bg-dark-300"
|
|
onClick={(e) => {
|
|
e.preventDefault();
|
|
closeModal();
|
|
}}
|
|
>
|
|
<HiXMark size={18} className="dark:text-dark-9000 text-light-900" />
|
|
</button>
|
|
</div>
|
|
<Input
|
|
id="name"
|
|
placeholder="Name"
|
|
{...register("name", { required: true })}
|
|
onKeyDown={async (e) => {
|
|
if (e.key === "Enter") {
|
|
e.preventDefault();
|
|
await handleSubmit(onSubmit)();
|
|
}
|
|
}}
|
|
/>
|
|
</div>
|
|
|
|
<div className="mt-12 flex items-center justify-end border-t border-light-600 px-5 pb-5 pt-5 dark:border-dark-600">
|
|
<div>
|
|
<Button type="submit" isLoading={createBoard.isPending}>
|
|
Create board
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
);
|
|
}
|