2021-08-25 12:03:36 +00:00
|
|
|
//
|
|
|
|
// Copyright 2021 New Vector Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import SwiftUI
|
2021-11-01 14:52:00 +00:00
|
|
|
import Introspect
|
2021-08-25 12:03:36 +00:00
|
|
|
|
|
|
|
@available(iOS 14.0, *)
|
2021-09-13 10:36:33 +00:00
|
|
|
/// A bordered style of text input
|
|
|
|
///
|
|
|
|
/// As defined in:
|
|
|
|
/// https://www.figma.com/file/X4XTH9iS2KGJ2wFKDqkyed/Compound?node-id=2039%3A26415
|
2021-08-25 12:03:36 +00:00
|
|
|
struct BorderedInputFieldStyle: TextFieldStyle {
|
2021-08-25 18:24:33 +00:00
|
|
|
|
2022-05-12 16:40:36 +00:00
|
|
|
@Environment(\.theme) private var theme: ThemeSwiftUI
|
|
|
|
@Environment(\.isEnabled) private var isEnabled: Bool
|
2021-08-25 12:03:36 +00:00
|
|
|
|
|
|
|
var isEditing: Bool = false
|
|
|
|
var isError: Bool = false
|
|
|
|
|
|
|
|
private var borderColor: Color {
|
2022-05-12 16:40:36 +00:00
|
|
|
if isError {
|
2021-09-01 11:34:38 +00:00
|
|
|
return theme.colors.alert
|
2021-08-25 12:03:36 +00:00
|
|
|
} else if isEditing {
|
2021-09-01 11:34:38 +00:00
|
|
|
return theme.colors.accent
|
2021-08-25 12:03:36 +00:00
|
|
|
}
|
2022-05-12 16:40:36 +00:00
|
|
|
return theme.colors.quinaryContent
|
2021-08-25 12:03:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private var accentColor: Color {
|
|
|
|
if isError {
|
2021-09-01 11:34:38 +00:00
|
|
|
return theme.colors.alert
|
2021-08-25 12:03:36 +00:00
|
|
|
}
|
2021-09-01 11:34:38 +00:00
|
|
|
return theme.colors.accent
|
2021-08-25 12:03:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private var textColor: Color {
|
2021-11-01 14:52:00 +00:00
|
|
|
if (theme.identifier == ThemeIdentifier.dark) {
|
|
|
|
return (isEnabled ? theme.colors.primaryContent : theme.colors.tertiaryContent)
|
|
|
|
} else {
|
|
|
|
return (isEnabled ? theme.colors.primaryContent : theme.colors.quarterlyContent)
|
2021-08-25 12:03:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var backgroundColor: Color {
|
2021-09-01 11:34:38 +00:00
|
|
|
if !isEnabled && (theme.identifier == ThemeIdentifier.dark) {
|
|
|
|
return theme.colors.quinaryContent
|
2021-08-25 12:03:36 +00:00
|
|
|
}
|
2021-09-01 11:34:38 +00:00
|
|
|
return theme.colors.background
|
2021-08-25 12:03:36 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 14:52:00 +00:00
|
|
|
private var placeholderColor: Color {
|
|
|
|
return theme.colors.tertiaryContent
|
|
|
|
}
|
|
|
|
|
2021-08-25 14:46:02 +00:00
|
|
|
private var borderWidth: CGFloat {
|
2021-11-01 14:52:00 +00:00
|
|
|
return isEditing || isError ? 2.0 : 1.5
|
2021-08-25 12:03:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func _body(configuration: TextField<_Label>) -> some View {
|
2021-11-01 14:52:00 +00:00
|
|
|
let rect = RoundedRectangle(cornerRadius: 8.0)
|
2021-08-25 13:21:43 +00:00
|
|
|
return configuration
|
2021-09-01 11:34:38 +00:00
|
|
|
.font(theme.fonts.callout)
|
2021-08-25 12:03:36 +00:00
|
|
|
.foregroundColor(textColor)
|
|
|
|
.accentColor(accentColor)
|
2021-11-01 14:52:00 +00:00
|
|
|
.frame(height: 48.0)
|
|
|
|
.padding(.horizontal, 8.0)
|
2021-08-25 12:03:36 +00:00
|
|
|
.background(backgroundColor)
|
|
|
|
.clipShape(rect)
|
2021-08-25 14:46:02 +00:00
|
|
|
.overlay(rect.stroke(borderColor, lineWidth: borderWidth))
|
2021-11-01 14:52:00 +00:00
|
|
|
.introspectTextField { textField in
|
|
|
|
textField.returnKeyType = .done
|
|
|
|
textField.clearButtonMode = .whileEditing
|
|
|
|
textField.attributedPlaceholder = NSAttributedString(string: textField.placeholder ?? "",
|
|
|
|
attributes: [NSAttributedString.Key.foregroundColor: UIColor(placeholderColor)])
|
|
|
|
}
|
2021-08-25 12:03:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@available(iOS 14.0, *)
|
|
|
|
struct BorderedInputFieldStyle_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
Group {
|
|
|
|
VStack {
|
|
|
|
TextField("Placeholder", text: .constant(""))
|
|
|
|
.textFieldStyle(BorderedInputFieldStyle())
|
|
|
|
TextField("Placeholder", text: .constant(""))
|
|
|
|
.textFieldStyle(BorderedInputFieldStyle(isEditing: true))
|
|
|
|
TextField("Placeholder", text: .constant("Web"))
|
|
|
|
.textFieldStyle(BorderedInputFieldStyle())
|
|
|
|
TextField("Placeholder", text: .constant("Web"))
|
|
|
|
.textFieldStyle(BorderedInputFieldStyle(isEditing: true))
|
|
|
|
TextField("Placeholder", text: .constant("Web"))
|
2021-08-25 18:24:33 +00:00
|
|
|
.textFieldStyle(BorderedInputFieldStyle())
|
|
|
|
.disabled(true)
|
2021-08-25 12:03:36 +00:00
|
|
|
TextField("Placeholder", text: .constant("Web"))
|
|
|
|
.textFieldStyle(BorderedInputFieldStyle(isEditing: true, isError: true))
|
|
|
|
}
|
|
|
|
.padding()
|
|
|
|
VStack {
|
|
|
|
TextField("Placeholder", text: .constant(""))
|
|
|
|
.textFieldStyle(BorderedInputFieldStyle())
|
|
|
|
TextField("Placeholder", text: .constant(""))
|
|
|
|
.textFieldStyle(BorderedInputFieldStyle(isEditing: true))
|
|
|
|
TextField("Placeholder", text: .constant("Web"))
|
|
|
|
.textFieldStyle(BorderedInputFieldStyle())
|
|
|
|
TextField("Placeholder", text: .constant("Web"))
|
|
|
|
.textFieldStyle(BorderedInputFieldStyle(isEditing: true))
|
|
|
|
TextField("Placeholder", text: .constant("Web"))
|
2021-08-25 18:24:33 +00:00
|
|
|
.textFieldStyle(BorderedInputFieldStyle())
|
|
|
|
.disabled(true)
|
2021-08-25 12:03:36 +00:00
|
|
|
TextField("Placeholder", text: .constant("Web"))
|
|
|
|
.textFieldStyle(BorderedInputFieldStyle(isEditing: true, isError: true))
|
|
|
|
}
|
|
|
|
.padding()
|
|
|
|
.theme(ThemeIdentifier.dark)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|