TTMT.ManageWebGUI/src/components/forms/command-form.tsx

69 lines
1.7 KiB
TypeScript
Raw Normal View History

2025-09-24 16:13:57 +07:00
"use client";
import { useForm } from "@tanstack/react-form";
import { z } from "zod";
import { Textarea } from "@/components/ui/textarea";
interface ShellCommandFormProps {
2025-09-26 17:56:55 +07:00
command: string;
onCommandChange: (value: string) => void;
disabled?: boolean;
2025-09-24 16:13:57 +07:00
}
2025-09-26 17:56:55 +07:00
export function ShellCommandForm({
command,
onCommandChange,
disabled,
}: ShellCommandFormProps) {
2025-09-24 16:13:57 +07:00
const form = useForm({
2025-09-26 17:56:55 +07:00
defaultValues: { command },
onSubmit: () => {},
2025-09-24 16:13:57 +07:00
});
return (
2025-09-26 17:56:55 +07:00
<form
onSubmit={(e) => {
e.preventDefault();
form.handleSubmit();
}}
className="space-y-5"
>
<form.Field
name="command"
validators={{
onChange: ({ value }: { value: string }) => {
const schema = z
.string()
.min(1, "Nhập command để thực thi")
.max(500, "Command quá dài");
const result = schema.safeParse(value);
if (!result.success) {
return result.error.issues.map((i) => i.message);
}
return [];
},
}}
children={(field) => (
<div className="w-full px-0">
<Textarea
className="w-full h-[25vh]"
placeholder="Nhập lệnh..."
value={field.state.value}
onChange={(e) => {
field.handleChange(e.target.value);
onCommandChange(e.target.value);
2025-09-24 16:13:57 +07:00
}}
2025-09-26 17:56:55 +07:00
disabled={disabled}
2025-09-24 16:13:57 +07:00
/>
2025-09-26 17:56:55 +07:00
{field.state.meta.errors?.length > 0 && (
<p className="text-sm text-red-500">
{String(field.state.meta.errors[0])}
</p>
)}
</div>
)}
/>
</form>
2025-09-24 16:13:57 +07:00
);
}