Skip to content

Commit a09e7e3

Browse files
committed
Add an optional height parameter to RoundedBorderTextFieldStyle so that the password field has the same height when the secure content is shown and hidden.
1 parent 6293b14 commit a09e7e3

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

WooCommerce/Classes/ViewRelated/Authentication/AccountCreationFormFieldView.swift

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ struct AccountCreationFormFieldView: View {
5555
insets: .init(top: RoundedBorderTextFieldStyle.Defaults.insets.top,
5656
leading: RoundedBorderTextFieldStyle.Defaults.insets.leading,
5757
bottom: RoundedBorderTextFieldStyle.Defaults.insets.bottom,
58-
trailing: Layout.secureFieldRevealButtonHorizontalPadding * 2 + Layout.secureFieldRevealButtonDimension * scale))
59-
)
58+
trailing: Layout.secureFieldRevealButtonHorizontalPadding * 2 + Layout.secureFieldRevealButtonDimension * scale),
59+
height: 44 * scale
60+
))
6061
.keyboardType(viewModel.keyboardType)
6162

6263
// Button to show/hide the text field content.
@@ -101,13 +102,24 @@ struct AccountCreationFormField_Previews: PreviewProvider {
101102
isSecure: false,
102103
errorMessage: nil,
103104
isFocused: true))
104-
AccountCreationFormFieldView(viewModel: .init(header: "Choose a password",
105-
placeholder: "Password",
106-
keyboardType: .default,
107-
text: .constant("wwwwwwwwwwwwwwwwwwwwwwww"),
108-
isSecure: true,
109-
errorMessage: "Too simple",
110-
isFocused: false))
111-
.environment(\.sizeCategory, .extraExtraExtraLarge)
105+
VStack {
106+
AccountCreationFormFieldView(viewModel: .init(header: "Choose a password",
107+
placeholder: "Password",
108+
keyboardType: .default,
109+
text: .constant("wwwwwwwwwwwwwwwwwwwwwwww"),
110+
isSecure: true,
111+
errorMessage: "Too simple",
112+
isFocused: false))
113+
.environment(\.sizeCategory, .medium)
114+
115+
AccountCreationFormFieldView(viewModel: .init(header: "Choose a password",
116+
placeholder: "Password",
117+
keyboardType: .default,
118+
text: .constant("wwwwwwwwwwwwwwwwwwwwwwww"),
119+
isSecure: true,
120+
errorMessage: "Too simple",
121+
isFocused: false))
122+
.environment(\.sizeCategory, .extraExtraExtraLarge)
123+
}
112124
}
113125
}

WooCommerce/Classes/ViewRelated/ReusableViews/SwiftUI Components/TextFieldStyles.swift

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,24 @@ struct RoundedBorderTextFieldStyle: TextFieldStyle {
66
private let focusedBorderColor: Color
77
private let unfocusedBorderColor: Color
88
private let insets: EdgeInsets
9+
private let height: CGFloat?
910

11+
/// - Parameters:
12+
/// - focused: Whether the field is focused or not.
13+
/// - focusedBorderColor: The border color when the field is focused.
14+
/// - unfocusedBorderColor: The border color when the field is not focused.
15+
/// - insets: The insets between the background border and the text input.
16+
/// - height: An optional fixed height for the field.
1017
init(focused: Bool,
1118
focusedBorderColor: Color = Defaults.focusedBorderColor,
1219
unfocusedBorderColor: Color = Defaults.unfocusedBorderColor,
13-
insets: EdgeInsets = Defaults.insets) {
20+
insets: EdgeInsets = Defaults.insets,
21+
height: CGFloat? = nil) {
1422
self.focused = focused
1523
self.focusedBorderColor = focusedBorderColor
1624
self.unfocusedBorderColor = unfocusedBorderColor
1725
self.insets = insets
26+
self.height = height
1827
}
1928

2029
func _body(configuration: TextField<Self._Label>) -> some View {
@@ -24,7 +33,9 @@ struct RoundedBorderTextFieldStyle: TextFieldStyle {
2433
RoundedRectangle(cornerRadius: 8, style: .continuous)
2534
.stroke(focused ? focusedBorderColor: unfocusedBorderColor,
2635
lineWidth: focused ? 2: 1)
36+
.frame(height: height)
2737
)
38+
.frame(height: height)
2839
}
2940
}
3041

@@ -51,6 +62,20 @@ struct TextFieldStyles_Previews: PreviewProvider {
5162
TextField("placeholder", text: .constant("custom insets"))
5263
.textFieldStyle(RoundedBorderTextFieldStyle(focused: false, insets: .init(top: 20, leading: 0, bottom: 10, trailing: 50)))
5364
.frame(width: 150)
65+
HStack {
66+
TextField("placeholder", text: .constant("text field"))
67+
.textFieldStyle(RoundedBorderTextFieldStyle(focused: true))
68+
SecureField("placeholder", text: .constant("secure"))
69+
.textFieldStyle(RoundedBorderTextFieldStyle(focused: true))
70+
}
71+
.environment(\.sizeCategory, .extraExtraExtraLarge)
72+
HStack {
73+
TextField("placeholder", text: .constant("text field"))
74+
.textFieldStyle(RoundedBorderTextFieldStyle(focused: true, height: 100))
75+
SecureField("placeholder", text: .constant("secure"))
76+
.textFieldStyle(RoundedBorderTextFieldStyle(focused: true))
77+
}
78+
.environment(\.sizeCategory, .extraExtraExtraLarge)
5479
}
5580
.preferredColorScheme(.dark)
5681
}

0 commit comments

Comments
 (0)