Skip to content

Commit 2a793d8

Browse files
author
Reed Es
committed
simplified package; added images to README
1 parent 20668ab commit 2a793d8

6 files changed

Lines changed: 43 additions & 172 deletions

File tree

Images/PresetsPicker.png

28.3 KB
Loading

Images/TextField.png

14.6 KB
Loading

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,30 @@ _SwiftTextFieldPreset_ is part of the [OpenAlloc](https://github.com/openalloc)
88

99
## Features
1010

11+
Text Field | Presets Picker
12+
:---:|:---:
13+
![](https://github.com/openalloc/SwiftTextFieldPreset/blob/main/Images/TextField.png) | ![](https://github.com/openalloc/SwiftTextFieldPreset/blob/main/Images/PresetsPicker.png.png)
14+
1115
* Builds on existing `TextField` component
1216
* Presently targeting .macOS(.v13), .iOS(.v16), .watchOS(.v9)
1317
* Only Apple's _Collections_ as an external dependency
1418

19+
## Installation
20+
21+
In your `Package.swift`:
22+
23+
To package `.dependencies` add
24+
25+
```swift
26+
.package(url: "https://github.com/openalloc/SwiftTextFieldPreset.git", .upToNextMajor(from: "1.1.2")),
27+
```
28+
29+
To product `.dependencies` add
30+
31+
```swift
32+
.product(name: "TextFieldPreset", package: "SwiftTextFieldPreset"),
33+
```
34+
1535
## Example
1636

1737
A simple example where the presets are `String` type:
@@ -45,7 +65,7 @@ struct MyView: View {
4565
}
4666
```
4767

48-
Note that presets can be more than `String`, such as `struct`s with additional values that can be assigned via the `onSelect` callback.
68+
Note that presets can be more than `String`, such as `struct`s with additional values that can used to populate your target value/object via the `onSelect` callback.
4969

5070
## TODO
5171

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// PresetsPickerSingle.swift
2+
// PresetsPicker.swift
33
//
44
// Copyright 2023 OpenAlloc LLC
55
//
@@ -20,70 +20,33 @@ import Collections
2020
import os
2121
import SwiftUI
2222

23-
public struct PresetsPickerSingle<Key, PresettedItem, Label>: View
23+
struct PresetsPicker<Key, PresettedItem, Label>: View
2424
where Key: Hashable & CustomStringConvertible,
2525
PresettedItem: PresettableItem,
2626
Label: View
2727
{
28-
public typealias PresetsDictionary = OrderedDictionary<Key, [PresettedItem]>
28+
typealias PresetsDictionary = OrderedDictionary<Key, [PresettedItem]>
2929

3030
// MARK: - Parameters
3131

32-
private let presets: PresetsDictionary
33-
private let onSelect: (PresettedItem) -> Void
34-
private let label: (PresettedItem) -> Label
35-
36-
public init(presets: PresetsDictionary,
37-
onSelect: @escaping (PresettedItem) -> Void,
38-
label: @escaping (PresettedItem) -> Label)
39-
{
40-
self.onSelect = onSelect
41-
self.presets = presets
42-
self.label = label
43-
}
44-
45-
// MARK: - Locals
46-
47-
@State private var selected: PresettedItem?
32+
let presets: PresetsDictionary
33+
let onSelect: (PresettedItem) -> Void
34+
let label: (PresettedItem) -> Label
4835

4936
// MARK: - Views
5037

51-
public var body: some View {
52-
platformView {
38+
var body: some View {
39+
List {
5340
ForEach(presets.elements, id: \.key) { element in
5441
Section(header: Text(element.key.description)) {
5542
ForEach(element.value, id: \.self) { item in
56-
label(item)
57-
#if os(watchOS)
58-
.onTapGesture {
59-
selected = item
60-
}
61-
#endif
43+
Button(action: { onSelect(item) },
44+
label: { label(item) })
6245
}
6346
}
6447
}
6548
}
66-
.onChange(of: selected) { val in
67-
guard let val else { return }
68-
onSelect(val)
69-
}
7049
}
71-
72-
#if os(watchOS)
73-
private func platformView(content: () -> some View) -> some View {
74-
List {
75-
content()
76-
}
77-
}
78-
#endif
79-
80-
#if !os(watchOS)
81-
private func platformView(content: () -> some View) -> some View {
82-
List(selection: $selected) {
83-
content()
84-
}
85-
}
86-
#endif
8750
}
8851

8952
struct PresetsPickerSingle_Previews: PreviewProvider {
@@ -105,7 +68,7 @@ struct PresetsPickerSingle_Previews: PreviewProvider {
10568
@State var selected: String?
10669
var body: some View {
10770
NavigationStack {
108-
PresetsPickerSingle(presets: presets) { _ in
71+
PresetsPicker(presets: presets) { _ in
10972

11073
} label: {
11174
Text($0.text)

Sources/PresetsPickerMulti.swift

Lines changed: 0 additions & 112 deletions
This file was deleted.

Sources/TextFieldPreset.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,16 @@ public struct TextFieldPreset<ButtonLabel, GroupKey, PresettedItem, PickerLabel>
6666
TextField(text: $text,
6767
prompt: Text(prompt),
6868
axis: axis) { EmptyView() }
69-
// #if os(iOS)
70-
// .lineLimit(5)
71-
// #endif
7269

73-
Button(action: { isPresented = true }, label: buttonLabel)
70+
Button(action: { isPresented = true },
71+
label: buttonLabel)
7472
.buttonStyle(.borderless)
7573
}
7674
.sheet(isPresented: $isPresented) {
7775
NavigationStack {
78-
PresetsPickerSingle(presets: presets,
79-
onSelect: selectAction,
80-
label: pickerLabel)
76+
PresetsPicker(presets: presets,
77+
onSelect: selectAction,
78+
label: pickerLabel)
8179
.toolbar {
8280
ToolbarItem(placement: .cancellationAction) {
8381
Button("Cancel") { isPresented = false }
@@ -110,16 +108,18 @@ struct TextFieldWithPresets_Previews: PreviewProvider {
110108
@State var name: String = "New Exercise"
111109
var body: some View {
112110
Form {
113-
TextFieldPreset($name, prompt: "Enter name", axis: .vertical, presets: presets, pickerLabel: {
114-
Text($0.description)
115-
})
111+
Section("TextField with Preset") {
112+
TextFieldPreset($name, prompt: "Enter name", axis: .vertical, presets: presets, pickerLabel: {
113+
Text($0.description)
114+
})
115+
}
116116
}
117117
.padding()
118118
}
119119
}
120120

121121
static var previews: some View {
122122
TestHolder()
123-
.accentColor(.orange)
123+
//.accentColor(.orange)
124124
}
125125
}

0 commit comments

Comments
 (0)