View Models

How to use view models to configure the appearance of components.

All components in ComponentsKit use view models to configure their appearance. These models are shared between both SwiftUI and UIKit components.

For example, you can configure an input field like this:

let inputFieldVM = InputFieldVM {
  $0.title = "Email"
  $0.placeholder = "Enter your email"
  $0.isRequired = true
}

All view models in ComponentsKit do not have memberwise initializers. Instead, they conform to the ComponentVM protocol, which defines an initializer that modifies default values:

/// Initializes a new instance by applying a transformation closure to the default values.
///
/// - Parameter transform: A closure that defines the transformation.
init(_ transform: (_ value: inout Self) -> Void)

This approach has two main benefits:

  1. It allows you to set parameters in any order, making the initializers easier to use.
  2. Future changes can be made without breaking your code, as it simplifies deprecation.