2022-01-15 22:13:23 +08:00
|
|
|
//
|
|
|
|
// EditableText.swift
|
|
|
|
// PushDeer
|
|
|
|
//
|
|
|
|
// Created by HEXT on 2022/1/10.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct EditableText: View {
|
|
|
|
var placeholder = ""
|
|
|
|
@State var value = ""
|
|
|
|
var onCommit: (_ value: String) -> Void = { value in
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-02-14 01:13:26 +08:00
|
|
|
var body: some View {
|
2022-02-27 23:02:58 +08:00
|
|
|
TextField(placeholder, text: $value, onEditingChanged: { focus in
|
|
|
|
print("focus", focus, placeholder, value)
|
|
|
|
if !focus {
|
|
|
|
// 输入框失去焦点的时候也保存
|
|
|
|
self.onCommit(value)
|
|
|
|
}
|
|
|
|
}, onCommit: {
|
|
|
|
// enter回车键也会收键盘, 使其失去焦点
|
|
|
|
// print("修改文本:", value)
|
|
|
|
// self.onCommit(value)
|
2022-01-15 22:13:23 +08:00
|
|
|
})
|
|
|
|
.font(.system(size: 20))
|
|
|
|
.foregroundColor(Color.accentColor)
|
2022-02-14 01:13:26 +08:00
|
|
|
.submitLabelDone()
|
2022-01-15 22:13:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct EditableText_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
VStack {
|
|
|
|
EditableText(value: "你好")
|
|
|
|
EditableText(placeholder: "请输入")
|
|
|
|
EditableText()
|
|
|
|
EditableText(placeholder: "请输入") { value in
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|