Skip to content

Commit

Permalink
feat: Apply TextField changes
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhello0507 committed Sep 20, 2024
1 parent 2a44107 commit d605e28
Show file tree
Hide file tree
Showing 9 changed files with 358 additions and 65 deletions.
27 changes: 27 additions & 0 deletions Source/DDS/Component/TextField/Extension/View+advancedFocus.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// File.swift
//
//
// Created by hhhello0507 on 8/21/24.
//

import SwiftUI

struct AdvancedFocusViewModifier: ViewModifier {

@FocusState private var focused: Bool

func body(content: Content) -> some View {
content
.focused($focused)
.onTapGesture {
focused = true
}
}
}

public extension View {
func advancedFocus() -> some View {
self.modifier(AdvancedFocusViewModifier())
}
}
103 changes: 103 additions & 0 deletions Source/DDS/Component/TextField/Internal/BaseTextField.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//
// File.swift
//
//
// Created by hhhello0507 on 8/21/24.
//

import SwiftUI

internal struct BaseTextField: View {

@FocusState private var focused
@State private var animatedFocusing: Bool = false

private var isHighlighted: Bool {
animatedFocusing || !text.isEmpty
}

// MARK: - Parameters
// text
private let hint: String
@Binding private var text: String
private let font: Font
private let supportText: String?

// state
private let isSecured: Bool
private let isEnabled: Bool
private let isError: Bool

// interaction
private let isFirstResponder: Bool

// style
private let colors: TextFieldColors

init(
_ hint: String,
text: Binding<String>,
font: Font,
supportText: String?,
isSecured: Bool,
isEnabled: Bool,
isError: Bool,
isFirstResponder: Bool,
colors: TextFieldColors
) {
self.hint = hint
self._text = text
self.font = font
self.supportText = supportText
self.isSecured = isSecured
self.isEnabled = isEnabled
self.isError = isError
self.isFirstResponder = isFirstResponder
self.colors = colors
}

var body: some View {
Group {
if isSecured {
SecureField("", text: $text)
} else {
TextField("", text: $text)
}
}
.focused($focused)
.overlay {
Text(hint)
.foreground(colors.hintColor)
.scaleEffect(
isHighlighted ? 0.75 : 1,
anchor: .topLeading
)
.padding(.top, isHighlighted ? -30 : 0)
.frame(maxWidth: .infinity, alignment: .leading)
}
// style
.font(font)
.foreground(colors.foregroundColor)
.tint(
isError
? colors.errorColor
: colors.primaryColor
)
// interaction
.disabled(!isEnabled)
.onAppear {
focused = true
}
.onChange(of: focused) { newValue in
withAnimation(.spring(duration: 0.1)) {
animatedFocusing = newValue
}
}
// optimization
#if canImport(UIKit)
.textInputAutocapitalization(.never)
#endif
.autocorrectionDisabled()
.textContentType(.init(rawValue: ""))
}
}
50 changes: 50 additions & 0 deletions Source/DDS/Component/TextField/Internal/TextFieldIcon.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// File.swift
//
//
// Created by hhhello0507 on 8/21/24.
//

import SwiftUI

internal struct TextFieldIcon: View {

let isHide: Bool
let isSecured: Bool
let isEnabled: Bool
let isError: Bool
let colors: TextFieldColors
let action: () -> Void

var body: some View {
Button {
if isEnabled {
action()
}
} label: {
Image(
icon: isError
? .exclamationmarkCircle
: isSecured
? isHide ? .eyeSlash : .eye
: .xmarkCircle
)
.resizable()
.renderingMode(.template)
.foreground(
isError
? colors.errorColor
: colors.iconColor
)
.frame(width: 24, height: 24)
.padding(4)
.opacity(
isError
? 1
: isEnabled
? 0.5
: 0
)
}
}
}
Loading

0 comments on commit d605e28

Please sign in to comment.