92 lines
2.6 KiB
TypeScript
92 lines
2.6 KiB
TypeScript
import React, { useState, useEffect } from 'react';
|
|
|
|
interface FormModalProps {
|
|
isOpen: boolean;
|
|
onClose: () => void;
|
|
onSave: (name: string) => void;
|
|
title: string;
|
|
initialValue?: string;
|
|
}
|
|
|
|
const FormModal: React.FC<FormModalProps> = ({
|
|
isOpen,
|
|
onClose,
|
|
onSave,
|
|
title,
|
|
initialValue = '',
|
|
}) => {
|
|
const [name, setName] = useState(initialValue);
|
|
|
|
useEffect(() => {
|
|
setName(initialValue);
|
|
}, [initialValue]);
|
|
|
|
const handleSubmit = (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
if (name.trim()) {
|
|
onSave(name.trim());
|
|
}
|
|
};
|
|
|
|
if (!isOpen) return null;
|
|
|
|
return (
|
|
<div
|
|
className="fixed inset-0 bg-black bg-opacity-60 flex justify-center items-center z-50 p-4 transition-opacity"
|
|
onClick={onClose}
|
|
>
|
|
<div
|
|
className="bg-white rounded-xl shadow-2xl w-full max-w-md transform transition-all"
|
|
onClick={(e) => e.stopPropagation()}
|
|
>
|
|
<form onSubmit={handleSubmit}>
|
|
<div className="p-6">
|
|
<div className="flex justify-between items-center mb-4">
|
|
<h2 className="text-2xl font-bold text-gray-800">{title}</h2>
|
|
<button
|
|
type="button"
|
|
onClick={onClose}
|
|
className="text-gray-400 hover:text-gray-600 text-2xl"
|
|
>
|
|
×
|
|
</button>
|
|
</div>
|
|
<div>
|
|
<label htmlFor="itemName" className="block text-sm font-medium text-gray-700 mb-1">
|
|
Nama
|
|
</label>
|
|
<input
|
|
type="text"
|
|
id="itemName"
|
|
value={name}
|
|
onChange={(e) => setName(e.target.value)}
|
|
className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:ring-red-500 focus:border-red-500"
|
|
placeholder={`Masukkan nama...`}
|
|
required
|
|
autoFocus
|
|
/>
|
|
</div>
|
|
</div>
|
|
<div className="bg-gray-50 px-6 py-4 rounded-b-xl flex justify-end space-x-3">
|
|
<button
|
|
type="button"
|
|
onClick={onClose}
|
|
className="bg-gray-200 text-gray-700 px-4 py-2 rounded-lg hover:bg-gray-300 transition duration-300 font-medium"
|
|
>
|
|
Batal
|
|
</button>
|
|
<button
|
|
type="submit"
|
|
className="bg-red-600 text-white px-4 py-2 rounded-lg hover:bg-red-700 transition duration-300 font-medium shadow-sm"
|
|
>
|
|
Simpan
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default FormModal;
|