Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SwiftUI: How can I show/hide properties in the initializer based on default values? #164

Open
gitHendrix opened this issue Sep 16, 2024 · 0 comments

Comments

@gitHendrix
Copy link

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.

GhostButton

For example we see how GhostButton is displayed:

GhostButton(
    "Your title",
    size: .large,
    hugContent: false
) {
    // Place your action here
}

The Problem

The trouble is its displaying the defaults of size: .large and hugContent: false. And it's common Swift convention of not writing out property values if they are the default. 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 if theres only one value its like so. but that may be more difficult to implement. so this  is not a blocker but the above is.
GhostButton("Your title") {
    // Place your action here
}

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant