How can I add Textfield to the alert while using AlertState
#2164
-
I'm trying to add TextField to the alert view. .alert("Enter your name", isPresented: $showingAlert) {
TextField("Enter your name", text: $name)
Button("OK", action: submit)
} However, while using Here is some part of my code: // RoomListFeature.swift
var body: some ReducerOf<Self> {
Reduce<State, Action> { state, action in
switch action {
case .startRoomButtonTapped: // when the user tapped button to start a NEW room
state.alert = AlertState {
TextState("Start a new room?") // 🤔 I want to add text field to receive the name of the new room.
} actions: {
ButtonState(action: .startRoom) {
TextState("Start")
}
}
return .none
case .alert(.presented(.startRoom)): // when the user tapped "Start" button on the alert
state.room = RoomReducer.State(room: Room(...))
return .none
case .alert:
return .none
}
}
.ifLet(\.$alert, action: /Action.alert)
} // RoomList.swift
struct RoomList: View {
let store: StoreOf<RoomListFeature>
var body: some View {
WithViewStore(self.store, observe: { $0 }) { viewStore in
List {
// ...
}
.alert(
store: self.store.scope(
state: \.$alert,
action: { .alert($0) }
)
) // 🤔 No additional parameter to customize the alert view
}
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
stephencelis
Jun 2, 2023
Replies: 1 comment 3 replies
-
This is duplicated with discussion #1357 |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think @kamcma's solution of using Swift's
alert
API directly is still the way to go for now:#2048 (comment)
We'd love to ship a
TextFieldState
in the future, but at the end of the day it'll be a race to maintainAlertState
compatibility with all the alert features Apple provides in the future, so dropping down to the core APIs will always unblock you.