2026-03-10 18:30:18 +09:00
|
|
|
|
"use client";
|
2025-10-14 10:34:18 +09:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 계산기 위젯 컴포넌트
|
|
|
|
|
|
* - 기본 사칙연산 지원
|
|
|
|
|
|
* - 실시간 계산
|
|
|
|
|
|
* - 대시보드 위젯으로 사용 가능
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2026-03-10 18:30:18 +09:00
|
|
|
|
import React, { useState, useEffect } from "react";
|
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
|
import { DashboardElement } from "@/components/admin/dashboard/types";
|
2025-10-14 10:34:18 +09:00
|
|
|
|
|
|
|
|
|
|
interface CalculatorWidgetProps {
|
2025-10-15 18:25:16 +09:00
|
|
|
|
element?: DashboardElement;
|
2025-10-14 10:34:18 +09:00
|
|
|
|
className?: string;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-10 18:30:18 +09:00
|
|
|
|
export default function CalculatorWidget({ element, className = "" }: CalculatorWidgetProps) {
|
|
|
|
|
|
const [display, setDisplay] = useState<string>("0");
|
2025-10-14 10:34:18 +09:00
|
|
|
|
const [previousValue, setPreviousValue] = useState<number | null>(null);
|
|
|
|
|
|
const [operation, setOperation] = useState<string | null>(null);
|
|
|
|
|
|
const [waitingForOperand, setWaitingForOperand] = useState<boolean>(false);
|
|
|
|
|
|
|
|
|
|
|
|
// 숫자 입력 처리
|
|
|
|
|
|
const handleNumber = (num: string) => {
|
|
|
|
|
|
if (waitingForOperand) {
|
|
|
|
|
|
setDisplay(num);
|
|
|
|
|
|
setWaitingForOperand(false);
|
|
|
|
|
|
} else {
|
2026-03-10 18:30:18 +09:00
|
|
|
|
setDisplay(display === "0" ? num : display + num);
|
2025-10-14 10:34:18 +09:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 소수점 입력
|
|
|
|
|
|
const handleDecimal = () => {
|
|
|
|
|
|
if (waitingForOperand) {
|
2026-03-10 18:30:18 +09:00
|
|
|
|
setDisplay("0.");
|
2025-10-14 10:34:18 +09:00
|
|
|
|
setWaitingForOperand(false);
|
2026-03-10 18:30:18 +09:00
|
|
|
|
} else if (display.indexOf(".") === -1) {
|
|
|
|
|
|
setDisplay(display + ".");
|
2025-10-14 10:34:18 +09:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 연산자 입력
|
|
|
|
|
|
const handleOperation = (nextOperation: string) => {
|
|
|
|
|
|
const inputValue = parseFloat(display);
|
|
|
|
|
|
|
|
|
|
|
|
if (previousValue === null) {
|
|
|
|
|
|
setPreviousValue(inputValue);
|
|
|
|
|
|
} else if (operation) {
|
|
|
|
|
|
const currentValue = previousValue || 0;
|
|
|
|
|
|
const newValue = calculate(currentValue, inputValue, operation);
|
2026-03-10 18:30:18 +09:00
|
|
|
|
|
2025-10-14 10:34:18 +09:00
|
|
|
|
setDisplay(String(newValue));
|
|
|
|
|
|
setPreviousValue(newValue);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
setWaitingForOperand(true);
|
|
|
|
|
|
setOperation(nextOperation);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 계산 수행
|
|
|
|
|
|
const calculate = (firstValue: number, secondValue: number, operation: string): number => {
|
|
|
|
|
|
switch (operation) {
|
2026-03-10 18:30:18 +09:00
|
|
|
|
case "+":
|
2025-10-14 10:34:18 +09:00
|
|
|
|
return firstValue + secondValue;
|
2026-03-10 18:30:18 +09:00
|
|
|
|
case "-":
|
2025-10-14 10:34:18 +09:00
|
|
|
|
return firstValue - secondValue;
|
2026-03-10 18:30:18 +09:00
|
|
|
|
case "×":
|
2025-10-14 10:34:18 +09:00
|
|
|
|
return firstValue * secondValue;
|
2026-03-10 18:30:18 +09:00
|
|
|
|
case "÷":
|
2025-10-14 10:34:18 +09:00
|
|
|
|
return secondValue !== 0 ? firstValue / secondValue : 0;
|
|
|
|
|
|
default:
|
|
|
|
|
|
return secondValue;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 등호 처리
|
|
|
|
|
|
const handleEquals = () => {
|
|
|
|
|
|
const inputValue = parseFloat(display);
|
|
|
|
|
|
|
|
|
|
|
|
if (previousValue !== null && operation) {
|
|
|
|
|
|
const newValue = calculate(previousValue, inputValue, operation);
|
|
|
|
|
|
setDisplay(String(newValue));
|
|
|
|
|
|
setPreviousValue(null);
|
|
|
|
|
|
setOperation(null);
|
|
|
|
|
|
setWaitingForOperand(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 초기화
|
|
|
|
|
|
const handleClear = () => {
|
2026-03-10 18:30:18 +09:00
|
|
|
|
setDisplay("0");
|
2025-10-14 10:34:18 +09:00
|
|
|
|
setPreviousValue(null);
|
|
|
|
|
|
setOperation(null);
|
|
|
|
|
|
setWaitingForOperand(false);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 백스페이스
|
|
|
|
|
|
const handleBackspace = () => {
|
|
|
|
|
|
if (!waitingForOperand) {
|
|
|
|
|
|
const newDisplay = display.slice(0, -1);
|
2026-03-10 18:30:18 +09:00
|
|
|
|
setDisplay(newDisplay || "0");
|
2025-10-14 10:34:18 +09:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 부호 변경
|
|
|
|
|
|
const handleSign = () => {
|
|
|
|
|
|
const value = parseFloat(display);
|
|
|
|
|
|
setDisplay(String(value * -1));
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 퍼센트
|
|
|
|
|
|
const handlePercent = () => {
|
|
|
|
|
|
const value = parseFloat(display);
|
|
|
|
|
|
setDisplay(String(value / 100));
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-10-17 12:04:40 +09:00
|
|
|
|
// 키보드 입력 처리
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
const handleKeyDown = (event: KeyboardEvent) => {
|
|
|
|
|
|
const key = event.key;
|
|
|
|
|
|
|
|
|
|
|
|
// 숫자 키 (0-9)
|
|
|
|
|
|
if (/^[0-9]$/.test(key)) {
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
handleNumber(key);
|
|
|
|
|
|
}
|
|
|
|
|
|
// 연산자 키
|
2026-03-10 18:30:18 +09:00
|
|
|
|
else if (key === "+" || key === "-" || key === "*" || key === "/") {
|
2025-10-17 12:04:40 +09:00
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
handleOperation(key);
|
|
|
|
|
|
}
|
|
|
|
|
|
// 소수점
|
2026-03-10 18:30:18 +09:00
|
|
|
|
else if (key === ".") {
|
2025-10-17 12:04:40 +09:00
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
handleDecimal();
|
|
|
|
|
|
}
|
|
|
|
|
|
// Enter 또는 = (계산)
|
2026-03-10 18:30:18 +09:00
|
|
|
|
else if (key === "Enter" || key === "=") {
|
2025-10-17 12:04:40 +09:00
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
handleEquals();
|
|
|
|
|
|
}
|
|
|
|
|
|
// Escape 또는 c (초기화)
|
2026-03-10 18:30:18 +09:00
|
|
|
|
else if (key === "Escape" || key.toLowerCase() === "c") {
|
2025-10-17 12:04:40 +09:00
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
handleClear();
|
|
|
|
|
|
}
|
|
|
|
|
|
// Backspace (지우기)
|
2026-03-10 18:30:18 +09:00
|
|
|
|
else if (key === "Backspace") {
|
2025-10-17 12:04:40 +09:00
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
handleBackspace();
|
|
|
|
|
|
}
|
|
|
|
|
|
// % (퍼센트)
|
2026-03-10 18:30:18 +09:00
|
|
|
|
else if (key === "%") {
|
2025-10-17 12:04:40 +09:00
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
handlePercent();
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 이벤트 리스너 등록
|
2026-03-10 18:30:18 +09:00
|
|
|
|
window.addEventListener("keydown", handleKeyDown);
|
2025-10-17 12:04:40 +09:00
|
|
|
|
|
|
|
|
|
|
// 클린업
|
|
|
|
|
|
return () => {
|
2026-03-10 18:30:18 +09:00
|
|
|
|
window.removeEventListener("keydown", handleKeyDown);
|
2025-10-17 12:04:40 +09:00
|
|
|
|
};
|
|
|
|
|
|
}, [display, previousValue, operation, waitingForOperand]);
|
|
|
|
|
|
|
2025-10-14 10:34:18 +09:00
|
|
|
|
return (
|
2026-03-10 18:30:18 +09:00
|
|
|
|
<div className={`bg-background h-full w-full p-2 sm:p-3 ${className}`}>
|
|
|
|
|
|
<div className="flex h-full flex-col gap-1.5 sm:gap-2">
|
2025-10-15 18:25:16 +09:00
|
|
|
|
{/* 제목 */}
|
2026-03-10 18:30:18 +09:00
|
|
|
|
<h3 className="text-foreground text-center text-sm font-semibold sm:text-base">
|
|
|
|
|
|
{element?.customTitle || "계산기"}
|
|
|
|
|
|
</h3>
|
|
|
|
|
|
|
2025-10-14 10:34:18 +09:00
|
|
|
|
{/* 디스플레이 */}
|
2026-03-10 18:30:18 +09:00
|
|
|
|
<div className="bg-background border-border min-h-[60px] rounded-lg border-2 p-2 shadow-inner sm:min-h-[80px] sm:p-4">
|
|
|
|
|
|
<div className="flex h-full flex-col justify-center text-right">
|
|
|
|
|
|
<div className="mb-0.5 h-3 sm:mb-1 sm:h-4">
|
2025-10-14 10:34:18 +09:00
|
|
|
|
{operation && previousValue !== null && (
|
2026-03-10 18:30:18 +09:00
|
|
|
|
<div className="text-muted-foreground text-[10px] sm:text-xs">
|
2025-10-14 10:34:18 +09:00
|
|
|
|
{previousValue} {operation}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
2026-03-10 18:30:18 +09:00
|
|
|
|
<div className="text-foreground truncate text-lg font-bold sm:text-2xl">{display}</div>
|
2025-10-14 10:34:18 +09:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
{/* 버튼 그리드 */}
|
2026-03-10 18:30:18 +09:00
|
|
|
|
<div className="grid flex-1 grid-cols-4 gap-1 sm:gap-2">
|
2025-10-14 10:34:18 +09:00
|
|
|
|
{/* 첫 번째 줄 */}
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
|
|
|
|
|
onClick={handleClear}
|
2026-03-10 18:30:18 +09:00
|
|
|
|
className="text-destructive hover:bg-destructive/10 hover:text-destructive h-full text-xs font-semibold select-none sm:text-base"
|
2025-10-14 10:34:18 +09:00
|
|
|
|
>
|
|
|
|
|
|
AC
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
|
|
|
|
|
onClick={handleSign}
|
2026-03-10 18:30:18 +09:00
|
|
|
|
className="text-foreground hover:bg-muted h-full text-xs font-semibold select-none sm:text-base"
|
2025-10-14 10:34:18 +09:00
|
|
|
|
>
|
|
|
|
|
|
+/-
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
|
|
|
|
|
onClick={handlePercent}
|
2026-03-10 18:30:18 +09:00
|
|
|
|
className="text-foreground hover:bg-muted h-full text-xs font-semibold select-none sm:text-base"
|
2025-10-14 10:34:18 +09:00
|
|
|
|
>
|
|
|
|
|
|
%
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="default"
|
2026-03-10 18:30:18 +09:00
|
|
|
|
onClick={() => handleOperation("÷")}
|
|
|
|
|
|
className="bg-primary hover:bg-primary/90 text-primary-foreground h-full text-xs font-semibold select-none sm:text-base"
|
2025-10-14 10:34:18 +09:00
|
|
|
|
>
|
|
|
|
|
|
÷
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
|
|
|
|
|
|
{/* 두 번째 줄 */}
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
2026-03-10 18:30:18 +09:00
|
|
|
|
onClick={() => handleNumber("7")}
|
|
|
|
|
|
className="hover:bg-muted h-full text-sm font-semibold select-none sm:text-lg"
|
2025-10-14 10:34:18 +09:00
|
|
|
|
>
|
|
|
|
|
|
7
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
2026-03-10 18:30:18 +09:00
|
|
|
|
onClick={() => handleNumber("8")}
|
|
|
|
|
|
className="hover:bg-muted h-full text-sm font-semibold select-none sm:text-lg"
|
2025-10-14 10:34:18 +09:00
|
|
|
|
>
|
|
|
|
|
|
8
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
2026-03-10 18:30:18 +09:00
|
|
|
|
onClick={() => handleNumber("9")}
|
|
|
|
|
|
className="hover:bg-muted h-full text-sm font-semibold select-none sm:text-lg"
|
2025-10-14 10:34:18 +09:00
|
|
|
|
>
|
|
|
|
|
|
9
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="default"
|
2026-03-10 18:30:18 +09:00
|
|
|
|
onClick={() => handleOperation("×")}
|
|
|
|
|
|
className="bg-primary hover:bg-primary/90 text-primary-foreground h-full text-xs font-semibold select-none sm:text-base"
|
2025-10-14 10:34:18 +09:00
|
|
|
|
>
|
|
|
|
|
|
×
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
|
|
|
|
|
|
{/* 세 번째 줄 */}
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
2026-03-10 18:30:18 +09:00
|
|
|
|
onClick={() => handleNumber("4")}
|
|
|
|
|
|
className="hover:bg-muted h-full text-sm font-semibold select-none sm:text-lg"
|
2025-10-14 10:34:18 +09:00
|
|
|
|
>
|
|
|
|
|
|
4
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
2026-03-10 18:30:18 +09:00
|
|
|
|
onClick={() => handleNumber("5")}
|
|
|
|
|
|
className="hover:bg-muted h-full text-sm font-semibold select-none sm:text-lg"
|
2025-10-14 10:34:18 +09:00
|
|
|
|
>
|
|
|
|
|
|
5
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
2026-03-10 18:30:18 +09:00
|
|
|
|
onClick={() => handleNumber("6")}
|
|
|
|
|
|
className="hover:bg-muted h-full text-sm font-semibold select-none sm:text-lg"
|
2025-10-14 10:34:18 +09:00
|
|
|
|
>
|
|
|
|
|
|
6
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="default"
|
2026-03-10 18:30:18 +09:00
|
|
|
|
onClick={() => handleOperation("-")}
|
|
|
|
|
|
className="bg-primary hover:bg-primary/90 text-primary-foreground h-full text-xs font-semibold select-none sm:text-base"
|
2025-10-14 10:34:18 +09:00
|
|
|
|
>
|
|
|
|
|
|
-
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
|
|
|
|
|
|
{/* 네 번째 줄 */}
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
2026-03-10 18:30:18 +09:00
|
|
|
|
onClick={() => handleNumber("1")}
|
|
|
|
|
|
className="hover:bg-muted h-full text-sm font-semibold select-none sm:text-lg"
|
2025-10-14 10:34:18 +09:00
|
|
|
|
>
|
|
|
|
|
|
1
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
2026-03-10 18:30:18 +09:00
|
|
|
|
onClick={() => handleNumber("2")}
|
|
|
|
|
|
className="hover:bg-muted h-full text-sm font-semibold select-none sm:text-lg"
|
2025-10-14 10:34:18 +09:00
|
|
|
|
>
|
|
|
|
|
|
2
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
2026-03-10 18:30:18 +09:00
|
|
|
|
onClick={() => handleNumber("3")}
|
|
|
|
|
|
className="hover:bg-muted h-full text-sm font-semibold select-none sm:text-lg"
|
2025-10-14 10:34:18 +09:00
|
|
|
|
>
|
|
|
|
|
|
3
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="default"
|
2026-03-10 18:30:18 +09:00
|
|
|
|
onClick={() => handleOperation("+")}
|
|
|
|
|
|
className="bg-primary hover:bg-primary/90 text-primary-foreground h-full text-xs font-semibold select-none sm:text-base"
|
2025-10-14 10:34:18 +09:00
|
|
|
|
>
|
|
|
|
|
|
+
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
|
|
|
|
|
|
{/* 다섯 번째 줄 */}
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
2026-03-10 18:30:18 +09:00
|
|
|
|
onClick={() => handleNumber("0")}
|
|
|
|
|
|
className="hover:bg-muted col-span-2 h-full text-sm font-semibold select-none sm:text-lg"
|
2025-10-14 10:34:18 +09:00
|
|
|
|
>
|
|
|
|
|
|
0
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
|
|
|
|
|
onClick={handleDecimal}
|
2026-03-10 18:30:18 +09:00
|
|
|
|
className="hover:bg-muted h-full text-sm font-semibold select-none sm:text-lg"
|
2025-10-14 10:34:18 +09:00
|
|
|
|
>
|
|
|
|
|
|
.
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="default"
|
|
|
|
|
|
onClick={handleEquals}
|
2026-03-10 18:30:18 +09:00
|
|
|
|
className="bg-success hover:bg-success/90 text-primary-foreground h-full text-xs font-semibold select-none sm:text-base"
|
2025-10-14 10:34:18 +09:00
|
|
|
|
>
|
|
|
|
|
|
=
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|