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

Allow null values in trailing and leading actions #40

Merged
merged 1 commit into from
Jun 24, 2023

Conversation

arondeparon
Copy link
Contributor

What

This pull requests adds a React.isValidElement check right before the trailing and leading elements are cloned.

Why?

Without this, any child that is passed will automatically be cloned, even if it is not a valid React element. If that is the case, a fatal error will be thrown, breaking the entire application.

This pull request fixes that by simply returning the child as-is if it is not a React element.

Use cases?

I came across this issue because I want to conditionally show certain actions. Right now, it's impossible to do it like this:

<LeadingActions>
    {someCondition ? (
        <SwipeAction
            destructive={true}
            onClick={() => {
                doSomething()
            }}>
            Do something
        </SwipeAction>
    ) : null}
    {anotherCondition ? (
        <SwipeAction
            destructive={true}
            onClick={() => {
                doAnotherThing()
            }}>
            Do another thing
        </SwipeAction>
    ) : null}
</LeadingActions>

If ONE of these conditions is false, I would expect the other element to still be rendered. This is not the case, because the returned null is seen as a child, which is subsequently cloned (causing an error).

@codecov-commenter
Copy link

codecov-commenter commented Jun 24, 2023

Codecov Report

Patch coverage: 75.00% and project coverage change: -0.35 ⚠️

Comparison is base (38a60e5) 90.25% compared to head (2678bdc) 89.91%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #40      +/-   ##
==========================================
- Coverage   90.25%   89.91%   -0.35%     
==========================================
  Files           5        5              
  Lines         462      466       +4     
  Branches      167      169       +2     
==========================================
+ Hits          417      419       +2     
- Misses         45       47       +2     
Impacted Files Coverage Δ
src/LeadingActions.js 77.77% <75.00%> (-7.94%) ⬇️
src/TrailingActions.js 77.77% <75.00%> (-7.94%) ⬇️

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@marekrozmus marekrozmus merged commit 39cebe6 into marekrozmus:main Jun 24, 2023
@marekrozmus
Copy link
Owner

@all-contributors please add @arondeparon for code

@arondeparon Thank you :)

@allcontributors
Copy link
Contributor

@marekrozmus

I've put up a pull request to add @arondeparon! 🎉

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

Successfully merging this pull request may close these issues.

3 participants