TableColumn.swift 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /// A labelled column with a view for each row in a table.
  2. public struct TableColumn<RowValue, Content: View> {
  3. /// The label displayed at the top of the column (also known as the column title).
  4. public var label: String
  5. /// The content displayed for this column of each row of the table.
  6. public var content: (RowValue) -> Content
  7. }
  8. extension TableColumn {
  9. /// Creates a column.
  10. ///
  11. /// - Parameters:
  12. /// - label: The label displayed at the top of the column (also known as
  13. /// the column title).
  14. /// - content: The content displayed for this column of each row of the
  15. /// table.
  16. public init(_ label: String, @ViewBuilder content: @escaping (RowValue) -> Content) {
  17. self.label = label
  18. self.content = content
  19. }
  20. }
  21. extension TableColumn where Content == Text {
  22. /// Creates a column with that displays a string property and has a text
  23. /// label.
  24. ///
  25. /// - Parameters:
  26. /// - label: The label displayed at the top of the column (also known as
  27. /// the column title).
  28. /// - keyPath: A key path to the string value to display.
  29. public init(_ label: String, value keyPath: KeyPath<RowValue, String>) {
  30. self.label = label
  31. self.content = { row in
  32. Text(row[keyPath: keyPath])
  33. }
  34. }
  35. }