Skip to content

Commit

Permalink
Add toggle field
Browse files Browse the repository at this point in the history
  • Loading branch information
joeshonm committed May 4, 2020
1 parent 69ec7ac commit a0f8186
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
25 changes: 16 additions & 9 deletions Sources/SimpleForm/SimpleFormField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ public struct SimpleFormField: View, Identifiable {
self.model.validation = validation
}

public init(toggleField label:String, name:String,value:Bool = false) {
self.model.type = .toggle
self.model.label = label
self.model.name = name
self.model.value = value
}




Expand All @@ -57,8 +64,8 @@ public struct SimpleFormField: View, Identifiable {
.overlay(
RoundedRectangle(cornerRadius: 4)
.stroke(self.model.errors.count > 0 ? Color.red : Color.gray, lineWidth: 1)
)
)

} else if(self.model.type == .picker) {
Picker(selection: Binding(get: {
return self.model.pickerSelection
Expand All @@ -68,6 +75,12 @@ public struct SimpleFormField: View, Identifiable {
}), label: Text("\(self.model.labelPosition == .placeholder ? self.model.label : "")")) {
self.model.pickerDisplay
}
} else if(self.model.type == .toggle) {
Toggle(self.model.label, isOn: Binding(get: {
return self.model.value as! Bool
}, set: { (newValue) in
self.model.value = newValue
}))
} else {
EmptyView()
}
Expand All @@ -76,15 +89,9 @@ public struct SimpleFormField: View, Identifiable {
ForEach(self.model.errors, id: \.self) { error in
Text("\(error)").font(.footnote)
}

}
}

}
}

//struct SimpleFormField_Previews: PreviewProvider {
// static var previews: some View {
// SimpleFormField()
// }
//}
1 change: 1 addition & 0 deletions Sources/SimpleForm/SimpleFormFieldType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ import SwiftUI
public enum SimpleFormFieldType {
case text
case picker
case toggle
}
5 changes: 0 additions & 5 deletions Sources/SimpleForm/SimpleFormSection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,3 @@ public struct SimpleFormSection: View, Identifiable {
}
}

//struct SimpleFormSection_Previews: PreviewProvider {
// static var previews: some View {
// SimpleFormSection()
// }
//}

0 comments on commit a0f8186

Please sign in to comment.