For SwiftUI, Vitamin provides two possibilities to have a VitaminTextField
.
- A
VitaminTextField
struct which provides an all-in-one component ready to use. - A
vitaminTextFieldStyle
modifier which has more customizable possibilities like applying theVitaminTextField
to aTextField
or aSecureField
.
import SwiftUI
import VitaminSwiftUI
import VitaminCore
struct ExampleView: View {
@State var text = ""
@State var state: VitaminTextFieldState = .standard
var body: some View {
// VitaminTextField all-in-one component
VitaminTextField(style: .outlined,
text: VitaminTextField.TextConfiguration(label: "Firstname",
placeholder: "Ex: John",
text: $text),
state: $state)
// VitaminTextField modifier
TextField("Ex: John", text: $text)
.vitaminTextFieldStyle(style: .outlined,
label: "Firstname",
state: $state)
}
}
The VitaminTextField
component can be use with the following parameters:
style
to represent the style to use. Optional. Default value:.outlined
.text
is aTextConfiguration
object to setup all texts in the component. SeeTextConfiguration
below. Mandatory.state
is a binding variable to represent the current state. Mandatory.icon
is aIconConfiguration
object to setup the icon in the component. SeeIconConfiguration
below. Optional. Default value:nil
.characterLimit
: is aCharacterLimitConfiguration
object to setup the character limit in the component. SeeCharacterLimitConfiguration
below. Optional. Default value:nil
.
The VitaminTextFieldStyle
modifier can be use on TextField
and SecureField
with the following parameters:
style
to represent the style to use. Optional. Default value:.outlined
.label
to represent the text above theTextField
. Mandatory.helperText
to represent the text below theTextField
. Optional. Default value:nil
.state
is a binding variable to represent the current state. Mandatory.icon
is aIconConfiguration
object to setup the icon in the component. SeeIconConfiguration
below. Optional. Default value:nil
.characterLimit
: is aCharacterLimitConfiguration
object to setup the character limit in the component. SeeCharacterLimitConfiguration
below. Optional. Default value:nil
.
The TextConfiguration
object is use for text related content on the VitaminTextField
component.
It can be use with the following parameters:
label
to represent the text above theTextField
. Mandatory.placeholder
to represent the text inside theTextField
. Mandatory.helperText
to represent the text below theTextField
. Optional. Default value:nil
.text
is a binding variable to represent the text taped by the user inside theTextField
.
The IconConfiguration
object is use to display a custom icon and to handle an action on the icon.
It can be use with the following parameters:
icon
to represent the icon to display. Mandatory.action
A closure to handle an action on the icon. Optional. Default value:nil
.
The CharacterLimitConfiguration
object is use to add a maximum number of characters.
text
is a binding variable to represent the text taped by the user inside theTextField
. It's the same variable than for theTextField
initilization. Mandatory.limit
to represent the maximum number of characters. Mandatory.
VitaminTextField
handles 2 styles:
.filled
.outlined
The default style is .outlined
.
VitaminTextField
handles 5 states:
.standard
.active
.error
.success
.disabled
The default state is .standard
.
You can manually change the state
thanks to the state property that you need to provide.
The VitaminTextField component will automatically set the state from .standard
to .active
when the field gets the focus.
However, the VitaminTextFieldStyle modifier will not do this by default. If you want to update the state automatically you need to use the VitaminTextField.updateActiveState(state:editing:)
method inside the onEditingChanged
parameter of the TextField
.
TextField("Ex: John", text: $text) { editing in
state = VitaminTextField.updateActiveState(state: state, editing: editing)
}
.vitaminTextFieldStyle(style: .outlined,
label: "Firstname",
state: $state)
If you want to set up a validation on the TextField
content, you can use methods provided by SwiftUI
.
If you are on iOS 14 and above, use onChange(of:perform:)
(Apple Documentation).
If you are on iOS 13, use onReceive(_:perform:)
(Apple Documentation).
// iOS 14 and above
VitaminTextField(style: .outlined,
text: VitaminTextField.TextConfiguration(label: "Firstname",
placeholder: "Ex: John",
text: $text),
state: $state)
.onChange(of: text) { newValue in
// Do your validation here.
// Ex: If you want to update the state of the text field.
state = .error
}
// iOS 13
VitaminTextField(style: .outlined,
text: VitaminTextField.TextConfiguration(label: "Firstname",
placeholder: "Ex: John",
text: $text),
state: $state)
.onReceive(Just(text)) { newValue in
// Do your validation here.
// Ex: If you want to update the state of the text field.
state = .error
}
Check the showcase if you want to see a full implementation.