Learn how to configure the country selection component.
The country selection component displays a searchable drop-down for selecting a country. At minimum, you can create it with a label:
import SwiftUI
import PXPCheckoutSDK
let config = CountrySelectionComponentConfig(
label: "Country",
placeholder: "Select country"
)
let country = try pxpCheckout.create(.countrySelection, componentConfig: config) as! CountrySelectionComponent
country.buildContent()You can customise the dropdown styling, validation messages, and event handlers:
var config = CountrySelectionComponentConfig(
label: "Country",
placeholder: "Search or select",
guideText: "Billing country",
noCountryText: "No matching country",
validations: CountrySelectionValidationResource(CS01: "Select a country"),
dropdownStyles: CountrySelectionDropdownStyles(
maxHeight: 280,
cornerRadius: 8,
selectedOptionStyle: CountrySelectionOptionStyle(
backgroundColor: Color.blue.opacity(0.12),
textColor: .primary
),
unselectedOptionStyle: CountrySelectionOptionStyle(
verticalPadding: 10,
horizontalPadding: 12
),
emptyStateStyle: CountrySelectionOptionStyle(
textColor: .secondary
)
),
onChange: { /* selection changed */ },
onValidationFailed: { _ in }
)
config.componentStyles = FieldStyle(cornerRadius: 8)
config.displayValidIcon = true
config.validationOnChange = true
let country = try pxpCheckout.create(.countrySelection, componentConfig: config) as! CountrySelectionComponent| Property | Description |
|---|---|
validationsCountrySelectionValidationResource? | Custom validation messages for CS01 error code. Overrides SDK default message. Defaults to nil. |
noCountryTextString? | The message displayed when search matches no country. Shown in the dropdown when the user's search query has no results. Defaults to SDK localisation. |
dropdownStylesCountrySelectionDropdownStyles? | The dropdown panel and option styling. Controls the appearance of the country selection dropdown menu. Defaults to nil. |
labelString? | The field label text. Defaults to nil. |
placeholderString? | The placeholder text shown when no country is selected. Displayed in the dropdown trigger. Defaults to nil. |
guideTextString? | The helper text displayed below the field. Defaults to nil. |
accessibilityLabelString? | The accessibility label for the control. Defaults to nil. |
isRequiredBool | Whether the field is required. For the country selection component, this is always true and can't be changed. A country must be selected before the payment can be submitted. |
componentStylesFieldStyle? | The container styling. Defaults to nil. Properties: color, font, fontWeight, fontSize, labelFont, labelColor, valueFont, valueColor, backgroundColor, borderColor, borderWidth, cornerRadius, padding, margin, textAlignment, opacity, shadow (ShadowStyle), icon (IconStyle), labelSpacing. |
inputStylesFieldInputStateStyles? | The input styling for different states. Defaults to nil. States: base (default unfocused), active (focused), valid (has valid value), invalid (has invalid value). Each state supports: color, placeholderColor, fontWeight, fontSize, backgroundColor, borderColor, borderWidth, cornerRadius, padding, minHeight. |
labelStylesFieldLabelStateStyles? | The label styling by state. Defaults to nil. States: base (default), active (focused), valid (has valid value), invalid (has invalid value). Each state supports: color, font. Additional property: minWidth (CGFloat? - default: 80). |
labelPositionLabelPosition? | The label position relative to input. Options: above, below, left, right. Defaults to nil. |
invalidTextStyleFieldMessageStyle? | The error message styling. Defaults to nil. Properties: color, font. |
invalidTextPositionTextPosition? | The error message placement. Defaults to nil. |
guideTextStyleFieldMessageStyle? | The guide text styling. Defaults to nil. Properties: color, font. |
invalidIconImage? | The custom invalid state icon. Defaults to nil. |
validIconImage? | The custom valid state icon. Defaults to nil. |
displayValidIconBool | Whether to show the valid icon. Defaults to false. |
displayInvalidIconBool | Whether to show the invalid icon. Defaults to true. |
tooltipAccessibilityLabelString? | The tooltip accessibility label. Defaults to nil. |
displayRequiredIconBool? | Whether to display the required indicator. Defaults to nil. |
allowToCopyPasteBool | Whether to allow copy and paste. Defaults to true. |
validationOnBlurBool | Whether to validate when the field loses focus. Defaults to true. |
validationOnChangeBool | Whether to validate on each change. Defaults to false. |
isDisabledBool | Whether the field is disabled. Defaults to false. |
You can customise the dropdown panel and option rows:
| Property | Description |
|---|---|
backgroundColorColor? | The dropdown panel background colour. Defaults to nil. |
borderColorColor? | The panel border colour. Defaults to nil. |
borderWidthCGFloat? | The panel border width in points. Defaults to nil. |
cornerRadiusCGFloat? | The panel corner radius in points. Defaults to nil. |
shadowShadowStyle? | The panel shadow configuration. Properties: color, radius, x, y. Defaults to nil. |
maxHeightCGFloat? | The maximum scroll height in points. Limits the vertical size of the dropdown list. Defaults to nil. |
selectedOptionStyleCountrySelectionOptionStyle? | The styling for the currently selected option. Defaults to nil. |
unselectedOptionStyleCountrySelectionOptionStyle? | The styling for unselected options in the dropdown. Defaults to nil. |
emptyStateStyleCountrySelectionOptionStyle? | The styling for empty search state display. Shown when no countries match the search query. Defaults to nil. |
| Property | Description |
|---|---|
backgroundColorColor? | The option row background colour. Defaults to nil. |
textColorColor? | The country name text colour. Defaults to nil. |
fontFont? | The country name font. Defaults to nil. |
horizontalPaddingCGFloat? | The row horizontal padding in points. Defaults to nil. |
verticalPaddingCGFloat? | The row vertical padding in points. Defaults to nil. |
The country selection component provides event handlers for user interaction and validation:
| Callback | Description |
|---|---|
onChange: (() -> Void)? | Called when the selection changes. |
onFocus: (() -> Void)? | Called when the field gains focus. |
onBlur: (() -> Void)? | Called when the field loses focus. |
onValidationPassed: (([ValidationResult]) -> Void)? | Called when validation passes. Receives an array of validation results. |
onValidationFailed: (([ValidationResult]) -> Void)? | Called when validation fails. Receives an array of validation results containing error details. |
For more information about event patterns, see Events.
The country selection component validates user input against the following rules:
| Code | Condition | Default message |
|---|---|---|
| CS01 | Required but no country selected | "Please select country" |
Validation triggers based on your configuration (validationOnBlur, validationOnChange). Override the default message using the validations property.
Returns SwiftUI content for the country selection dropdown:
country.buildContent()Configure custom styling for the dropdown panel and options:
var config = CountrySelectionComponentConfig(label: "Country")
config.dropdownStyles = CountrySelectionDropdownStyles(
maxHeight: 200,
borderColor: Color(.separator),
borderWidth: 1,
selectedOptionStyle: CountrySelectionOptionStyle(
backgroundColor: .blue.opacity(0.1),
textColor: .primary
)
)
let country = try pxpCheckout.create(.countrySelection, componentConfig: config) as! CountrySelectionComponentUse in combination with other billing fields:
let country = try pxpCheckout.create(
.countrySelection,
componentConfig: CountrySelectionComponentConfig(label: "Country")
) as! CountrySelectionComponent
let postcode = try pxpCheckout.create(
.postcode,
componentConfig: PostcodeComponentConfig(label: "Postcode")
) as! PostcodeComponent
let address = try pxpCheckout.create(
.address,
componentConfig: AddressComponentConfig(label: "Address")
) as! AddressComponent
var submitConfig = CardSubmitComponentConfig()
submitConfig.avsRequest = true
submitConfig.billingAddressComponents = BillingAddressComponents(
countrySelectionComponent: country,
postcodeComponent: postcode,
addressComponent: address
)Use this component in Billing address or wider address flows. For validation details, see Data validation.