SPB Git

Repository Public

SchemaFormView.swift 5.1 KB 100 lines swift

1import MCCCore2import SwiftUI34/// Inputs generated from a JSON Schema (`params_schema` of an action type or a job template). The values are only5/// collected here; the server validates them against the schema.6struct SchemaFormView: View {7    let fields: [SchemaField]8    @Binding var values: [String: JSONValue]9    @Binding var rawJSON: [String: String]10    var missing: [String] = []11    var invalid: [String] = []12    @Environment(\.mccTheme) private var theme1314    var body: some View {15        if fields.isEmpty {16            Text(tr("form.no_parameters")).font(.callout).foregroundStyle(theme.textSecondary)17        } else {18            VStack(alignment: .leading, spacing: Tokens.Space.s4) {19                ForEach(fields) { field in20                    VStack(alignment: .leading, spacing: Tokens.Space.s1) {21                        HStack(spacing: Tokens.Space.s2) {22                            Text(field.title).font(.callout.weight(.medium)).foregroundStyle(theme.textPrimary)23                            if field.required { Text(tr("form.required")).font(.caption).foregroundStyle(theme.textSecondary) }24                        }25                        control(for: field)26                            .accessibilityLabel(field.title)27                            .accessibilityHint(field.help ?? "")28                            .accessibilityIdentifier("param-\(field.name)")29                        if let help = field.help { Text(help).font(.caption).foregroundStyle(theme.textTertiary) }30                        if missing.contains(field.name) { Text(tr("form.missing")).font(.caption).foregroundStyle(StatusTone.warning.color(theme)) }31                        if invalid.contains(field.name) { Text(tr("form.invalid_json")).font(.caption).foregroundStyle(StatusTone.critical.color(theme)) }32                    }33                }34            }35        }36    }3738    @ViewBuilder39    private func control(for field: SchemaField) -> some View {40        switch field.kind {41        case .string:42            TextField(field.title, text: Binding(get: { values[field.name]?.stringValue ?? "" },43                                                 set: { values[field.name] = $0.isEmpty ? nil : .string($0) }))44                .textFieldStyle(.roundedBorder)45        case .choice(let options):46            Picker(field.title, selection: Binding(get: { values[field.name]?.stringValue ?? "" }, set: { values[field.name] = $0.isEmpty ? nil : .string($0) })) {47                if !field.required || values[field.name] == nil { Text("—").tag("") }48                ForEach(options, id: \.self) { Text($0).tag($0) }49            }50            .labelsHidden()51            .fixedSize()52        case .integer(let min, let max):53            HStack {54                TextField(field.title, text: Binding(get: { values[field.name]?.intValue.map(String.init) ?? "" },55                                                     set: { values[field.name] = Int($0).map { .number(Double($0)) } }))56                    .textFieldStyle(.roundedBorder)57                    .frame(maxWidth: 140)58                if min != nil || max != nil {59                    Text(tr("form.range", min.map(String.init) ?? "…", max.map(String.init) ?? "…")).font(.caption).foregroundStyle(theme.textTertiary)60                }61            }62        case .number(let min, let max):63            HStack {64                TextField(field.title, text: Binding(get: { values[field.name]?.doubleValue.map { Format.number($0, fractionDigits: 6, locale: Locale(identifier: "en_US_POSIX")) } ?? "" },65                                                     set: { values[field.name] = Double($0.replacingOccurrences(of: ",", with: ".")).map(JSONValue.number) }))66                    .textFieldStyle(.roundedBorder)67                    .frame(maxWidth: 140)68                if min != nil || max != nil {69                    Text(tr("form.range", min.map { Format.number($0) } ?? "…", max.map { Format.number($0) } ?? "…")).font(.caption).foregroundStyle(theme.textTertiary)70                }71            }72        case .boolean:73            Toggle(field.title, isOn: Binding(get: { values[field.name]?.boolValue ?? false }, set: { values[field.name] = .bool($0) }))74                .labelsHidden()75        case .json:76            TextEditor(text: Binding(get: { rawJSON[field.name] ?? values[field.name]?.prettyPrinted() ?? "" }, set: { rawJSON[field.name] = $0 }))77                .font(.body.monospaced())78                .frame(minHeight: 70)79                .overlay(RoundedRectangle(cornerRadius: Tokens.Radius.sm).strokeBorder(theme.border))80        }81    }82}8384/// Read-only rendering of a free-form JSON object (parameters, results).85struct JSONBlock: View {86    let value: JSONValue87    @Environment(\.mccTheme) private var theme88    var body: some View {89        ScrollView(.horizontal) {90            Text(value.prettyPrinted())91                .font(.callout.monospaced())92                .foregroundStyle(theme.textPrimary)93                .textSelection(.enabled)94                .padding(Tokens.Space.s3)95        }96        .frame(maxWidth: .infinity, alignment: .leading)97        .background(theme.inset, in: RoundedRectangle(cornerRadius: Tokens.Radius.md))98    }99}100
Files main
Menu

Theme