// deployment-edit-service/ServiceCreateForm.tsx — 创建 Service 表单(多端口 + 快速预设) // 受控组件:接收 svcForm + setSvcForm + selectorLabels,内部管理端口增删改。 import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Minus, Plus, Zap } from 'lucide-react'; import i18n from '@/i18n'; import type { SvcFormState } from './types'; import { PORT_PRESETS } from './types'; interface Props { svcForm: SvcFormState; setSvcForm: React.Dispatch>; selectorLabels: Record; deploymentName: string; onCancel: () => void; onCreate: () => void; } export function ServiceCreateForm({ svcForm, setSvcForm, selectorLabels, deploymentName, onCancel, onCreate }: Props) { // ──── 端口操作 ──── const addPort = (preset?: typeof PORT_PRESETS[number]) => { setSvcForm(s => ({ ...s, ports: [...s.ports, preset ? { name: preset.name, port: String(preset.port), targetPort: String(preset.targetPort), protocol: preset.protocol as 'TCP' | 'UDP', nodePort: '' } : { name: '', port: '', targetPort: '', protocol: 'TCP' as const, nodePort: '' } ], })); }; const updatePort = (i: number, field: string, value: string) => { setSvcForm(s => ({ ...s, ports: s.ports.map((p, idx) => idx === i ? { ...p, [field]: value } : p), })); }; const removePort = (i: number) => { if (svcForm.ports.length <= 1) return; setSvcForm(s => ({ ...s, ports: s.ports.filter((_, idx) => idx !== i) })); }; return (
创建新 Service
{/* 基本信息 */}
setSvcForm(s => ({ ...s, name: e.target.value }))} placeholder={`${deploymentName}-svc`} className="h-7 mt-0.5 dg-input text-[var(--dg-text-secondary)] text-xs font-mono" />
{/* 快速预设 */}
{PORT_PRESETS.map(p => ( ))}
{/* 端口列表 */}
{svcForm.ports.map((p, i) => (
{/* 名称 */} updatePort(i, 'name', e.target.value)} placeholder="http" className="h-7 w-[72px] dg-input text-[var(--dg-text-secondary)] text-xs font-mono" /> {/* 协议 */} {/* port → targetPort */} updatePort(i, 'port', e.target.value)} type="number" placeholder="port" className="h-7 w-[72px] dg-input text-[var(--dg-text-secondary)] text-xs font-mono" /> → updatePort(i, 'targetPort', e.target.value)} type="number" placeholder="targetPort" className="h-7 w-[72px] dg-input text-[var(--dg-text-secondary)] text-xs font-mono" /> {/* NodePort (仅 NodePort 类型) */} {svcForm.type === 'NodePort' && ( updatePort(i, 'nodePort', e.target.value)} type="number" placeholder="30000-32767" className="h-7 w-[80px] dg-input text-[var(--dg-text-secondary)] text-xs font-mono" /> )} {/* 删除 */}
))} {/* 添加自定义端口 */}
{/* Selector 预览 */}
selector (自动检测): {Object.keys(selectorLabels).length > 0 ? ( Object.entries(selectorLabels).map(([k, v]) => ( {k}={v} )) ) : ( {i18n.t('kubernetes.deploymentedits.s5847b0f9')} )}
); }