Skip to content

feat(swiftui): add A2UITextFieldStyle for the Basic Catalog TextField - #4361

Merged
pinieb merged 9 commits into
a2ui-project:mainfrom
karolkulesza:feat/swiftui-textfield-style
Dec 1, 2026
Merged

pinieb merged 9 commits into
a2ui-project:mainfrom
karolkulesza:feat/swiftui-textfield-style

Conversation

@karolkulesza

Copy link
Copy Markdown
Contributor

Problem

#3257 asks for a way to restyle the SwiftUI Basic Catalog components. #4024 added A2UIButtonStyle and A2UICardStyle, and the issue stays open for Text, TextField and Image. A2UITextField still hard-codes its look: 10 pt padding, a gray fill, an 8 pt corner radius, and a border and label that turn red while a check fails. To change that, an app has to replace the whole component, copying the data binding, the variants and the accessibility modifiers.

Part of #3257. This PR covers TextField only. Text and Image are also changed by the open #4123.

Changes

  • Adds A2UITextFieldStyle, A2UITextFieldStyleConfiguration and View.a2uiTextFieldStyle(_:) to BasicCatalogSwiftUI, built the same way as A2UIButtonStyle in feat(swift): add A2UICardStyle, A2UIButtonStyle, and Surface.makeWithBasicCatalogs #4024. The protocol refines DynamicProperty and the style is resolved through a generic wrapper view, so a style's @Environment values (such as \.colorScheme) resolve.

  • The configuration has:

    • label: the label text, or nil when the label property is empty. It stays hidden from assistive technologies, as before.
    • field: the input control for the variant (SecureField for obscured, a vertical TextField for longText, the decimal pad on iOS for number), bound to value and carrying the accessibility identifier, label and hint.
    • variant (shortText, longText, number, obscured; unknown values resolve to shortText), errorMessage (the first failing error check) with hasError, isDisabled (from the environment's isEnabled), and node.
  • DefaultA2UITextFieldStyle (.automatic) draws what A2UITextField drew before, with the same values.

  • A2UITextField still renders A2UICheckMessage below the style's view, so errors and warning/info hints look the same under any style. The accessibility modifiers moved from the padded box onto the input control, so a custom style can't drop them. They still reach the same native text field.

  • The style is read in a private host view inside A2UITextField.body, so tests that call .body outside a hosted view don't read the environment.

  • New tests in ComponentStyleTests:

    • a custom style gets the label, variant, error message, disabled state and node;
    • an unknown variant resolves to shortText, and an ancestor .disabled(true) sets isDisabled;
    • a style resolves \.colorScheme in Dark Mode inside a Surface;
    • the default style renders every variant with a failing check;
    • under a custom style, the a2ui-<id> element in the platform accessibility tree has the explicit accessibility label and hint, or the label property when there is no explicit label.

    The existing A2UITextField, check message, accessibility and adapter conformance tests pass unchanged.

  • A "Styling Basic Catalog components" section in swift/README.md with a text field style example, and a swift/CHANGELOG.md entry.

Verified on macOS (Xcode toolchain, Swift 6.1 package):

  • swift build
  • swift test --enable-swift-testing --no-parallel --filter A2UISwiftUITests: 203 tests. All pass except A2UIModalTests.keyboardActivationOfButtonTriggerRunsActionOnceAndPresentsSheet, which also fails on main when run headless.
  • swift test --enable-swift-testing --no-parallel --skip A2UISwiftUITests: 809 tests pass.
  • scripts/fix_format.sh --check --only-swift and swift-format lint --strict on the changed files.

Pre-launch Checklist

One time:

For this PR:

  • I have updated the relevant CHANGELOG.md file.
  • I updated/added relevant documentation.
  • My code changes (if any) have tests.
  • If my branch is on a fork, I have verified that scripts/e2e_test.sh passes.

If you need help, consider asking for advice on the discussion board.

Add A2UITextFieldStyle, A2UITextFieldStyleConfiguration and
View.a2uiTextFieldStyle(_:), following A2UIButtonStyle and A2UICardStyle.
DefaultA2UITextFieldStyle (.automatic) keeps the current look.

The configuration carries the label, the input control, the variant, the
first failing error check message and the disabled state. The input
keeps its data binding and accessibility modifiers under any style, and
A2UITextField still renders the check message below the styled field.

Part of a2ui-project#3257.
Add a test that hosts A2UITextField under a custom A2UITextFieldStyle and
reads the accessibility label and hint of the a2ui-<id> element from the
platform accessibility tree, with and without an explicit accessibility
label.

Also restore the DocC symbol links in the new style files, add the blank
line after their license headers, drop the unused OrderedJSON import from
A2UITextField.swift and a no-op modifier from the README example.
@github-actions github-actions Bot added the status: needs-triage auto-managed: https://github.com/a2ui-project/a2ui/blob/main/scripts/triage.mjs label Nov 22, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces customizable styling for A2UITextField components in BasicCatalogSwiftUI by adding A2UITextFieldStyle, A2UITextFieldStyleConfiguration, and the a2uiTextFieldStyle view modifier. It refactors A2UITextField to delegate its layout and rendering to the resolved style, provides a default implementation, and includes comprehensive tests. Feedback on the changes suggests removing redundant .accessibilityIdentifier modifiers within the input view builder of A2UITextFieldStyleConfiguration.Field to simplify the code.

Field.body already sets the identifier on the whole input.
@Varun-S10 Varun-S10 self-assigned this Nov 24, 2026
@github-actions github-actions Bot removed the status: needs-triage auto-managed: https://github.com/a2ui-project/a2ui/blob/main/scripts/triage.mjs label Nov 24, 2026
@Varun-S10

Copy link
Copy Markdown
Collaborator

Hi @karolkulesza, Thank you for raising the pr to fix the issue. We have verified the pr and it does resolve the issue. I will escalate this to team to review. Thank you for your patience and understanding.
Hi @pinieb, Could you please take a look?

@pinieb pinieb left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking up A2UITextFieldStyle, @karolkulesza! The protocol design, DynamicProperty environment resolution, and accessibility preservation on Field align cleanly with A2UIButtonStyle and A2UICardStyle.

A few things to update before merging:

  • Let styles own the full body (including checkMessage): Rather than keeping an outer VStack in A2UITextField, let A2UITextField pass straight through to A2UITextFieldStyleHost, move A2UICheckMessage into DefaultA2UITextFieldStyle, and expose a composable checkMessage view on A2UITextFieldStyleConfiguration. That lets custom styles control full layout and validation UI without risking duplicate error messages from errorMessage. We opened #4461 to track a composable A2UICheckMessageStyle separately, so there is no need to implement check styling here.
  • Minor cleanup & test coverage: Drop the duplicate .accessibilityIdentifier on Field.body, use @accessibility in the v1.0 accessibility test, and add assertions verifying check message rendering and two-way value binding / reactive validation updates under a custom style.

@pinieb pinieb added status: waiting-for-author-response lang: swift Affects SDKs or apps in the Swift programming language component: SwiftUI adapter Swift UI framework adapter labels Nov 30, 2026
A2UITextField now passes straight through to its style.
A2UITextFieldStyleConfiguration gains a checkMessage view wrapping
A2UICheckMessage, and DefaultA2UITextFieldStyle renders it below the
field, so a custom style controls the whole layout and can leave the
message out to present errorMessage its own way.

Drops the identifier that a2uiAccessibilityControl already sets on the
field, uses @accessibility in the v1.0 accessibility test, and adds
assertions for the check message under the built-in and custom styles
and a hosted test that typing under a custom style writes the bound
value and updates the checks the style sees.

@pinieb pinieb left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making those updates, @karolkulesza!

Verified in 2cbef774:

  • A2UITextField passes straight through to A2UITextFieldStyleHost, A2UITextFieldStyleConfiguration exposes checkMessage, and DefaultA2UITextFieldStyle renders it below the input—giving custom styles full layout control while keeping README.md, CHANGELOG.md, and DocC comments up to date.
  • Dropped the redundant .accessibilityIdentifier on Field.body and switched customTextFieldStyleKeepsInputAccessibility to @accessibility.
  • The expanded ComponentStyleTests now verify .automatic across all four variants, optional rendering of configuration.checkMessage, and end-to-end typing and reactive check updates under a custom style via HostedViewDriver.

Looks great—ready to merge once the remaining Swift Core (iOS) test job goes green!

@pinieb
pinieb merged commit 0b51a17 into a2ui-project:main Dec 1, 2026
54 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: SwiftUI adapter Swift UI framework adapter lang: swift Affects SDKs or apps in the Swift programming language status: needs review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants