import SwiftUI // MARK: - 日历事件输入组件 struct CalendarInputView: View { @Binding var eventTitle: String @Binding var eventDescription: String @Binding var startDate: Date @Binding var endDate: Date @Binding var location: String @FocusState var focusedField: CalendarField? // 日历字段枚举 enum CalendarField: Hashable { case title, description, location } var body: some View { VStack(spacing: 16) { // 事件标题 VStack(alignment: .leading, spacing: 8) { HStack { Text("事件标题") .font(.subheadline) .foregroundColor(.primary) Text("*") .foregroundColor(.red) Spacer() } TextField("会议标题", text: $eventTitle) .textFieldStyle(RoundedBorderTextFieldStyle()) .focused($focusedField, equals: .title) } // 事件描述 VStack(alignment: .leading, spacing: 8) { HStack { Text("事件描述") .font(.subheadline) .foregroundColor(.primary) Spacer() } TextField("事件详细描述", text: $eventDescription) .textFieldStyle(RoundedBorderTextFieldStyle()) .focused($focusedField, equals: .description) } // 开始时间 VStack(alignment: .leading, spacing: 8) { HStack { Text("开始时间") .font(.subheadline) .foregroundColor(.primary) Text("*") .foregroundColor(.red) Spacer() } DatePicker("开始时间", selection: $startDate, displayedComponents: [.date, .hourAndMinute]) .datePickerStyle(CompactDatePickerStyle()) .labelsHidden() } // 结束时间 VStack(alignment: .leading, spacing: 8) { HStack { Text("结束时间") .font(.subheadline) .foregroundColor(.primary) Text("*") .foregroundColor(.red) Spacer() } DatePicker("结束时间", selection: $endDate, displayedComponents: [.date, .hourAndMinute]) .datePickerStyle(CompactDatePickerStyle()) .labelsHidden() } // 地点 VStack(alignment: .leading, spacing: 8) { HStack { Text("地点") .font(.subheadline) .foregroundColor(.primary) Spacer() } TextField("会议地点", text: $location) .textFieldStyle(RoundedBorderTextFieldStyle()) .focused($focusedField, equals: .location) } // 时间验证提示 if endDate <= startDate { VStack(alignment: .leading, spacing: 8) { HStack { Image(systemName: "exclamationmark.triangle") .font(.caption) .foregroundColor(.orange) Text("时间设置提示") .font(.caption) .foregroundColor(.primary) Spacer() } Text("结束时间必须晚于开始时间") .font(.caption) .foregroundColor(.orange) } .padding(.horizontal, 12) .padding(.vertical, 8) .background( RoundedRectangle(cornerRadius: 8) .fill(Color.orange.opacity(0.1)) ) } } .toolbar { ToolbarItemGroup(placement: .keyboard) { Spacer() Button("完成") { focusedField = nil } .foregroundColor(.blue) .font(.system(size: 16, weight: .medium)) } } .onAppear { // 设置默认时间 if startDate == Date() { startDate = Date() endDate = Calendar.current.date(byAdding: .hour, value: 1, to: startDate) ?? startDate } } } } #Preview { CalendarInputView( eventTitle: .constant(""), eventDescription: .constant(""), startDate: .constant(Date()), endDate: .constant(Calendar.current.date(byAdding: .hour, value: 1, to: Date()) ?? Date()), location: .constant("") ) }