You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
5.2 KiB

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("")
)
}