Skip to content

Commit dacbe5c

Browse files
display OK action if no actions are added to the alert
1 parent 41839c9 commit dacbe5c

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

Examples/DemosApp/DemosApp/ComponentsPreview/PreviewPages/AlertPreview.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ struct AlertPreview: View {
4444
get: { return self.model.primaryButton != nil },
4545
set: { newValue in
4646
self.model.primaryButton = newValue ? Self.initialPrimaryButton : nil
47-
48-
if !self.hasButtons {
49-
self.model.closesOnOverlayTap = true
50-
}
5147
}
5248
))
5349
if self.model.primaryButton != nil {
@@ -64,10 +60,6 @@ struct AlertPreview: View {
6460
get: { return self.model.secondaryButton != nil },
6561
set: { newValue in
6662
self.model.secondaryButton = newValue ? Self.initialSecondaryButton : nil
67-
68-
if !self.hasButtons {
69-
self.model.closesOnOverlayTap = true
70-
}
7163
}
7264
))
7365
if self.model.secondaryButton != nil {
@@ -88,7 +80,6 @@ struct AlertPreview: View {
8880
Text("Danger Background").tag(ComponentColor.danger.background)
8981
}
9082
Toggle("Closes On Overlay Tap", isOn: self.$model.closesOnOverlayTap)
91-
.disabled(!self.hasButtons)
9283
Picker("Content Paddings", selection: self.$model.contentPaddings) {
9384
Text("12px").tag(Paddings(padding: 12))
9485
Text("16px").tag(Paddings(padding: 16))
@@ -167,9 +158,6 @@ Enim habitant laoreet inceptos scelerisque senectus, tellus molestie ut. Eros ri
167158
set: { self.model.secondaryButton = $0 }
168159
)
169160
}
170-
var hasButtons: Bool {
171-
return self.model.primaryButton != nil || self.model.secondaryButton != nil
172-
}
173161
}
174162

175163
#Preview {

Sources/ComponentsKit/Components/Alert/Models/AlertVM.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,10 @@ extension AlertVM {
8686

8787
extension AlertVM {
8888
static let buttonsSpacing: CGFloat = 12
89+
90+
static let defaultButtonVM = ButtonVM {
91+
$0.title = "OK"
92+
$0.color = .primary
93+
$0.style = .filled
94+
}
8995
}

Sources/ComponentsKit/Components/Alert/UKAlertController.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import UIKit
22

33
/// A controller that presents an alert with a title, message, and up to two action buttons.
44
///
5+
/// All actions in an alert dismiss the alert after the action runs. If no actions are present, a standard “OK” action is included.
6+
///
57
/// - Example:
68
/// ```swift
79
/// let alert = UKAlertController(
@@ -87,15 +89,16 @@ public class UKAlertController: UKCenterModalController {
8789
} else {
8890
self.body = self.subtitleLabel
8991
}
90-
if self.alertVM.primaryButton.isNotNil || self.alertVM.secondaryButton.isNotNil {
91-
self.footer = self.buttonsStackView
92-
}
92+
self.footer = self.buttonsStackView
9393

94-
if self.alertVM.primaryButton.isNotNil {
94+
switch (self.alertVM.primaryButton.isNotNil, self.alertVM.secondaryButton.isNotNil) {
95+
case (true, true):
9596
self.buttonsStackView.addArrangedSubview(self.primaryButton)
96-
}
97-
if self.alertVM.secondaryButton.isNotNil {
9897
self.buttonsStackView.addArrangedSubview(self.secondaryButton)
98+
case (false, true):
99+
self.buttonsStackView.addArrangedSubview(self.secondaryButton)
100+
case (_, false):
101+
self.buttonsStackView.addArrangedSubview(self.primaryButton)
99102
}
100103

101104
self.primaryButton.action = { [weak self] in
@@ -125,6 +128,7 @@ public class UKAlertController: UKCenterModalController {
125128
if let primaryButtonVM = self.alertVM.primaryButtonVM {
126129
self.primaryButton.model = primaryButtonVM
127130
}
131+
self.primaryButton.model = self.alertVM.primaryButtonVM ?? AlertVM.defaultButtonVM
128132
if let secondaryButtonVM = self.alertVM.secondaryButtonVM {
129133
self.secondaryButton.model = secondaryButtonVM
130134
}

0 commit comments

Comments
 (0)