Skip to content

Commit d18085e

Browse files
authored
Merge pull request #1 from Torear797/C-1
[C-1] fix chip inits
2 parents 74d0c00 + 1e3a958 commit d18085e

3 files changed

Lines changed: 56 additions & 16 deletions

File tree

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# osX files
2+
.DS_Store
3+
.Trashes
4+
5+
# Swift Package Manager
6+
.swiftpm
7+
8+
# User Interface
9+
*UserInterfaceState.xcuserstate
10+
11+
/.build
12+
/Packages
13+
xcuserdata/
14+
DerivedData/
15+
.swiftpm/configuration/registries.json
16+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
17+
.netrc

README.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,29 @@ VStack {
139139
### Custom Label
140140

141141
```swift
142-
Chip(isOn: $isOn) {
143-
Text("My Custom Label")
144-
.font(.system(size: 25))
145-
}
142+
Chip(isOn: $isOn) {
143+
HStack {
144+
Image(systemName: "xmark")
145+
146+
Text("My Custom Label")
147+
.font(.system(size: 25))
148+
}
149+
}
150+
```
151+
152+
### Custom Action
153+
154+
```swift
155+
Chip(isOn: $isOn) {
156+
HStack {
157+
Image(systemName: "xmark")
158+
159+
Text("My Custom Label")
160+
.font(.system(size: 25))
161+
}
162+
} action: {
163+
print("Alohomora")
164+
}
146165
```
147166

148167
## Add an extension for convenience

Sources/Chip/Chip.swift

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ public struct Chip<Label> : View where Label : View {
1515

1616
@Environment(\.chipStyle) var style
1717

18+
// MARK: Init
19+
20+
init(
21+
isOn: Binding<Bool>,
22+
action: @escaping () -> Void = {},
23+
@ViewBuilder label: () -> Label
24+
) {
25+
self.action = action
26+
self.label = AnyView(label())
27+
_isOn = isOn
28+
}
29+
1830
// MARK: View
1931

2032
public var body: some View {
@@ -38,17 +50,7 @@ public struct Chip<Label> : View where Label : View {
3850

3951
// MARK: - Initializers
4052

41-
public extension Chip where Label == Text {
42-
init(
43-
isOn: Binding<Bool>,
44-
action: @escaping () -> Void = {},
45-
@ViewBuilder label: () -> Label
46-
) {
47-
self.action = action
48-
self.label = AnyView(label())
49-
_isOn = isOn
50-
}
51-
53+
public extension Chip {
5254
init(
5355
isOn: Bool = false,
5456
action: @escaping () -> Void = {},
@@ -58,7 +60,9 @@ public extension Chip where Label == Text {
5860
self.label = AnyView(label())
5961
_isOn = .constant(isOn)
6062
}
61-
63+
}
64+
65+
public extension Chip where Label == Text {
6266
init(
6367
_ titleKey: LocalizedStringKey,
6468
isOn: Binding<Bool>,

0 commit comments

Comments
 (0)