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

View File

@@ -1,6 +1,6 @@
"use client";
import React from "react";
import React, { useEffect } from "react";
interface PopupProps extends React.HTMLAttributes<HTMLDivElement> {
show?: boolean;
@@ -14,8 +14,31 @@ const Popup: React.FC<PopupProps> = ({
show = false,
title,
children,
onClickClose,
onClickOk,
...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;
return (
@@ -30,13 +53,10 @@ const Popup: React.FC<PopupProps> = ({
)}
{children}
<div className="flex justify-between">
<button onClick={props.onClickClose} className="btn btn-sm">
<button onClick={onClickClose} className="btn btn-sm">
Close
</button>
<button
onClick={props.onClickOk}
className="btn btn-sm btn-success"
>
<button onClick={onClickOk} className="btn btn-sm btn-success">
Ok
</button>
</div>