-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared.tsx
More file actions
113 lines (106 loc) · 3.14 KB
/
Copy pathshared.tsx
File metadata and controls
113 lines (106 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"use client";
import { Plus, X } from "lucide-react";
import { useState } from "react";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Input } from "@/components/ui/input";
/** Card section used by every guided form. */
export function FormSection({
title,
description,
children,
actions,
}: {
title: string;
description?: string;
children: React.ReactNode;
actions?: React.ReactNode;
}) {
return (
<Card>
<CardHeader className="flex flex-row items-start justify-between gap-2 space-y-0">
<div className="space-y-1">
<CardTitle className="text-sm">{title}</CardTitle>
{description && <p className="text-xs text-muted-foreground">{description}</p>}
</div>
{actions}
</CardHeader>
<CardContent className="space-y-4">{children}</CardContent>
</Card>
);
}
/** Chip-style editor for string arrays (hostnames, SANs, args…). */
export function StringListEditor({
values,
onChange,
placeholder,
mono = true,
}: {
values: string[];
onChange: (values: string[]) => void;
placeholder?: string;
mono?: boolean;
}) {
const [draft, setDraft] = useState("");
function add() {
const v = draft.trim();
if (!v || values.includes(v)) return;
onChange([...values, v]);
setDraft("");
}
return (
<div className="flex flex-wrap items-center gap-1.5">
{values.map((v) => (
<Badge
key={v}
variant="secondary"
className={`gap-1 pr-1 text-[11px] font-normal ${mono ? "font-mono" : ""}`}
>
{v}
<button
type="button"
onClick={() => onChange(values.filter((x) => x !== v))}
className="rounded-sm p-0.5 hover:bg-muted-foreground/20"
aria-label={`Remove ${v}`}
>
<X className="size-3" />
</button>
</Badge>
))}
<div className="flex items-center gap-1.5">
<Input
value={draft}
onChange={(e) => setDraft(e.target.value)}
onKeyDown={(e) => e.key === "Enter" && (e.preventDefault(), add())}
placeholder={placeholder}
className={`h-7 w-48 text-xs ${mono ? "font-mono" : ""}`}
/>
<Button type="button" variant="outline" size="sm" className="h-7" onClick={add}>
<Plus className="size-3.5" />
</Button>
</div>
</div>
);
}
/** Standard remove button for array-item rows. */
export function RemoveRowButton({ onClick, label }: { onClick: () => void; label: string }) {
return (
<Button
type="button"
variant="ghost"
size="icon"
className="size-7 text-muted-foreground hover:text-destructive"
onClick={onClick}
aria-label={label}
>
<X className="size-3.5" />
</Button>
);
}
/** Coerce an Input's string to number | undefined for port-style fields. */
export function numberOrUndefined(value: string): number | undefined {
if (value.trim() === "") return undefined;
const n = Number(value);
return Number.isFinite(n) ? n : undefined;
}