-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2a44107
commit d605e28
Showing
9 changed files
with
358 additions
and
65 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
Source/DDS/Component/TextField/Extension/View+advancedFocus.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
103
Source/DDS/Component/TextField/Internal/BaseTextField.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
50
Source/DDS/Component/TextField/Internal/TextFieldIcon.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.