SwiftUI: How can I show/hide properties in the initializer based on default values?
While the documentation speaks of hiding the default when it comes to modifiers there is no mention of how to do this with properties in a Struct's initializer.
And so far I've been unable to achieve it.
For example we see how GhostButton is displayed:
GhostButton(
"Your title",
size: .large,
hugContent: false
) {
// Place your action here
}
The Problem
The trouble is that it is displaying the defaults of size: .large and hugContent: false. And that goes against the Swift convention of not writing out property values if they are the default values. As it stands now the above code would get flagged in code review.
This is how developers should write it in the above case.
GhostButton(
"Your title"
) {
// Place your action here
}
// Ideally the code should be written like this
GhostButton("Your title") {
// Place your action here
}
// Though I realize that this be more difficult to implement so think of this latest one as a nice to have
For reference here's how I have it coded:
struct PDLGhostButton_doc: FigmaConnect {
@FigmaString("Label")
var label: String = "Label"
@FigmaEnum("Density", mapping: ["Large": .large, "Medium": .medium, "Small": .small], hideDefault: true)
var density: PedalButton.Size = .large
@FigmaBoolean("Hug content", hideDefault: true)
var hugContent: Bool = false
var body: some View {
GhostButton(
label,
size: density,
hugContent: hugContent
) {
// Place your action here
}
}
}
I would have thought that the 'hideDefault` would be respected and the property not shown in the initializer. Can this be currently done? if not then I'd like to make a feature request that properties on the initializer should be not shown if hideDefault is set to true.
running version 1.1.3,
Mac OS X: 14.6.1 (23G93)
Hey @gitHendrix, I think you're right that hideDefault should also affect the initializer. I'll add this to our backlog. Thanks!
I am facing the same issue. I was wondering if there is any update on this bug fix @tomduncalf-figma ? The hidesDefault parameters successfully hide view modifiers, but it's not doing it for initializer parameters.