SPB Git

Repository Public MIT

doc-glass-table.md 5.5 KB 102 lines markdown

1# GlassTable23| | |4|---|---|5| Identifier | `glass-table` |6| Module | `SPGlassData` |7| Kind | `native-wrapper` — the system `Table` on macOS and regular-width iPad, a system `List` in compact width |8| Platforms | iOS 26, iPadOS 26, macOS 26 |9| Example | `Examples/Gallery/Data/GlassTableExample.swift` |1011## Minimal usage1213```swift14GlassTable(cities, columns: [15    GlassTableColumn("Name", id: "name") { $0.name },16    GlassTableColumn("Population", id: "population", sortingBy: { $0.population }) {17        $0.population.formatted(.number)18    },19])20```2122Rows are any `Identifiable` values. The table never modifies the host's collection: it sorts a copy for display.2324## Variants2526| Variant | Use |27|---|---|28| Native table | macOS, and iPad or iPhone in regular width. Sortable headers, system selection. |29| Compact list | iPhone and narrow iPad windows. The first column is the row's main text, the other columns are labelled values, and a "Sort by" menu in the section header replaces the headers. |30| `selection: .none` | Rows are not selectable (default). |31| `selection: .single($id)` | At most one row. In the list form, tapping the selected row clears it. |32| `selection: .multiple($ids)` | Any number of rows. |3334Columns are values:3536| Initialiser | Sorting |37|---|---|38| `GlassTableColumn(title, id:, text:)` | By the displayed text, with `localizedStandardCompare` (case-insensitive, numbers by value). |39| `GlassTableColumn(title, id:, sortingBy:, text:)` | By a typed `Comparable` value (number, date), while displaying formatted text. |40| `GlassTableColumn(title, id:, text:, comparator:)` | By a custom comparison returning `ComparisonResult`. |4142## Parameters4344| Parameter | Type | Default | Meaning |45|---|---|---|---|46| `rows` | `Sequence` of `Identifiable` | — | The rows, in the host's order. |47| `columns` | `[GlassTableColumn<Row>]` | — | Columns, leading to trailing. |48| `sort` | `Binding<GlassTableSort?>` | `.constant(nil)` | Sorted column and direction; `nil` keeps the host's order. |49| `selection` | `GlassTableSelection<Row.ID>` | `.none` | Selection mode and binding. |50| `emptyTitle` | `LocalizedStringKey?` | `nil` | Text of the empty state; defaults to the localised "No data". |5152`GlassTableColumn.title` is a `LocalizedStringResource`, looked up in the host application's strings.5354### Column options and the compact table5556| Modifier | Meaning |57|---|---|58| `column.aligned(.trailing)` | Trailing alignment and tabular digits, for numbers. |59| `column.badged { row in tone }` | Draws the cell's text as a soft `GlassBadge` of the returned tone; `nil` keeps plain text. |6061In compact width, a table of up to four columns is drawn as a dense table on an opaque surface: uppercase sortable headers with a direction chevron, hairlines, the first column emphasised, and a selected row marked by an accent edge, a tint and a checkmark. A short table takes its own height; a long one scrolls and stays lazy. With more than four columns, or at accessibility text sizes, each row becomes a stack of labelled values in a `List`, as before. `GlassTableLogic.usesDenseCompactForm(columnCount:isAccessibilitySize:)` and `columnWeights(count:)` are pure and public.6263## States6465- **Empty**: a `ContentUnavailableView` with `emptyTitle`.66- **Sorted**: the header shows the system sort indicator; in the list form the menu shows the sorted column with a chevron and exposes "Sorted ascending / descending" as its accessibility value.67- **Selected**: system highlight in the table; a checkmark and the `.isSelected` trait in the list.68- Pressed, focused and disabled (`.disabled(_:)`) are drawn by the system.6970## Advanced example7172```swift73struct Invoice: Identifiable { let id: UUID; let client: String; let due: Date; let amount: Decimal }7475@State private var sort: GlassTableSort? = GlassTableSort(columnID: "due")76@State private var selected: Invoice.ID?7778GlassTable(invoices, columns: [79    GlassTableColumn("Client", id: "client") { $0.client },80    GlassTableColumn("Due", id: "due", sortingBy: { $0.due }) { $0.due.formatted(date: .abbreviated, time: .omitted) },81    GlassTableColumn("Amount", id: "amount", sortingBy: { $0.amount }) { $0.amount.formatted(.currency(code: "CAD")) },82], sort: $sort, selection: .single($selected), emptyTitle: "No invoice")8384// The same pure function is available to sort data elsewhere (export, tests):85let ordered = GlassTableLogic.sorted(invoices, by: sort, columns: columns)86```8788## Accessibility8990- The native table keeps the system's table semantics, keyboard navigation and VoiceOver column reading.91- In the list form each row is one element combining the main text and the labelled values; selection is exposed by the `.isSelected` trait and a checkmark, not by colour.92- The sort direction is given by a symbol and by text ("Sorted ascending").93- Text follows Dynamic Type; values are formatted by the host with `FormatStyle`.9495## Limits9697- Cells are text. A table with custom cell views should use the system `Table` directly.98- One sort key at a time: the system table's secondary sort keys are not exposed. The sort is stable, so sorting successively by two columns gives a two-level order.99- Rows are sorted in `body` on every update (`O(n log n)`); for very large collections sort in the model and pass `sort: .constant(nil)`.100- The system `Table` shows only its first column in compact width, which is why GlassTable switches to a list there. The switch follows `horizontalSizeClass` and was compiled for iOS but not exercised on a device in this phase.101- Column widths are left to the system.102
Files main
Menu

Theme