Good day.
I can't understand why the name is not transferred to the "DropDown Header"?
import SwiftUI
let actionButtonTest = ModelActionButton()
private var width: CGFloat = 200
private var height: CGFloat = 40
private var paddingV: CGFloat = 10
private var paddingH: CGFloat = 10
private var radius: CGFloat = 10.0
private var shadowRadius: CGFloat = 0
private var shadowX: CGFloat = 0
private var shadowY: CGFloat = 0
private var shadow: Color = .black
private var backColor: Color = .gray
private var forColor: Color = .white
struct DropDownAction {
var title: String
var action: () -> ()
}
struct DropDownButton: View {
var actionModel: DropDownAction
var select: (DropDownAction) -> ()
var body: some View {
Button(actionModel.title.uppercased(), action: {
self.select(self.actionModel)
})
}
}
struct DropMenu: View {
var menuActions: [DropDownAction]
var select: (DropDownAction) -> ()
var body: some View {
VStack(spacing: 10) {
ForEach(menuActions, id: \.title) {
DropDownButton(actionModel: $0, select: self.select)
.frame(width: width, height: height, alignment: .leading)
}
}
.parametrsButtom()
}
}
struct DropDownHeader: View {
@State var select: DropDownAction
var action: () -> ()
var expand: Bool
var body: some View {
Button(action: action) {
HStack {
Text(select.title.uppercased()) // ??????? What transfer @select?
Spacer()
Image(systemName: "chevron.\(expand ? "up" : "down")")
Text("\(select.title)")
}
.frame(width: width, height: height, alignment: .leading)
}
.parametrsButtom()
}
}
struct DropDownView: View {
var menuActions: [DropDownAction]
@State var expand = false
@Binding var select: DropDownAction
var body: some View {
VStack(alignment: .center, spacing: 0) {
DropDownHeader(select: select, action: {
expand.toggle()
}, expand: expand)
if expand {
DropMenu(menuActions: menuActions) { action in
//action.action()
select = action
print(action)
print(select)
self.expand = false
}
}
}
.animation(.spring())
}
}
struct DropDownView_Previews: PreviewProvider {
//@State static var textTest: String = action.actionButton.first?.title ?? "Error"
@State static var select: DropDownAction = action.actionButton.first!
static var previews: some View {
DropDownView(menuActions: actionButtonTest.actionButton, select: $select)
}
}
struct ParametrsButtom: ViewModifier {
func body(content: Content) -> some View {
content
.padding(.vertical, paddingV)
.padding(.horizontal, paddingH)
.foregroundColor(forColor)
.background(backColor)
.cornerRadius(radius)
.shadow(color: shadow, radius: shadowRadius, x: shadowX, y: shadowY)
}
}
extension View {
func parametrsButtom() -> some View {
self.modifier(ParametrsButtom())
}
}
//---------
import SwiftUI
let action = ModelActionButton()
@main
struct DropButtonApp: App {
@State var select: DropDownAction = action.actionButton.first!
var body: some Scene {
WindowGroup {
DropDownView(menuActions: action.actionButton, select: $select)
}
}
}
//---------
import Foundation
class ModelActionButton {
let actionButton: [DropDownAction] = [
.init(title: "First", action: {} ),
.init(title: "Second", action: {} ),
.init(title: "Third", action: {} )
]
}