AttachmentElement

public struct AttachmentElement
extension AttachmentElement: View
extension AttachmentElement: _ViewEmptyChecking

AttachmentElement is a foundational UI component used by AttachmentGroup for displaying and managing attachment items in different states (normal, uploading, error).

This protocol defines the core properties and interactions required for attachment rendering:

  • Displaying attachment information
  • Handling state changes through control states
  • Managing user interactions like previewing and deletion
  • Supporting dynamic updates to attachment metadata

AttachmentElement serves as the base protocol for more specialized attachment components like Attachment, AttachmentWithError, and AttachmentInProgress.

Usage

// Basic usage with required properties
AttachmentElement(
    attachmentInfo: myAttachmentInfo,
    controlState: .normal,
    onPreview: { attachmentInfo in
        // Handle preview action
        previewController.preview(attachmentInfo.primaryURL)
    },
    onExtraInfoChange: { extraInfo in
        // Update attachment with new metadata
        updateAttachment(with: extraInfo)
    },
    onDelete: { attachmentInfo in
        // Handle deletion
        deleteAttachment(attachmentInfo)
    }
)

// In context of looping through attachment array
ForEach(configuration.attachments.indices, id: \.self) { index in
    AttachmentElement(
        attachmentInfo: configuration.attachments[index],
        controlState: configuration.controlState,
        onPreview: { info in
            previewManager.showPreview(for: info)
        },
        onExtraInfoChange: { extraInfo in
            // Update with new metadata while preserving state
            if case .uploaded(let destURL, let srcURL, _) = configuration.attachments[index] {
                configuration.attachments[index] = .uploaded(
                    destinationURL: destURL,
                    sourceURL: srcURL,
                    extraInfo: extraInfo
                )
            }
        },
        onDelete: { info in
            configuration.attachments.remove(at: index)
        }
    )
}