Skip to content

Commit

Permalink
fix editing TextField causes re-render
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleoflqj committed Dec 15, 2024
1 parent 2888151 commit b568289
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set(CMAKE_CONFIGURATION_TYPES Debug Release CACHE STRING INTERNAL FORCE)
set(CMAKE_TOOLCHAIN_FILE "${CMAKE_CURRENT_LIST_DIR}/cmake/ios.cmake")

project(fcitx5-ios VERSION 0.1.0 LANGUAGES CXX Swift)
set(CMAKE_OSX_DEPLOYMENT_TARGET 15)
set(CMAKE_OSX_DEPLOYMENT_TARGET 17) # I really need onChange for focus out.
set(CMAKE_Swift_LANGUAGE_VERSION 5.9)
set(CMAKE_CXX_STANDARD 17)

Expand Down
28 changes: 15 additions & 13 deletions src/config/StringView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,28 @@ struct StringView: View, OptionViewProtocol {
let label: String
let data: [String: Any]
@Binding var value: Any
@State private var text: String = ""
@FocusState private var isFocused: Bool

var body: some View {
HStack {
if !label.isEmpty {
Text(label)
}
TextField(
"",
text: Binding<String>(
get: { value as! String },
set: {
// Avoid unnecessary write.
if value as! String != $0 {
value = $0
}
TextField("", text: $text)
.focused($isFocused)
// Don't update real-time. It changes parent state so the whole view is re-rendered.
// Don't use onSubmit. It requires Enter key to be pressed.
.onChange(of: isFocused) {
// Avoid unnecessary write.
if !isFocused && value as! String != text {
value = text
}
)
)
// Leading for List item, trailing for String option.
.multilineTextAlignment(label.isEmpty ? .leading : .trailing)
}
// Leading for List item, trailing for String option.
.multilineTextAlignment(label.isEmpty ? .leading : .trailing)
}.onAppear {
text = value as! String
}
}
}

0 comments on commit b568289

Please sign in to comment.