Skip to content

Commit 899222f

Browse files
author
Reed Es
committed
Six new multi-select variants; simple select/unselect for Stack/Grid
1 parent 0c8bdb1 commit 899222f

26 files changed

+1441
-28
lines changed

README.md

+10-6
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ macOS | iOS
1616
* Presently targeting macOS v11+ and iOS v14+\*
1717
* Supporting both value and reference semantics (including Core Data, which uses the latter)
1818
* Option to support a bound data source, where inline controls can directly mutate your data model
19+
* Support for single-select, multi-select, or no selection
1920
* Option to sort by column, with indicators and concise syntax
20-
* Option to specify a row background
21-
* Option to specify a row overlay
21+
* Option to specify a row background and/or overlay
2222
* On macOS, option for a hovering highlight, to indicate which row the mouse is over
2323
* MINIMAL use of View erasure (i.e., use of `AnyView`), which can impact scalability and performance\*\*
2424
* No external dependencies!
@@ -28,11 +28,9 @@ Three table types are supported, as determined by the mechanism by which their h
2828
### List
2929
* Based on SwiftUI's `List`
3030
* Option to support moving of rows through drag and drop
31-
* Support for single-select, multi-select, or no selection at all
3231

3332
### Stack
3433
* Based on `ScrollView`/`LazyVStack`
35-
* Support for single-select and no selection at all
3634

3735
### Grid
3836
* Based on `ScrollView`/`LazyVGrid`
@@ -107,14 +105,14 @@ While `LazyVGrid` is used here to wrap the header and row items, you could alter
107105

108106
## Tabler Views
109107

110-
_Tabler_ offers twenty-one (21) variants of table views from which you can choose. They break down along the following lines:
108+
_Tabler_ offers twenty-seven (27) variants of table views from which you can choose. They break down along the following lines:
111109

112110
* Table View - the View name
113111
* Type - each of the three table types differ in how they render:
114112
- **List** - based on `List`
115113
- **Stack** - based on `ScrollView`/`LazyVStack`
116114
- **Grid** - based on `ScrollView`/`LazyVGrid`
117-
* Select - single-select, multi-select, or selection not supported
115+
* Select - single-select, multi-select, or no selection
118116
* Value - if checked, can be used with value types (e.g., struct values)
119117
* Reference - if checked, can be used with reference types (e.g., class objects, Core Data, etc.)
120118
* Filter - if checked, `config.filter` is supported (see caveat below)
@@ -136,12 +134,18 @@ Table View | Type | Select | Value | Reference | Filter
136134
`TablerStack1` | **Stack** | Single | ✓ | ✓ | ✓
137135
`TablerStack1B` | **Stack** | Single | ✓ | | ✓\*
138136
`TablerStack1C` | **Stack** | Single | | ✓ |
137+
`TablerStackM` | **Stack** | Multi | ✓ | ✓ | ✓
138+
`TablerStackMB` | **Stack** | Multi | ✓ | | ✓\*
139+
`TablerStackMC` | **Stack** | Multi | | ✓ |
139140
`TablerGrid` | **Grid** | | ✓ | ✓ | ✓
140141
`TablerGridB` | **Grid** | | ✓ | |
141142
`TablerGridC` | **Grid** | | | ✓ |
142143
`TablerGrid1` | **Grid** | Single | ✓ | ✓ | ✓
143144
`TablerGrid1B` | **Grid** | Single | ✓ | |
144145
`TablerGrid1C` | **Grid** | Single | | ✓ |
146+
`TablerGridM` | **Grid** | Multi | ✓ | ✓ | ✓
147+
`TablerGridMB` | **Grid** | Multi | ✓ | |
148+
`TablerGridMC` | **Grid** | Multi | | ✓ |
145149

146150
\* filtering with bound values likely not scalable as implemented. If you can find a better way to implement, please submit a pull request!
147151

Sources/Grid/Internal/GridItemMod1.swift

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// GridItemMod.swift
2+
// GridItemMod1.swift
33
//
44
// Copyright 2022 FlowAllocator LLC
55
//
@@ -18,6 +18,7 @@
1818

1919
import SwiftUI
2020

21+
/// Support for single-select Grid-based rows
2122
struct GridItemMod1<Element>: ViewModifier
2223
where Element: Identifiable
2324
{
@@ -34,10 +35,17 @@ struct GridItemMod1<Element>: ViewModifier
3435
content
3536
.padding(config.itemPadding)
3637

37-
#if os(macOS) || targetEnvironment(macCatalyst)
38+
// simple tap to select (or unselect)
3839
.contentShape(Rectangle())
39-
.onTapGesture { selected = element.id }
40+
.onTapGesture {
41+
if selected == element.id {
42+
selected = nil
43+
} else {
44+
selected = element.id
45+
}
46+
}
4047

48+
#if os(macOS) || targetEnvironment(macCatalyst)
4149
.onHover { if $0 { hovered = element.id } }
4250
//.frame(maxWidth: .infinity) // NOTE this centers the grid item
4351
.background(hovered == element.id ? config.hoverColor : Color.clear)
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// GridItemModM.swift
3+
//
4+
// Copyright 2022 FlowAllocator LLC
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
19+
import SwiftUI
20+
21+
/// Support for multi-select Grid-based rows
22+
struct GridItemModM<Element>: ViewModifier
23+
where Element: Identifiable
24+
{
25+
typealias Config = TablerGridConfig<Element>
26+
typealias Hovered = Element.ID?
27+
typealias Selected = Set<Element.ID>
28+
29+
let config: Config
30+
let element: Element
31+
@Binding var hovered: Hovered
32+
@Binding var selected: Selected
33+
34+
func body(content: Content) -> some View {
35+
content
36+
.padding(config.itemPadding)
37+
38+
// simple tap to select (or unselect)
39+
.contentShape(Rectangle())
40+
.onTapGesture {
41+
if selected.contains(element.id) {
42+
selected.remove(element.id)
43+
} else {
44+
selected.insert(element.id)
45+
}
46+
}
47+
48+
#if os(macOS) || targetEnvironment(macCatalyst)
49+
.onHover { if $0 { hovered = element.id } }
50+
//.frame(maxWidth: .infinity) // NOTE this centers the grid item
51+
.background(hovered == element.id ? config.hoverColor : Color.clear)
52+
#endif
53+
}
54+
}

Sources/Grid/TablerGrid1.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import SwiftUI
2020

21-
/// Grid-based table, with support for single-selection
21+
/// Grid-based table, with support for single-select
2222
public struct TablerGrid1<Element, Header, Row, RowBack, RowOver, Results>: View
2323
where Element: Identifiable,
2424
Header: View,

Sources/Grid/TablerGrid1B.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import SwiftUI
2020

21-
/// Grid-based table, with support for single-selection and bound values from RandomAccessCollection
21+
/// Grid-based table, with support for single-select and bound value types
2222
public struct TablerGrid1B<Element, Header, Row, RowBack, RowOver, Results>: View
2323
where Element: Identifiable,
2424
Header: View,

Sources/Grid/TablerGrid1C.swift

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
// limitations under the License.
1717
//
1818

19-
import CoreData
2019
import SwiftUI
2120

2221
/// Grid-based table, with support for reference types

Sources/Grid/TablerGridB.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import SwiftUI
2020

21-
/// Grid-based table, with support for bound values from RandomAccessCollection
21+
/// Grid-based table, with support for bound value types
2222
public struct TablerGridB<Element, Header, Row, RowBack, RowOver, Results>: View
2323
where Element: Identifiable,
2424
Header: View,

Sources/Grid/TablerGridC.swift

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
// limitations under the License.
1717
//
1818

19-
import CoreData
2019
import SwiftUI
2120

2221
/// Grid-based table, with support for reference types

0 commit comments

Comments
 (0)