| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- import React, { useState, useEffect, useRef } from 'react';
- import { X, Check } from 'lucide-react';
- interface InputDrawerProps {
- isOpen: boolean;
- onClose: () => void;
- title: string;
- onSubmit: (value: string) => void;
- placeholder?: string;
- defaultValue?: string;
- submitLabel?: string;
- }
- export const InputDrawer: React.FC<InputDrawerProps> = ({
- isOpen,
- onClose,
- title,
- onSubmit,
- placeholder = '',
- defaultValue = '',
- submitLabel = '确定'
- }) => {
- const [value, setValue] = useState(defaultValue);
- const inputRef = useRef<HTMLInputElement>(null);
- useEffect(() => {
- if (isOpen) {
- setValue(defaultValue);
- setTimeout(() => {
- inputRef.current?.focus();
- }, 50);
- }
- }, [isOpen, defaultValue]);
- const handleSubmit = (e?: React.FormEvent) => {
- e?.preventDefault();
- if (value.trim()) {
- onSubmit(value.trim());
- onClose();
- }
- };
- if (!isOpen) return null;
- return (
- <div className="fixed inset-0 z-50 overflow-hidden">
- <div className="absolute inset-0 bg-black bg-opacity-30 transition-opacity" onClick={onClose} />
- <div className="absolute inset-y-0 right-0 max-w-sm w-full flex pointer-events-none">
- {/* pointer-events-none on wrapper, auto on content to allow closing by clicking left side */}
- <div className="flex-1 flex flex-col bg-white shadow-xl animate-in slide-in-from-right duration-300 pointer-events-auto">
- <div className="flex items-center justify-between px-4 py-3 border-b border-gray-200">
- <h2 className="text-lg font-medium text-gray-900">{title}</h2>
- <button
- onClick={onClose}
- className="text-gray-400 hover:text-gray-500 focus:outline-none"
- >
- <X size={24} />
- </button>
- </div>
- <form onSubmit={handleSubmit} className="p-6 flex-1 flex flex-col">
- <label className="block text-sm font-medium text-gray-700 mb-2">
- {title}
- </label>
- <input
- ref={inputRef}
- type="text"
- value={value}
- onChange={(e) => setValue(e.target.value)}
- placeholder={placeholder}
- className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none transition-all"
- />
- <div className="mt-6 flex justify-end gap-3">
- <button
- type="button"
- onClick={onClose}
- className="px-4 py-2 text-sm font-medium text-gray-700 bg-white border border-gray-300 rounded-lg hover:bg-gray-50"
- >
- 取消
- </button>
- <button
- type="submit"
- disabled={!value.trim()}
- className="px-4 py-2 text-sm font-medium text-white bg-blue-600 rounded-lg hover:bg-blue-700 disabled:opacity-50 flex items-center gap-2"
- >
- {submitLabel}
- </button>
- </div>
- </form>
- </div>
- </div>
- </div>
- );
- };
|