added key events to popup component

This commit is contained in:
2024-10-18 20:29:57 +00:00
parent 7302b27dc3
commit 6ec8894d44
2 changed files with 36 additions and 9 deletions

View File

@@ -1,9 +1,8 @@
"use client"; "use client";
import React from "react"; import React, { useState, useRef } from "react";
import AddDocumentIcon from "@/components/Icons/AddDocument"; import AddDocumentIcon from "@/components/Icons/AddDocument";
import Popup from "@/components/Popup"; import Popup from "@/components/Popup";
import { useState } from "react";
import Pterodactyl from "@/components/Pterodactyl"; import Pterodactyl from "@/components/Pterodactyl";
import { useSearchParams } from "next/navigation"; import { useSearchParams } from "next/navigation";
@@ -14,11 +13,18 @@ const Index = () => {
const serverId = urlParams.get("serverid") || ""; const serverId = urlParams.get("serverid") || "";
const pathParam = urlParams.get("path") || "/"; const pathParam = urlParams.get("path") || "/";
const apiKey = `${process.env.NEXT_PUBLIC_API_KEY}`; const apiKey = `${process.env.NEXT_PUBLIC_API_KEY}`;
const inputRef = useRef<HTMLInputElement>(null);
const pterodactyl = new Pterodactyl(); const pterodactyl = new Pterodactyl();
function togglePopup() { function togglePopup() {
setShowPopup(!showPopup); setShowPopup(!showPopup);
if (!showPopup) {
// set focus on input
setTimeout(() => {
inputRef.current?.focus();
}, 100);
}
} }
function handleOk() { function handleOk() {
@@ -42,6 +48,7 @@ const Index = () => {
> >
<div> <div>
<input <input
ref={inputRef}
type="text" type="text"
className="input input-sm input-bordered mb-4 text-base-content" className="input input-sm input-bordered mb-4 text-base-content"
placeholder="File name" placeholder="File name"

View File

@@ -1,6 +1,6 @@
"use client"; "use client";
import React from "react"; import React, { useEffect } from "react";
interface PopupProps extends React.HTMLAttributes<HTMLDivElement> { interface PopupProps extends React.HTMLAttributes<HTMLDivElement> {
show?: boolean; show?: boolean;
@@ -14,8 +14,31 @@ const Popup: React.FC<PopupProps> = ({
show = false, show = false,
title, title,
children, children,
onClickClose,
onClickOk,
...props ...props
}) => { }) => {
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === "Escape" && onClickClose) {
onClickClose(event as unknown as React.MouseEvent<HTMLButtonElement>);
}
if (event.key === "Enter") {
if (onClickOk) {
onClickOk(event as unknown as React.MouseEvent<HTMLButtonElement>);
}
}
};
if (show) {
window.addEventListener("keydown", handleKeyDown);
}
return () => {
window.removeEventListener("keydown", handleKeyDown);
};
}, [show, onClickClose]);
if (!show) return null; if (!show) return null;
return ( return (
@@ -30,13 +53,10 @@ const Popup: React.FC<PopupProps> = ({
)} )}
{children} {children}
<div className="flex justify-between"> <div className="flex justify-between">
<button onClick={props.onClickClose} className="btn btn-sm"> <button onClick={onClickClose} className="btn btn-sm">
Close Close
</button> </button>
<button <button onClick={onClickOk} className="btn btn-sm btn-success">
onClick={props.onClickOk}
className="btn btn-sm btn-success"
>
Ok Ok
</button> </button>
</div> </div>